POJ 3468 A Easy problem with integers: Segment tree simple operation note Update to interval rather than leaf node

Source: Internet
Author: User

#include <cstdio>#include<iostream>using namespacestd;#defineSize 100000structnode{intL, R; Long LongSum, INC; intMid () {return(L+R)/2; }}tree[size*3];voidCreattree (intRootintLintR//Build Interval Tree{Tree[root]. L=L; Tree[root]. R=R; Tree[root]. Sum=0; Tree[root]. INC=0; if(l==R)return; Creattree (Root*2+1, L, (l+r)/2 ); Creattree (Root*2+2, (L+R)/2+1, R);}voidInsert (intRootintIintj)//position I insert J{        if(Tree[root]. L = =Tree[root]. R) {Tree[root]. Sum=J; return ; } Tree[root]. Sum+=J; if(i<=Tree[root]. Mid ()) Insert (Root*2+1, I, J); ElseInsert (Root*2+2, I, j);}voidADD (intRootintSintEintj)//interval [s, e] on each element plus J{        if(Tree[root]. L==s && Tree[root]. r==e) {Tree[root]. INC+=J; return ; } Tree[root]. Sum+ = (e-s+1)*J; if(s>Tree[root]. Mid ()) Add (Root*2+2, S, E, j); Else            if(e<=Tree[root]. Mid ()) Add (Root*2+1, S, E, j); Else{Add (root*2+1, S, Tree[root].            Mid (), j); ADD (Root*2+2, Tree[root]. Mid () +1, E, J); }}Long LongQuery (intRootintSintE//query interval [s,e] and{        if(Tree[root]. L==s && Tree[root]. r==e)returnTree[root]. Sum+tree[root]. inc* (e-s+1); Tree[root]. Sum+ = (Tree[root]. R-tree[root]. L +1)*Tree[root].        INC; ADD (2*root+1, Tree[root]. L, Tree[root]. Mid (), Tree[root].        INC); ADD (2*root+2, Tree[root]. Mid () +1, Tree[root]. R, Tree[root].        INC); Tree[root]. INC=0; if(e<=Tree[root]. Mid ())returnQuery (root*2+1, S, e); Else            if(s>Tree[root]. Mid ())returnQuery (root*2+2, S, e); Else                returnQuery (root*2+1, S, Tree[root]. Mid ()) +Query (Root*2+2, Tree[root]. Mid () +1, E);}intMain () {intN, Q, A, B, C; CharCMD; scanf ("%d%d", &n, &Q); Creattree (0,1, N);  for(intI=1; i<=n; i++) {scanf ("%d", &a); Insert (0, I, a); }         for(intI=0; i<q; i++) {cin>>CMD; if(CMD = ='Q') {scanf ("%d%d", &a, &b); printf ("%i64d\n", Query (0, A, b)); }                Else{scanf ("%d%d%d", &a, &b, &c); ADD (0, A, b, c); }        }        return 0;}

Although the code is a bit long, but is the basic operation of the line tree, so the overall idea is clear, there is a version of the node with the left and right child node pointer, the province of space, the best time to avoid the wrong game, usually when the exercise is good, and now I go with a pointer to do it again.

POJ 3468 A Easy problem with integers: Segment tree simple operation note Update to interval rather than leaf node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.