hdu 4267 A Simple Problem with Integers http://acm.hdu.edu.cn/showproblem.php?pid=4267
多个树状数组,区间跟新,单点询问,转化为单点更新,求和
View Code
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int N=50010; 6 int c[11][10][N]; 7 int a[N]; 8 int lowbit(int x) 9 { 10 return x&(-x); 11 } 12 void add(int x,int p,int maxn,int *c) 13 { 14 for(int i=p;i<=maxn;i+=lowbit(i)) c[i]+=x; 15 } 16 int sum(int p,int *c) 17 { 18 int s=0; 19 for(int i=p;i>0;i-=lowbit(i)) s+=c[i]; 20 return s; 21 } 22 int main() 23 { 24 int n; 25 while(~scanf("%d",&n)) 26 { 27 for(int i=0;i<n;i++) scanf("%d",&a[i]); 28 memset(c,0,sizeof(c)); 29 int m; 30 scanf("%d",&m); 31 while(m--) 32 { 33 int op; 34 scanf("%d",&op); 35 if(op==1) 36 { 37 int x,y,k,z; 38 scanf("%d%d%d%d",&x,&y,&k,&z); 39 x--; y--; 40 add(z,x/k+1,(n-x%k)/k+1,c[k][x%k]); 41 add(-z,(y-x%k)/k+2,(n-x%k)/k+1,c[k][x%k]); 42 } 43 else 44 { 45 int x; 46 scanf("%d",&x); 47 x--; 48 int ans=a[x]; 49 for(int i=1;i<=10;i++) ans+=sum(x/i+1,c[i][x%i]); 50 printf("%d\n",ans); 51 } 52 } 53 } 54 return 0; 55 }