Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 67813 | Accepted: 20922 | |
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
Source
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100000
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
struct node
{
int left,right;
ll sum,inc;
}nod[3*maxn];
int num[maxn];
bool cmp(int a,int b)
{
return a>b;
}
void build(int i,int l,int r)
{
nod[i].left=l;
nod[i].right=r;
nod[i].inc=0;
if(l==r)
{
nod[i].sum=num[l];
return;
}
int mid=(l+r)/2;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
nod[i].sum=nod[i<<1].sum+nod[i<<1|1].sum;
}
void add(int i,int a,int b,ll c)//在结点i的区间(a,b)上+c
{
if(nod[i].left==a&&nod[i].right==b)
{
nod[i].inc+=c;//找到区间后就不维护sum了
return;
}
nod[i].sum+=c*(b-a+1);
int mid=(nod[i].left+nod[i].right)/2;
if(b<=mid)add(i<<1,a,b,c);
else if(a>mid)add(i<<1|1,a,b,c);
else
{
add(i<<1,a,mid,c);
add(i<<1|1,mid+1,b,c);
}
}
ll query(int i,int a,int b)
{
if(nod[i].left==a&&nod[i].right==b)
{
return nod[i].sum+(b-a+1)*nod[i].inc;
}
nod[i].sum+=(nod[i].right-nod[i].left+1)*nod[i].inc;
int mid=(nod[i].left+nod[i].right)/2;
add(i<<1,nod[i].left,mid,nod[i].inc);
add(i<<1|1,mid+1,nod[i].right,nod[i].inc);/...!
nod[i].inc=0;
if(b<=mid)return query(i<<1,a,b);
else if(a>mid)return query(i<<1|1,a,b);
else return query(i<<1,a,mid)+query(i<<1|1,mid+1,b);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m,a,b,c;
char s;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
build(1,1,n);
for(int i=1;i<=m;i++)
{
cin>>s;
if(s=='C')
{
scanf("%d%d%d",&a,&b,&c);
add(1,a,b,c);
}
else
{
scanf("%d%d",&a,&b);
printf("%I64d\n",query(1,a,b));
}
}
}
return 0;
}