#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define pb push_back
#define f first
#define s second
int N, M; vector<int> A; pii tree[400010];
void build(int x, int l, int r) { int mid = (l + r) / 2;
if (l == r) { tree[x].f = A[l]; return ; }
build(2 * x + 1, l, mid); build(2 * x + 2, mid + 1, r); tree[x].f = max(tree[2 * x + 1].f, tree[2 * x + 2].f);
}
void pushdown(int x, int l, int r) { int mid = (l + r) / 2;
tree[x].f += tree[x].s;
if (l < r) { tree[2 * x + 1].s += tree[x].s; tree[2 * x + 2].s += tree[x].s; }
tree[x].s = 0;
}
void update(int x, int l, int r, int tl, int tr) { int mid = (l + r) / 2;
if (r < tl || l > tr) return ; if (tl <= l && r <= tr) { tree[x].s++; return ; }
pushdown(x, l, r); update(2 * x + 1, l, mid, tl, tr); update(2 * x + 2, mid + 1, r, tl, tr);
pushdown(2 * x + 1, l, mid); pushdown(2 * x + 2, mid + 1, r); tree[x].f = max(tree[2 * x + 1].f, tree[2 * x + 2].f);
}
int queryMax(int x, int l, int r, int tl, int tr) { int mid = (l + r) / 2;
pushdown(x, l, r); if (r < tl || l > tr) return -1; if (tl <= l && r <= tr) return tree[x].f;
return max(queryMax(2 * x + 1, l, mid, tl, tr), queryMax(2 * x + 2, mid + 1, r, tl, tr));
}
int queryPos(int x, int l, int r, int val) { int mid = (l + r) / 2;
pushdown(x, l, r); if (l == r) return l;
pushdown(2 * x + 1, l, mid); pushdown(2 * x + 2, mid + 1, r);
if (tree[2 * x + 1].f >= val) return queryPos(2 * x + 1, l, mid, val);
else if (tree[2 * x + 2].f >= val) return queryPos(2 * x + 2, mid + 1, r, val); return N;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M; for (int i = 0; i < N; i++) { int X; cin >> X; A.pb(X); }
sort(A.begin(), A.end()); build(1, 0, N - 1);
for (int i = 0; i < M; i++) { char C; int A, B; cin >> C >> A >> B;
if (C == 'F') { int START = queryPos(1, 0, N - 1, B);
if (START + A >= N) update(1, 0, N - 1, START, N - 1);
else { int LAST = queryMax(1, 0, N - 1, START + A - 1, START + A - 1);
int POS1 = queryPos(1, 0, N - 1, LAST); int POS2 = queryPos(1, 0, N - 1, LAST + 1);
update(1, 0, N - 1, START, POS1 - 1); update(1, 0, N - 1, POS1 + POS2 - START - A, POS2 - 1);
}
}
if (C == 'C') cout << queryPos(1, 0, N - 1, B + 1) - queryPos(1, 0, N - 1, A) << "\n";
}
}
Compilation message
grow.cpp: In function 'void pushdown(int, int, int)':
grow.cpp:19:42: warning: unused variable 'mid' [-Wunused-variable]
19 | void pushdown(int x, int l, int r) { int mid = (l + r) / 2;
| ^~~
grow.cpp: In function 'void update(int, int, int, int, int)':
grow.cpp:27:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
27 | if (r < tl || l > tr) return ; if (tl <= l && r <= tr) { tree[x].s++; return ; }
| ^~
grow.cpp:27:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
27 | if (r < tl || l > tr) return ; if (tl <= l && r <= tr) { tree[x].s++; return ; }
| ^~
grow.cpp: In function 'int queryPos(int, int, int, int)':
grow.cpp:45:5: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
45 | else if (tree[2 * x + 2].f >= val) return queryPos(2 * x + 2, mid + 1, r, val); return N;
| ^~~~
grow.cpp:45:85: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
45 | else if (tree[2 * x + 2].f >= val) return queryPos(2 * x + 2, mid + 1, r, val); return N;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
4332 KB |
Output is correct |
2 |
Correct |
159 ms |
4724 KB |
Output is correct |
3 |
Correct |
64 ms |
4736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
52 ms |
1664 KB |
Output is correct |
6 |
Correct |
63 ms |
1988 KB |
Output is correct |
7 |
Correct |
6 ms |
620 KB |
Output is correct |
8 |
Correct |
41 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
1900 KB |
Output is correct |
2 |
Correct |
66 ms |
2028 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
47 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
2156 KB |
Output is correct |
2 |
Correct |
69 ms |
2028 KB |
Output is correct |
3 |
Correct |
15 ms |
620 KB |
Output is correct |
4 |
Correct |
62 ms |
2028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
2928 KB |
Output is correct |
2 |
Correct |
143 ms |
4332 KB |
Output is correct |
3 |
Correct |
17 ms |
1644 KB |
Output is correct |
4 |
Correct |
48 ms |
4204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
3948 KB |
Output is correct |
2 |
Correct |
140 ms |
4332 KB |
Output is correct |
3 |
Correct |
56 ms |
4588 KB |
Output is correct |
4 |
Correct |
18 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
4204 KB |
Output is correct |
2 |
Correct |
103 ms |
4332 KB |
Output is correct |
3 |
Correct |
58 ms |
4716 KB |
Output is correct |
4 |
Correct |
17 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
4716 KB |
Output is correct |
2 |
Correct |
148 ms |
4204 KB |
Output is correct |
3 |
Correct |
36 ms |
3692 KB |
Output is correct |
4 |
Correct |
113 ms |
4076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
4460 KB |
Output is correct |
2 |
Correct |
146 ms |
4588 KB |
Output is correct |
3 |
Correct |
250 ms |
4844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
5228 KB |
Output is correct |