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; ll read() { ll x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } const int N = 1E5 + 10; int n, m; ll a[N]; const int T = N << 2; ll sumt[T], maxt[T]; inline int lc(int x) { return x << 1; } inline int rc(int x) { return x << 1 | 1; } inline int mp(int l, int r) { return (l + r) >> 1; } inline void pushUp(int x) { sumt[x] = sumt[lc(x)] + sumt[rc(x)]; maxt[x] = max(maxt[lc(x)], maxt[rc(x)]); } void build(int l, int r, int x) { if (l == r) sumt[x] = maxt[x] = a[l]; else { int mid = mp(l, r); build(l, mid, lc(x)); build(mid + 1, r, rc(x)); pushUp(x); } } void update(int L, int R, int l, int r, int x) { if (maxt[x] <= 2) return; if (l == r) sumt[x] = floor(log2(sumt[x]) + 1), maxt[x] = sumt[x]; else { int mid = mp(l, r); if (L <= mid) update(L, R, l, mid, lc(x)); if (R > mid) update(L, R, mid + 1, r, rc(x)); pushUp(x); } } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = read(); build(1, n, 1); for (int i = 1; i <= m; i++) { int l = read(), r = read(); update(l, r, 1, n, 1); cout << sumt[1] << endl; } return 0; }
|