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): 2178    Accepted Submission(s): 709


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

题意:

应该很简单了,自行解决吧

解析:

这题因为数据规模是50000,所以说要是最坏的情况的话,即a=1,b=50000,k=1时间规模就是50000*50000,肯定超时(当然竞赛的时候也可以直接编试试人品,说不定这就是个水题,直接过了呢,事实上我试了,的确过不了啊,超时),然后查了解题报告之后才发现可以用树状数组来求。

关于树状数组的话,结合我前面的结题报告poj 3067 japan这一题的话,其实也挺好求的。主要思路就是:把2需要输出的结果分成两部分,一部分是原有的,一部分是修改的,输出的时候就是原有的加上修改的就行了。而这里面修改的部分就可以用树状数组来模拟了。

树状数组分成两种:一种是修改点来求区间的和,如poj 3067 japan,另一种就是对区间做出修改来求点的值,但是这一种只能修改区间上连续的点,而这一题是隔k的值修改,所以就很巧妙的把对于取余k相同的点全都存在另一个数组里。对于这两种来说的话,第一种是向上修改,向下统计,第二种是向下修改,向上统计(这是为什么我还没搞懂,但是先这么记着吧,详见大牛网址http://blog.csdn.net/shahdza/article/details/6314818

#include<stdio.h>
#include<string.h>
#define Max 50001
int n,num[Max];

int node[11][11][Max];//第一个存k的值,第二个存num数组下标取余k的值,第三个就是树状数组了


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

void update(int border,int k,int c,int mod)
{
	for(int i = border ; i > 0;i -= lowbit(i))//向下修改
		node[k][mod][i] += c;
}

int getvalue(int x)
{
	int ans = 0;
	for(int k = 1 ; k <= 10 ; k ++)//假如2的前面已经进行了多次1操作且k不同的话,就需要枚举所有的k的值都加一遍
	{
		int mod = x % k;
		for(int j = x ; j <= n; j += lowbit(j))//向上统计
			ans += node[k][mod][j];
	}
	return ans;
}

int main()
{
	while(scanf("%d",&n) != EOF)
	{
		memset(num,0,sizeof(num));
		memset(node,0,sizeof(node));
		for(int i = 1; i <= n ; i ++)//经验表明,从1开始最好
			scanf("%d",&num[i]);

		int op,count;
		scanf("%d",&count);
		while(count--)
		{
			scanf("%d",&op);
			if(op == 1)
			{
				int a,b,k,c;
				scanf("%d%d%d%d",&a,&b,&k,&c);
				update(b,k,c,a%k);		//把从1到b所有的都+c
				update(a-1,k,-c,a%k);		//把从1到a所有的都-c,相当于从a到b的加c
			}
			else
			{
				int x;
				scanf("%d",&x);
				int sum = getvalue(x);
				printf("%d\n",sum + num[x]);//把原有值和增加量加在一起
			}
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值