fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26.  
  27. int consistency1(int n, int k) {
  28.  
  29. int ans = 0;
  30. int s = 0, e = 0;
  31.  
  32. while(e<n && s<n-1){
  33. if(e==s) e++;
  34. if(a[e] - a[s] > k){
  35. s++;
  36. }
  37. else{
  38. ans += (e-s);
  39. e++;
  40. }
  41. }
  42. return ans;
  43.  
  44. }
  45.  
  46.  
  47. //* Template 2 :
  48. //* Invalid window => sum > k
  49. //* Valid window => sum <= k
  50.  
  51. int consistency2(int n, int k) {
  52.  
  53.  
  54. int ans = 0;
  55. int s = 0, e = 0;
  56.  
  57. while(e<n && s<n-1){
  58. if(e==s) e++;
  59.  
  60. while(s<e && a[e]-a[s] > k) s++;
  61. if(s==e) continue;
  62.  
  63. ans += (e-s);
  64. e++;
  65. }
  66. return ans;
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int practice(int n, int k) {
  79.  
  80. int ans = 0;
  81.  
  82.  
  83.  
  84. return ans;
  85. }
  86.  
  87.  
  88. void solve() {
  89.  
  90. int n, k;
  91. cin >> n >> k;
  92.  
  93. a.resize(n);
  94.  
  95. for(int i=0; i<n; i++) cin >> a[i];
  96.  
  97. cout << consistency1(n, k) << " " << consistency2(n, k) << endl;
  98.  
  99. // cout << consistency1(n, k) << " -> " << practice(n, k) << endl;
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. int32_t main() {
  108. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  109.  
  110. int t = 1;
  111. // cin >> t;
  112. while (t--) {
  113. solve();
  114. }
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0.01s 5308KB
stdin
5 6
1 5 10 15 20
stdout
4 4