题目:A Simple Problem with Integers
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; const int N = 50100; struct node { int l; int r; int w; int add[55] ; }p[N*4]; int a[N], b[11][11]; void build(int x,int l,int r) { p[x].l = l; p[x].r = r; memset(p[x].add,0,sizeof(p[x].add)); if(l == r) { p[x].w = a[l]; return ; } int mid = (l + r)>>1; build(2*x,l,mid); build(2*x+1,mid+ 1,r); p[x].w = p[x*2].w + p[x*2+1].w ; } void push_down(int x) { if(p[x].w) { p[x*2].w +=p[x].w; p[x*2+1].w +=p[x].w; p[x].w = 0; for(int i =0 ; i < 55;i++) { p[x*2].add[i] +=p[x].add[i]; p[x*2+1].add[i]+=p[x].add[i]; p[x].add[i] = 0; } } } void update(int x,int l,int r,int num,int i,int j) { if(p[x].l == l&&p[x].r == r) { p[x].w +=num; p[x].add[b[i][j]]+=num ; return ; } push_down(x); int mid = (p[x].l + p[x].r)>>1 ; if(l > mid)update(x*2+1,l,r,num,i,j); else { if(r<=mid) update(x*2,l,r,num,i,j); else { update(x*2,l,mid,num,i,j); update(x*2+1,mid+1,r,num,i,j); } } } int query(int x,int pos) { if(p[x].l == pos&&p[x].r == pos) { int tmp = 0; for(int i = 1 ; i <= 10 ;i++) tmp+=p[x].add[b[i][pos%i]]; return tmp + a[pos] ; } push_down(x) ; int mid = (p[x].l + p[x].r)>> 1; if(pos <= mid) return query(2*x,pos); else return query(x*2+1,pos); } int main() { int n,s,e,k,c,i,j,q,ty; int cnt = 0; for(i = 1; i<= 10;i++) { for(j = 0;j< i;j++) b[i][j] = cnt++; //由于内存卡的紧,所以这样编号,不能开 10*10 的 } while(scanf("%d",&n)!=EOF) { for(i =1 ; i <=n;i++) { scanf("%d",&a[i]) ; } build(1,1,n); scanf("%d",&q); while(q--) { scanf("%d",&ty); if(ty==1) { scanf("%d%d%d%d",&s,&e,&k,&c); update(1,s,e,c,k,s%k); //这里为什么是 s% k 呢 ,因为 : } //(i- s)%k = (i%k - s%k + k)%k = 0 的话 i%k 应该 = s%k ; else { int pos; scanf("%d",&pos); int ans = query(1,pos); printf("%d\n",ans); } } } return 0; }