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