题目地址:http://poj.org/problem?id=3468

思路:updata()区间增减,query()区间求和,会超int用long long

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 100010;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
ll add[maxn<<2];
ll sum[maxn<<2];

void pushup(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
if(add[rt])
{
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
sum[rt<<1] += (m -(m>>1)) * add[rt];
sum[rt<<1|1] += (m>>1) * add[rt];
add[rt] = 0;
}
}
void build(int l,int r,int rt)
{
add[rt] = 0;
if(l == r)
{
scanf("%I64d",&sum[rt]);
return;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
void updata(int L,int R,int c,int l,int r,int rt)
{
if(l >= L && r <= R)
{
sum[rt] += (r-l+1) * c;
add[rt] += c;
return;
}
pushdown(rt,r-l+1);//查询或者更改的时候传递增量
int m = (l + r) >> 1;
if(L <= m) updata(L,R,c,lson);
if(R > m) updata(L,R,c,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(l >= L && r <= R) return sum[rt];
pushdown(rt,r-l+1);//查询或者更改的时候传递增量
int m = (l + r) >> 1;
ll ans = 0;
if(L <= m) ans += query(L,R,lson);
if(R > m) ans += query(L,R,rson);
return ans;
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q) != EOF)
{
build(1,n,1);
char op[2];
for(int i=0; i<q; i++)
{
int a,b,c;
scanf("%s",op);
if(op[0] == 'C')
{
scanf("%d%d%d",&a,&b,&c);
updata(a,b,c,1,n,1);
}
else
{
scanf("%d%d",&a,&b);
printf("%I64d\n",query(a,b,1,n,1));
}
}
}
return 0;
}