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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章