A Simple Problem with Integers(线段树 + 延迟标记)

A Simple Problem with Integers
Time Limit: 5000MS  Memory Limit: 131072K
Total Submissions: 48231  Accepted: 14226
Case Time Limit: 2000MS

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. 

 

    题意:

    有N(1到100000)个数和M(1到100000)个操作,给出这个N个数的每个数的值Ai(-100000000到1000000000)。操作有两类,第一类Q a b,代表将[a,b]这区间内的数相加输出结果;第二类C a b c,代表将[a,b]区间内的每个数都+c处理。输出每当Q询问时候的结果。

 

    思路:

    线段树。延迟标记。将1到N进行线段树处理,用ad域来标记。

 

    AC:

#include<stdio.h>
#define MAX 250000+5

typedef struct
{
	__int64 l;    //区间起点
	__int64 r;    //区间终点
	__int64 sum;  //区间总和
	__int64 ad;   //标记增加量
}node;
node no[MAX];
__int64 num[MAX];
__int64 s;

void build(__int64 from,__int64 to,__int64 i)
{
	__int64 mid=(from+to)/2;
	no[i].l=from;
	no[i].r=to;
	no[i].ad=0;   //一开始增加量都为0
	if(from==to)
	{
		no[i].sum=num[from];
		return;
	}
	build(from,mid,2*i);
	build(mid+1,to,2*i+1);
	no[i].sum=no[i*2].sum+no[i*2+1].sum;
}

void find(__int64 from,__int64 to,__int64 i)
{
	__int64 mid=(no[i].l+no[i].r)/2;
	if(from==no[i].l&&to==no[i].r)
	{
		if(!no[i].ad)
		{
			s+=no[i].sum;
			return;
		}
		else
		{
			s+=no[i].sum+(no[i].r-no[i].l+1)*no[i].ad;
//这两天一直纠结这个问题
//想着如果在这里往下传标记行不行
//后来发现是忘了维护sum值了,就这点纠结了两天
//RE的原因是如果[1,1]的时候往下传就没有意义了,但是为了确保能下传,数组要开大点
//还有是+=,而不是直接=
//no[i].sum+=(no[i].r-no[i].l+1)*no[i].ad;
//no[2*i].ad+=no[i].ad;
//no[2*i+1].as+=no[i].ad;
//no[i].ad=0;
//其实这里可以不需要作标记下传操作的,仅仅只是好奇一下
			return;
		}
	}
	else
	{
		if(no[i].ad)
		{
			no[i].sum+=(no[i].r-no[i].l+1)*no[i].ad;
			no[2*i].ad+=no[i].ad;
			no[2*i+1].ad+=no[i].ad;
			no[i].ad=0;
//传给左右儿子之后才能清零
		}
		if(from>=mid+1)         find(from,to,2*i+1);
		if(to<=mid)		find(from,to,2*i);
		if(from<=mid&&to>=mid+1)
		{
			find(from,mid,2*i);
			find(mid+1,to,2*i+1);
		}
	}
}

void add(__int64 from,__int64 to,__int64 i,__int64 w)
{
	__int64 mid=(no[i].l+no[i].r)/2;
	if(no[i].l==from&&no[i].r==to)
	{
		no[i].ad+=w;
//当找到这个区间之后,增量标记域增加
//而不是直接总和增加
		return;
	}
	else
	{
		if(no[i].ad!=0)
		{
			no[i].sum+=(no[i].r-no[i].l+1)*no[i].ad;
//当下一次查询的时候,标记域的值ad使总和sum增加
			no[2*i].ad+=no[i].ad;
			no[2*i+1].ad+=no[i].ad;
//增加完后则往下传
			no[i].ad=0;
//传完之后本身标记的值已经使总和sum增加完毕,故变为0
		}
		no[i].sum+=(to-from+1)*w;
//这里的相加不是标记域所导致的,是所输入的区间所导致的
		if(from>=mid+1) add(from,to,2*i+1,w);
		if(to<=mid)		add(from,to,2*i,w);
		if(from<=mid&&to>=mid+1)
		{
			add(from,mid,2*i,w);
			add(mid+1,to,2*i+1,w);
		}
//继续查找线段树,找到相同的区间则标记
	}
}

int main()
{
	__int64 n,m;
	scanf("%I64d%I64d",&n,&m);
	for(__int64 i=1;i<=n;i++)
		scanf("%I64d",&num[i]);
	build(1,n,1);
	while(m--)
	{
		char c;
		scanf(" %c",&c);
		if(c=='Q')
		{
			__int64 from,to;
			scanf("%I64d%I64d",&from,&to);
			s=0;
			find(from,to,1);
			printf("%I64d\n",s);
		}
		if(c=='C')
		{
			__int64 from,to;
			__int64 w;
			scanf("%I64d%I64d%I64d",&from,&to,&w);
			add(from,to,1,w);
		}
	}
	return 0;
}

 

    总结:

    1.维护ad的同时也要维护sum值,记住;

    2.注意数据范围;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值