#include <bits/stdc++.h>
using namespace std;
#define task ""
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int MAX = 100100;
int n, m;
int h[MAX];
int st[MAX << 2];
void build(int id, int l, int r) {
if (l == r) st[id] = h[l];
else {
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
}
}
void push(int id) {
if (!st[id]) return;
st[id << 1] += st[id];
st[id << 1 | 1] += st[id];
st[id] = 0;
}
void update(int id, int l, int r, int u, int v) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
++st[id];
return;
}
push(id);
int mid = l + r >> 1;
update(id << 1, l, mid, u, v);
update(id << 1 | 1, mid + 1, r, u, v);
}
void update(int u, int v) {
if (u > v) return;
update(1, 1, n, u, v);
}
int get(int id, int l, int r, int pos) {
if (l == r) return st[id];
int mid = l + r >> 1; push(id);
if (pos <= mid) return get(id << 1, l, mid, pos); return get(id << 1 | 1, mid + 1, r, pos);
}
int get(int pos) {
return get(1, 1, n, pos);
}
int posRight(int value) {
if (get(1) > value) return 0;
int l = 1, r = n + 1;
while (r - l > 1) {
int mid = l + r >> 1;
if (get(mid) <= value) l = mid; else r = mid;
}
return l;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
}
sort(h + 1, h + 1 + n);
build(1, 1, n);
while (m--) {
char op; cin >> op; int u, v; cin >> u >> v;
if (op == 'C') cout << posRight(v) - posRight(u - 1) << '\n';
else {
if (get(n) < v) continue;
int l = v, r = 1000100000;
int st = posRight(v - 1);
while (r - l > 1) {
int mid = (1LL * l + r) >> 1;
if (posRight(mid) - st <= u) l = mid; else r = mid;
}
int en = posRight(l);
if (en - st > u) update(max(st + 1, en - u + 1), en);
else {
update(st + 1, en); u -= en - st;
int otherEn = posRight(get(en + 1));
update(max(en + 1, otherEn - u + 1), otherEn);
}
}
// for (int i = 1; i <= n; ++i)
// cout << get(i) << ' '; cout << '\n';
}
return 0;
}
Compilation message
grow.cpp: In function 'void build(int, int, int)':
grow.cpp:25:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'void update(int, int, int, int, int)':
grow.cpp:44:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int get(int, int, int, int)':
grow.cpp:55:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = l + r >> 1; push(id);
| ~~^~~
grow.cpp:56:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
56 | if (pos <= mid) return get(id << 1, l, mid, pos); return get(id << 1 | 1, mid + 1, r, pos);
| ^~
grow.cpp:56:55: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
56 | if (pos <= mid) return get(id << 1, l, mid, pos); return get(id << 1 | 1, mid + 1, r, pos);
| ^~~~~~
grow.cpp: In function 'int posRight(int)':
grow.cpp:66:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = l + r >> 1;
| ~~^~~
grow.cpp: In function 'int main()':
grow.cpp:76:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:77:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
2968 KB |
Output is correct |
2 |
Execution timed out |
1014 ms |
3336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
404 KB |
Output is correct |
3 |
Correct |
4 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
131 ms |
1348 KB |
Output is correct |
6 |
Correct |
150 ms |
1648 KB |
Output is correct |
7 |
Correct |
27 ms |
468 KB |
Output is correct |
8 |
Correct |
28 ms |
1196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
1628 KB |
Output is correct |
2 |
Correct |
168 ms |
1788 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
46 ms |
1352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
1936 KB |
Output is correct |
2 |
Correct |
182 ms |
1736 KB |
Output is correct |
3 |
Correct |
82 ms |
588 KB |
Output is correct |
4 |
Correct |
154 ms |
1788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
498 ms |
2176 KB |
Output is correct |
2 |
Correct |
897 ms |
3080 KB |
Output is correct |
3 |
Correct |
33 ms |
1048 KB |
Output is correct |
4 |
Correct |
872 ms |
3132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
791 ms |
2712 KB |
Output is correct |
2 |
Correct |
818 ms |
3020 KB |
Output is correct |
3 |
Execution timed out |
1082 ms |
3252 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
553 ms |
2808 KB |
Output is correct |
2 |
Correct |
440 ms |
3020 KB |
Output is correct |
3 |
Execution timed out |
1074 ms |
3300 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1020 ms |
3476 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
592 ms |
3208 KB |
Output is correct |
2 |
Correct |
884 ms |
3324 KB |
Output is correct |
3 |
Execution timed out |
1027 ms |
2984 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
4008 KB |
Output is correct |