Language: C++

#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=a;i<b;++i)
#define allof(a) a.begin(),a.end()

typedef pair< int, int > pii;

int N, Q;
vector< int > s, id;
set< int > read;

int count(int r) {
	if ((int)read.size() == 0) return N;
	
	set< int >::iterator it = read.end();
	int res = 0, cur = N;
	
	do {
		--it;
		int its = *it;	
		if (its < cur) res += cur - its - 1;
		cur = lower_bound(allof(s), s[its] - r) - s.begin();
	} while (it != read.begin());
	
	res += cur;	
	return res;
}

int check(int x) {
	int lb = 0, ub = (int)1e9 + 10;
	
	while (ub - lb > 1) {
		int med = (lb + ub) / 2;		
		if (x < count(med)) lb = med;
		else ub = med;
	}
	
	if (count(lb) <= x) return lb;
	return ub;
}

void query() {
	for_(i,0,Q) {
		string t; int a; cin >> t >> a;
		
		if (t == "ADD") read.insert(id[a-1]);	
		
		if (t == "REMOVE") read.erase(id[a-1]);
		
		if (t == "CHECK") {
			int res = check(a);
			if (res > (int)1e9) cout << "NA" << endl;
			else cout << res << endl;
		} 
	}
}

int main() {
	cin >> N >> Q;
	
	vector< pii > vp;
	for_(i,0,N) {
		int s; cin >> s;
		vp.push_back(pii(s, i));
	}
	
	sort(allof(vp));
	
	s.assign(N, 0);
	id.assign(N, 0);
		
	for_(i,0,N) {
		s[i] = vp[i].first;
		id[vp[i].second] = i;
	}
	
	query();
	
	return 0;
}

Compiler output:
a.cc:11:12: error: 'std::set<int> read' redeclared as different kind of entity
   11 | set< int > read;
      |            ^~~~
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
                 from /usr/include/signal.h:328,
                 from /usr/include/c++/14/csignal:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:116,
                 from a.cc:1:
/usr/include/unistd.h:371:16: note: previous declaration 'ssize_t read(int, void*, size_t)'
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc: In function 'int count(int)':
a.cc:14:23: error: request for member 'size' in 'read', which is of non-class type 'ssize_t(int, void*, size_t)' {aka 'long int(int, void*, long unsigned int)'}
   14 |         if ((int)read.size() == 0) return N;
      |                       ^~~~
a.cc:16:40: error: request for member 'end' in 'read', which is of non-class type 'ssize_t(int, void*, size_t)' {aka 'long int(int, void*, long unsigned int)'}
   16 |         set< int >::iterator it = read.end();
      |                                        ^~~
a.cc:24:29: error: request for member 'begin' in 'read', which is of non-class type 'ssize_t(int, void*, size_t)' {aka 'long int(int, void*, long unsigned int)'}
   24 |         } while (it != read.begin());
      |                             ^~~~~
a.cc: In function 'void query()':
a.cc:47:38: error: request for member 'insert' in 'read', which is of non-class type 'ssize_t(int, void*, size_t)' {aka 'long int(int, void*, long unsigned int)'}
   47 |                 if (t == "ADD") read.insert(id[a-1]);
      |                                      ^~~~~~
a.cc:49:41: error: request for member 'erase' in 'read', which is of non-class type 'ssize_t(int, void*, size_t)' {aka 'long int(int, void*, long unsigned int)'}
   49 |                 if (t == "REMOVE") read.erase(id[a-1]);
      |                                         ^~~~~