HDU - 4267 A Simple Problem with Integers(树状数组的逆操作)

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)

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 

题意:给你两个操作1 a b k c代表的是将区间[a, b]之间的能使得(i-a)%k = 0的数加上c,2 a是查询值

思路:用树状数组做,但是和一般的树状数组不一样,这里是将sum和add操作交换了(这点需要好好理解一下,我们通过两次的以前的sum操作将区间[a, b]赋值,然后再通过以前的add操作找到可以影响到它的结点求总的操作值),将取模公式转换成:i%k=a%k,那么我们注意到k是比较小的,所以我们可以先记录a%k时操作的结果,也就是开arr[x][k][a%k]表示,最后再求和计算x坐标的结果是枚举余数就行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 50005;

int arr[maxn][11][11];
int n, num[maxn];

inline int lowbit(int x) {
	return x & (-x);
}  

inline void Add(int x, int k, int mod, int c){
	while (x > 0) {
		arr[x][k][mod] += c;
		x -= lowbit(x);
	}
}  

inline int sum(int x, int a) {
	int ans = 0;
	while (x < maxn) {
		for (int i = 1; i <= 10; i++){
			ans += arr[x][i][a%i];
		}
		x += lowbit(x);
	}
	return ans;
}  

int main() {
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; i++)
			scanf("%d", &num[i]);
		memset(arr, 0, sizeof(arr));
		int m;
		scanf("%d", &m);
		while (m--) {
			int op, a, b, k, c;
			scanf("%d", &op);
			if (op == 1) {
				scanf("%d%d%d%d", &a, &b, &k, &c);
				Add(b, k, a%k, c);
				Add(a-1, k, a%k, -c);
			}
			else{
				scanf("%d", &a);
				int ans = sum(a, a);
				printf("%d\n", ans + num[a]);
			}
		}
	}
	return 0;
}
时间: 2024-10-13 23:59:39

HDU - 4267 A Simple Problem with Integers(树状数组的逆操作)的相关文章

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) 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

poj 3468 A Simple Problem with Integers 树状数组 或 线段树

题目链接:http://poj.org/problem?id=3468 一般说来 树状数组是 [单点更新 区间查询] 而本题是“区间更新 区间查询” 所以要改成维护前缀和 推公式的过程见<挑战程序设计竞赛>第181~182面 代码中bit0是i的零次项 bit1是i的一次项 以后以此类推 在[l, r]加数的时候 写出公式 在l的地方 一次项及以上的直接写 然后在零次项那减去 在r的地方 一次项及以上的减掉之前加上的 然后再零次项那加上“公式化简之后的最终结果 减去 之前在零次项加的那一项”

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

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): 3708    Accepted Submission(s): 1139 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

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): 4191    Accepted Submission(s): 1309 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

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): 5402    Accepted Submission(s): 1710 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状数组的单点查询:求某点a的值就是求数组中1~a的和. (i-a)%k==0把区间分隔开了,不能直接套用树状数组的区间修改单点查询 这道题的K很小,所以可以枚举k,对于每个k,建立k个树状数组,所以一共建立55棵树 所以就可以多建几棵树..然后就可以转换为成段更新了~~ [AC] 1 #include

POJ3468 A Simple Problem with Interger [树状数组,差分]

题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 130735   Accepted: 40585 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One

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 a