【POJ 3468】A Simple Problem with Integers

来源:互联网 发布:ai作图软件 编辑:程序博客网 时间:2024/06/05 05:41

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 68616 Accepted: 21153Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

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

Source

POJ Monthly--2007.11.25, Yang Yi

树状数组维护带修改的区间和~


首先这道题用线段树打标记很好做,但是树状数组可以更快的实现~


树状数组本身是不支持区间修改的,那么我们怎么办呢?


s[i]表示最开始的前缀和。


数组de[i],表示i-n的所有数都加上de[i]。


当把区间l-r都加上c的时候,de[l]+=c,de[r+1]-=c


我们看看怎么计算修改后的前缀和(前x个数)

  原始和+修改和

=s[x]+del[1]*x+del[2]*(x-1)+...+del[x]*1


=s[x]+sigma(del[i]*(x-i+1))


=s[x]+sigma((1+x)*del[i]-i*del[i])    (1<=i<=x)


=s[x]+(1+x)*sigma(del[i])-sigma(i*del[i])   (1<=i<=x)


那么我们可以用两个树状数组:

d维护del[i]的前缀和;

d[i]维护del[i]*i的前缀和。


然后求l-r的区间和的时候,用前缀和相减就可以了~


#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cstdlib>#define LL long long#define M 100000+5using namespace std;int n,T;LL d[M],di[M],s[M];int lowbit(int x){return x&(-x);}void Update1(int x,LL k){for (int i=x;i<=n;i+=lowbit(i))d[i]+=k;}void Update2(int x,LL k){for (int i=x;i<=n;i+=lowbit(i))di[i]+=k;}LL Getsum1(int x){LL ans=0;for (int i=x;i;i-=lowbit(i))ans+=d[i];return ans;}LL Getsum2(int x){LL ans=0;for (int i=x;i;i-=lowbit(i))ans+=di[i];return ans;}int main(){scanf("%d%d",&n,&T);s[0]=0;LL x;for (int i=1;i<=n;i++)scanf("%I64d",&x),s[i]=s[i-1]+x;while (T--){char ch[3];int l,r;LL c;scanf("%s",ch);scanf("%d%d",&l,&r);if (ch[0]=='C'){scanf("%I64d",&c);Update1(l,c);Update1(r+1,-c);Update2(l,c*(LL)l);Update2(r+1,(-c)*(LL)(r+1));}else{LL ans=0;ans=s[r]-s[l-1];ans+=(Getsum1(r)*(LL)(r+1)-Getsum2(r));ans-=(Getsum1(l-1)*(LL)l-Getsum2(l-1));printf("%I64d\n",ans);}}return 0;}

感悟:

1.WA是因为getsum1/2搞反。。POJ要用I64d

1 0
原创粉丝点击