1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <iostream> #include <algorithm> #include <string> #include <cstring> using namespace std; const int maxn = 200005; typedef long long ll; struct node{ ll sum; }rt[maxn << 2]; ll sum[maxn]; ll a[maxn], b[maxn]; void update(int node, int l,int r, int pos){ if(l == r && l == pos){ rt[node].sum++; return; } int mid = (l + r) >> 1; if(pos <= mid) update(node << 1, l, mid, pos); else update(node << 1|1, mid + 1, r, pos); rt[node].sum = rt[node << 1].sum + rt[node << 1|1].sum; } long long query(int node, int l, int r, int pos) { if(l == r){ return rt[node].sum; } int mid = (l + r) >> 1; if(pos <= mid){ return rt[node<<1|1].sum + query(node << 1, l, mid, pos); } else{ return query(node << 1|1, mid + 1, r, pos); } } int sz; int getid(ll num){ return lower_bound(b + 1, b + sz + 1, num ) - b; } int main() { ll n, m; cin >> n >> m; for(int i = 1; i <= n; ++i){ scanf("%lld",&a[i]); sum[i] = sum[i - 1] + a[i]; b[i] = sum[i]; } sort(b + 1 , b + n + 1); sz = unique(b + 1, b+ n + 1) - b - 1; update(1, 1, sz, getid(sum[1])); long long Sum = 0; for(int i = 2; i <= n; ++i) { long long num = sum[i] - m; long long kk = getid(num + 1); if(kk == sz + 1){ update(1,1,sz,getid(sum[i])); continue; } long long num1 = query(1,1,sz,getid(num + 1)); Sum += num1; update(1,1,sz,getid(sum[i])); } for(int i = 1; i <= n; ++i) { if(sum[i] < m)Sum++; } cout << Sum << '\n'; }
|