Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 130688 | Accepted: 40570 | |
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
POJ Monthly--2007.11.25, Yang Yi
题解
线段树区间增加模板题
代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
ll arr[maxn];
struct SegTree
{
ll Sum[maxn<<2],Max[maxn<<2],cnt[maxn<<2],lz[maxn<<2];
void init()
{
memset(Sum,0,sizeof(Sum));
memset(Max,0,sizeof(Max));
memset(cnt,0,sizeof(cnt));
memset(lz,0,sizeof(lz));
}
void push_up(int rt) /// 上更新函数
{
Sum[rt] = Sum[rt<<1] + Sum[rt<<1|1];
Max[rt] = max(Max[rt<<1],Max[rt<<1|1]);
cnt[rt] = cnt[rt<<1] + cnt[rt<<1|1];
}
void push_down(int rt) /// 单点(区间)增加的下更新函数
{
if(!lz[rt]) return ;
lz[rt<<1] += lz[rt];
lz[rt<<1|1] += lz[rt];
Sum[rt<<1] += lz[rt]*cnt[rt<<1];
Sum[rt<<1|1] += lz[rt]*cnt[rt<<1|1];
Max[rt<<1] += lz[rt];
Max[rt<<1|1] += lz[rt];
lz[rt] = 0;
}
void build(ll arr[],int l,int r,int rt) /// 数组建立线段树
{
if(l == r) {
Max[rt] = arr[l];
Sum[rt] = arr[l];
cnt[rt] = 1;
lz [rt] = 0;
return;
}
int mid = (l + r) >> 1;
build(arr,l,mid,rt<<1);
build(arr,mid+1,r,rt<<1|1);
push_up(rt);
}
void activate(int pos,ll val,int l,int r,int rt) /// 单点建立线段树
{
if(l == r) {
Sum[rt] += val;
Max[rt] += val;
cnt[rt] ++;
return ;
}
push_down(rt);
int mid = (l + r) >> 1;
if(pos <= mid) activate(pos,val,l,mid,rt<<1);
else activate(pos,val,mid+1,r,rt<<1|1);
push_up(rt);
}
void update(int pos,ll val,int l,int r,int rt) /// 单点修改(增加)
{
if(l == r) {
Sum[rt] += val; /// 增加
Max[rt] += val; ///
lz[rt] += val; ///
return;
}
push_down(rt);
int mid = (l + r) >> 1;
if(pos <= mid) update(pos,val,l,mid,rt<<1);
else update(pos,val,mid+1,r,rt<<1|1);
push_up(rt);
}
void update(int ql,int qr,ll val,int l,int r,int rt) /// 区间修改(增加)
{
if(ql == l && qr == r) {
Sum[rt] += val*cnt[rt]; /// 增加
Max[rt] += val; ///
lz[rt] += val; ///
return;
}
push_down(rt);
int mid = (l + r) >> 1;
if(qr <= mid) update(ql,qr,val,l,mid,rt<<1);
else if(ql > mid) update(ql,qr,val,mid+1,r,rt<<1|1);
else {
update(ql,mid,val,l,mid,rt<<1);
update(mid+1,qr,val,mid+1,r,rt<<1|1);
}
push_up(rt);
}
ll Max_query(int ql,int qr,int l,int r,int rt) /// 区间最大值查询
{
if(ql == l && qr == r) {
return Max[rt];
}
push_down(rt);
int mid = (l + r) >> 1;
if(qr <= mid) return Max_query(ql,qr,l,mid,rt<<1);
if(ql > mid) return Max_query(ql,qr,mid+1,r,rt<<1|1);
return max(Max_query(ql,mid,l,mid,rt<<1),Max_query(mid+1,qr,mid+1,r,rt<<1|1));
}
ll Sum_query(int ql,int qr,int l,int r,int rt) /// 区间求和查询
{
if(ql == l && qr == r) {
return Sum[rt];
}
push_down(rt);
int mid = (l + r) >> 1;
if(qr <= mid) return Sum_query(ql,qr,l,mid,rt<<1);
if(ql > mid) return Sum_query(ql,qr,mid+1,r,rt<<1|1);
return Sum_query(ql,mid,l,mid,rt<<1) + Sum_query(mid+1,qr,mid+1,r,rt<<1|1);
}
}seg;
int main()
{
int n,q,l,r;
ll val;
char ch;
while(~scanf("%d%d",&n,&q))
{
//seg.init();
for(int i=1;i<=n;i++) scanf("%lld",&arr[i]);
seg.build(arr,1,n,1);
while(q--)
{
getchar();
scanf("%c",&ch);
if(ch == 'Q') {
scanf("%d%d",&l,&r);
printf("%lld\n",seg.Sum_query(l,r,1,n,1));
}
else if(ch == 'C') {
scanf("%d%d%lld",&l,&r,&val);
seg.update(l,r,val,1,n,1);
}
}
}
return 0;
}