P5459 [BJOI2016]回转寿司
求多少个 $l, r$ 满足 $L\le s _r - s _ l \le R$ 。
查看代码
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
| #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e5 + 10; int n, L, R; LL ans, s[N]; void cdq(int l, int r) { if (l == r) return; int mid = l + r >> 1; cdq(l, mid); cdq(mid + 1, r); int hd = l, tl = l - 1; for (int i = mid + 1; i <= r; i++) { while (tl + 1 <= mid && s[i] - s[tl + 1] >= L) tl++; while (hd <= mid && s[i] - s[hd] >= R) hd++; ans += tl - hd + 1; } sort(s + l, s + r + 1); } int main() { scanf("%d%d%d", &n, &L, &R); for (int i = 1; i <= n; i++) { scanf("%lld", &s[i]); s[i] += s[i - 1]; } cdq(0, n); printf("%lld", ans); return 0; }
|