upd 10.24:通过卡常以及对 getans 函数的常数优化,通过了 @KevinZ99 的 Hack。
upd 10.25:通过调整块长通过了 @KevinZ99 加强版 Hack。
P4587 的带修版本。
考虑新加入一个数对序列 Yuuki 数的影响。如果存在一个数 ,,那么从 到 的所有数都无法被表示。
这启示我们一个贪心的思路:维护一个数 ,表示当前可以表示出小于等于 的所有数。初始 ,此时我们查询当前区间 所有数字的和,记为 。
如果 ,代表 的数最多只能凑出 ,而其他的数个个 ,不能对答案造成影响,此时答案就是 。
如果 ,代表 的数还能凑出 的数,那么 以内的数都可以被凑出了,此时令 并且继续查询是否能凑出更大的数。
此时可以用莫队维护答案了,普通带修莫队即可。对值域进行分块,方便查询区间 数字的和。
设 为查询区间内 的和,时间复杂度为 ,实际上远远跑不满。加个快读轻松进 1s。
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define F first
#define S second
using namespace std;
const int N=1e6+5;
int n,m,a[N],B,ans[N],ar[N],b[N],vn,cnt[N],sum[N],pos[N],lp[N],rp[N],vbn;
int tmp[N];
int l=1,r=0,t=0;
char ibuf[1<<22],*p1=ibuf,*p2=ibuf;
#define gc() (p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline int read(){
int x=0,f=1;char c=gc();
while(c<'0'||c>'9'){ if(c=='-') f=-1; c=gc(); }
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48), c=gc();
return x*f;
}
char obuf[1<<20],*pp=obuf;
inline void push(char c){
if(pp-obuf==(1<<20))fwrite(obuf,1,1<<20,stdout),pp=obuf;
*pp++=c;
}
inline void write(int x){
if(x<0){ push('-'); x=-x; }
static int st[12]; int top=0;
if(!x) push('0');
while(x) st[++top]=x%10, x/=10;
while(top) push(st[top--]^48);
push('\n');
}
struct qry{
int l,r,t,x;
}q[N];
struct mod{
int p,old,nw;
}chg[N];
bool cmp(qry a,qry b){
if(a.l/B!=b.l/B) return a.l<b.l;
if(a.r/B!=b.r/B) return a.r<b.r;
return a.t<b.t;
}
void add(int p){
int x=ar[p];
cnt[x]++;
sum[pos[x]]+=b[x];
}
void del(int p){
int x=ar[p];
cnt[x]--;
sum[pos[x]]-=b[x];
}
void update(int p,int x){
if(l<=p&&p<=r){
del(p);
ar[p]=x;
add(p);
}else ar[p]=x;
}
int getans(){
int cur=0;
for(int blk=1;blk<=vbn;blk++){
int L=lp[blk],R=rp[blk];
if(cur+1<b[L]) return cur+1;
if(cur+1>=b[R]) cur+=sum[blk];
else{
for(int i=L;i<=R;i++){
if(b[i]>cur+1) return cur+1;
cur+=cnt[i]*b[i];
}
}
}
return cur+1;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read(),m=read();
int bn=0;
for(int i=1;i<=n;i++){
a[i]=read();
tmp[i]=a[i];
b[++bn]=a[i];
}
int cm=0,qm=0;
for(int i=1;i<=m;i++){
int op;
op=read();
if(op==1){
int x,y;
x=read(),y=read();
chg[++cm].p=x;
chg[cm].old=tmp[x];
chg[cm].nw=y;
tmp[x]=y;
b[++bn]=y;
}else{
int L,R;
L=read();
R=read();
q[++qm].l=L; q[qm].r=R; q[qm].t=cm; q[qm].x=qm;
}
}
sort(b+1,b+bn+1);
vn=unique(b+1,b+bn+1)-b-1;
int vB=sqrt(vn)+1;
vbn=(vn+vB-1)/vB;
for(int i=1;i<=vn;i++) pos[i]=(i-1)/vB+1;
for(int i=1;i<=vbn;i++){
lp[i]=(i-1)*vB+1;
rp[i]=min(i*vB,vn);
}
for(int i=1;i<=n;i++) ar[i]=lower_bound(b+1,b+vn+1,a[i])-b;
for(int i=1;i<=cm;i++){
chg[i].old=lower_bound(b+1,b+vn+1,chg[i].old)-b;
chg[i].nw=lower_bound(b+1,b+vn+1,chg[i].nw)-b;
}
if(cm==0){
B=max(1LL,(long long)(1.0*n/sqrt((double)max(1LL,qm))));
}else{
B=max(1LL,(long long)(
powl((long double)n,2.0L/3.0L)*
powl((long double)cm,1.0L/3.0L)/
powl((long double)max(1LL,qm),1.0L/3.0L)
));
}
B*=1.75;
sort(q+1,q+qm+1,cmp);
for(int i=1;i<=qm;i++){
while(t<q[i].t){ t++; update(chg[t].p,chg[t].nw); }
while(t>q[i].t){ update(chg[t].p,chg[t].old); t--; }
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(l<q[i].l) del(l++);
while(r>q[i].r) del(r--);
ans[q[i].x]=getans();
}
for(int i=1;i<=qm;i++) write(ans[i]);
fwrite(obuf,1,pp-obuf,stdout);
return 0;
}