Language: C++

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
using namespace std;

//common
typedef long long i64,ll;

#define BR "\n"
#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,l,r) for(int i=(l);i<(int)(r);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

//debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp,act)
#define STOP(e)
#else
#define DUMP(x)  cerr << #x << " = " << (x)
#define DUMPLN(x)  DUMP(x) <<endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x)<<endl
#define LINE()    cerr<< " (L" << __LINE__ << ")"
#define LINELN()    LINE()<<endl
#define CHECK(exp,act)  if(exp!=act){DUMPLN(exp);DEBUGLN(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}

template<class T> inline string toString(const vector<vector<T> >& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

string BITtoString(int bit){
    stringstream ss;
    while(bit!=0){ss<<(bit%2);bit/=2;}
    string res=ss.str();reverse(ALL(res));
    return res;
}

template<typename T,typename V> inline T pmod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}

class Main{
public:
	
	void run(){
		int N,Q;cin >> N>>Q;
		vector<pair<ll,int>> ss(N);
		REP(i,N){
			cin >> ss[i].first;
			ss[i].second=i;
		}
		sort(ALL(ss));
		vector<int> sv(N);REP(i,N)sv[i]=ss[i].first;
		vector<int> itossi(N);REP(i,N)itossi[ss[i].second]=i;

		map<int,int> lmap;
		REP(q,Q){
			string Comm;cin >> Comm;
			if(Comm=="ADD"){
				int a;cin >> a;a--;
				lmap[itossi[a]]=1;
			}else if(Comm=="REMOVE"){
				int a;cin >> a;a--;
				lmap.erase(itossi[a]);
			}else{
				int x;cin >> x;
				vector<int> ls;EACH(it,lmap)ls.push_back(it->first);
				sort(ALL(ls));
				//count
				ll INF=1LL<<38;
				ll l=-1,r=INF;
				while(r-l>1){
					ll m=(l+r)/2;int pd=-1;
					int team=0;
					REP(i,ls.size()){
						int d=max(lower_bound(ALL(sv),sv[ls[i]]-m)-sv.begin(),pd);
						team+=ls[i]-d+1;
						pd=ls[i]+1;
					}
					int los=N-team;
					if(los<=x)r=m;
					else l=m;
				}
				if(r!=INF)cout << r <<endl;
				else cout <<"NA" <<endl;
			}
		}


	}
};

 int main(){
	//ios::sync_with_stdio(false);
 	Main().run();
 	return 0;
 }

Compiler output:
a.cc: In member function 'void Main::run()':
a.cc:121:58: error: no matching function for call to 'max(__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type, int&)'
  121 |                                                 int d=max(lower_bound(ALL(sv),sv[ls[i]]-m)-sv.begin(),pd);
      |                                                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 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/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:121:58: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
  121 |                                                 int d=max(lower_bound(ALL(sv),sv[ls[i]]-m)-sv.begin(),pd);
      |                                                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:12:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:121:58: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
  121 |                                                 int d=max(lower_bound(ALL(sv),sv[ls[i]]-m)-sv.begin(),pd);
      |                                                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~