POJ—3468—线段树区间更新

A Simple Problem with Integers

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

     题意:给你n个整数和q个操作,有两种操作,C l r x表示把[l, r]中所有数都加x,Q l r表示输出[l, r]中数值的和。

      就是线段树的区间更新求和问题。

介绍Lazy思想:lazy-tag思想,记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。
 
在此通俗的解释的Lazy意思,比如现在需要对[a,b]区间值进行加val操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,它的节点标记为o,这时 de[o].L == a && de[o].R == b 这时我们可以一步更新此时o节点的sum的值,de[o].sum += (de[o].R - de[o].L + 1) * val,注意关键的时刻来了,如果此时按照常规的线段树的update操作,这时候还应该更新o子节点的sum值,而Lazy思想恰恰是暂时不更新o子节点的sum值,到此就return,直到下次需要用到o子节点的值的时候才去更新,这样避免许多可能无用的操作,从而节省时间。
 

#include<stdio.h>
#include<string.h>
typedef long long ll;

const int maxn = 100005;
struct stu{
	int L,R;
	ll sum;
}de[maxn<<2];

int a[maxn],lazy[maxn<<2];//lazy数组起一个标记和更新的作用

inline void pushup(int o)//向上回溯求和
{
	de[o].sum = de[o<<1].sum + de[o<<1|1].sum;
}

void pushdown(int o)//与单点更新的不同就是有向下的过程
{
	ll mid = (de[o].L + de[o].R) >> 1;
	lazy[o<<1] += lazy[o];
	de[o<<1].sum += (mid - de[o].L + 1) * lazy[o];//这个就是父节点向下更新的操作
	lazy[o<<1|1] += lazy[o];
	de[o<<1|1].sum += (de[o].R - mid) * lazy[o];
	lazy[o] = 0;//然后还有归零
	return ;
}
void build(int l,int r,int o)
{
	lazy[o] = 0;
	de[o].L = l,de[o].R = r;
	if(l == r){
		de[o].sum = a[l];
		return ;
	}
	ll mid = (l + r) >> 1;
	build(l,mid,o<<1);
	build(mid+1,r,o<<1|1);
	pushup(o);
}
void update(int o,int l,int r,int val)
{
	if(de[o].L == l && de[o].R == r){
		de[o].sum += (de[o].R - de[o].L + 1) * val;
		lazy[o] += val;
		return ;
	}
	if(lazy[o] != 0) pushdown(o);//区间更新要有这个向下的过程
	
	ll mid = (de[o].L + de[o].R) >> 1;
	if(r <= mid) update(o<<1,l,r,val);
    else if(l>mid) update(o<<1|1,l,r,val);
    else{
    	update(o<<1,l,mid,val);
		update(o<<1|1,mid+1,r,val);
	}
	pushup(o);
}
ll query(int o,int l,int r)
{
	if(de[o].L == l && de[o].R == r){
		return de[o].sum;
	}
	if(lazy[o] != 0) pushdown(o);//这里也要有
	
	ll mid = (de[o].L + de[o].R) >> 1;
//	int sum = 0;//这里的注释是求和的分类操作,这个好像是错的,忘了,不过下面的是对的。
//	if(r > mid) sum += query(o<<1|1,mid+1,r);
//	if(l <= mid) sum += query(o<<1,l,mid);
//	return sum;
	if(r <= mid) return query(o<<1,l,r);//这个分类可以有好几种方式写
    else if(l>mid) return query(o<<1|1,l,r);
    else
        return query(o<<1,l,mid)+query(o<<1|1,mid+1,r);
}

int main()
{
	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		memset(lazy,0,sizeof lazy);
		for(int i = 1;i <= n; i++)
			scanf("%d",&a[i]);
		build(1,n,1);
		char s[3];
		int a,b,x;
		while(m--)
		{
			scanf("%s",s);
			if(s[0] == 'C'){
				scanf("%d %d %d",&a,&b,&x);
				update(1,a,b,x);
			}
			else{
				scanf("%d %d",&a,&b);
				printf("%lld\n",query(1,a,b));
			}
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值