#include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define ALL(A) A.begin(), A.end()
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define FOR2(i, l, r) for (int i = l; i >= r; i--)
#define ce cout<<endl;
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using str = string;
using T = pair<ll,int>;
const ll INF = 1e9 + 1;
const int N = 1e5;
struct SegmentTree {
int n;
vector<int> st, lz;
SegmentTree(int _n) {
n = _n;
st.assign(4 * n + 5, 0);
lz.assign(4 * n + 5, 0);
}
void down(int id) {
if (lz[id] == 0) return;
st[id * 2] += lz[id];
lz[id * 2] += lz[id];
st[id * 2 + 1] += lz[id];
lz[id * 2 + 1] += lz[id];
lz[id] = 0;
}
void build(int id, int l, int r, vector<int>& H) {
if (l == r) {
st[id] = H[l];
return;
}
int mid = (l + r) >> 1;
build(id * 2, l, mid, H);
build(id * 2 + 1, mid + 1, r, H);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
void update(int id, int l, int r, int u, int v) {
if (u > v || u > r || v < l) return;
if (u <= l && r <= v) {
st[id]++;
lz[id]++;
return;
}
down(id);
int mid = (l + r) >> 1;
update(id * 2, l, mid, u, v);
update(id * 2 + 1, mid + 1, r, u, v);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
int findFirst(int id, int l, int r, int x) {
if (st[id] < x) return n + 1;
if (l == r) return l;
down(id);
int mid = (l + r) >> 1;
if (st[id * 2] >= x)
return findFirst(id * 2, l, mid, x);
else
return findFirst(id * 2 + 1, mid + 1, r, x);
}
int getVal(int id, int l, int r, int pos) {
if (l == r) return st[id];
down(id);
int mid = (l + r) >> 1;
if (pos <= mid) return getVal(id * 2, l, mid, pos);
else return getVal(id * 2 + 1, mid + 1, r, pos);
}
void upd(int l, int r) { update(1, 1, n, l, r); }
int queryFirst(int x) { return findFirst(1, 1, n, x); }
int get(int pos) { return getVal(1, 1, n, pos); }
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> H(n + 1);
FOR(i,1,n) cin >> H[i];
sort(H.begin() + 1, H.end());
SegmentTree st(n);
st.build(1, 1, n, H);
while (q--) {
char type;
cin >> type;
if (type == 'F') {
int c, h;
cin >> c >> h;
int L = st.queryFirst(h);
if (L > n) continue;
int R = min(n, L + c - 1);
int valR = st.get(R);
int first_r = st.queryFirst(valR);
int last_r = st.queryFirst(valR + 1) - 1;
int num_at_valR = R - first_r + 1;
if (L <= first_r - 1) st.upd(L, first_r - 1);
st.upd(last_r - num_at_valR + 1, last_r);
}
else {
int l, r;
cin >> l >> r;
int posL = st.queryFirst(l);
int posR = st.queryFirst(r + 1);
cout << posR - posL << endl;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| # | 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... |