fork download
  1. #include <bits/stdc++.h>
  2. #define FIO ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  3. #define ll long long
  4. #define ull unsigned long long
  5. #define ld long double
  6. #define Yes cout << "YES\n"
  7. #define No cout << "NO\n"
  8. using namespace std;
  9. #define IO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  10. const int MX = 2e5 + 5 , MOD = 1e9 + 7;
  11. #define all(v) v.begin() , v.end()
  12. #define rall(v) v.rbegin() , v.rend()
  13. #define nl '\n'
  14.  
  15. vector<vector<int>>adj(MX);
  16. vector<int>indeg(MX);
  17. vector<bool>vis(MX);
  18. vector<bool>inprogg(MX);
  19. queue<int>q;
  20. vector<int>ans(MX);
  21. vector<int>parent(MX);
  22. void solve()
  23. {
  24. int n, d;
  25. cin >> n >>d;
  26. int a[n];
  27. for(int i = 0 ; i < n ; i++){
  28. cin >> a[i];
  29. }
  30.  
  31. ll ans = 0;
  32. for(ll i = 0 , j = 0 ; i < n ; i++){
  33. while(a[i] - a[j] > d) j++;
  34. ans += (i - j) * (i - j - 1) / 2;
  35. }
  36. cout << ans;
  37. }
  38.  
  39. int main()
  40. {
  41. FIO
  42. #ifndef ONLINE_JUDGE
  43. IO
  44. #endif
  45. int tt = 1;
  46. // cin >> tt;
  47. while (tt--) {
  48. solve();
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 10112KB
stdin
Standard input is empty
stdout
Standard output is empty