POJ - 3468 A Simple Problem with Integers
Posted zzhzzh123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ - 3468 A Simple Problem with Integers相关的知识,希望对你有一定的参考价值。
线段树区间修改,每个数均加上一个值。区间查询和。
#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
",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的主要内容,如果未能解决你的问题,请参考以下文章
A Simple Problem with Integers POJ - 3468
POJ - 3468 A Simple Problem with Integers
[poj3468]A Simple Problem with Integers
POJ3468 A Simple Problem with Integers 分块