Language: C++

#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

const int MAX_N=1<<19;
// ZOgØðàÂO[ozñ
int dat[2*MAX_N-1];
int n;
int tail=0;

// io[©çindexðÁè
map<int,int> numToIndex;

// ú»
void init(int n_){
    n=1;
    // vfðñÌ×«æÉ
    while(n<n_)n*=2;
    fill(dat,dat+2*MAX_N-1,0);
    for(int i = n-1; i < 2*MAX_N-1; i++)
        dat[i]=INT_MAX;
}
//// kÔÚÌlðaÉÏX
//void update(int k,int a){
//    k+=n-1;
//    dat[k]=a;
//    while(k>0){
//        k=(k-1)/2;
//        dat[k]=min(dat[k*2+1],dat[k*2+2]);
//    }
//}

// idxÌÔðèÄAzñÌöÉÇÁ
void query0(int idx){
    int k=tail;
    k+=n-1;
    dat[k]=idx;
    // idx©çzñÌêðtø«Å«éæ¤É·é
    numToIndex[idx]=tail;
    // ãèÈªçlðÁ³¹Ä¢­
    while(k>0){
        k=(k-1)/2;
        dat[k]++;
    }
    tail++;
}
// zñÌxÔÚÌvfðí
void query1(int x){
    int k=x;
    k+=n-1;
    // í
    dat[k]=INT_MAX;
    // ãèÈªçlðÁ³¹Ä¢­
    while(k>0){
        k=(k-1)/2;
        dat[k]--;
    }
}

//// cÁÄévfð¦ÄkÔÚÌêÉ éÅÌÔðoÍ
//int query2(int a,int b,int k,int l,int r){
//    if(r<=a||b<=l)
//        return INT_MAX;
//    if(a<=1&&r<=b)
//        return dat[k];
//    //int vl=query(a,b,k*2+1,l,(l+r)/2);
//    //int vr=query(a,b,k*2+2,(l+r)/2,r);
//    // ¶ÆEÌð¦é
//    int vl=dat[k*2+1];
//    int vr=dat[k*2+2];
////    return min(vl,vr);
//}

// ¡cÁÄ¢évfÌ¤¿AidxÔÚÌÅÌÔðoÍ
int query2(int idx,int place=0,int child=0){
    // ñªßéàÌ
    if(2*place+1>=n-1){
        if(idx==1){
            if(dat[2*place+1]==INT_MAX)
                return dat[2*place+2];
            return dat[2*place+1];
        }
        return dat[2*place+2];
    }
    // ¶ÆEÌð¦é
    int vl=dat[place*2+1];
    int vr=dat[place*2+2];
    // ¶ÉidxÔÚª¶Ý
    int res;
    if(idx<=vl)
        res=query2(idx,place*2+1,vl);
    // EÉidxÔÚª¶Ý
    else
        res=query2(idx-vl,place*2+2,vr);
    return res;
}
// àÁÆàÃ¢vfðí·é
void oldDelete(int place=0){
    if(2*place+1>=n-1){
        // Ôðæ¾
        int num=dat[2*place+1];
        // tø«
        int idx=numToIndex[num];
        query1(idx);
        return;
    }
    // ¶ÆEÌð¦é
    int vl=dat[place*2+1];
    // ¶ÉidxÔÚª¶Ý
    int res;
    if(vl>=1)
        oldDelete(place*2+1);
    // EÉidxÔÚª¶Ý
    else
        oldDelete(place*2+2);
}

// ÔªxÌÅÌðí
void query3(int x){
    int index=numToIndex[x];
    query1(index);
}

// [a,b)ÉcÁÄ¢éÅÌÌð½¹,
int query(int a,int b,int k,int l,int r){
    if(r<=a||b<=l)
        return INT_MAX;
    if(a<=1&&r<=b)
        return dat[k];
    int vl=query(a,b,k*2+1,l,(l+r)/2);
    int vr=query(a,b,k*2+2,(l+r)/2,r);
    return min(vl,vr);
}

// Ô©çio[ðÁè
map<int,int> indexToNum;

void solve(){
    int q,lim;
    while(cin>>q>>lim&&q!=0){
        init(q+1);
        //n=q+1;
        tail=0;
        numToIndex.clear();
        int kosu=0;
        for(int i = 0; i < q; i++){
            int qe,x;
            cin>>qe>>x;
            if(qe==0){
                query0(x);
                kosu++;
                // àµlimðz¦½çAàÁÆàÃ­o^³ê½àÌ(idxªàÁÆà¬³¢àÌ)ðí·é
                if(kosu>lim){
                    oldDelete();
                    kosu--;
                }
            }
            else if(qe==1){
                query1(x-1);
                kosu--;
            }
            else if(qe==2)
                cout<<query2(x)<<endl;
            else{
                query3(x);
                kosu--;
            }
        }
        cout<<"end"<<endl;
    }
}


int main(){

    solve();

    return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:40:16: error: 'INT_MAX' was not declared in this scope
   40 |         dat[i]=INT_MAX;
      |                ^~~~~~~
a.cc:16:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   15 | #include <cstring>
  +++ |+#include <climits>
   16 | #include <sstream>
a.cc: In function 'void query1(int)':
a.cc:71:12: error: 'INT_MAX' was not declared in this scope
   71 |     dat[k]=INT_MAX;
      |            ^~~~~~~
a.cc:71:12: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'int query2(int, int, int)':
a.cc:98:32: error: 'INT_MAX' was not declared in this scope
   98 |             if(dat[2*place+1]==INT_MAX)
      |                                ^~~~~~~
a.cc:98:32: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc: In function 'int query(int, int, int, int, int)':
a.cc:146:16: error: 'INT_MAX' was not declared in this scope
  146 |         return INT_MAX;
      |                ^~~~~~~
a.cc:146:16: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'