fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int,int> p;
  5.  
  6. main(){
  7. int n,k;
  8. cin>>n>>k;
  9. vector<int>v(n);
  10. for(auto &it:v)cin>>it;
  11. int c=0;
  12. priority_queue<p>maxPq;
  13. priority_queue<p,vector<p>,greater<p>>minPq;
  14. for(int i=0,j=0;j<n;j++){
  15. maxPq.push({v[j],j});
  16. minPq.push({v[j],j});
  17. while(maxPq.top().first-minPq.top().first>k){
  18. i=min(maxPq.top().second,minPq.top().second)+1;
  19. while(maxPq.top().second<i)maxPq.pop();
  20. while(minPq.top().second<i)minPq.pop();
  21. }
  22. c+=j-i+1;
  23. }
  24. cout<<c<<"\n";
  25. }
Success #stdin #stdout 0.01s 5304KB
stdin
7 3
2 6 4 3 6 8 9
stdout
16