Language: C++

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;
typedef long long ll;

int s[1<<20];
int size;

// k人目の状態をseg-treeで管理
// ノードは区間内の生存者の合計を保持
void init(int n){
	size = 1;
	while(size < n){
		size *= 2;
	}
	memset(s, 0, sizeof(int)*size*2);
}

// k番目にaを加算
void add(int k, int a){
	k += size-1;
	s[k] += a;
	while(k > 0){
		k = (k-1)/2;
		s[k]+=a;
	}
}

// 生存者のn番目の位置を探す
int query(int n, int k, int l, int r){
	if(s[k] < n || s[k] == 0) return -1;
	if(r - l == 1){
		return k-(size-1);
	}
	int chl = k*2+1, chr = k*2+2;
	int ret = query(n, chl, l, (l+r)/2);
	if(ret != -1) return ret;
	return query(n-s[chl], chr, (l+r)/2, r);
}


int main(){
	int q, lim;
	while(cin >> q >> lim, q|lim){
		init(q);
		map<int, int> idmap;
		vector<int> v;
		int idx = 0;
		rep(i, q){
			int type, x;
			cin >> type >> x;
			int pos;
			switch(type){
			case 0:
				v.push_back(x);
				idmap[x] = idx;
				add(idx++, 1);
				// 生存者がlim人を超えたら一番左を殺す
				if(s[0] > lim){
					add(query(1, 0, 0, size), -1);
				}
				break;
			case 1:
				add(query(x, 0, 0, size), -1);
				break;
			case 2:
				pos = query(x, 0, 0, size);
				cout << v[pos] << endl;
				break;
			case 3:
				add(idmap[x], -1);
				break;
			}
		}
		cout << "end" << endl;
    }

	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:23:9: error: reference to 'size' is ambiguous
   23 |         size = 1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc:24:15: error: reference to 'size' is ambiguous
   24 |         while(size < n){
      |               ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc:25:17: error: reference to 'size' is ambiguous
   25 |                 size *= 2;
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc:27:34: error: reference to 'size' is ambiguous
   27 |         memset(s, 0, sizeof(int)*size*2);
      |                                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc:27:9: error: 'memset' was not declared in this scope
   27 |         memset(s, 0, sizeof(int)*size*2);
      |         ^~~~~~
a.cc:8:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    7 | #include <queue>
  +++ |+#include <cstring>
    8 | 
a.cc: In function 'void add(int, int)':
a.cc:32:14: error: reference to 'size' is ambiguous
   32 |         k += size-1;
      |              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc: In function 'int query(int, int, int, int)':
a.cc:44:27: error: reference to 'size' is ambiguous
   44 |                 return k-(size-1);
      |                           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc: In function 'int main()':
a.cc:71:60: error: reference to 'size' is ambiguous
   71 |                                         add(query(1, 0, 0, size), -1);
      |                                                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc:75:52: error: reference to 'size' is ambiguous
   75 |                                 add(query(x, 0, 0, size), -1);
      |                                                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~
a.cc:78:54: error: reference to 'size' is ambiguous
   78 |                                 pos = query(x, 0, 0, size);
      |                                                      ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:18:5: note:                 'int size'
   18 | int size;
      |     ^~~~