#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; } 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--){ int c; scanf("%d",&c); if(c==1){ int64 v; scanf("%lld",&v); push(small,&v); if(getSize(small)>=k){ pop(small,&v); push(large,&v); } } else if(isEmpty(large)){ printf("-1\n"); } else { int64 v; pop(large,&v); printf("%lld\n",v); } } return; } int main(void){ run(); return 0; }