A Simple Problem with Integers POJ - 3468

A Simple Problem with Integers

 POJ - 3468 

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

 

题目大意:

要求能够通过快速查询给定区间内的值(l,r)的和或者给定区间都加上一个数X

Q表示查询区间内的和

C 表示给定区间内的每一个值加上X

 

线段树代码:需要注意线段树的内存分配需要4*n

#include<iostream>
#include<stdio.h> 
using namespace std;
typedef long long ll;

//const int DATSIZE = (1<<18)-1;
const int DATSIZE = 100010<<2;

ll Asum[DATSIZE];
ll Bsum[DATSIZE];

char Qc[100005];
int Ql[100005];
int Qr[100005];

void add(int a,int b, int x, int k , int l, int r){
	if(a<=l && r<=b){
		Asum[k]+=x;
	}else if(l<b && r>a){
		Bsum[k]+=(min(r,b) - max(a,l))*x;
		add(a,b,x,k*2+1,l,(l+r)/2);	
		add(a,b,x,k*2+2,(l+r)/2,r);
	}
}

ll sum(int a,int b, int k,int l,int r){
	if(r<=a || b<=l) return 0;
	else if(a<=l && r<=b){
		return Asum[k]*(r-l)+Bsum[k];
	}else{
		ll res = Asum[k]*(min(r,b) - max(a,l));
		res+= sum(a,b,k*2+1,l,(l+r)/2);
		res+= sum(a,b,k*2+2,(l+r)/2,r);
		return res;
	}
}


int main(){
//	printf("%lld\n",DATSIZE);
	int N;
	int Q;
	scanf("%d %d",&N,&Q);
	for(int i=0;i<N;i++){
		int input;
		scanf("%d",&input);
		add(i,i+1,input,0,0,N);
	}
	for(int i=0;i<Q;i++){
		char q_c;
		int left;
		int right; 
		int add_x;
		getchar();
		scanf("%c",&q_c);
		if(q_c=='C'){
			scanf("%d %d %d",&left,&right,&add_x);
			add(left-1,right,add_x,0,0,N);
			
		}else if(q_c=='Q'){
			scanf("%d %d",&left,&right);
			printf("%lld\n",sum(left-1,right,0,0,N));
		}else{
			printf("error\n");
		}	
	}
	return 0;
} 

树状数组代码:需要注意树状数组分配需要2*n

#include<iostream>
#include<stdio.h> 
using namespace std;
typedef long long ll;

//const int DATSIZE = (1<<18)-1;
const int DATSIZE = 100010<<1;

ll bit0[DATSIZE];
ll bit1[DATSIZE];

void add(ll *bit,int n,int i,int x){
	while(i<=n){
		bit[i]+=x;
		i+=-i&i;		
	}
}


ll sum(ll *bit,int n,int i){
	
	ll res =0;	
	while(i>0){
		res+=bit[i];
		i-=-i&i;		
	}
	return res;
}

int main(){
//	printf("%lld\n",DATSIZE);
	int N;
	int Q;
	scanf("%d %d",&N,&Q);
	for(int i=1;i<=N;i++){
		int input;
		scanf("%d",&input);
		add(bit0,N,i,input);
	}
	for(int i=0;i<Q;i++){
		char q_c;
		int left;
		int right; 
		int add_x;
		getchar();
		scanf("%c",&q_c);
		if(q_c=='C'){
			scanf("%d %d %d",&left,&right,&add_x);
			add(bit0,N,left,-add_x*(left-1));
			add(bit0,N,right+1,add_x*right);
			add(bit1,N,left,add_x);
			add(bit1,N,right+1,-add_x);
		}else if(q_c=='Q'){
			scanf("%d %d",&left,&right);
			ll res = sum(bit0,N,right)+(right)*sum(bit1,N,right);
			res -= (sum(bit0,N,left-1)+(left-1)*sum(bit1,N,left-1)); 
			printf("%lld\n",res);
		}else{
			printf("error\n");
		}	
	}
	return 0;
} 

bit0和bit1分别进行不同的维护

bit1 用于维护对应区间所共同相加的值x(即区间内每一个元素都加上x)

bit0用于维护除了bit1所维护的值

相应的

suma和sumb同理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值