#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int maxn=1e5+7; int belong[maxn],l[maxn],r[maxn],block,num,n,q; ll a[maxn],Max[maxn]; void build(){ block=sqrt(n); num=n/block;if(n%block!=0) num++; for(int i=1;i<=num;++i){//num 不要写成n l[i]=(i-1)*block+1;r[i]=i*block; } r[num]=n;//收敛 for(int i=1;i<=n;++i){ belong[i]=(i-1)/block+1; } for(int i=1;i<=n;++i){ Max[belong[i]]=max(Max[belong[i]],a[i]); } } void update(int x,int y){//单点更新 a[x]+=y; Max[belong[x]]=max(Max[belong[x]],a[x]); } ll ask(int x,int y){ //变量与数组重名 ll ans=-1; if(belong[x]==belong[y]){ for(int i=x;i<=y;++i){ ans=max(ans,a[i]); } return ans; } for(int i=x;i<=r[belong[x]];++i){ ans=max(ans,a[i]); } for(int i=belong[x]+1;i<belong[y];++i){ ans=max(ans,Max[i]); } for(int i=l[belong[y]];i<=y;++i){ ans=max(ans,a[i]); } return ans; } int main(){ scanf("%d%d",&n,&q); build(); int i,t,x,y; for(i=0;i<q;++i){ scanf("%d%d%d",&t,&x,&y); if(t==1){ update(x,y); } else{ printf("%lld\n",ask(x,y)); } } return 0; }
时间: 2024-10-16 01:59:39