标签:algo int lowbit sum dbr ring nbsp pre 模板
Problem Description
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 50000+5; 6 int aa[N]; 7 int c[N]; 8 int n; 9 int lowbit(int x) { 10 return x&-x; 11 } 12 13 void add(int x, int a) { 14 while(x <= n){ 15 c[x] += a; 16 x += lowbit(x); 17 } 18 } 19 20 int sum(int x) { 21 int ans=0; 22 while(x) { 23 ans += c[x]; 24 x -= lowbit(x); 25 } 26 return ans; 27 } 28 29 int main(){ 30 int t,a,b,cas; 31 char op[20]; 32 scanf("%d",&t); 33 cas=1; 34 while(t--){ 35 scanf("%d",&n); 36 memset(c,0,sizeof(c));//WA在了这里 37 for(int i=1;i<=n;i++){ 38 scanf("%d",&aa[i]); 39 add(i, aa[i]); 40 } 41 printf("Case %d:\n",cas++); 42 while(scanf("%s",op),op[0]!=‘E‘){ 43 scanf("%d%d",&a,&b); 44 if(op[0]==‘Q‘){ 45 printf("%d\n",sum(b)-sum(a-1)); 46 } 47 else if(op[0]==‘S‘) 48 add(a, -b); 49 else 50 add(a, b); 51 } 52 } 53 return 0; 54 }
标签:algo int lowbit sum dbr ring nbsp pre 模板
原文地址:https://www.cnblogs.com/cake-lover-77/p/10297884.html