poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063

Description

You have N integers, A1A2, ... , 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 A1A2, ... , 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.

Source

field=source&key=POJ+Monthly--2007.11.25" style="text-decoration:none">POJ Monthly--2007.11.25, Yang Yi

代码例如以下:

//线段树功能:update:成段增减 query:区间求和
//此题为Poj 3468 代码

#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//lson和rson分辨表示结点的左儿子和右儿子
//rt表示当前子树的根(root),也就是当前所在的结点
#define LL long long
const int maxn = 111111;
//maxn是题目给的最大区间,而节点数要开4倍,确切的来说节点数要开大于maxn的最小2x的两倍
LL add[maxn<<2];//用来标记每一个节点,为0则表示没有标记,否则为标记。
LL sum[maxn<<2];//求和
void PushUp(int rt) //把当前结点的信息更新到父结点
{
	sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int len)//把当前结点的信息更新给儿子结点,len为分区间长度
 {//对某一个区间进行改变,假设被标记了,在查询的时候就得把改变传给子节点,由于查询的并不一定是当前区间
	if (add[rt]) //已经标记过。该区间被改变过
	{
		//由于rt的儿子节点可能被多次延迟标记。而且rt的儿子节点的延迟标记没有向rt的孙子节点移动,所以用“+=”
		add[rt<<1] += add[rt];
		add[rt<<1|1] += add[rt];
		/*此处用add[rt]乘以区间长度,不是add[rt<<1], 由于rt的儿子节点假设被多次标记,之前被标记时,
          就已经对sum[rt<<1]更新过了。

*/
		sum[rt<<1] += add[rt] * (len - (len >> 1));//更新左儿子的和
		sum[rt<<1|1] += add[rt] * (len >> 1);//更新右儿子的和
		add[rt] = 0;//将标记向儿子节点移动后。父节点的延迟标记去掉
	}
}
void build(int l,int r,int rt)
 {
	add[rt] = 0;//初始化为全部结点未被标记
	if (l == r)
	{
		scanf("%lld",&sum[rt]);
		return ;
	}
	int mid = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
	if (L <= l && r <= R)
	{
		add[rt] += c;
		sum[rt] += (LL)c * (r - l + 1);//更新代表某个区间的节点和,该节点不一定是叶子节点
		return ;
	}
	/*当要对被延迟标记过的这段区间的儿子节点进行更新时,先要将延迟标记向儿子节点移动
    当然,假设一直没有对该段的儿子节点更新,延迟标记就不须要向儿子节点移动,这样就使
    更新操作的时间复杂度仍为O(logn),也是使用延迟标记的原因。
    */
	PushDown(rt , r - l + 1);//向下传递
	int mid = (l + r) >> 1;
	if (L <= mid)
		update(L , R , c , lson);//更新左儿子
	if (mid < R)
		update(L , R , c , rson);//更新右儿子
	PushUp(rt);//向上传递更新和
}
LL query(int L,int R,int l,int r,int rt)
 {
	if (L <= l && r <= R)
	{
		return sum[rt];
	}//要取rt子节点的值时。也要先把rt的延迟标记向下移动
	PushDown(rt , r - l + 1);
	int mid = (l + r) >> 1;
	LL ret = 0;
	if (L <= mid)
		ret += query(L , R , lson);
	if (mid < R)
		ret += query(L , R , rson);
	return ret;
}
int main()
{
	int N , Q;
	scanf("%d%d",&N,&Q);//N为节点数
	build(1 , N , 1); //建树
	while (Q--)//Q为询问次数
	{
		char op[2];
		int a , b , c;
		scanf("%s",op);
		if (op[0] == 'Q')
		{
			scanf("%d%d",&a,&b);
			printf("%lld\n",query(a , b , 1 , N , 1));
		}
		else
		{
			scanf("%d%d%d",&a,&b,&c);//c为区间a到b添加的值
			update(a , b , c , 1 , N , 1);
		}
	}
	return 0;
}
时间: 2024-10-19 12:03:42

poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)的相关文章

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

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

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 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. T

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS 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

POJ3468__A Simple Problem with Integers (线段树)

本文出自blog.csdn.net/svitter --我大C++的指针岂是尔等能够简单领悟! 题意 给N个节点,标号A1~An,然后有Q个操作,操作分为Q i j,查询i,j间的区间和.C i j k,i到j个数字,每个数字增加k,并且输出. 输入输出分析 给N,Q,然后跟操作.注意判断Q,C使用scanf("%s"). 测试数据: 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 Samp

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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