#include #include #include typedef long long int int64; typedef struct pairingHeapNode{ void *val; struct pairingHeapNode *next; struct pairingHeapNode *child; } heapNode; typedef struct pairingHeap{ heapNode *root; int heapSize; size_t size; int (*cmp)(const void *,const void *); } heap; heapNode* newHeapNode(void *v,size_t size){ heapNode *res=(heapNode *)malloc(sizeof(heapNode)); res->val=malloc(size); memcpy(res->val,v,size); res->next=NULL; res->child=NULL; return res; } void freeOneNode(heapNode *t){ free(t->val); free(t); return; } void freeAllNode(heapNode *t){ if(t==NULL) return; freeAllNode(t->next); freeAllNode(t->child); freeOneNode(t); return; } heap* newHeap(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(heap *h){ return h->heapSize; } int isEmpty(heap *h){ return getSize(h)==0; } heapNode* merge(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)){ b->next=a->child; a->child=b; return a; } else { a->next=b->child; b->child=a; return b; } } void push(heap *h,void *v){ heapNode *p=newHeapNode(v,h->size); h->heapSize++; h->root=merge(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; free(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=merge(r,merge(a,b,cmp),cmp); lst=next; } if(lst!=NULL){ r=merge(r,lst,cmp); } h->root=r; return; } int cmpMin(const void *a,const void *b){ return *((int64 *)a)<=*((int64 *)b); } int cmpMax(const void *a,const void *b){ return *((int64 *)a)>=*((int64 *)b); } 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; }