COGS 723. [SDOI2007] Super Array

Source: Internet
Author: User

★★☆ input file: arr.in output file: arr.out Simple comparison
Time limit: 1 s memory limit: 4 MB

source:sdoi2007 Day2
"Problem description"

General array of people are often used, I believe many students have not seen the following super array.
The
Super array stores some positive integers, and it also supports the following two operations
(1), insert an element, the command is "I key". Key is the number to insert.
(2), the output of the K-large element and delete the element, the command is "D K". Output the K-large element and delete it.
"K" refers to: in the existing number, if from small to large order, from the smallest beginning as the first big count,
always count to the first K.
now give a super array with an empty start, so maintain the array.
"Input" (arr.in)
the first line n, m:n<=1, M<=100 000. Indicates that the number of insertions is 1 to n, in total M-bar
commands (including insertions and deletions).
The
following m lines, one command per line, are described in the title. A space between the letters in each command and the numbers that follow.
ensure that the input data is correct, the number of deletions must exist.

"Output" (arr.out)
for each delete command, output the number of deletes in the order of the Delete command, each line
"Sample Input"
Ten
I
I
I
d 3
I
D 2
D 2
d 1
I
I
"Sample Output"
About
$
the
$
splay

Dragon Slayer Blades click on Send

#include <cstdio>#include<cctype>#defineN 105000intdata[n],cnt[n],siz[n],ch[n][2],fa[n],root,cn,n,m;inlineintSonintx) {returnch[fa[x]][1]==x;} InlinevoidReadint&x) {RegisterCharCh=GetChar ();  for(x=0;! IsDigit (CH); ch=GetChar ());  for(; isdigit (CH); x=x*Ten+ch-'0', ch=GetChar ());} InlinevoidPushup (intRT) {    intl=ch[rt][0],r=ch[rt][1]; SIZ[RT]=siz[l]+siz[r]+cnt[rt];}voidRotateintx) {    intY=fa[x],z=fa[y],b=son (x), C=son (y), a=ch[x][!b]; if(z) ch[z][c]=x; Elseroot=x; FA[X]=Z; if(a) fa[a]=y; CH[Y][B]=A; ch[x][!b]=y; Fa[y]=x;    Pushup (y); Pushup (x);}voidSplay (intXinti) {     while(fa[x]!=i) {inty=fa[x],z=Fa[y]; if(z==i) rotate (x); Else        {            if(Son (x) = =Son (y)) rotate (y), rotate (x); Elserotate (x), rotate (x); }    }}voidInsint&rt,intx) {    if(!RT) {RT=++cn; DATA[CN]=x; CNT[CN]=siz[cn]=1; Splay (CN,0); return; }    if(data[rt]==x) {Cnt[rt]++; SIZ[RT]++; Splay (RT,0); return; }    if(x<Data[rt]) {INS (ch[rt][0],x); fa[ch[rt][0]]=RT;    Pushup (RT); }    Else{ins (ch[rt][1],x); fa[ch[rt][1]]=RT;    Pushup (RT); }}intGetkth (intRtintk) {    intl=ch[rt][0]; if(siz[l]+1&LT;=K&AMP;&AMP;K&LT;=SIZ[L]+CNT[RT])returnRT; if(siz[l]+1&GT;K)returnGetkth (ch[rt][0],k); if(cnt[rt]+siz[l]<k) k-=siz[l]+cnt[rt],getkth (ch[rt][1],k);}intGETMN (intRT) {    intret=-1, p=RT;  while(p) {ret=p; P=ch[p][0]; }    returnret;}voidDelintRtintx) {    if(data[rt]==x) {if(cnt[rt]>1) {Siz[rt]--; CNT[RT]--; }        Else{splay (RT,0); intP=GETMN (ch[rt][1]); if(p!=-1) {splay (P,RT); Root=p;fa[p]=0; ch[p][0]=ch[rt][0]; fa[ch[rt][0]]=p; }            Else{root=ch[rt][0]; fa[ch[rt][0]]=0; }        }        return; }    if(x<Data[rt]) {del (ch[rt][0],x);    Pushup (RT); }    Else{del (ch[rt][1],x);    Pushup (RT); }}intMain () {freopen ("arr.in","R", stdin); Freopen ("Arr.out","W", stdout);    Read (n); read (m); Charstr[5];  for(intk;m--;) {scanf ("%s", str);        Read (k); if(str[0]=='I') ins (root,k); Else        {            intx=data[getkth (root,k)]; printf ("%d\n", x);        Del (ROOT,X); }    }    return 0;}intsb=Main ();intMainintargcChar*argv[]) {;}

COGS 723. [SDOI2007] Super Array

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.