Language: C++

#include <cstdio>
#include <map>

#define MAX_Q (400000)
#define ll I64

using namespace std;

typedef __int64 lint;


int bit[MAX_Q + 1];

void add(int pos, int val)
{
	while (pos <= MAX_Q){
		bit[pos] += val;
		pos += pos & -pos;
	}
}

int sum(int pos)
{
	int ret = 0;
	
	while (pos){
		ret += bit[pos];
		pos &= (pos - 1);
	}
	
	return (ret);
}

void print(int x, map<int, lint> val)
{
	int l = 0, r = MAX_Q;
	
	while (l != r){
		int center = (l + r) / 2;
		
		if (sum(center) < x){
			l = center + 1;
		}
		else {
			r = center;
		}
	}
	
	printf("%lld\n", val[r]);
}

void erase(int x)
{
	int l = 0, r = MAX_Q;
	
	while (l != r){
		int center = (l + r) / 2;
		
		if (sum(center) < x){
			l = center + 1;
		}
		else {
			r = center;
		}
	}
	
	add(r, -1);
}

int main()
{
	int q, lim;
	
	while (scanf("%d %d", &q, &lim), lim){
		memset(bit, 0, sizeof(bit));
		map<lint, int> pos;
		map<int, lint> val;
		
		int p = 1;
		
		while (q--){
			int qtype, x;
			scanf("%d %lld", &qtype, &x);
			
			if (qtype == 0){
				add(p, 1);
				pos[x] = p;
				val[p++] = x;
				if (sum(MAX_Q) > lim){
					erase(1);
				}
			}
			if (qtype == 1){
				erase(x);
			}
			if (qtype == 2){
				print(x, val);
			}
			if (qtype == 3){
				add(pos[x], -1);
			}
		}
		
		printf("end\n");
	}
	
	return (0);
}

Compiler output:
a.cc:9:9: error: '__int64' does not name a type; did you mean '__int64_t'?
    9 | typedef __int64 lint;
      |         ^~~~~~~
      |         __int64_t
a.cc:34:28: error: 'lint' was not declared in this scope; did you mean 'int'?
   34 | void print(int x, map<int, lint> val)
      |                            ^~~~
      |                            int
a.cc:34:32: error: template argument 2 is invalid
   34 | void print(int x, map<int, lint> val)
      |                                ^
a.cc:34:32: error: template argument 4 is invalid
a.cc: In function 'void print(int, int)':
a.cc:49:29: error: invalid types 'int[int]' for array subscript
   49 |         printf("%lld\n", val[r]);
      |                             ^
a.cc: In function 'int main()':
a.cc:75:17: error: 'memset' was not declared in this scope
   75 |                 memset(bit, 0, sizeof(bit));
      |                 ^~~~~~
a.cc:3:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    2 | #include <map>
  +++ |+#include <cstring>
    3 | 
a.cc:76:21: error: 'lint' was not declared in this scope; did you mean 'int'?
   76 |                 map<lint, int> pos;
      |                     ^~~~
      |                     int
a.cc:76:30: error: template argument 1 is invalid
   76 |                 map<lint, int> pos;
      |                              ^
a.cc:76:30: error: template argument 3 is invalid
a.cc:76:30: error: template argument 4 is invalid
a.cc:77:30: error: template argument 4 is invalid
   77 |                 map<int, lint> val;
      |                              ^
a.cc:87:36: error: invalid types 'int[int]' for array subscript
   87 |                                 pos[x] = p;
      |                                    ^
a.cc:88:36: error: invalid types 'int[int]' for array subscript
   88 |                                 val[p++] = x;
      |                                    ^
a.cc:100:40: error: invalid types 'int[int]' for array subscript
  100 |                                 add(pos[x], -1);
      |                                        ^