Language: C++

# define _CRT_SECUREj_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12
template<class T> void debug(T a) { for (auto iiiiiiiiii : a)cout << iiiiiiiiii << endl; }

#define int long long
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
const int MAX_N = 1000000;
const int MAX_S = 1000000000;
int index[MAX_N];
vi a;
list<int> b;
int ok[MAX_N + 1];
signed main()
{
	int n, q;
	cin >> n >> q;
	FOR(i, n)
	{
		cin >> index[i];
		a.push_back(index[i]);
	}
	sort(ALL(a));
	FOR(c, q)
	{
		string Q; int x;
		cin >> Q >> x;
		
		if (Q == "ADD")
		{
			x--;
			b.insert(b.begin(),index[x]);
			auto it = lower_bound(ALL(a), index[x]);
			a.erase(it);
		}
		else if (Q == "REMOVE")
		{
			x--;
			for (auto it = b.begin(); it != b.end(); it++)
			{
				if (*it == index[x])
				{
					b.erase(it);
					break;
				}
			}
			a.push_back(index[x]);
			sort(ALL(a));
		}
		else
		{
			memset(ok, 0, sizeof ok);
			for (auto it = b.begin(); it != b.end(); it++)
			{
				auto r = lower_bound(ALL(a), *it);
				auto l = lower_bound(ALL(a), *it - MAX_S);
				if (r == l)continue;
				int L = l - a.begin(),R=r-a.begin();
				ok[L]++; ok[R]--;
			}
			int count = 0;
			for (int i = 0; i < a.size(); i++)
			{
				ok[i + 1] += ok[i];
				if (ok[i] == 0)
				{
					count++;
				}
			}
			if (count > x) { cout << "NA" << endl; }
			else
			{
				int left = 0, right = MAX_S;
				int middle;
				while (right!=left)
				{
					middle = (left + right) / 2;
					memset(ok, 0, sizeof ok);
					for (auto it = b.begin(); it != b.end(); it++)
					{
						auto r = lower_bound(ALL(a), *it);
						auto l = lower_bound(ALL(a), *it - middle);
						if (r == l)continue;
						int L = l - a.begin(), R = r - a.begin();
						ok[L]++; ok[R]--;
					}
					count = 0;
					for (int i = 0; i < a.size(); i++)
					{
						//cout << "ok[" << i << "]:" << ok[i] << endl;;
						ok[i + 1] += ok[i];
						if (ok[i] == 0)
						{
							count++;
						}
					}
					//cout << middle << " "; DEBUG(count);
					if (count > x)
					{
						left = middle+1;
					}
					else if (count <= x)
					{
						right = middle;
					}
				}
				cout << left << endl;
			}
		}
	}
}

Compiler output:
a.cc:46:16: error: 'long long int index [1000000]' redeclared as different kind of entity
   46 | int index[MAX_N];
      |                ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from a.cc:11:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'int main()':
a.cc:56:29: error: invalid types '<unresolved overloaded function type>[long long int]' for array subscript
   56 |                 cin >> index[i];
      |                             ^
a.cc:57:34: error: invalid types '<unresolved overloaded function type>[long long int]' for array subscript
   57 |                 a.push_back(index[i]);
      |                                  ^
a.cc:68:49: error: invalid types '<unresolved overloaded function type>[long long int]' for array subscript
   68 |                         b.insert(b.begin(),index[x]);
      |                                                 ^
a.cc:69:60: error: invalid types '<unresolved overloaded function type>[long long int]' for array subscript
   69 |                         auto it = lower_bound(ALL(a), index[x]);
      |                                                            ^
a.cc:77:49: error: invalid types '<unresolved overloaded function type>[long long int]' for array subscript
   77 |                                 if (*it == index[x])
      |                                                 ^
a.cc:83:42: error: invalid types '<unresolved overloaded function type>[long long int]' for array subscript
   83 |                         a.push_back(index[x]);
      |                                          ^