#include "bits/stdc++.h"
using namespace std;
const int N = (int) 1e5 + 22;
int t[4 * N], d[4 * N], a[N];
void push(int v, int l, int r) {
t[v] += d[v];
if (l + 1 != r) {
d[v * 2 + 1] += d[v];
d[v * 2 + 2] += d[v];
}
d[v] = 0;
}
void update(int v, int l, int r, int l1, int r1, int x) {
push(v, l, r);
if (l1 >= r || l >= r1) {
return;
}
if (l1 <= l && r <= r1) {
d[v] = x;
push(v, l, r);
return;
}
int m = (l + r) / 2;
update(v * 2 + 1, l, m, l1, r1, x);
update(v * 2 + 2, m, r, l1, r1, x);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
void build(int v, int l, int r) {
if (l + 1 == r) {
t[v] = a[l];
return;
}
int m = (l + r) / 2;
build(v * 2 + 1, l, m);
build(v * 2 + 2, m, r);
t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
}
int lower(int v, int l, int r, int x) {
push(v, l, r);
if (t[v] < x) {
return r;
}
if (l + 1 == r) {
return l;
}
int m = (l + r) / 2;
push(v * 2 + 1, l, m);
push(v * 2 + 2, m, r);
if (t[v * 2 + 1] >= x) {
return lower(v * 2 + 1, l, m, x);
} else {
return lower(v * 2 + 2, m, r, x);
}
}
int get(int v, int l, int r, int l1, int r1) {
push(v, l, r);
if (l1 >= r || l >= r1) {
return 0;
}
if (l1 <= l && r <= r1) {
return t[v];
}
int m = (l + r) / 2;
return max(get(v * 2 + 1, l, m, l1, r1), get(v * 2 + 2, m, r, l1, r1));
}
void solve() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
build(0, 0, n);
while (q--) {
char c;
cin >> c;
if (c == 'C') {
int l, r;
cin >> l >> r;
int posl = lower(0, 0, n, l);
int posr = lower(0, 0, n, r + 1);
cout << posr - posl << '\n';
} else {
int cnt, x;
cin >> cnt >> x;
int pos = lower(0, 0, n, x);
if (n - pos <= cnt) {
update(0, 0, n, pos, n, 1);
continue;
}
int val = get(0, 0, n, pos + cnt - 1, pos + cnt);
int pos2 = lower(0, 0, n, val);
update(0, 0, n, pos, pos2, 1);
cnt -= pos2 - pos;
int pos3 = lower(0, 0, n, val + 1);
update(0, 0, n, pos3 - cnt, pos3, 1);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
4680 KB |
Output is correct |
2 |
Correct |
104 ms |
4968 KB |
Output is correct |
3 |
Correct |
36 ms |
4936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
4 ms |
2384 KB |
Output is correct |
3 |
Correct |
3 ms |
2384 KB |
Output is correct |
4 |
Correct |
2 ms |
2384 KB |
Output is correct |
5 |
Correct |
39 ms |
3408 KB |
Output is correct |
6 |
Correct |
46 ms |
3664 KB |
Output is correct |
7 |
Correct |
5 ms |
2384 KB |
Output is correct |
8 |
Correct |
23 ms |
3152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
3684 KB |
Output is correct |
2 |
Correct |
55 ms |
3716 KB |
Output is correct |
3 |
Correct |
2 ms |
2384 KB |
Output is correct |
4 |
Correct |
29 ms |
3428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
3912 KB |
Output is correct |
2 |
Correct |
50 ms |
3672 KB |
Output is correct |
3 |
Correct |
9 ms |
2640 KB |
Output is correct |
4 |
Correct |
50 ms |
3912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
3912 KB |
Output is correct |
2 |
Correct |
102 ms |
4680 KB |
Output is correct |
3 |
Correct |
13 ms |
2896 KB |
Output is correct |
4 |
Correct |
31 ms |
4680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
4460 KB |
Output is correct |
2 |
Correct |
95 ms |
4680 KB |
Output is correct |
3 |
Correct |
35 ms |
5092 KB |
Output is correct |
4 |
Correct |
16 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
4600 KB |
Output is correct |
2 |
Correct |
62 ms |
4688 KB |
Output is correct |
3 |
Correct |
35 ms |
4944 KB |
Output is correct |
4 |
Correct |
12 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
5192 KB |
Output is correct |
2 |
Correct |
95 ms |
4680 KB |
Output is correct |
3 |
Correct |
22 ms |
3920 KB |
Output is correct |
4 |
Correct |
65 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
5104 KB |
Output is correct |
2 |
Correct |
97 ms |
4972 KB |
Output is correct |
3 |
Correct |
160 ms |
5192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
5600 KB |
Output is correct |