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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 50; const ll lx = -1e10 - 1e9, rx = 1e10; ll L, R, a[maxn]; int n, tot, root;
struct node { ll lc, rc; ll cnt; #define lc(x) tr[x].lc #define rc(x) tr[x].rc #define cnt(x) tr[x].cnt } tr[55 * maxn];
int build() { ++tot; lc(tot) = rc(tot) = cnt(tot) = 0; return tot; }
void up(int p) { cnt(p) = cnt(lc(p)) + cnt(rc(p)); }
void update(int p, ll l, ll r, ll x) { if (l == r) { cnt(p) += 1; return; } ll mid = l + (r - l) / 2; if (x <= mid) { if (!lc(p)) lc(p) = build(); update(lc(p), l, mid, x); } if (x > mid) { if (!rc(p)) rc(p) = build(); update(rc(p), mid + 1, r, x); } up(p); }
ll ask(int p, ll l, ll r, ll L, ll R) { if (L <= l && r <= R) return cnt(p); ll mid = l + (r - l) / 2, ans = 0; if (L <= mid && lc(p)) ans += ask(lc(p), l, mid, L, R); if (R > mid && rc(p)) ans += ask(rc(p), mid + 1, r, L, R); return ans; }
int main() { root = build(); update(root, lx, rx, 0); cin >> n >> L >> R; for (int i = 1; i <= n; ++i) { cin >> a[i]; a[i] += a[i - 1]; } ll ans = 0; for (int i = 1; i <= n; ++i) { ans += ask(root, lx, rx, a[i] - R, a[i] - L); update(root, lx, rx, a[i]); } cout << ans; return 0; }
|