正题
题意
经典线段树题,不过是单点修改。
解题思路
直接线段树
代码
#include<cstdio>
using namespace std;
struct treenode{int l,r,cover;
}tree[400000];
int n,m,x,y;
char c;
void build(int k,int a,int b)//线段树
{tree[k].l=a;tree[k].r=b;if (a==b) return;int wz=(a+b)/2;build(k*2,a,wz);build(k*2+1,wz+1,b);
}
int find(int k,int a,int b)//查找
{if (tree[k].r==b && tree[k].l==a) return tree[k].cover;//返回值if (b<=tree[k*2].r) return find(k*2,a,b);else if (a>=tree[k*2+1].l) return find(k*2+1,a,b);else return find(k*2,a,tree[k*2].r)+find(k*2+1,tree[k*2+1].l,b);
}
void updata(int k,int a,int num)//修改
{if (tree[k].r==a && tree[k].l==a) {tree[k].cover+=num;//修改return;}if (a<=tree[k*2].r) updata(k*2,a,num);else updata(k*2+1,a,num);tree[k].cover=tree[k*2].cover+tree[k*2+1].cover;//更新
}
int main()
{scanf("%d%d",&n,&m);build(1,1,n);for (int i=1;i<=m;i++){scanf("\n%c%d%d",&c,&x,&y);if (c=='M'){updata(1,x,y);}else{printf("%d\n",find(1,x,y));}}
}