Codeforces 52C Circular RMQ

Source: Internet
Author: User

Segment Tree interval Update maintenance min ... Remember to devolve the mark ...

If a complete interval on the segment tree is modified, then the minimum and maximum values are added to the corresponding values,

Change is because part of the change and the other part has not changed, so maintenance is good.

When asked, also remember to devolve the mark ...

Data structure quickly forget, put a board.

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intMAXN = 2e5+1;structseg{ll Lazy; ll Min;} TR[MAXN<<2];intA[MAXN];intN;#defineLid (id<<1)#defineRID (id<<1|1)voidBuildintL =0,intR = N1,intID =1){    if(L = = r) {Tr[id]. Min = A[l];return; }//Tr[rt].lazy}    intMid = (l+r) >>1, LC = lid, rc =rid;    Build (L,MID,LC); Build (Mid+1, R,RC); Tr[id]. Min=min (TR[LC]. MIN,TR[RC]. Min);}intql,qr;ll Val;#defineModify (id,v) Tr[id].lazy + = V; Tr[id]. Min + = V;voidUpdata (intL =0,intR = N1,intID =1){    if(QL&LT;=L&AMP;&AMP;R&LT;=QR) {Modify (Id,val)return; } intMid = (l+r) >>1, LC = lid, rc =rid; if(Tr[id].lazy) {ll&t =Tr[id].lazy; Modify (lc,t) Modify (rc,t) T=0; }    if(ql<=mid)    {Updata (L,MID,LC); }    if(qr>mid) {Updata (mid)+1, R,RC); } Tr[id]. Min=min (TR[LC]. MIN,TR[RC]. Min);}Constll INF =0x3f3f3f3f3f3f3f3f; ll query (intL =0,intR = N1,intID =1){    if(ql<=l&&r<=qr) {        returnTr[id].    Min; }    intMid = (l+r) >>1, LC = lid, rc =rid; if(Tr[id].lazy) {ll&t =Tr[id].lazy;        Modify (lc,t);        Modify (rc,t); T=0; } LL ret=INF; if(ql<=mid) {ret=min (ret,query (L,MID,LC)); }    if(qr>mid) {ret= Min (Ret,query (mid+1, R,RC)); }    returnret;}BOOLSscan_l (int&x,Char*&s) {     while(!isdigit (*s) && *s! ='-'){        if(!*s)return false; S++; }    BOOLFG; if(*s = ='-') FG =true, x =0; Elsex = *s-'0', FG =false;  while(S++,isdigit (*s)) x = x*Ten+*s-'0'; if(FG) x =-x; return true;}Charline[666];intMain () {//freopen ("In.txt", "R", stdin);scanf"%d",&N);  for(inti =0; I < n; i++) scanf ("%d", A +i);    Build (); intM scanf"%d\n",&m);  while(m--) {gets (line); Char*p =Line ; intnum[3], I;  for(i =0; I <3; i++ ){            if(!sscan_l (NUM[I],P)) Break; }        if(i = =2){            intL = num[0],r = num[1]; if(l<=r) {QL= L; QR =R; printf ("%i64d\n", query ()); }Else{QL=0; QR =R; ll ans=query (); QL= L; QR = n1; Ans=min (Ans,query ()); printf ("%i64d\n", ans); }        }Else {            intL = num[0], R = num[1]; val = num[2]; if(l<=R) {QL= L; QR =R;            Updata (); }Else{QL=0; QR =R;                Updata (); QL= L; QR = n1;            Updata (); }        }    }    return 0;}

Codeforces 52C Circular RMQ

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.