Poj 3468 a simple problem with integers (segment tree Interval Update)

Source: Internet
Author: User

Address: poj 3468

When I came back from playing basketball, I felt a little impulsive .. The brainless game made eight wa requests .. The R-l is written as l-R during the update...

This is the bare question of Interval Update. Interval Update adds a lazy tag and a latency tag. The Lazy tag is updated downward only when it is queried downward. All others are based on the Line Segment tree.

The Code is as follows:

#include <iostream>#include <cstdio>#include <cstring>#include <math.h>#include <stack>using namespace std;#define LL long long#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int MAXN=1e5+10;LL sum[MAXN<<2];LL lazy[MAXN<<2];void PushUp(int rt){    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown(int rt, int m){    if(lazy[rt])    {        lazy[rt<<1]+=lazy[rt];        lazy[rt<<1|1]+=lazy[rt];        sum[rt<<1]+=lazy[rt]*(m-(m>>1));        sum[rt<<1|1]+=lazy[rt]*(m>>1);        lazy[rt]=0;    }}void build(int l, int r, int rt){    lazy[rt]=0;    if(l==r)    {        scanf("%lld",&sum[rt]);        return ;    }    int mid=(l+r)>>1;    build(lson);    build(rson);    PushUp(rt);}void update(int ll, int rr, int x, int l, int r, int rt){    if(ll<=l&&rr>=r)    {        lazy[rt]+=x;        sum[rt]+=(LL)x*(r-l+1);        return ;    }    PushDown(rt, r-l+1);    int mid=(l+r)>>1;    if(ll<=mid) update(ll,rr,x,lson);    if(rr>mid) update(ll,rr,x,rson);    PushUp(rt);}LL query(int ll, int rr, int l, int r, int rt){    if(ll<=l&&rr>=r)    {        return sum[rt];    }    PushDown(rt,r-l+1);    LL ans=0;    int mid=(l+r)>>1;    if(ll<=mid) ans+=query(ll,rr,lson);    if(rr>mid) ans+=query(ll,rr,rson);    return ans;}int main(){    int n, q, i, a, b, c;    char ch[3];    scanf("%d%d",&n,&q);    build(1,n,1);    while(q--)    {        getchar();        scanf("%s",&ch);        if(ch[0]=='Q')        {            scanf("%d%d",&a,&b);            LL ans=query(a,b,1,n,1);            printf("%lld\n",ans);        }        else        {            scanf("%d%d%d",&a,&b,&c);            update(a,b,c,1,n,1);        }    }    return 0;}


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.