Valley 3380 "template" Two-force balance tree (tree-like array set of weight-value segment tree)

Source: Internet
Author: User

Title Description

You need to write a data structure (which can refer to the title of the topic) to maintain an ordered sequence, which requires the following actions:

    1. Query K ranking within the range

    2. Values that are ranked K in the query interval

    3. To modify a value on one of the values

    4. Query k in the interval of the precursor (the precursor is defined as strictly less than X, and the largest number, if not present output-2147483647)

    5. Query k in the interval successor (subsequent definition is strictly greater than X, and the smallest number, if there is no output 2147483647)
Note that the above two requirements are not the same as TYVJ or BZOJ, please note the input format input output format:

The first row of two numbers n,m indicates an ordered sequence of N and M operations

The second line has n number, indicating ordered sequence

There are m lines below, opt denotes operation label

If Opt=1 is Operation 1, then there are three numbers l,r,k indicating the rank of the query k in the interval [l,r]

If Opt=2 is Operation 2, then there are three numbers l,r,k representing the number of k in the query interval [l,r]

If Opt=3 is Operation 3, then there are two numbers pos,k to change the number of POS positions to K

If Opt=4 is Operation 4, then there are three numbers l,r,k representing the precursor of k in the query interval [l,r]

If the opt=5 is Operation 5, then there are three l,r,k to indicate the successor of K in the query interval [l,r].

Output format:

For the operation 1,2,4,5 each output row, indicating the query result

Input and Output Sample input example # #:
9 64 2 2 1 9 4 0 1 12 1 4 33 4 102 1 4 31 2 5 94 3 9 55 2 8 5
Sample # # of output:
24349
Description

Time limit: 2s,128m

N,m \leq 5\cdot {10}^4N,M≤5⋅10?4 ??   Guaranteed ordered sequence all values meet  [0 at any time, {ten} ^8] [0,10 ? 8 ?? /span>

Source: bzoj3196/tyvj1730 Two force balance tree, in this acknowledgement

This data is for the original Luo gu. (Special note: This data does not guarantee that operation 5, 6 must exist, so please be sure to consider the situation does not exist)

#include <cstdio>#include<iostream>#include<algorithm>#defineN 50001#defineM n*400#defineLowbit (x) x& (-X)#defineCalmid int mid=l+r>>1;#definePreA a[0]=0; while (R) A[++a[0]]=root[r],r-=lowbit (R);#definePreb b[0]=0; while (l) b[++b[0]]=root[l],l-=lowbit (L);#defineGOLEFTA for (int i=1;i<=a[0];i++) a[i]=lc[a[i];#defineGOLEFTB for (int i=1;i<=b[0];i++) b[i]=lc[b[i];#defineGorighta for (int i=1;i<=a[0];i++) a[i]=rc[a[i];#defineGORIGHTB for (int i=1;i<=b[0];i++) b[i]=rc[b[i];#defineGotmpa for (int i=1;i<=a[0];i++) tmp+=sum[lc[a[i]];#defineGOTMPB for (int i=1;i<=b[0];i++) tmp-=sum[lc[b[i]];intN,m,tot;introot[n],a[n],hash[n<<1];inta[ -],b[ -];namespacecmt{intCnt=0, Lc[m],rc[m],sum[m]; voidInsertint&k,intLintRintPosintW) {if(!k) k=++CNT; SUM[K]+=W; if(L==R)return; intMid=l+r>>1; if(pos<=mid) Insert (LC[K],L,MID,POS,W); ElseInsert (rc[k],mid+1, r,pos,w); }    intGetRank (intLintRintWBOOLty) {        inttmp=0; if(l==r) {if(!ty)return 0; Else             {                 for(intI=1; i<=a[0];i++) tmp+=Sum[a[i]];  for(intI=1; i<=b[0];i++) tmp-=Sum[b[i]]; returntmp; }} Gotmpa; GOTMPB;        Calmid; if(w<=mid) {golefta; goleftb; returnGetRank (l,mid,w,ty); }        Else{gorighta; gorightb; returnTmp+getrank (mid+1, R,w,ty); }    }    intGetnum (intLintRintW) {if(L==R)returnHash[l]; inttmp=0; Gotmpa; GOTMPB;        Calmid; if(w<=tmp)            {golefta; goleftb; returnGetnum (L,MID,W); }        Else{gorighta; gorightb; returnGetnum (mid+1, r,w-tmp); }    }}namespacebit{voidAddintPosintKeyintval) {         while(pos<=N) {Cmt::insert (Root[pos],1, Tot,key,val); POS+=Lowbit (POS); }    }    intQrank (intLintRintKBOOLty)        {PreA;        Preb; returnCmt::getrank (1, Tot,k,ty); }    intQnum (intLintRintk) {PreA;        Preb; returnCmt::getnum (1, tot,k); }    voidModifyintPosintW) {a[0]=0;inttmp=POS;  while(pos<=n) a[++a[0]]=pos,pos+=Lowbit (POS);  for(intI=1; i<=a[0];i++) Cmt::insert (Root[a[i]),1, tot,a[tmp],-1);  for(intI=1; i<=a[0];i++) Cmt::insert (Root[a[i]),1, Tot,w,1); A[TMP]=W; }}namespacesolution{structNode {intOP,OPL,OPR,OPK;    }e[n]; voidReadint&x) {x=0;CharC=GetChar ();  while(!isdigit (c)) c=GetChar ();  while(IsDigit (c)) {x=x*Ten+c-'0'; C=GetChar ();} }     voidinit () {read (n); read (m);  for(intI=1; i<=n;i++) read (A[i]), hash[i]=A[i]; intOpt,l,r,k;inttmp=N;  for(intI=1; i<=m;i++) {read (opt); if(opt==3) Read (l), read (k); ElseRead (l), read ( R), read (k); E[i].op=opt; E[i].opl=l; e[i].opk=K; if(opt!=3) e[i].opr=R; if(opt!=2) hash[++tmp]=K; } std::sort (Hash+1, hash+tmp+1); Tot=std::unique (hash+1, hash+tmp+1)-(hash+1);  for(intI=1; i<=n;i++) {A[i]=std::lower_bound (hash+1, hash+tot+1, A[i])-Hash; Bit::add (I,a[i],1); }         for(intI=1; i<=m;i++)        if(e[i].op!=2) E[i].opk=std::lower_bound (hash+1, hash+tot+1, E[I].OPK)-Hash; }    voidsolve () { for(intI=1; i<=m;i++)        {            if(e[i].op==1) printf ("%d\n", Bit::qrank (e[i].opl-1, E[I].OPR,E[I].OPK,0)+1); Else if(e[i].op==2) printf ("%d\n", Bit::qnum (e[i].opl-1, E[I].OPR,E[I].OPK)); Else if(e[i].op==3) bit::modify (E[I].OPL,E[I].OPK); Else if(e[i].op==4)             {                intTmp=bit::qrank (e[i].opl-1, E[I].OPR,E[I].OPK,0); if(!tmp) printf ("-2147483647\n"); Elseprintf"%d\n", Bit::qnum (e[i].opl-1, e[i].opr,tmp)); }            Else            {                intTmp=bit::qrank (e[i].opl-1, E[I].OPR,E[I].OPK,1); if(tmp==e[i].opr-e[i].opl+1) printf ("2147483647\n"); Elseprintf"%d\n", Bit::qnum (e[i].opl-1, e[i].opr,tmp+1)); }        }    } } intMain () {using namespacesolution;    Init (); Solve ();}

Valley 3380 "template" Two-force balance tree (tree-like array set of weight-value segment tree)

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.