Untitled
unknown
plain_text
8 months ago
1.7 kB
1
Indexable
Never
#include <bits/stdc++.h> using namespace std; const int size = 2e5 + 5; // 20K streamer int n, raw[size], bit[size], mx[size << 1], mn[size << 1]; void add(int i, int v) { while (i <= n) { bit[i] += v; i += i & -i; } } int get(int i) { int res = 0; while (i) { res += bit[i]; i -= i & -i; } return res; } void update(int i) { i += n - 1; mx[i] = mn[i] = raw[i - n + 1]; while (i >> 1) { i >>= 1; mx[i] = max(mx[i << 1], mx[i << 1 | 1]); mn[i] = min(mn[i << 1], mn[i << 1 | 1]); } } int query(int l, int r) { int mxr = 0, mnr = INT_MAX; for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) mxr = max(mxr, mx[l]), mnr = min(mnr, mn[l]), l++; if (r & 1) r--, mxr = max(mxr, mx[r]), mnr = min(mnr, mn[r]); } return mxr - mnr; } int upd(int i, int v) { raw[i] += v; add(i, v); update(i); return raw[i]; } void init(int N, int mSubscriber[]) { n = N; for (int i = 1; i <= n; i++) { bit[i] = 0; mx[i + n - 1] = mn[i + n - 1] = raw[i] = mSubscriber[i - 1]; } for (int i = n - 1; i; i--) { mx[i] = max(mx[i << 1], mx[i << 1 | 1]); mn[i] = min(mn[i << 1], mn[i << 1 | 1]); } for (int i = 1; i <= n; i++) add(i, raw[i]); } int subscribe(int mId, int mNum) { return upd(mId, mNum); } int unsubscribe(int mId, int mNum) { return upd(mId, -mNum); } int count(int sId, int eId) { return get(eId) - get(sId - 1); } int calculate(int sId, int eId) { return query(sId, eId); }
Leave a Comment