POJ [3468] A Simple Problem with Integers [Line segment tree interval update]

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 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 

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

Sample Output 

4
55
9
15

Hint 

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

The main idea:

 For n integers, two operations are performed.

1. Add a given number to all integers in an interval;

2. Find the sum of all integers in an interval.

analysis:

When it comes to interval operations, consider using a line segment tree. Each node maintains the sum of integers in the interval and uses delay markers to reduce the depth of interval updates.

See the code for specific explanation.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cstdio> 
#include <map> 
#include <iomanip>
 
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
//#define INF 0x3f3f3f3f
 
using namespace std;

const int maxn=1e5+5;
const ll INF=0x3f3f3f3f3f3f3f3f;
 
struct segtree{
	int l,r;
	ll sum;		//sum维护该区间内整数和 
	ll tag; 	//延迟标记	
};
 
segtree t[maxn*4];

ll a[maxn];

void pushup(int p){//区间合并,向上拓展 
	t[p].sum=t[2*p].sum+t[2*p+1].sum;
}

void pushdown(int p){//将父节点的状态向下传递(延迟标记的传递) 
	if(t[p].tag){//说明父节点的状态已被改变 
		int lson=p*2,rson=p*2+1;
		int mid=(t[p].l+t[p].r)/2;
		t[lson].tag+=t[p].tag;		//注意这里一定是要累加,因为该区间有可能被父节点多次更新 
		t[rson].tag+=t[p].tag;
		t[lson].sum+=(t[lson].r-t[lson].l+1)*t[p].tag;
		t[rson].sum+=(t[rson].r-t[rson].l+1)*t[p].tag;
		t[p].tag=0;//父节点传递完成后标志复原 
	}
}
 
void build(int p,int l,int r){//建树 
	int mid=(l+r)/2;
	t[p].l=l;
	t[p].r=r;
	t[p].tag=0;			//初始化延迟标记 
	if(l==r){
		scanf("%lld",&t[p].sum);
		return;
	}
	build(p*2,l,mid);
	build(2*p+1,mid+1,r);
	pushup(p);
}
 
ll query(int p,int L,int R){
	if(L<=t[p].l&&R>=t[p].r){
		return t[p].sum;
	}
	int lson=p*2;
	int rson=p*2+1;
	ll tsum=0;
	int mid=(t[p].l+t[p].r)/2;
	pushdown(p);		//在向下访问之前,先向下更新 
	if(L<=mid){		//如果左侧还有区间 
		tsum+=query(lson,L,R);
	}
	if(R>mid){ 		//如果右侧还有区间
		tsum+=query(rson,L,R);
	} 
	return tsum;
} 
 

 
void update(int p,int L,int R,ll num){
	int lson=p*2,rson=p*2+1;
	if(L<=t[p].l&&R>=t[p].r){//区间被完全覆盖 
		t[p].tag+=num;		//这里累加,原因同上 
		t[p].sum+=(t[p].r-t[p].l+1)*num;
		return;//由于此处直接返回,所以需要延迟标记,此处未更新该节点的子树 
	}
	pushdown(p);//进入子节点前先传递延迟标记,便于接下来的递归更新 
	int mid=(t[p].l+t[p].r)/2;
	if(L<=mid){
		update(lson,L,R,num);
	} 
	if(R>mid){
		update(rson,L,R,num);
	}
	pushup(p);//子节点更新返回后要更新父节点 
}

 
 
int main(){
	int n,q;
	scanf("%d%d",&n,&q);
	build(1,1,n);
	char ch[5];
	for(int i=1;i<=q;i++){
		scanf("%s",ch);
		if(ch[0]=='C'){
			int a,b;
			ll c;
			scanf("%d%d%lld",&a,&b,&c);
			update(1,a,b,c);
		}
		else{
			int a,b;
			scanf("%d%d",&a,&b);
			printf("%lld\n",query(1,a,b));
		}
	} 
	return 0;
}

 

Published 30 original articles · won 5 · 900 views

Guess you like

Origin blog.csdn.net/qq_42840665/article/details/102790580