标签:val scanf ptree == cst char har i++ ase
单点更新(HDU1166)
#include <cstdio> #include <iostream> using namespace std; const int MAXN=2e5+10; const int MAXNODE=MAXN<<2;//一般开到4倍 int sum; struct node { int l; int r; int value; }tree[MAXNODE]; int father[MAXN];//用来储存第x个元素在线段数的结点位置 void BuildTree(int i,int l,int r) { tree[i].l=l; tree[i].r=r; tree[i].value=0; if(l==r) { father[l]=i; return; } BuildTree(i<<1,l,(l+r)/2); BuildTree((i<<1)+1,(l+r)/2+1,r); } void UpTree(int ri) { if(ri==1) return; int fi=ri/2; int a=tree[fi<<1].value; int b=tree[(fi<<1)+1].value; tree[fi].value=a+b; UpTree(fi); } int MAX; void Query(int i,int l,int r) { if(tree[i].l==l&&tree[i].r==r) { sum+=tree[i].value; return; } i=i<<1; if(l<=tree[i].r) { if(r<=tree[i].r) Query(i,l,r); else Query(i,l,tree[i].r); } i++; if(r>=tree[i].l) { if(l>=tree[i].l)Query(i,l,r); else Query(i,tree[i].l,r); } } int main() { int n,m,g,a,b,t; char ch[100]; scanf("%d",&t); for(int k=1;k<=t;k++) { scanf("%d",&n); BuildTree(1,1,n); for(int i=1;i<=n;i++) { scanf("%d",&g); tree[father[i]].value=g; UpTree(father[i]); } printf("Case %d:\n",k); while(scanf(" %s",ch)!=EOF) { sum=0; if(ch[0]==‘E‘) break; scanf("%d%d",&a,&b); if(ch[0]==‘Q‘) { Query(1,a,b); printf("%d\n",sum); } else if(ch[0]==‘A‘) { tree[father[a]].value+=b; UpTree(father[a]); } else if(ch[0]==‘S‘) { tree[father[a]].value-=b; UpTree(father[a]); } } } return 0; }
标签:val scanf ptree == cst char har i++ ase
原文地址:http://www.cnblogs.com/a249189046/p/7353311.html