您的位置:首页 > 其它

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

2015-03-31 21:34 309 查看
A Simple Problem with Integers

Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 69589Accepted: 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

POJ Monthly--2007.11.25, Yang Yi

#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-5;
const int MS=100005;

LL sum[MS];
LL C[2][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=0;
while(x>0)
{
res+=C[no][x];
x-=lowbit(x);
}
return res;
}

void solve()
{
scanf("%d%d",&N,&Q);
sum[0]=0;
for(int i=1;i<=N;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-1];
}
memset(C,0,sizeof(C));
int l,r;
LL c;
char cmd[10];
for(int i=1;i<=Q;i++)
{
scanf("%s",cmd);
if(cmd[0]=='Q')
{
scanf("%d%d",&l,&r);
LL ans=sum[r]-sum[l-1]+(getsum(0,r)-getsum(1,r)*(N-r))-(getsum(0,l-1)-getsum(1,l-1)*(N-l+1));
printf("%lld\n",ans);
}
else
{
scanf("%d%d%lld",&l,&r,&c);
updata(0,l,c*(N-l+1));
updata(0,r+1,(N-r)*(-c));

updata(1,l,c);
updata(1,r+1,-c);
}
}
}

int main()
{
solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐