Hduoj【树状数组】【-.-】

/*A Simple Problem with Integers
Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3885    Accepted Submission(s): 1197


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)

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
2012 ACM/ICPC Asia Regional Changchun Online 
*/
#include<stdio.h>
#include<string.h>
int a1[50010], c1[11][11][50010];
int lowbit(int x)
{
	return x&(-x);
}
int sum(int x, int a, int n)
{
	int sum = 0, i;
	while(x <= n)
	{
		for(i  = 1; i <= 10 ; i++)
		{
			sum += c1[i][a%i][x];
			
		}
		x += lowbit(x);
	}
	return sum;
}
void change(int i, int k, int k1, int e)
{
	while(i > 0)
	{
		c1[k][k1][i] += e;
		i -= lowbit(i);
	}
}
int main()
{
	int i, j, k, m, n;
	while(scanf("%d", &n) != EOF)
	{
		memset(c1, 0, sizeof(c1));
		for(i = 1; i <= n; i++)
			scanf("%d", &a1[i]);
		scanf("%d", &m);
		while(m--)
		{
			int o, a, b, c, d;
			scanf("%d", &o);
			if(o == 1)
			{
				scanf("%d%d%d%d", &a, &b , &c, &d);
				change(a-1, c, a%c, -d);//a前面的全-d 
				change(b, c, a%c, d);//b到前面的全+d 
			}
			else
			{
				scanf("%d", &a);
				printf("%d\n", sum(a,a,n) + a1[a]);
			}
		} 
	}
	return 0;
}

题意:给出一个数组,分别对每隔k个点进行加减,并且要实现点查询。

体会 :难,我只是套用了别人的模板,没理解到底什么意思,等来年有机会了定回头来搞定它。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值