Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 76346 | Accepted: 23520 | |
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 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 Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" 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题目地址
http://poj.org/problem?id=3468
题目大意
给定Q (1 ≤ Q ≤ 100,000)个数A1,A2 … AQ,, 以及可能多次进行的两个操作: 1) 对某个区间Ai … Aj的每个数都加n(n可变 ) 2) 求某个区间Ai … Aj的数的和
思路
这道题是区间更新,只存和,会导致每次加数的时候都要更新到叶子节点,速度太慢(O(nlogn)),这是必 须要避免的。
在增加时,如果要加的区间正好覆盖一个 节点,则增加其节点的Inc值,不再往下走 ,否则要更新nSum(加上本次增量),再将增 量往下传。这样更新的复杂度就是O(log(n))
在查询时,如果待查区间不是正好覆盖一 个节点,就将节点的Inc往下带,然后将Inc 代表的所有增量累加到nSum上后将Inc清0 ,接下来再往下查询。 Inc往下带的过程也是 区间分解的过程,复杂度也是O(log(n))
#include<iostream>
using namespace std;
struct Node
{
int l,r;
long long nsum;
long long inc;
Node *pleft,*pright;
};
Node tree[200010];
int ncount=0;
int mid(Node *proot)
{
return (proot->l+proot->r)/2;
}
void buildtree(Node *proot,int l,int r)
{
proot->l=l;
proot->r=r;
proot->nsum=0;
proot->inc=0;
if(l==r)
return;
ncount++;
proot->pleft=tree+ncount;
ncount++;
proot->pright=tree+ncount;
buildtree(proot->pleft,l,(l+r)/2);
buildtree(proot->pright,(l+r)/2+1,r);
}
void Inseert(Node *proot,int i,int v)
{
if(proot->l==i&&proot->r==i)
{
proot->nsum=v;
return;
}
proot->nsum+=v;
if(i<=mid(proot))
Inseert(proot->pleft,i,v);
else
Inseert(proot->pright,i,v);
}
void add(Node *proot,int a,int b,long long c)
{
if(proot->l==a&&proot->r==b)
{
proot->inc+=c;
return;
}
proot->nsum+=(b-a+1)*c;
if(a>=mid(proot)+1)
add(proot->pright,a,b,c);
else if(b<=mid(proot))
add(proot->pleft,a,b,c);
else
{
add(proot->pleft,a,mid(proot),c);
add(proot->pright,mid(proot)+1,b,c);
}
}
long long querysum(Node *proot,int a,int b)
{
if(proot->l==a&&proot->r==b)
{
return proot->nsum+(b-a+1)*proot->inc;
}
proot->nsum+=(proot->r-proot->l+1)*proot->inc;
add(proot->pleft,proot->l,mid(proot),proot->inc);
add(proot->pright,mid(proot)+1,proot->r,proot->inc);
proot->inc=0;
if(b<=mid(proot))
return querysum(proot->pleft,a,b);
else if(a>=mid(proot)+1)
return querysum(proot->pright,a,b);
else{
return querysum(proot->pleft,a,mid(proot))+querysum(proot->pright,mid(proot)+1,b);
}
}
int main()
{
int n,q,a,b,c;
char cmd[10];
scanf("%d%d",&n,&q);
int i,j,k;
ncount=0;
buildtree(tree,1,n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
Inseert(tree,i,a);
}
for(i=0;i<q;i++)
{
scanf("%s",cmd);
if(cmd[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
add(tree,a,b,c);
}
else{
scanf("%d%d",&a,&b);
printf("%I64d\n",querysum(tree,a,b));
}
}
return 0;
}