POJ3468 A Simple Problem with Integers(线段树)

POJ3468 A Simple Problem with Integers(线段树)

Description
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 Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+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

题意

一道简单的线段树区间更新水题。线段树内维护的是各个区间中的元素之和。这题结点的值要使用long long

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<functional> 
#include<map>
using namespace std;
typedef long long ll;
const int N=1e5+10,NN=2e3+10,INF=0x3f3f3f3f;
const ll MOD=1e9+7;
struct Tree{
	int l;
	int r;
	int len;//该区间内含有的元素个数
	ll sum;//结点的值
	int mid(){
		return (l+r)/2;
	}
}tree[N<<2];
int n,q; 
ll pre[N],lazy[N<<2];
void pushup(int rt){
	tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void pushdown(int len,int rt){
	if(lazy[rt]){
		lazy[rt<<1]+=lazy[rt];
		lazy[rt<<1|1]+=lazy[rt];
		tree[rt<<1].sum+=lazy[rt]*(len-(len>>1));
		tree[rt<<1|1].sum+=lazy[rt]*(len>>1);
		lazy[rt]=0;
	}
}
void buildtree(int left,int right,int rt){
	tree[rt].l=left;
	tree[rt].r=right;
	tree[rt].len=right-left+1;
	lazy[rt]=0;
	if(left==right){
		tree[rt].sum=pre[left];
		return ;
	}
	int mid=tree[rt].mid();
	buildtree(left,mid,rt<<1);//维护左子树
	buildtree(mid+1,right,rt<<1|1);//维护右子树
	pushup(rt);
}
ll query(int left,int right,int rt){
	if(tree[rt].l==left&&tree[rt].r==right) return tree[rt].sum;
	int mid=tree[rt].mid();
	pushdown(tree[rt].len,rt);
	ll ans=0;
	if(right<=mid) ans+=query(left,right,rt<<1);
	else if(left>mid) ans+=query(left,right,rt<<1|1);
	else{
		ans+=query(left,mid,rt<<1);
		ans+=query(mid+1,right,rt<<1|1);
	}
	return ans;
}
void update(int left,int right,ll k,int rt){
	if(tree[rt].l==left&&tree[rt].r==right){
		lazy[rt]+=k;
		tree[rt].sum+=k*(right-left+1);
		return ;
	}
	if(tree[rt].l==tree[rt].r) return ;
	pushdown(tree[rt].len,rt);
	int mid=tree[rt].mid();
	if(right<=mid) update(left,right,k,rt<<1);
	else if(left>mid) update(left,right,k,rt<<1|1);
	else{
		update(left,mid,k,rt<<1);
		update(mid+1,right,k,rt<<1|1);
	}
	pushup(rt);
}
void init(){
}
int main(){
	scanf("%d%d",&n,&q);
	for(int i=1;i<=n;i++) scanf("%lld",&pre[i]);
	buildtree(1,n,1);
	while(q--){
		char ch;
		scanf(" %c",&ch);
		if(ch=='Q'){
			int u,v;
			scanf("%d%d",&u,&v);
			printf("%lld\n",query(u,v,1));
		}
		else if(ch=='C'){
			int u,v;
			ll w;
			scanf("%d%d%lld",&u,&v,&w);
			update(u,v,w,1);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Hc_Soap/article/details/107463695