Topic Connection:
http://poj.org/problem?id=3468
Main topic:
Given the number of N, there are two operations:
1: "C a b c", each number in [A, a,] is added C.
2: "Q a b", for the sum of each number in [A, a,].
Problem Solving Ideas:
If the segment tree is updated to each node, the number of nodes and the number of queries will cause tle, so defining a flag variable within each node indicates that the next layer of the current node is updated, and each query is updated to the next level if necessary.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 Const intMAXN =400010;7 Const intINF =0x3f3f3f3f;8 #defineLL __int649 structnodeTen { One LL L, R; A LL sum, add; - LL Mid () - { the return(L + R)/2; - } - }; - node TREE[MAXN]; + LL Res; - + voidBuild (ll Root, ll L, ll R) A { atTree[root]. L =l; -Tree[root]. R =R; -Tree[root].sum = Tree[root].add =0; - - if(L = =R) - return ; inBuild (2*root+1, L, Tree[root]. Mid ()); -Build (2*root+2, Tree[root]. Mid () +1, R); to } + voidInsert (ll root, ll S, ll E, ll X) - { theTree[root].sum + = x * (e-s +1); * if(Tree[root]. L = = S && e = = Tree[root]. R//Update to Interval $ {Panax NotoginsengTree[root].add + =x; - return ; the } + if(e <=Tree[root]. Mid ()) AInsert (2*root+1, S, e, x); the Else if(Tree[root]. Mid () <s) +Insert (2*root+2, S, e, x); - Else $ { $Insert (2*root+1, S, Tree[root]. Mid (), x); -Insert (2*root+2, Tree[root]. Mid () +1, E, x); - } the } - voidquery (ll Root, ll S, ll E)Wuyi { the - if(Tree[root]. L = = S && E = =Tree[root]. R) Wu { -Res + =tree[root].sum; About return ; $ } - if(Tree[root].add) -{//continue to update down -tree[2*root+1].add + =Tree[root].add; Atree[2*root+2].add + =Tree[root].add; +tree[2*root+1].sum + = Tree[root].add * (tree[2*root+1]. r-tree[2*root+1]. L +1); thetree[2*root+2].sum + = Tree[root].add * (tree[2*root+2]. r-tree[2*root+2]. L +1); -Tree[root].add =0; $ } the if(e <=Tree[root]. Mid ()) theQuery (2*root+1, S, e); the Else if(Tree[root]. Mid () <s) theQuery (2*root+2, S, e); - Else in { theQuery (2*root+1, S, Tree[root]. Mid ()); theQuery (2*root+2, Tree[root]. Mid () +1, e); About } the } the intMain () the { + LL N, M, num; - while(SCANF ("%i64d%i64d", &n, &m)! =EOF) the {BayiBuild (0,1, n); the for(intI=1; i<=n; i++) the { -scanf ("%i64d", &num); -Insert (0, I, I, num); the } the Charstr[2]; the LL S, E; the while(M--) - { thescanf ("%s%i64d%i64d", str, &s, &e); the if(str[0] =='Q') the {94res =0; theQuery (0, S, e); theprintf ("%i64d\n", res); the }98 Else About { -scanf ("%i64d", &num);101Insert (0, S, E, num);102 }103 }104 } the return 0;106}
Summer Training Mania Brush Series--POJ 3468 A simple problem with integers (segment tree + interval update)