您的位置:首页 > 其它

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

2016-09-17 22:12 483 查看
A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 97217   Accepted: 30358
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   这个题考察的是树状数组的区间更新而不是单点更新,单点更新会超时的; 树状数组天生用来动态维护数组前缀和,其特点是每次更新一个元素的值,查询只能查数组的前缀和,

但这个题目求的是某一区间的数组和,而且要支持批量更新某一区间内元素的值,怎么办呢?实际上,

还是可以把问题转化为求数组的前缀和。

 

    首先,看更新操作update(s, t, d)把区间A[s]...A[t]都增加d,我们引入一个数组delta[i],表示

A[i]...A
的共同增量,n是数组的大小。那么update操作可以转化为:

1)令delta[s] = delta[s] + d,表示将A[s]...A
同时增加d,但这样A[t+1]...A
就多加了d,所以

2)再令delta[t+1] = delta[t+1] - d,表示将A[t+1]...A
同时减d

 

    然后来看查询操作query(s, t),求A[s]...A[t]的区间和,转化为求前缀和,设sum[i] = A[1]+...+A[i],则

                            A[s]+...+A[t] = sum[t] - sum[s-1],

那么前缀和sum[x]又如何求呢?它由两部分组成,一是数组的原始和,二是该区间内的累计增量和, 把数组A的原始

值保存在数组org中,并且delta[i]对sum[x]的贡献值为delta[i]*(x+1-i),那么

                            sum[x] = org[1]+...+org[x] + delta[1]*x + delta[2]*(x-1) + delta[3]*(x-2)+...+delta[x]*1

                                         = org[1]+...+org[x] + segma(delta[i]*(x+1-i))

                                         = segma(org[i]) + (x+1)*segma(delta[i]) - segma(delta[i]*i),1 <= i <= x

这其实就是三个数组org[i], delta[i]和delta[i]*i的前缀和,org[i]的前缀和保持不变,事先就可以求出来,delta[i]和

delta[i]*i的前缀和是不断变化的,可以用两个树状数组来维护。

#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 100010
#define ll long long
using namespace std;
ll c1
;//c1[i]表示i~n共同增加c1[i]
ll c2
;//c2[i]表示i~n一共增加c1[i]*i=c2[i]
ll ans
;//存放的前缀和
ll n,m;
string op;
ll lowbit(ll x)
{
return x&(-x);
}
void update(ll x,ll val,ll *c)
{
while(x<=n)
{
c[x]+=val;
x+=lowbit(x);
}
}
ll getsum(ll x,ll *c)
{
ll s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main()
{
freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
while(scanf("%lld%lld",&n,&m)!=EOF)
{
memset(c1,0,sizeof c1);
memset(c2,0,sizeof c2);
memset(ans,0,sizeof ans);
for(int i=1;i<=n;i++)
{
scanf("%lld",&ans[i]);
ans[i]+=ans[i-1];
}
getchar();
for(int i=1;i<=m;i++)
{
cin>>op;
if(op=="C")
{
ll s1,s2,s3;
scanf("%lld%lld%lld",&s1,&s2,&s3);
update(s1,s3,c1);//c1~n共同增加了s3
update(s2+1,-s3,c1);//上一步操作使得s2~n多增加了s3所以这一步要减去
update(s1,s1*s3,c2);
update(s2+1,-(s2+1)*s3,c2);
}
else if(op=="Q")
{
ll s1,s2;
scanf("%lld%lld",&s1,&s2);
ll cur=ans[s2]-ans[s1-1];//首先等于s1~s2这个区间的基础值
cur+=getsum(s2,c1)*(s2+1)-getsum(s2,c2);//0~s2对前缀和的影响
cur-=getsum(s1-1,c1)*(s1)-getsum(s1-1,c2);//0~s1对前缀和的影响
printf("%lld\n",cur);
}
}
// for(int i=1;i<=n;i++)
// cout<<getsum(i)<<" ";
// cout<<endl;
}
}

 

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