```cpp
#include<bits/stdc++.h>
using namespace std;
inline long long read(){
long long x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-48,ch=getchar();
return x*t;
}
const long long SIZE=1000005;
struct Tree{
long long sum,l,r;
}t[SIZE*4];
long long a[SIZE],n,ques;
void build(long long l,long long r,long long p){
t[p].l=l,t[p].r=r;
if(l==r){t[p].sum=a[l];return;}
long long mid=l+r>>1;
build(l,mid,p*2);
build(mid+1,r,p*2+1);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void change(long long p,long long x,long long k){
if(t[p].l==t[p].r){t[p].sum+=k;return;}
long long mid=t[p].l+t[p].r>>1;
if(x<=mid)change(p*2,x,k);
else change(p*2+1,x,k);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
long long ask(long long p,long long l,long long r){
if(t[p].l==t[p].r)return t[p].sum;
if(t[p].l>r||t[p].r<l)return 0;
long long mid=t[p].l+t[p].r>>1,s=0;
if(l<=mid)s+=ask(p*2,l,r);
if(r>mid)s+=ask(p*2+1,l,r);
return s;
}
int main(){
n=read(),ques=read();
for(register int i=1;i<=n;i++)a[i]=read();
build(1,n,1);
while(ques--){
long long opt=read();
if(opt==1){
long long i=read(),x=read();
change(1,i,x);
}
if(opt==2){
long long l=read(),r=read();
printf("%d\n",ask(1,l,r));
}
}
return 0;
}
```
by AffineRing @ 2021-01-03 09:06:26
我已经尽力卡常了
by AffineRing @ 2021-01-03 09:07:14
`i++`改成`++i`
by houpingze @ 2021-01-03 09:11:08
@[Point_Lord](/user/399250) 这题不需要long long我记得,你和我以前犯了同样的错误:用"%d"输出long long
by w23c3c3 @ 2021-01-03 09:21:25
```
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
```
by MilkyCoffee @ 2021-01-03 09:21:37
还有把l,r放到函数参数里面,开内存也要时间
位运算代替中间的`*2`和`*2+1`
~~结构体比较慢(?~~
by w23c3c3 @ 2021-01-03 09:30:52
而且这题不卡线段树常数吧,我的线段树最慢的才跑300ms
by w23c3c3 @ 2021-01-03 09:31:35
@[w23c3c3](/user/109942) 据说位运算都是在自欺欺人,这点优化编译器好像会自己给你加上
by HMP_Haoge @ 2021-01-03 09:33:49
你把全部long long换成int,这题不卡long long
by HMP_Haoge @ 2021-01-03 09:35:18
这样子吗,那我不懂了
by w23c3c3 @ 2021-01-03 09:37:28