HDU4267 树状数组 不连续区间修改(三维)
A Simple Problem with Integers
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
题解:
树状数组的区间修改,单点查询,注意这里的不连续区间修改
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long ll; const int N = 50000 + 10; int C[11][11][N],n; int getsum(int x,int a) { int sum = 0; while(x > 0) { for(int i = 1; i <= 10; i++) sum += C[i][a%i][x]; x -= x & (-x); } return sum; } void update(int a,int b,int x,int v) { while(x <= n) { C[a][b][x] += v; x += x & (-x); } } int main() { int o[N],a,b,c,k,q,t; while(~scanf("%d", &n)) { memset(C,0,sizeof(C)); for(int i = 1; i <= n; i++) scanf("%d", & o[i]); scanf("%d", &q); while(q --) { scanf("%d", &t); if(t == 1) { scanf("%d%d%d%d",&a,&b,&k,&c); update(k,a%k,a,c); update(k,a%k,b+1,-c); } else { scanf("%d", &a); printf("%d\n", getsum(a,a) + o[a]); } } } return 0; }