#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define int long long
#define alperen_t int32_t
#define pii pair<int,int>
#define vi vector<int>
#define ff first
#define ss second
#define sp << " " <<
#define all(x) x.begin(),x.end()
#define big(x) ((int)(x.size()))
using namespace std;
const int MOD = 998244353, inf = 1e18, LIM = 1e9;
const int N = 5e3+10;
struct ST {
vi t,lazy;
ST(int nn) {
t.assign(4*nn+1,0);
lazy.assign(4*nn+1,0);
}
void push(int node,bool leaf) {
t[node]+=lazy[node];
if (!leaf) {
lazy[2*node]+=lazy[node];
lazy[2*node+1]+=lazy[node];
}
lazy[node] = 0;
}
void update(int node,int l,int r,int L,int R,int v) {
push(node,l==r);
if (l > R || r < L) return;
if (l >= L && r <= R) {
lazy[node]+=v;
push(node,l==r);
return;
}
int m = (l+r) >> 1;
update(2*node,l,m,L,R,v),update(2*node+1,m+1,r,L,R,v);
t[node]= max(t[2*node],t[2*node+1]);
}
int query(int node,int l,int r,int L,int R) {
push(node,l==r);
if (l > R || r < L) return -1;
if (l >= L && r <= R) return t[node];
int m = (l+r) >> 1;
return max(query(2*node,l,m,L,R),query(2*node+1,m+1,r,L,R));
}
int walk(int node,int l,int r,int x) {
//<=x olan son indis
push(node,l==r);
if (l == r) {
if (t[node] <= x) return l;
return l-1;
}
int m = (l+r) >> 1;
push(2*node,l==m);
push(2*node+1,m+1==r);
if (t[2*node] <= x) return walk(2*node+1,m+1,r,x);
return walk(2*node,l,m,x);
}
};
void solve() {
int n,q;
cin >> n >> q;
vi a(n+1);
for (int i=1;i<=n;i++) cin >> a[i];
sort(1+all(a));
ST st(n);
for (int i=1;i<=n;i++) st.update(1,1,n,i,i,a[i]);
while (q--) {
char c;
cin >> c;
if (c == 'F') {
int c,h;
cin >> c >> h;
int mx = st.query(1,1,n,1,n);
if (mx < h) continue;
int l = h;
int r = 2e9;
while (l<=r) {
int m = (l+r) >> 1;
if (st.walk(1,1,n,m)-st.walk(1,1,n,h-1) <= c) l = m+1;
else r = m-1;
}
//cout << r << endl;
//r'ye kadar tam cover
int solb = st.walk(1,1,n,h-1)+1,sagb = st.walk(1,1,n,r);
st.update(1,1,n,st.walk(1,1,n,h-1)+1,st.walk(1,1,n,r),1);
int rem = c-(sagb-solb+1);
if (mx > r && rem>0) {
int p = st.walk(1,1,n,r+1);
st.update(1,1,n,p-rem+1,p,1);
}
}
else {
int l,r;
cin >> l >> r;
cout << st.walk(1,1,n,r)-st.walk(1,1,n,l-1) << '\n';
}
/* for (int i=1;i<=n;i++) cout << st.query(1,1,n,i,i) << ' ';
cout << '\n'; */
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Dodi
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
#ifdef Dodi
cerr << "TIME TAKEN: " << (double)clock()/(double)CLOCKS_PER_SEC << "seconds!\n";
#endif
}
# | 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... |