比树状数组套主席树不知道高到哪里去了,solve(l,r,L,R)就是对于L,R的操作区间的答案都在l,r区间里,然后递归下去
复杂度O(nlognlogn),每个操作会执行logn次就是o(nlogn),带上bit就是loglogn
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=500000+10,maxn=1000000+10,inf=0x3f3f3f3f;
int a[N],cnt,tot,ans[N];
struct query{
int x,y,k,pos,ty;
}c[N],t1[N],t2[N];
struct bit{
int sum[N];
void update(int i,int v)
{
for(;i<N;i+=i&(-i))sum[i]+=v;
}
int query(int i)
{
int ans=0;
for(;i;i-=i&(-i))ans+=sum[i];
return ans;
}
}b;
char s[10];
void solve(int l,int r,int L,int R)
{
if(l>r||L>R)return ;
if(l==r)
{
for(int i=L;i<=R;i++)if(c[i].ty)ans[c[i].pos]=l;
return ;
}
int m=(l+r)>>1,p1=0,p2=0;
for(int i=L;i<=R;i++)
{
if(c[i].ty)
{
int x=b.query(c[i].y)-b.query(c[i].x-1);
if(x>=c[i].k)t1[++p1]=c[i];
else c[i].k-=x,t2[++p2]=c[i];
}
else
{
if(c[i].x<=m)b.update(c[i].pos,c[i].y),t1[++p1]=c[i];
else t2[++p2]=c[i];
}
}
for(int i=1;i<=p1;i++)if(!t1[i].ty)b.update(t1[i].pos,-t1[i].y);
for(int i=1;i<=p1;i++)c[L+i-1]=t1[i];
for(int i=1;i<=p2;i++)c[L+p1+i-1]=t2[i];
solve(l,m,L,L+p1-1),solve(m+1,r,L+p1,R);
}
int main()
{
cnt=tot=0;
int n,m;scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),c[++cnt]={a[i],1,0,i,0};
for(int i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]=='Q')
{
++cnt;
scanf("%d%d%d",&c[cnt].x,&c[cnt].y,&c[cnt].k);
c[cnt].pos=++tot,c[cnt].ty=1;
}
else
{
int x,y;scanf("%d%d",&x,&y);
c[++cnt]={a[x],-1,0,x,0};c[++cnt]={a[x]=y,1,0,x,0};
}
}
solve(-inf,inf,1,cnt);
for(int i=1;i<=tot;i++)printf("%d\n",ans[i]);
return 0;
}
/********************
********************/