#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> i) & 1LL)
#define ll long long
template<class T> bool minimize(T &x, const T &y) {
return x > y ? x = y, 1 : 0;
}
template<class T> bool maximize(T &x, const T &y) {
return x < y ? x = y, 1 : 0;
}
const int MAX = 1e5 + 5;
int n, q;
int h[MAX];
int minValue[4 * MAX], maxValue[4 * MAX];
int lazy[MAX];
void build(int i, int l, int r) {
if (l == r) {
minValue[i] = maxValue[i] = h[l];
return;
}
int u = i << 1, v = i << 1 | 1, m = (l + r) >> 1;
build(u, l, m); build(v, m + 1, r);
minValue[i] = min(minValue[u], minValue[v]);
maxValue[i] = max(maxValue[u], maxValue[v]);
}
void setAll(int i, int value) {
minValue[i] += value, maxValue[i] += value;
lazy[i] += value;
}
void pushDown(int i) {
int u = i << 1, v = i << 1 | 1;
setAll(u, lazy[i]), setAll(v, lazy[i]);
lazy[i] = 0;
}
void update(int i, int l, int r, int u, int v) {
if (l > v || u > r) return;
if (u <= l && r <= v) {
setAll(i, 1);
return;
}
pushDown(i);
int m = (l + r) >> 1;
update(i << 1, l, m, u, v);
update(i << 1 | 1, m + 1, r, u, v);
minValue[i] = min(minValue[i << 1], minValue[i << 1 | 1]);
maxValue[i] = max(maxValue[i << 1], maxValue[i << 1 | 1]);
}
// Return first pos >= x.
int getFirst(int i, int l, int r, int x) {
if (l == r) return maxValue[i] >= x ? l : l + 1;
pushDown(i);
int u = i << 1, v = i << 1 | 1, m = (l + r) >> 1;
if (maxValue[u] >= x) return getFirst(u, l, m, x);
else return getFirst(v, m + 1, r, x);
}
// Return last pos <= x
int getLast(int i, int l, int r, int x) {
if (l == r) return minValue[i] <= x ? l : l - 1;
pushDown(i);
int u = i << 1, v = i << 1 | 1, m = (l + r) >> 1;
if (minValue[v] <= x) return getLast(v, m + 1, r, x);
else return getLast(u, l, m, x);
}
int getValue(int i, int l, int r, int pos) {
if (l == r) return minValue[i];
pushDown(i);
int u = i << 1, v = i << 1 | 1, m = (l + r) >> 1;
if (pos > m) return getValue(v, m + 1, r, pos);
else return getValue(u, l, m, pos);
}
void update(int l, int r) {
update(1, 1, n, l, r);
}
int getFirst(int x) {
return getFirst(1, 1, n, x);
}
int getLast(int x) {
return getLast(1, 1, n, x);
}
int getValue(int pos) {
return getValue(1, 1, n, pos);
}
void updateQuery(int c, int h) {
if (getValue(n) < h) return;
int L = getFirst(h);
if (L + c - 1 > n) {
update(L, n);
return;
}
int value = getValue(L + c - 1);
int R = getLast(value);
int r = L - 1;
if (getValue(L) < value) r = getLast(value - 1);
if (r >= L) update(L, r);
update(R - (c - (r - L + 1)) + 1, R);
}
void answerQuery(int L, int R) {
cout << getLast(R) - getFirst(L) + 1 << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
#define TASK ""
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> q;
FOR(i, 1, n) cin >> h[i];
sort(h + 1, h + n + 1);
build(1, 1, n);
char t; int L, R;
while (q--) {
cin >> t >> L >> R;
if (t == 'F') updateQuery(L, R);
if (t == 'C') answerQuery(L, R);
}
return (0 ^ 0);
}
//hiuwsss
Compilation message
grow.cpp: In function 'int main()':
grow.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
144 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
23 ms |
3568 KB |
Output is correct |
6 |
Correct |
25 ms |
3664 KB |
Output is correct |
7 |
Correct |
3 ms |
2648 KB |
Output is correct |
8 |
Correct |
15 ms |
3232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
3672 KB |
Output is correct |
2 |
Correct |
28 ms |
3932 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
25 ms |
3504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
3928 KB |
Output is correct |
2 |
Correct |
27 ms |
3720 KB |
Output is correct |
3 |
Correct |
5 ms |
2652 KB |
Output is correct |
4 |
Correct |
27 ms |
4016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
4184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
4680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
4692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
5456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
5980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |