#include #include #include typedef struct pairingHeapNode{ struct pairingHeapNode *next; struct pairingHeapNode *child; unsigned char val[]; } heapNode; typedef struct pairingHeap{ heapNode *root; int heapSize; size_t size; int (*cmp)(const void *,const void *); } heap; heapNode* newHeapNode(const void *v,const size_t size){ heapNode *res=(heapNode *)malloc(sizeof(heapNode)+size); memcpy(res->val,v,size); res->next=NULL; res->child=NULL; return res; } void freeOneNode(heapNode *t){ free(t); return; } void freeAllNode(heapNode *t){ if(t==NULL) return; freeAllNode(t->next); freeAllNode(t->child); freeOneNode(t); return; } heap* newHeap(const size_t size,int (*cmp)(const void *,const void *)){ heap *res=(heap *)malloc(sizeof(heap)); res->root=NULL; res->heapSize=0; res->size=size; res->cmp=cmp; return res; } int getSize(const heap *h){ return h->heapSize; } int isEmpty(const heap *h){ return getSize(h)==0; } heapNode* meld(heapNode *a,heapNode *b,int (*cmp)(const void *a,const void *b)){ if(a==NULL) return b; if(b==NULL) return a; if(cmp(a->val,b->val)<=0){ b->next=a->child; a->child=b; return a; } else { a->next=b->child; b->child=a; return b; } } void push(heap *h,const void *v){ heapNode *p=newHeapNode(v,h->size); h->heapSize++; h->root=meld(h->root,p,h->cmp); return; } void pop(heap *h,void *v){ memcpy(v,h->root->val,h->size); heapNode *lst=h->root->child; int (*cmp)(const void *a,const void *b)=h->cmp; freeOneNode(h->root); h->heapSize--; heapNode *r=NULL; while(lst!=NULL && lst->next!=NULL){ heapNode *a=lst; heapNode *b=lst->next; heapNode *next=lst->next->next; a->next=b->next=NULL; r=meld(r,meld(a,b,cmp),cmp); lst=next; } if(lst!=NULL){ r=meld(r,lst,cmp); } h->root=r; return; } typedef long long int int64; int cmpMin(const void *a,const void *b){ int64 p=*(int64 *)a; int64 q=*(int64 *)b; return p==q?0:p-q<0?-1:1; } int cmpMax(const void *a,const void *b){ int64 p=*(int64 *)a; int64 q=*(int64 *)b; return p==q?0:p-q>0?-1:1; } inline int64 readInt(void){ int64 res=0; char c=fgetc(stdin); while(!(c=='-' || ('0'<=c && c<='9'))) c=fgetc(stdin); int sign=1; if(c=='-'){ sign=-1; c=fgetc(stdin); } while('0'<=c && c<='9'){ res=10*res+c-'0'; c=fgetc(stdin); } return res*sign; } inline void printInt(int64 v){ if(v<0){ fputc('-',stdout); v=-v; } else if(v==0){ fputc('0',stdout); return; } char c[20]; int k=0; while(v>0){ c[k++]=v%10+'0'; v/=10; } while(k>0) fputc(c[--k],stdout); } void run(void){ int q,k; scanf("%d%d",&q,&k); heap *small=newHeap(sizeof(int64),cmpMax); heap *large=newHeap(sizeof(int64),cmpMin); while(q--){ if(readInt()==1){ int64 v=readInt(); push(small,&v); if(getSize(small)>=k){ pop(small,&v); push(large,&v); } } else if(isEmpty(large)){ printInt(-1); fputc('\n',stdout); } else { int64 v; pop(large,&v); printInt(v); fputc('\n',stdout); } } return; } int main(void){ run(); return 0; }