UVA-12436 Rip Van Winkle ' s Code (segment tree interval update)

Source: Internet
Author: User

The main idea: an array, four kinds of operations:

LongLong data[250001];void A (Int St,Intnd) {Forint i = st; I <= nd; i++) Data[i] = Data[i] + (i-st +1);}void B (Int St,Intnd) {Forint i = st; I <= nd; i++) Data[i] = Data[i] + (Nd-i +1);}void C (INT St, int nd, int x) {for ( int i = st; i <= nd; i++) data[i] = x;} Long Long S ( int st, int nd) { long long res = 0; For ( int i = st; i <= nd; i++) res + = Data[i]; return Res;}                 

Simulate these four kinds of operations.

Topic Analysis: Three kinds of update operation, one kind of inquiry operation. The three updates are actually two, the add update (arithmetic progression is still arithmetic progression) and the set update, and the add updated lazy tag records the first, last, and tolerance.

The code is as follows:

# include<bits/stdc++.h>using namespace std;# define LL long long# define MID (L + (R-L)/2) const int N=250000;STRUCT N    ode{LL sum;    LL x,d,st,ed;    BOOL Lazy_set; bool Lazy_add;};    Node tr[(n+5) *4+100];char op[2];void clear_lazy (int rt) {Tr[rt].lazy_add=false; Tr[rt].d=tr[rt].st=tr[rt].ed=0;}    void Change1 (int rt,int l,int r,ll st,ll ed,ll d) {tr[rt].lazy_add=true;    tr[rt].sum+= (LL) (r-l+1) * (st+ed)/2;    tr[rt].st+=st;    tr[rt].ed+=ed; Tr[rt].d+=d;}    void Change2 (int rt,int l,int r,ll x) {tr[rt].lazy_set=true;    Tr[rt].sum= (LL) (r-l+1) *x;    Tr[rt].x=x; Clear_lazy (RT);} void pushup (int rt) {tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;}        void pushdown (int rt,int l,int R) {if (Tr[rt].lazy_set) {change2 (rt<<1,l,mid,tr[rt].x);        Change2 (rt<<1|1,mid+1,r,tr[rt].x);    Tr[rt].lazy_set=false;        } if (Tr[rt].lazy_add) {LL st=tr[rt].st;        LL Ed=tr[rt].ed;        int d=tr[rt].d; Change1 (rt<<1,l,mid,st,st+d* (mid-l), d);        Change1 (rt<<1|1,mid+1,r,st+d* (mid-l+1), ed,d);    Clear_lazy (RT);    }}void Build (int rt,int l,int r) {tr[rt].lazy_set=false;    Tr[rt].lazy_add=false;    Tr[rt].sum=tr[rt].d=0;    Tr[rt].st=tr[rt].ed=0;    if (l==r) return;    Build (Rt<<1,l,mid); Build (Rt<<1|1,mid+1,r);} void update1 (int rt,int l,int r,int l,int r,ll D) {if (l<=l&&r<=r) {if (d>0) {Change1 (R        T,L,R,L-L+1,R-L+1,D);        }else{Change1 (rt,l,r,r-l+1,r-r+1,d);        }}else{pushdown (rt,l,r);        if (l<=mid) update1 (rt<<1,l,mid,l,r,d);        if (r>mid) update1 (rt<<1|1,mid+1,r,l,r,d);   Pushup (RT);   }}void update2 (int rt,int l,int r,ll l,ll r,ll x) {if (l<=l&&r<=r) {change2 (rt,l,r,x);       }else{pushdown (RT,L,R);       if (l<=mid) update2 (rt<<1,l,mid,l,r,x);       if (r>mid) update2 (rt<<1|1,mid+1,r,l,r,x);   Pushup (RT); }}ll query (int rt,int l,int R, LL l,ll R) {if (l<=l&&r<=r) return tr[rt].sum;    Pushdown (RT,L,R);    LL res=0;    if (l<=mid) res+=query (rt<<1,l,mid,l,r);    if (r>mid) res+=query (rt<<1|1,mid+1,r,l,r); return res;}    int main () {//freopen ("In.txt", "R", stdin);    int n;        while (scanf ("%d", &n)!=eof) {build (1,1,n);        LL A,b,c;            while (n--) {scanf ("%s", op);                if (op[0]== ' A ') {scanf ("%lld%lld", &a,&b);            Update1 (1,1,N,A,B,1LL);                }else if (op[0]== ' B ') {scanf ("%lld%lld", &a,&b);            Update1 (1,1,N,A,B,-1LL);                }else if (op[0]== ' C ') {scanf ("%lld%lld%lld", &a,&b,&c);            Update2 (1,1,N,A,B,C);                }else{scanf ("%lld%lld", &a,&b);            printf ("%lld\n", Query (1,1,n,a,b)); }}} return 0;}

UVA-12436 Rip Van Winkle ' s Code (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.