HDU 4267 A Simple Problem with Integers(线段树)

70 篇文章 0 订阅

A Simple Problem with Integers

Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5447    Accepted Submission(s): 1726


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

题意:对区间内所有对k取模余数相同的进行更新,查询某个值。
思路:果断线段树,也想到了延迟更新,但是中间实现的还是没想清楚。主要是因为每次需要取模对余数相同的进行更新,并不是对区间内所有都更新,这个地方比较难想。看了一下别人的题解发现用到了很巧妙的方法,直接空间换时间,建了55棵线段树,每棵线段树对应模k余数相同的,每次更新时都对余数相同的对应的线段树进行更新。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int MAX=50010;
int sum[55][MAX<<2];//线段树
int cot[MAX<<2];//是否更新 
int lo[15];//记录位置
int re[MAX];
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define delf int mid=(l+r)>>1

void build(int l,int r,int rt){
	cot[rt]=0;
	for(int i=0;i<55;i++)
		sum[i][rt]=0;
	if(l==r){
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	return;
}

void update(int L,int R,int k,int c,int l,int r,int rt){
	if(L<=l&&r<=R){
		int t=lo[k-1]+L%k;
		sum[t][rt]+=c;
		cot[rt]=1; 
		return;
	}
	int mid=(l+r)>>1;
	if(R<=mid){
		update(L,R,k,c,l,mid,rt<<1);
	}
	else if(L>mid){
		update(L,R,k,c,mid+1,r,rt<<1|1);
	}
	else{
		update(L,R,k,c,l,mid,rt<<1);
		update(L,R,k,c,mid+1,r,rt<<1|1);
	}
	return;
}

void pushdown(int rt){
	if(cot[rt]==0)
		return;
	cot[rt]=0;
	for(int i=0;i<55;i++){
		sum[i][rt<<1]+=sum[i][rt];
		sum[i][rt<<1|1]+=sum[i][rt];
		sum[i][rt]=0;
	}
	cot[rt<<1]=1;
	cot[rt<<1|1]=1;
	return;
}

int query(int l,int r,int rt,int k){
	if(l==r){
		int s=0;
		for(int i=1;i<=10;i++){
			s+=sum[lo[i-1]+k%i][rt];
		}
		return s;
	}
	pushdown(rt);
	int mid=(l+r)>>1;
	if(mid>=k)
		return query(l,mid,rt<<1,k);
	else
		return query(mid+1,r,rt<<1|1,k);
}

int main(){
	
	//freopen("data.txt","r",stdin);
	int n;
	lo[0]=0;
	lo[1]=1;
	for(int i=2;i<=10;i++)
		lo[i]=i+lo[i-1]; 
	while(~scanf("%d",&n)){
		build(1,n,1); 
		for(int i=1;i<=n;i++)
			scanf("%d",&re[i]);
		int m;
		scanf("%d",&m);
		while(m--){
			int q;
			scanf("%d",&q);
			if(q==1){
				int l,r,k,c;
				scanf("%d %d %d %d",&l,&r,&k,&c);	
				update(l,r,k,c,1,n,1);
			}
			else{
				int k;
				scanf("%d",&k);
				int s=re[k];
				s+=query(1,n,1,k);
				printf("%d\n",s);
			}
		}
	} 
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值