标签:des style blog io color ar os sp java
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Case 1: 6 33 59#include<stdio.h> #include<string.h> #define MAX 50010 int a[MAX]; int n; int lowbit(int N) { return N&(-N); } void sum(int i,int M) { while(i<=MAX) { a[i]+=M; i+=lowbit(i); } } int query(int j)//求前j个营地的总人数。 { int sum=0; while(j>0) { sum+=a[j]; j-=lowbit(j); } return sum; } int main() { int i,T,t,k,l,kase=1; char s[10]; scanf("%d",&T); while(T--) { memset(a,0,sizeof(a)); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&l); sum(i,l); } printf("Case %d:\n",kase++); while(1) { scanf("%s",s); if(!strcmp(s,"Query")) { scanf("%d%d",&k,&t); printf("%d\n",query(t)-query(k-1)); } if(!strcmp(s,"Add")) { scanf("%d%d",&k,&t); sum(k,t); } if(!strcmp(s,"Sub")) { scanf("%d%d",&k,&t); sum(k,-t); } if(!strcmp(s,"End")) break; } } return 0; }
标签:des style blog io color ar os sp java
原文地址:http://blog.csdn.net/hdd871532887/article/details/41217829