标签:pushd 线段 nod ble algo def namespace const sum
线段树区间修改,每个数均加上一个值。区间查询和。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
const int N = 1e5 + 50;
int n,m;
#define mid ((l + r) >> 1)
#define ls (nod << 1)
#define rs (nod << 1 | 1)
#define lson ls,l,mid
#define rson rs,mid + 1, r
struct Node{
long long add,sum,len;
}t[N << 2];
void pushup(int nod){
t[nod].sum = t[ls].sum + t[rs].sum;
}
void pushdown(int nod){
if(t[nod].add == 0) return ;
t[ls].add += t[nod].add; t[rs].add += t[nod].add;
t[ls].sum += t[ls].len * t[nod].add; t[rs].sum += t[rs].len * t[nod].add;
t[nod].add = 0;
}
void build(int nod,int l,int r){
t[nod].add = 0; t[nod].len = r - l + 1;
if(l == r){ scanf("%lld",&t[nod].sum); return ;}
build(lson); build(rson); pushup(nod);
}
void update(int nod,int l,int r,int ll,int rr,long long v){
if(l > rr || r < ll ) return ;
if(ll <= l && r <= rr){
t[nod].add += v; t[nod].sum += t[nod].len * v;
return ;
}
pushdown(nod);
update(lson,ll,rr,v); update(rson,ll,rr,v);
pushup(nod);
}
long long query(int nod,int l,int r,int ll,int rr){
if(l > rr || r < ll) return 0;
if(ll <= l && r <= rr) return t[nod].sum;
pushdown(nod);
return query(lson,ll,rr) + query(rson,ll,rr);
}
int main(){
scanf("%d%d",&n,&m);
build(1,1,n);
while(m --){
char c; scanf(" %c",&c);
if(c == ‘Q‘) { int l,r; scanf("%d%d",&l,&r); printf("%lld\n",query(1,1,n,l,r)); }
if(c == ‘C‘) { int l,r,v; scanf("%d%d%d",&l,&r,&v); update(1,1,n,l,r,v); }
}
return 0;
}
POJ - 3468 A Simple Problem with Integers
标签:pushd 线段 nod ble algo def namespace const sum
原文地址:https://www.cnblogs.com/zzhzzh123/p/13399300.html