Simple POJ 3468 A simple problem with integers, segment tree + interval update.

Source: Internet
Author: User

Description

You have N integers, a1, a2, ..., an. You need to deal with both kinds of operations. One type of operation is to add some given number to each number in a given interval. The other are to ask for the sum of numbers in a given interval.

Test instructions is well understood, is the standard line-segment tree interval update problem, can be said to be a template.

However, because the writing is not skilled, was the pit two WA, the wrong place in the code is marked out ...

The code is as follows:

#include <iostream>#include<cstdio>#defineLson l,m,po*2#defineRson m+1,r,po*2+1using namespacestd;Long Longbit[100005*4];Long Longcol[100005*4];voidPushup (intPO) {Bit[po]=bit[po*2]+bit[po*2+1];}voidPushdown (intPointLen) {    if(Col[po]) {Bit[po*2]+=(Long Long) col[po]* (len-(len/2)); Bit[po*2+1]+=(Long Long) col[po]* (len/2); Col[po*2]+=COL[PO];//Note is + =, not =!!! col[po*2+1]+=Col[po]; COL[PO]=0; }}voidBuild_tree (intLintRintPO) {    if(l==R) {Col[po]=0; CIN>>Bit[po]; return; }    intM= (L+R)/2;    Build_tree (Lson);    Build_tree (Rson); Pushup (PO);}Long LongQueryintQlintQrintLintRintPO) {    if(ql<=l&&qr>=R)returnBit[po]; Pushdown (PO, (R-l+1)); intM= (L+R)/2; if(qr<=M)returnquery (Ql,qr,lson); if(ql>M)returnquery (Ql,qr,rson); returnQuery (Ql,qr,rson) +query (Ql,qr,lson);}voidUpdateintUlinturintAddintLintRintPO) {    if(ul<=l&&ur>=R) {Bit[po]+=(Long Long) add* (r-l+1); COL[PO]+=(Long Long) add; return; } pushdown (Po,r-l+1); intM= (L+R)/2; if(ul<=M) Update (Ul,ur,add,lson); if(ur>M) Update (Ul,ur,add,rson); Pushup (PO);}intMain () {intn,q; CharC; intA,b,c;  while(~SCANF ("%d%d",&n,&Q) {Build_tree (1N1);  for(intI=0; i<q;++i) {cin>>b; if(c=='Q') {scanf ("%d%d",&a,&b); cout<<query (A, B,1N1) <<Endl; }            Else{scanf (" %d%d%d",&a,&b,&c); Update (A,B,C,1N1); }        }    }    return 0;}
View Code

(simple) POJ 3468 A easy problem with integers, segment tree + interval update.

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.