#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) {
int st = posRight(v - 1), lastValue = get(min(n, st + u)), en = posRight(lastValue);
int l = 0, r = min(n, st + u);
while (r - l > 1) {
int mid = l + r >> 1;
if (get(mid) == lastValue) r = mid; else l = mid;
}
if (r - 1 - st < u) {
update(st + 1, r - 1); u -= r - 1 - st;
update(max(r, en - u + 1), en);
} else update(st + 1, min(n, st + u));
}
// 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:95:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
95 | int mid = l + r >> 1;
| ~~^~~
grow.cpp:78:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:79:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
1860 KB |
Output is correct |
2 |
Correct |
288 ms |
1724 KB |
Output is correct |
3 |
Correct |
166 ms |
3300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
99 ms |
568 KB |
Output is correct |
6 |
Correct |
104 ms |
744 KB |
Output is correct |
7 |
Correct |
9 ms |
416 KB |
Output is correct |
8 |
Correct |
24 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
724 KB |
Output is correct |
2 |
Correct |
110 ms |
676 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
46 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
832 KB |
Output is correct |
2 |
Correct |
126 ms |
748 KB |
Output is correct |
3 |
Correct |
17 ms |
424 KB |
Output is correct |
4 |
Correct |
119 ms |
780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
1216 KB |
Output is correct |
2 |
Correct |
259 ms |
1920 KB |
Output is correct |
3 |
Correct |
29 ms |
696 KB |
Output is correct |
4 |
Correct |
159 ms |
1500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
1804 KB |
Output is correct |
2 |
Correct |
261 ms |
1836 KB |
Output is correct |
3 |
Correct |
193 ms |
1500 KB |
Output is correct |
4 |
Correct |
30 ms |
1020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
1872 KB |
Output is correct |
2 |
Correct |
146 ms |
1832 KB |
Output is correct |
3 |
Correct |
160 ms |
1620 KB |
Output is correct |
4 |
Correct |
30 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
1940 KB |
Output is correct |
2 |
Correct |
255 ms |
2968 KB |
Output is correct |
3 |
Correct |
45 ms |
2356 KB |
Output is correct |
4 |
Correct |
60 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
1956 KB |
Output is correct |
2 |
Correct |
260 ms |
1712 KB |
Output is correct |
3 |
Correct |
377 ms |
2212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
2196 KB |
Output is correct |