标签:style blog color io os ar for div sp
今天终于看懂树状函数了 看懂之后果然感觉比线段树简单便捷地多
就拿这题简单的单点更新来练手了
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n; int a[50000+10]; int c[50000+10]; int lowbit(int x) { return x&(-x); } int sum(int x) { int ret=0; while(x>0) { ret+=c[x];x-=lowbit(x); } return ret; } void add(int x,int d) { while(x<=n) { c[x]+=d;x+=lowbit(x); } } int main() { int t,coun=1; int i,j; char s[30]; cin>>t; while(t--) { memset(c,0,sizeof(c)); printf("Case %d:\n",coun++); cin>>n; for(i=1;i<=n;i++) { scanf("%d",&a[i]); add(i,a[i]); } while(scanf("%s",s)) { if(s[0]==‘E‘) break; else if(s[0]==‘A‘||s[0]==‘S‘) { int x,d; scanf("%d%d",&x,&d); if(s[0]==‘S‘) d*=-1; add(x,d); } else if(s[0]==‘Q‘) { int l,r,ans; scanf("%d%d",&l,&r); ans=sum(r)-sum(l-1); printf("%d\n",ans); } } } return 0; }
标签:style blog color io os ar for div sp
原文地址:http://www.cnblogs.com/sola1994/p/3979932.html