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 41
多个树状数组
根据操作,要每次隔开操作,为了使用树状数组,所以按照k的大小和余数分成多个树状数组。
把不同的点放到同一个线上,用树状数组记录操作的情况,然后最后相加。
#include<iostream> #include<algorithm> #include<math.h> #include<cstdio> #include<cstring> #include<string> using namespace std; const int maxn = 50005; const int low(int x){ return (x&-x); } int A[maxn], n, m, i, j, f[11][10][maxn]; void add(int x, int y, int z, int w) { for (int i = z; i <= n; i += low(i)) f[x][y][i] += w; } long long sum(int x, int y, int z) { long long total = 0; for (int i = z; i > 0; i -= low(i)) total += f[x][y][i]; return total; } int main(){ while (~scanf("%d", &n)) { memset(A, 0, sizeof(A)); memset(f, 0, sizeof(f)); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); cin >> m; while (m--) { int a, b, c, d, e; scanf("%d%d", &e, &a); if (e == 2) { long long tot = A[a]; for (int i = 1; i <= 10; i++) tot += sum(i, a%i, (a - 1) / i + 1); cout << tot << endl; } else { scanf("%d%d%d", &b, &c, &d); add(c, a%c, (a - 1) / c + 1 , d); add(c, a%c, (b - (a - 1) % c - 1) / c + 2, -d); } } } return 0; }