A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 69589   Accepted: 21437
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

Hint

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

Source

 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; LL sum[MS];
LL C[][MS];
int N,Q; int lowbit(int x)
{
return x&(-x);
} void updata(int no,int x,LL value)
{
while(x<=N)
{
C[no][x]+=value;
x+=lowbit(x);
}
} LL getsum(int no,int x)
{
LL res=;
while(x>)
{
res+=C[no][x];
x-=lowbit(x);
}
return res;
} void solve()
{
scanf("%d%d",&N,&Q);
sum[]=;
for(int i=;i<=N;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
}
memset(C,,sizeof(C));
int l,r;
LL c;
char cmd[];
for(int i=;i<=Q;i++)
{
scanf("%s",cmd);
if(cmd[]=='Q')
{
scanf("%d%d",&l,&r);
LL ans=sum[r]-sum[l-]+(getsum(,r)-getsum(,r)*(N-r))-(getsum(,l-)-getsum(,l-)*(N-l+));
printf("%lld\n",ans);
}
else
{
scanf("%d%d%lld",&l,&r,&c);
updata(,l,c*(N-l+));
updata(,r+,(N-r)*(-c)); updata(,l,c);
updata(,r+,-c);
}
}
} int main()
{
solve();
return ;
}
上一篇:poj 3468 A Simple Problem with Integers 线段树区间更新


下一篇:poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)