HDU-5096 ACM Rank (treap)

Source: Internet
Author: User
Tags define local

The problem of the balance tree, treap broken, more difficult to get the same number of penalties when the situation, the solution is to use a set at each node, compare the value of the time can be the number of questions and penalties to save together.

Initialization has a certain skill. A lot of details are error prone.

Write wrong A variable name look for a long time, awkward

#include <cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<vector>#include<map>#include<Set>#include<algorithm>using namespacestd;#definePB push_back#defineMP Make_pair#defineFi first#defineSe Second#defineCER (x) cout<< #x << ' = ' <<endltypedefLong Longll;Const intMAXN = 1e4+5, Maxpen = 1e6;//60*20+3000+666;intST[MAXN], VAL[MAXN], pen[maxn][Ten],LST[MAXN],LSA[MAXN];structcmp{BOOL operator()(intAintbConst{        returnLsa[a] <Lsa[b]; }};structnode{intch[2],r,s,v; Set<int,cmp>vs;} NDS[MAXN*2];queue<int>Fre;inlineintNewNode (intx) {    inti; Node&u = Nds[i =Fre.front ()];    Fre.pop (); u.ch[0] = u.ch[1] =0; U.R= rand (); U.S. =1; U.V =Val[x]; U.vs.clear ();    U.vs.insert (x); returni;} InlinevoidDelt (intx) {Fre.push (x);} InlineintcmpintAintb) {    if(A = = b)return-1; returnA>b?0:1;} InlinevoidMtinti) {Node&u =Nds[i]; U.S.= (int) u.vs.size () + nds[u.ch[0]].s + nds[u.ch[1]].s;} InlinevoidRotint&o,intd) {    intK = nds[o].ch[d^1]; Nds[o].ch[d^1] =Nds[k].ch[d]; NDS[K].CH[D]=o; MT (O);    MT (k); o=K;} InlinevoidInittreap (intN) {     for(inti =1; I < n; i++) Fre.push (i); nds[0].S =0;}voidInstint&o,intx) {    if(!o) {o= NewNode (x);return; } Node&u =Nds[o]; intD =CMP (u.v,val[x]); if(~d)        {Inst (u.ch[d],x); if(U.R < NDS[U.CH[D]].R) rot (o,d^1); //Else}Else{U.S.++; U.vs.insert (x); } MT (o);}voidRmov (int&o,intx) {Node&u =Nds[o]; intD =CMP (u.v,val[x]); if(~d)    {Rmov (u.ch[d],x); }Else {        if((int) U.vs.size () >1) {u.vs.erase (x); U.S.--; }Else {            if(u.ch[0] && u.ch[1]){                intD2 = nds[u.ch[0]].R > nds[u.ch[1]].R?1:0; Rot (O,D2);            Rmov (NDS[O].CH[D2],X); }Else{delt (o); if(!u.ch[0]) O = u.ch[1]; Elseo = u.ch[0]; }        }    }    if(o) MT (o);}intRank (intOintx) {Node&u =Nds[o]; intD =CMP (u.v,val[x]); if(d = =1)returnRank (u.ch[1],x); ints = nds[u.ch[1]].s; if(d = =0)returnS+ (int) u.vs.size () +rank (u.ch[0],x); returnS+1;}intKth (intOintk) {    if(!o | | k <=0|| K > Nds[o].s)return-1;//Node &u =Nds[o]; ints = nds[u.ch[1]].s; if(k = = S+1)return*(U.vs.begin ()); if(k <= s)returnKth (u.ch[1],k); returnKth (u.ch[0],k-s-(int) u.vs.size ());}voidRmvtree (into) {    if(nds[o].ch[0]) Rmvtree (nds[o].ch[0]); if(nds[o].ch[1]) Rmvtree (nds[o].ch[1]); Delt (o);}//#define LOCALintMain () {#ifdef LOCAL freopen ("Data.txt","R", stdin);#endif    intn,m; Charop[ -]; Inittreap (MAXN*2); intRoot =0;  while(~SCANF ("%d%d",&n,&M)) {        if(Root) rmvtree (root); Root=0; Fill (st,st+n,0); Fill (Val,val+n,0); Fill (Lst,lst+n,-5); memset (pen,0,sizeof(pen));  for(inti =0; i < N; i++) {Lsa[i]=-n+i;        Inst (root,i); }        intc =0;  while(~SCANF ("%s", op) &&*op! ='C'){            if(*op = ='S'){                intT,x,re;CharPro scanf"%d:%d:%c:%d",&t,&x,&pro,&re); intp = pro-'A'; if(T-lst[x] <5|| (st[x]& (1&LT;&LT;P)))Continue; LST[X]=T; if(Re = =1) {printf ("[%d][%c]\n", X,pro);                    Rmov (ROOT,X); LSA[X]= ++C; ST[X]|=1<<p; VAL[X]+ = maxpen-pen[x][p]-T;                Inst (root,x); }ElsePEN[X][P] + = -; }Else if(*op = ='R'){                intX scanf"%d",&x); printf ("%d\n", Rank (root,x)); }Else if(*op = ='T'){                intK scanf"%d",&k); printf ("%d\n", Kth (root,k)); }} scanf ("%s", op); Puts (""); }    return 0;}

HDU-5096 ACM Rank (treap)

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.