#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 100010;
struct node{
int l, r;
ll add, x;
}tr[N*4];
int n, m;
void build( int l, int r, int rt )
{
tr[rt].add = 0;
tr[rt].l = l;
tr[rt].r = r;
if( l == r )
{
scanf("%lld", &tr[rt].x );
return;
}
int mid = ( l + r ) >> 1;
build( lson );
build (rson );
tr[rt].x = tr[rt<<1].x + tr[rt<<1|1].x;
}
void down( int rt )
{
int m = ( tr[rt].r - tr[rt].l + 1);
if( tr[rt].add )
{
tr[rt<<1].x += (ll) (m - (m >> 1)) * tr[rt].add;
tr[rt<<1|1].x += (ll) (m >> 1) * tr[rt].add;
tr[rt<<1].add += tr[rt].add;
tr[rt<<1|1].add += tr[rt].add;
tr[rt].add = 0;
}
}
void update( int l, int r, int val, int rt )
{
if( tr[rt].l >= l && tr[rt].r <= r )
{
tr[rt].x += ( tr[rt].r - tr[rt].l + 1) * val;
tr[rt].add += val;
return;
}
down( rt );
int mid = ( tr[rt].l + tr[rt].r ) >> 1;
if( r <= mid )
update( l, r, val, rt << 1);
else if( l > mid )
update( l, r, val, rt << 1 | 1);
else
{
update( l, mid, val, rt << 1);
update( mid+1, r, val, rt << 1 | 1);
}
tr[rt].x = tr[rt<<1].x + tr[rt<<1|1].x;
}
ll query( int l, int r, int rt )
{
if( tr[rt].l >= l && tr[rt].r <= r )
{
return tr[rt].x;
}
down( rt );
int mid = (tr[rt].l + tr[rt].r ) >> 1;
if( r <= mid )
return query(l, r, rt << 1);
else if( l > mid )
return query(l, r, rt << 1 | 1);
else
return ( query(l, mid, rt << 1) + query( mid+1, r, rt << 1| 1));
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
build( 1, n, 1 );
ll c;
int a, b;
char op[3];
while( m-- )
{
scanf("%s", op);
if( op[0] == 'C' )
{
scanf("%d %d %lld", &a, &b, &c);
update( a, b, c, 1);
}
else
{
scanf("%d %d", &a, &b);
ll ans = query( a, b, 1 );
printf("%lld\n", ans);
}
}
}
return 0;
}
poj 3468 A Simple Problem with Integers 线段树成段更新模板
最新推荐文章于 2018-09-10 10:38:05 发布