Hdu 4267 A Simple Problem with Integers
A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1038 Accepted Submission(s): 371
Problem Description
Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. The other is to query the value of some element.
Input
There are a lot of test cases.
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)
The first line contains an integer N. (1 <= N <= 50000)
The second line contains N numbers which are the initial values of A1, A2, ... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1 a b k c" means adding c to each of Ai which satisfies a <= i <= b and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10, -1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)
Output
For each test case, output several lines to answer all query operations.
Sample Input
4
1 1 1 1
14
2 1
2 2
2 3
2 4 1 2 3
1 2
2 1 2 2 2 3 2 4 1 1 4 2 1 2 1 2 2 2 3 2 4
Sample Output
1 1 1 1 1 3 3 1 2 3 4 1
Source
Recommend
liuyiding
//怎么说呢、自己真的好弱
//线段树关键在于节点存储信息的意义便于求解
//本来不连续的更新,通过研究、使其成段更新,
h[i][j]代表某个数mod i 的余数为j ,共有55种情况
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <queue> #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; #define N 50010 struct node { int rec[55]; int sum; }st[N<<2]; int h[11][11]; void build(int l,int r,int k) { int m; for(m=0;m<55;m++) st[k].rec[m]=0; if(l==r) { scanf("%d",&st[k].sum); return ; } m=(l+r)>>1; build(lson); build(rson); } int kk,c,remain; void update(int &L,int &R,int l,int r,int k) { if(L<=l&&R>=r) { st[k].rec[h[kk][remain]]+=c; return ; } int m=(l+r)>>1; if(L<=m) update(L,R,lson); if(R>m) update(L,R,rson); } void down(int &k) { int i; for(i=0;i<55;i++) { if(st[k].rec[i]){ st[k<<1].rec[i]+=st[k].rec[i]; st[k<<1|1].rec[i]+=st[k].rec[i]; st[k].rec[i]=0; } } } int query(int &id,int l,int r,int k) { if(l==r) { int i,t=0; for(i=1;i<=10;i++) t+=st[k].rec[h[i][id%i]]; return t+st[k].sum; } int m=(l+r)>>1; down(k); if(id<=m) return query(id,lson); else return query(id,rson); } int main() { int n; int q,i,a,b=0; for(i=1;i<=10;i++) for(a=0;a<i;a++) h[i][a]=b++; while(scanf("%d",&n)!=EOF) { build(1,n,1); scanf("%d",&q); while(q--) { scanf("%d",&i); if(i==1) { scanf("%d %d %d %d",&a,&b,&kk,&c); remain=a%kk; update(a,b,1,n,1); } else { scanf("%d",&a); printf("%d\n",query(a,1,n,1)); } } } return 0; }