A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 130735 | Accepted: 40585 | |
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
分析:要求是在[l,r]的区间内修改或者查询和,当然会想到树状数组。但是因为每次是区间修改,所以需要转化一下。
//It is made by HolseLee on 17th May 2018
//POJ 3468
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Fi(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int N=1e5+;
ll n,m,sum[N],c[][N],ans;
inline ll lowbit(int x){return x&-x;}
inline void add(int k,int x,int y)
{for(int i=x;i<=n;i+=lowbit(i))c[k][i]+=y;}
inline ll get(int k,int x)
{ll ret=;for(int i=x;i>=;i-=lowbit(i))ret+=c[k][i];return ret;}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;int x,y,z;char opt;
Fi(i,,n)cin>>x,sum[i]=sum[i-]+x;
Fi(i,,m){cin>>opt;
if(opt=='C'){cin>>x>>y>>z;
add(,x,z);add(,y+,-z);
add(,x,x*z);add(,y+,-(y+)*z);}
else {cin>>x>>y;
ll ans=(sum[y]+(y+)*get(,y)-get(,y));
ans-=(sum[x-]+x*get(,x-)-get(,x-));
printf("%lld\n",ans);}}
return ;
}