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 77 78 79 80 81 82
| #include <cstdio> #include <cmath> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') (c == '-') && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; (flag) && (x = ~x + 1); } const int N = 1e6 + 10; int n, m, len, w[N], v[N], d[N]; void build(int x) { for (int i = x * len; i < min(n + 1, x * len + len); i++) v[i] = w[i]; sort(v + x * len, v + min(n + 1, x * len + len)); } void modify(int l, int r, int k) { if (l / len == r / len) { for (int i = l; i <= r; i++) w[i] += k; build(l / len); return; } for (int i = l; i < l / len * len + len; i++) w[i] += k; build(l / len); for (int i = r / len * len; i <= r; i++) w[i] += k; build(r / len); for (int i = l / len + 1; i < r / len; i++) d[i] += k; } int query(int l, int r, int k) { int res = 0; if (l / len == r / len) { for (int i = l; i <= r; i++) if (w[i] + d[l / len] >= k) res++; return res; } for (int i = l; i < l / len * len + len; i++) res += w[i] + d[l / len] >= k; for (int i = r / len * len; i <= r; i++) res += w[i] + d[r / len] >= k; for (int i = l / len + 1; i < r / len; i++) res += i * len + len - (lower_bound(v + i * len, v + i * len + len, k - d[i]) - v); return res; } int main() { read(n), read(m); len = sqrt(n); for (int i = 1; i <= n; i++) read(w[i]); for (int i = 0; i <= n / len; i++) build(i); for (char op; m; m--) { int l, r, k; op = getchar(); while (op != 'A' && op != 'M') op = getchar(); read(l), read(r), read(k); if (op == 'M') modify(l, r, k); else if (op == 'A') printf("%d\n", query(l, r, k)); } return 0; }
|