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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| const int maxn = 1e5 + 10; const int inf = 1e9; int N, M; int n = 0, tot = 0, qt = 0; int a[maxn], ans[maxn];
class Qry { public: int x, y, z; int op;
Qry() {} Qry(int x, int y, int z) : x(x), y(y), z(z) {} Qry(int x, int y) : x(x), y(y) {}
} qry[maxn * 3], lqry[maxn * 3], rqry[maxn * 3];
class Fwick { public: int C[maxn]; int n;
void _init(int n) { this->n = n; memset(C, 0, sizeof(C)); }
void add(int x, int y) { for(; x <= n; x += lowbit(x)) C[x] += y; }
int ask(int x) { int ret = 0; for(; x; x -= lowbit(x)) ret += C[x]; return ret; } } fwick;
void solve(int lval, int rval, int st, int ed) { if(st > ed) return; if(lval == rval) { _rep(i, st, ed) if(qry[i].op > 0) { ans[qry[i].op] = lval; } return; }
int mid = (lval + rval) >> 1;
int lt = 0, rt = 0; _rep(i, st, ed) { if(qry[i].op <= 0) { if(qry[i].y <= mid) { fwick.add(qry[i].x, qry[i].z); lqry[++lt] = qry[i]; } else rqry[++rt] = qry[i]; } else { int cnt = fwick.ask(qry[i].y) - fwick.ask(qry[i].x - 1); if(cnt >= qry[i].z) lqry[++lt] = qry[i]; else { qry[i].z -= cnt; rqry[++rt] = qry[i]; } } }
_rep(i, st, ed) { if(qry[i].op <= 0 && qry[i].y <= mid) fwick.add(qry[i].x, -qry[i].z); }
_rep(i, 1, lt) qry[st + i - 1] = lqry[i]; _rep(i, 1, rt) qry[st + lt + i - 1] = rqry[i]; solve(lval, mid, st, st + lt - 1); solve(mid + 1, rval, st + lt, ed); }
void init() { n = 0; tot = 0; qt = 0; memset(ans, 0, sizeof(ans)); }
int main() { freopen("input.txt", "r", stdin); int kase; cin >> kase; while (kase--) { init();
scanf("%d%d", &N, &M); // get arr[] _rep(i, 1, N) { scanf("%d", &a[i]); } // arr[] finished // -> query[...] _rep(i, 1, N) { qry[++tot] = Qry(i, a[i]); qry[tot].op = 0; qry[tot].z = 1; }
_rep(i, 1, M) { char cmd[2]; scanf("%s", cmd); if(cmd[0] == 'Q') { int l, r, k; scanf("%d%d%d", &l, &r, &k); qry[++tot] = Qry(l, r, k); qry[tot].op = ++qt; } else { int x, y; scanf("%d%d", &x, &y); qry[++tot] = Qry(x, a[x]); qry[tot].z = -1; qry[tot].op = -1;
qry[++tot] = Qry(x, y); qry[tot].z = 1; qry[tot].op = 0; a[x] = y; } }
// then solve fwick._init(maxn + 1); solve(0, inf, 1, tot);
_rep(i, 1, qt) printf("%d\n", ans[i]); } }
|