题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4267
解题 方法 利用 ( i - a )%k —> i%k == a%k 存进去 所有的余数;
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> using namespace std; int cnt[51234],N; int arr[12][12]; struct date { int lt,rt,num[56]; bool fell; }node[200005]; void build( int lt,int rt,int t ) { node[t].lt = lt; node[t].rt = rt;node[t].fell = false; memset(node[t].num,0,sizeof(node[t].num)); if( node[t].lt == node[t].rt ) return; int mid = (lt+rt)>>1; build( lt,mid,t<<1); build( mid+1,rt,t<<1|1); } void push_up( int t ) { for( int i = 0; i <= 55; i++ ) { node[t<<1].num[i] += node[t].num[i]; node[t<<1|1].num[i] += node[t].num[i]; node[t].num[i] = 0; node[t<<1].fell = node[t<<1|1].fell = true; node[t].fell = false; } } void update( int a,int lt,int rt,int k,int val,int t ) { if( node[t].lt == lt && node[t].rt == rt ) { node[t].fell = true; node[t].num[arr[k][(a%k)]] += val; return ; } if( node[t].fell ) push_up( t ); if( node[t<<1].rt >= rt ) update( a,lt,rt,k,val,t<<1 ); else if( node[t<<1|1].lt <= lt ) update( a,lt,rt,k,val,t<<1|1 ); else { update( a,lt,node[t<<1].rt,k,val,t<<1 ); update( a,node[t<<1|1].lt,rt,k,val,t<<1|1 ); } } int query( int pos,int t,int temp ) { if( node[t].lt == node[t].rt ) { for( int i = 1; i <= 10; i++ ) temp += node[t].num[arr[i][(pos%i)]]; return temp; } if( node[t].fell ) push_up( t ); if( pos <= node[t<<1].rt ) return query( pos,t<<1,temp ); else return query( pos,t<<1|1,temp ); } int main( ) { int i,j,M,tab,a,b,k,c,pos,t; for( t = 1,i = 1; i <= 10; i++ ) for( j = 0; j < i; j++ ) arr[i][j] = t++; while( scanf("%d",&N) != EOF ) { for( i = 1; i <= N; i++ ) scanf("%d",&cnt[i]); build( 1,N,1 ); scanf("%d",&M); for( i = 1; i <= M; i++ ) { scanf("%d",&tab); if( tab == 2 ) { scanf("%d",&pos); printf("%d\n",query( pos,1,0 )+cnt[pos]); } else { scanf("%d%d%d%d",&a,&b,&k,&c ); update( a,a,b,k,c,1 ); } } } return 0; }