Serie A championship: A typical segment tree c,q problem with n the number of a[i] (1~n), C, a, b,c in [A, b] plus C
Q a B asks for the and of [b].
#include <cstdio> #include <stdlib.h> #include <string.h> #include <string> #include <map > #include <cmath> #include <iostream> #include <queue> #include <stack> #include < Algorithm> #include <set>using namespace std; #define INF 1e8#define eps 1e-8#define ll __int64#define maxn 100005 #define MOD 1000000009struct node{ll l,r,sum;ll lazy;} Tree[maxn*10];ll a[maxn];void Pushup (ll RT) {Tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;} void Pushdown (ll RT) {if (tree[rt].lazy!=0) {tree[rt<<1].lazy+=tree[rt].lazy;//Note is + =, not =;tree[rt<<1|1]. lazy+=tree[rt].lazy;tree[rt<<1].sum+= (tree[rt<<1].r-tree[rt<<1].l+1) *tree[rt].lazy;tree[rt <<1|1].sum+= (tree[rt<<1|1].r-tree[rt<<1|1].l+1) *tree[rt].lazy;tree[rt].lazy=0;}} void Build (LL l,ll R,ll RT) {tree[rt].l=l;tree[rt].r=r;tree[rt].sum=0;tree[rt].lazy=0;if (l==r) {tree[rt].sum=a[l]; return;} ll mid= (l+r)/2;build (l,mid,rt<<1); build (mid+1,r,rt<<1|1); PuShup (RT);} void Update (ll rt,ll l,ll R,ll v) {pushdown (RT); if (tree[rt].l==l&&tree[rt].r==r) {Tree[rt].lazy=v;tree[rt]. sum+= (tree[rt].r-tree[rt].l+1) *v;return;} ll mid= (TREE[RT].L+TREE[RT].R) >>1;if (mid<l) update (RT<<1|1,L,R,V); else if (mid>=r) update (rt< <1,L,R,V); else{update (rt<<1,l,mid,v); update (RT<<1|1,MID+1,R,V);} Pushup (RT);} ll query (ll rt,ll l,ll R) {if (tree[rt].l==l&&tree[rt].r==r) return tree[rt].sum; Pushdown (RT), LL mid= (TREE[RT].L+TREE[RT].R) >>1,ret=0;if (mid<l) ret+=query (rt<<1|1,l,r), else if (mid >=R) Ret+=query (rt<<1,l,r); Else{ret+=query (Rt<<1,l,mid); Ret+=query (rt<<1|1,mid+1,r);} Pushup (RT); return ret;} int main () {ll n,m;while (~scanf ("%i64d%i64d", &n,&m)) {for (int i=1;i<=n;i++) scanf ("%i64d", &a[i]); Build (1,n,1), Char s[2];ll u,v,c;while (m--) {scanf ("%s", s), if (s[0]== ' Q ') {scanf ("%i64d%i64d", &u,&v);p rintf ( "%i64d\n", Query (1,u,v));} else {scanf ("%i64d%i64d%i64d", &u,&v,&c); Update (1,U,V,C);}}} return 0;} /*10 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 410 221 2 3 4 5 6 7 8 9 10Q 4 4C 1 3C 6 3C 6 9 3C 8 9-100c 7 9 3 C 7 3C 1 3Q 6 10Q 6 9Q 8 9Q 7 9Q 7 10Q 1 10Q 2 4C 3 6 3Q 9 9Q 1 1Q 5 5Q 6 6Q 7 7Q 6 8*/
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
PKU A simple problem with integers (segment tree update interval sum)