注意可能会爆int哦
代码:
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define esp 0.00000000001 const int N=2e5+10,M=1e6+10,inf=1e9; ll maxx[N<<2]; void buildtree(int l,int r,int pos) { if(l==r) { maxx[pos]=0; return; } int mid=(l+r)>>1; buildtree(l,mid,pos<<1); buildtree(mid+1,r,pos<<1|1); maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]); } void update(int p,ll c,int l,int r,int pos) { if(l==p&&r==p) { maxx[pos]+=c; return; } int mid=(l+r)>>1; if(p>mid) update(p,c,mid+1,r,pos<<1|1); else update(p,c,l,mid,pos<<1); maxx[pos]=max(maxx[pos<<1],maxx[pos<<1|1]); } ll query(int L,int R,int l,int r,int pos) { if(L<=l&&R>=r) return maxx[pos]; int mid=(l+r)>>1; ll ans=0; if(L<=mid) ans=max(ans,query(L,R,l,mid,pos<<1)); if(R>mid) ans=max(ans,query(L,R,mid+1,r,pos<<1|1)); return ans; } int main() { int x,y,i,t; while(~scanf("%d%d",&x,&t)) { buildtree(1,x,1); for(i=0;i<t;i++) { int flag; scanf("%d",&flag); if(flag==1) { int p; ll c; scanf("%d%lld",&p,&c); update(p,c,1,x,1); } else { int u,v; scanf("%d%d",&u,&v); printf("%lld\n",query(u,v,1,x,1)); } } } return 0; }