#include <bits/stdc++.h>
/*
Wake up, I'm wake up
Thu sang roi, em thay khong?
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n, m;
int a[N];
struct ST {
vector <int> st, lz, st1;
ST (int _n) {
st.assign(4 * (_n + 1), 0);
lz.assign(4 * (_n + 1), 0);
st1.assign(4 * (_n + 1), inf);
}
void lazy(int i, int l, int r) {
if (lz[i]) {
if (l < r) {
st[2 * i] += lz[i];
st[2 * i + 1] += lz[i];
st1[2 * i] += lz[i];
st1[2 * i + 1] += lz[i];
lz[2 * i] += lz[i];
lz[2 * i + 1] += lz[i];
}
lz[i] = 0;
}
}
void build(int i, int l, int r) {
if (l == r) {
st[i] = a[l];
st1[i] = a[l];
return;
}
int mid = (l + r) / 2;
build(2 * i, l, mid);
build(2 * i + 1, mid + 1, r);
st[i] = max(st[2 * i], st[2 * i + 1]);
st1[i] = min(st1[2 * i], st1[2 * i + 1]);
}
void update(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return;
if (u <= l && r <= v) {
lz[i] += val;
st[i] += val;
st1[i] += val;
return;
}
int mid = (l + r) / 2;
lazy(i, l, r);
update(2 * i, l, mid, u, v, val);
update(2 * i + 1, mid + 1, r, u, v, val);
st[i] = max(st[2 * i], st[2 * i + 1]);
st1[i] = min(st1[2 * i], st1[2 * i + 1]);
}
int get(int i, int l, int r, int u, int v) {
lazy(i, l, r);
if (u > r || v < l) return 0;
if (u <= l && r <= v) return st[i];
int mid = (l + r) / 2;
return max(get(2 * i, l, mid, u, v), get(2 * i + 1, mid + 1, r, u, v));
}
int findL(int i, int l, int r, int val) { // vi tri nho nhat >= val
// cout << "now: " << i << ' ' << l << ' ' << r << ' ' << val << ' ' << st[2 * i] << ' ' << st[2 * i + 1] << '\n';
lazy(i, l, r);
if (st[i] < val) return -1;
if (l == r) return l;
int mid = (l + r) / 2;
int ans = -1;
if (st[2 * i] >= val) ans = findL(2 * i, l, mid, val);
if (ans == -1) ans = findL(2 * i + 1, mid + 1, r, val);
return ans;
}
int findR(int i, int l, int r, int val) { // vi tri lon nhat < val
// cout << "now: " << i << ' ' << l << ' ' << r << ' ' << val << ' ' << st[2 * i] << ' ' << st[2 * i + 1] << '\n';
lazy(i, l, r);
if (st1[i] >= val) return -1;
if (l == r) return l;
int mid = (l + r) / 2;
int ans = -1;
if (st1[2 * i + 1] < val) ans = findR(2 * i + 1, mid + 1, r, val);
if (ans == -1) ans = findR(2 * i, l, mid, val);
return ans;
}
} st(N);
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
st.build(1, 1, n);
for (int i = 1; i <= m; i++) {
char type;
int x, y;
cin >> type >> x >> y;
if (type == 'F') {
int L = st.findL(1, 1, n, y);
if (L == -1) continue;
int R = min(L + x - 1, n);
int have = min(x, R - L + 1);
// cout << "at: " << R - L + 1 << ' ' << a << '\n';
// cout << L << ' ' << R << ' ' << have << '\n';
int value = st.get(1, 1, n, R, R);
// cout << "value: " << value << '\n';
int belowR = st.findR(1, 1, n, value);
// cout << "belowR: " << belowR << '\n';
int remain = have - (belowR - L + 1);
int last = st.findL(1, 1, n, value + 1);
// cout << "last: " << last << '\n';
if (last == -1) last = R + 1;
last--;
// cout << "remain: " << remain << '\n';
st.update(1, 1, n, L, belowR, 1);
st.update(1, 1, n, last - remain + 1, last, 1);
// for (int i = 1; i <= n; i++) {
// int x = st.get(1, 1, n, i, i);
// cout << x << ' ';
// }
// cout << '\n';
}
else if (type == 'C') {
int L = st.findL(1, 1, n, x);
int R = st.findR(1, 1, n, y + 1);
if (L == -1) L = n + 1;
if (R == -1) R = n;
// cout << L << ' ' << R << '\n';
cout << R - L + 1 << '\n';
}
}
return 0;
}
Compilation message (stderr)
grow.cpp: In function 'int main()':
grow.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
grow.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |