HDU 4267 A Simple Problem with Integers 树状数组

来源:http://acm.hdu.edu.cn/showproblem.php?pid=4267

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。

思路:很容易想到这是一个插线问点的问题,但是在更新值 的时候和平常的更新不同,这道题又增加了一个限制条件。因为k比较小(<=10),余数的情况也很少,因此我们可以开55棵树状数组,用cnt[x][k][mod] 表示x 对k 取余为mod,这样在询问的时候只要循环到10就可以 了。而且(i - a) % k == 0即是 i % k == a % k == mod,这样更新的时候就可以更新(1,b) 和(1,a)了。

代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 50010;
int cnt[N][11][11];
int num[N];
int inline lowbit(int x){
	return x & (-x);
}
void inline update(int x,int k,int mod,int add){
	while(x > 0){
	  cnt[x][k][mod] += add;
	  x -= lowbit(x);
	}
}
int inline sum(int x,int a){
	int s = 0;
	while(x < N){
		for(int i = 1; i <= 10; ++i){
		   s += cnt[x][i][a%i];
		}
		x += lowbit(x);
	}
	return s;
}
int main(){
	//freopen("1.txt","r",stdin);
	int n;
	while(scanf("%d",&n) != EOF){
	   for(int i = 1; i <= n; ++i)
		   scanf("%d",&num[i]);
	   memset(cnt,0,sizeof(cnt));
	   int m;
	   scanf("%d",&m);
	   while(m--){
	      int op,a,b,k,add;
		  scanf("%d",&op);
		  if(op == 1){
		     scanf("%d%d%d%d",&a,&b,&k,&add);
			 update(b,k,a%k,add);
			 update(a-1,k,a%k,-add);
		  }
		  else{
		    scanf("%d",&a);
			int ans = sum(a,a);
			printf("%d\n",ans + num[a]);
		  }
	   }
	}
	return 0;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值