poj 3468 (线段树,区间加减)—A Simple Problem with Integers

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
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 abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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
Hint

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



题目大意:给你一段数 你有两种操作,一种是算出a到b区间的所有数的和,一种是把a到b的数全部加上c



#include<iostream>
#include <cstdio>
#include <cstring>
typedef __int64 LL ;
using namespace std;
struct tree
{
    int l,r;
	LL sum, val; 
	int mid ()
	{
	    return (l+r)>>1;
	}
} t[400050];                                
LL ans;
void btree(int l,int r,int now)       //构造初始树
{
	t[now].l = l;
	t[now].r = r;
	t[now].val = 0;
    if(l==r)
	{
	     scanf("%I64d",&t[now].sum);
		 return;
	}
	int mid = t[now].mid();
	btree(l,mid,now<<1);
	btree(mid+1,r,now<<1|1);
    t[now].sum = t[now<<1].sum + t[now<<1|1].sum; 
}
void pdate(int now,int len)           
{
    if(t[now].val!=0)
	{
	    t[now<<1].val+=t[now].val;
		t[now<<1|1].val+=t[now].val;
		t[now<<1].sum+= t[now].val*(len-(len>>1));
		t[now<<1|1].sum+= t[now].val*(len>>1);
		t[now].val = 0;
	}
}                           
 //更新该区间的左右两个子区间,因为每次up的时候只更新了当前区间的val当下一次更新或查询的时候只要val不为0
//就要继续往下更新区间保证查询与更新的下一个区间是已经完全更新过的
void up(int l, int r, int now, int L, int R,LL v)
{
    if(L<=l&&r<=R)
	{
	    t[now].val+=v;
		t[now].sum+=(r -l + 1) * v;
		return;                              //只要当前区间在目标区间内,则当前区间就要被更新,然后返回。
	}
	pdate(now,r-l+1);
	int mid = t[now].mid();
	if(L<=mid)                              //如果目标区间有在当前区间的的左区间,就递归查询左区间,下面同理
	{
		up(l,mid,now<<1,L,R,v);
	}
	if(R>mid)                               
	{
	    up(mid+1,r,now<<1|1,L,R,v);
	}
	t[now].sum = t[now<<1].sum + t[now<<1|1].sum;   //更新完所有子区间后把自己的sum更新。

}
void tot(int l,int r, int now,int L,int R)
{
    if(L<=l && r<=R)                               //当前区间在目标区间里,答案加上当前区间的总合
	{
	    ans += t[now].sum;
		return;
	}
	pdate(now,r-l+1);                               //更新该区间的子区间
	int mid = t[now].mid(); 
	if(L>mid)                                        //如果目标区间在当前区间的左边 递归查询左区间,下面同理
	{
	    tot(mid+1,r,now<<1|1,L,R);
	}
	else if(R<=mid)
	{
	    tot(l,mid,now<<1,L,R);
	}
	else                                             //如果既不在左区间也不在右区间肯定处于该区间的左右两边了
	{
	    tot(l,mid,now<<1,L,R);
		tot(mid+1,r,now<<1|1,L,R);
	} 
}
int main()
{
	int n,q;
    while (scanf("%d %d",&n,&q)!=EOF)
	{
		ans = 0;
	    btree(1,n,1);
		LL i,a,b,c;
		char r;
		for(i = 0; i < q; i++)
		{
			ans = 0;
			getchar();
		     scanf("%c",&r);
			 scanf("%I64d %I64d",&a,&b);
			 if(r =='Q')
			 {
			     tot(1,n,1,a,b);
				 printf("%I64d\n",ans);
			 }	 
			 else 
			 {
				 scanf("%I64d",&c);
			     up(1,n,1,a,b,c);
			 }
		}
	}
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值