您的位置:首页 > 其它

BZOJ3212: Pku3468 A Simple Problem with Integers

2014-09-28 12:46 405 查看

3212: Pku3468 A Simple Problem with Integers

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 810 Solved: 354
[Submit][Status]

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

题解:

想了想树状数组如何维护区间修改,区间求和,觉得貌似很简单?

差分了之后 用树状数组维护 a[i] 以及 i*a[i] 即可

sum(1,n)=(n+1)*sigma(a[i])-sigma(i*a[i])

靠着树状数组刷进了第一页。。。

代码:

#include<cstdio>

#include<cstdlib>

#include<cmath>

#include<cstring>

#include<algorithm>

#include<iostream>

#include<vector>

#include<map>

#include<set>

#include<queue>

#include<string>

#define inf 1000000000

#define maxn 100000+1000

#define maxm 500+100

#define eps 1e-10

#define ll long long

#define pa pair<int,int>

#define for0(i,n) for(int i=0;i<=(n);i++)

#define for1(i,n) for(int i=1;i<=(n);i++)

#define for2(i,x,y) for(int i=(x);i<=(y);i++)

#define for3(i,x,y) for(int i=(x);i>=(y);i--)

#define mod 1000000007

using namespace std;

inline ll read()

{

ll x=0,f=1;char ch=getchar();

while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}

return x*f;

}
ll s[2][maxn],n,m;
inline void add(int k,int x,ll y)
{
for(;x<=n;x+=x&(-x))s[k][x]+=y;
}
inline ll sum(int k,int x)
{
ll t=0;
for(;x;x-=x&(-x))t+=s[k][x];
return t;
}

int main()

{

freopen("input.txt","r",stdin);

freopen("output.txt","w",stdout);

n=read();m=read();
ll x=0,y,z;
for(ll i=1;i<=n;i++)
{
y=read();
add(0,i,y-x);
add(1,i,i*(y-x));
x=y;
}
while(m--)
{
char ch[2];
scanf("%s",ch);
if(ch[0]=='C')
{
x=read();y=read();z=read();
add(0,x,z);add(1,x,x*z);
add(0,y+1,-z);add(1,y+1,(-z)*(y+1));
}
else
{
x=read();y=read();
ll sum1=(x)*sum(0,x-1)-sum(1,x-1);
ll sum2=(y+1)*sum(0,y)-sum(1,y);
printf("%lld\n",sum2-sum1);
}
}

return 0;

}


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