fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = (int)1e5;
  6. const int maxn = (int)5e5;
  7. const long long INF = (long long)1e17;
  8.  
  9. long long n, S, a[N + 5], sum[N + 5], BIT1[maxn + 5], BIT2[maxn + 5], ans;
  10. vector<long long>coor;
  11.  
  12. void update1(int x, int val){
  13. for(; x > 0; x -= x & -x) BIT1[x] += val;
  14. }
  15. void update2(int x, int val){
  16. for(; x <= maxn; x += x & -x) BIT2[x] += val;
  17. }
  18. long long get1(int x){
  19. long long s = 0;
  20. for(; x <= maxn; x += x & -x) s += BIT1[x];
  21. return s;
  22. }
  23. long long get2(int x){
  24. long long s = 0;
  25. for(; x > 0; x -= x & -x) s += BIT2[x];
  26. return s;
  27. }
  28. void nhap(){
  29. cin >> n >> S;
  30. for(int i = 1; i <= n; i++) cin >> a[i];
  31. }
  32. void prepare(){
  33. coor.push_back(-INF);
  34. for(int i = 0; i <= n; i++){
  35. if(i != 0) sum[i] = sum[i - 1] + a[i];
  36. coor.push_back(sum[i]);
  37. coor.push_back(sum[i] - S);
  38. coor.push_back(sum[i] + S);
  39. }
  40. sort(coor.begin(), coor.end());
  41. }
  42. void solve(){
  43. int tmp;
  44. for(int R = 0; R <= n; R++){
  45. tmp = lower_bound(coor.begin(), coor.end(), S + sum[R]) - coor.begin();
  46. ans += get1(tmp);
  47. tmp = lower_bound(coor.begin(), coor.end(), sum[R] - S) - coor.begin();
  48. ans += get2(tmp);
  49. tmp = lower_bound(coor.begin(), coor.end(), sum[R]) - coor.begin();
  50. update1(tmp - 1, 1);
  51. update2(tmp + 1, 1);
  52. }
  53. cout << ans;
  54. }
  55. int main() {
  56. ios_base::sync_with_stdio(0);
  57. cin.tie(0); cout.tie(0);
  58. nhap();
  59. prepare();
  60. solve();
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 7684KB
stdin
4 4 
5 -1 8 -5
stdout
6