```cpp
int find(int p,int x,int y)
{
if(x<=t[p].l&&y>=t[p].r)return t[p].sum;
int mid=(t[p].l+t[p].r)/2;
int s=0;
if(x<=mid)s+=find(p*2,x,y);
if(y>mid)s+=find(p*2+1,x,y);
return s;
}
```
by Falashiro @ 2020-01-31 15:30:18
@[BinaryTree](/user/204619) change对的啊
by Falashiro @ 2020-01-31 15:30:37
对的吗...~~说实话我没看懂他写的啥~~,反正我线段树跟他写法不是一个样子的
by pocafup @ 2020-01-31 15:37:16
@[Forever_Pursuit](/user/101800) 谢谢。但我按lxl的那个写法又模仿了一个find
```cpp
int find(int p,int x,int y)
{
if(x==t[p].l&&y==t[p].r)return t[p].sum;
int mid=(x+y)/2;
if(x>mid)return find(p*2,x,y); //完全在右子树
else if(y<=mid)return find(p*2+1,x,y); //完全在左子树
else return find(p*2,x,mid)+find(p*2+1,mid+1,y); //左右子树分别递归
}
```
这个为什么不对啊qwq
by wwhOvO @ 2020-01-31 15:45:05
@[BinaryTree](/user/204619) 原来的注释对的啊
by Falashiro @ 2020-01-31 15:50:44
第一行是
if(x<=t[p].l&&y>=t[p].r)return t[p].sum;
by Falashiro @ 2020-01-31 15:51:13
```cpp
if(x>mid)return find(p*2+1,x,y); //完全在右子树
else if(y<=mid)return find(p*2,x,y); //完全在左子树
```
by Falashiro @ 2020-01-31 15:52:28
lxl的写法通常是受树套树,leafy这种东西的影响的。。。线段树建议创造自己的写法
by 万弘 @ 2020-01-31 15:53:07
@[Forever_Pursuit](/user/101800) 谢谢。但是下面最终提交的代码为啥只能70分啊,又3个点RE了。。。
```cpp
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct SegmentTree
{
int l,r,dat;
long long sum;
};
SegmentTree t[1000001];
int n,m,a[1000001];
void build(int p,int x,int y)
{
t[p].l=x; t[p].r=y;
if(x==y){t[p].sum=a[x];return ;}
int mid=(x+y)/2;
build(p*2,x,mid);
build(p*2+1,mid+1,y);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void change(int p,int x,int v)
{
if(t[p].l==t[p].r){t[p].sum+=v;return ;}
int mid=(t[p].l+t[p].r)/2;
if(x<=mid)change(p*2,x,v);
else change(p*2+1,x,v);
t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
int find(int p,int x,int y)
{
if(x<=t[p].l&&y>=t[p].r)return t[p].sum;
int mid=(t[p].l+t[p].r)/2;
if(x>mid)return find(p*2+1,x,y);
else if(mid>=y)return find(p*2,x,y);
else return find(p*2,x,mid)+find(p*2+1,mid+1,y);
}
int main()
{
int w,x,y;
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
build(1,1,n);
for(int i=1;i<=m;i++)
{
cin>>w>>x>>y;
if(w==1)change(1,x,y);
else if(w==2)cout<<find(1,x,y)<<endl;
}
return 0;
}
```
by wwhOvO @ 2020-01-31 15:59:58
@[BinaryTree](/user/204619) 早就说了数组开小了……
by Falashiro @ 2020-01-31 16:01:15