Problem:
勉強会
プログラマー養成校のアカベ高校には、生徒自身で運営するユニークな勉強会があります。プログラマーは新しい技術を常に取り入れることが大切なので、この勉強会を通して自学自習の習慣を身につけることがこの活動のねらいです。
生徒は全部で
N
人おり、それぞれが入学時のプログラミングコンテストの結果で得られたスコアを持っています。勉強会では
N
人の生徒のうち何人かがリーダーになり、各リーダーがそれぞれのグループを運営するとともに、自らの運営するグループに参加します。
リーダー以外の生徒は、自分のスコアよりも低いスコアのリーダーが運営するグループには参加できません。また、0以上のある値
r
を１つ決め、グループに参加する生徒とリーダーのスコアの差が
r
以内となるようにしています。つまり、グループのリーダーのスコアが
s
のとき、自分のスコアが
s
を超えているか、あるいは
s
-
r
未満ならば、そのグループには参加できません。
あなたは勉強会の実行委員長であり、運営準備のためにシミュレーションを行うことにしました。シミュレーションでは、リーダーが誰もいない状態から始め、以下の操作を何回か繰り返します。
生徒をリーダーに加える。
生徒をリーダーから外す。
要求時点でのリーダーの組み合わせについて、どのグループにも参加できない生徒が
x
人以下になるような、最小の
r
を求める。
このようなシミュレーションを行うプログラムを作成してください。
入力
入力は1つのデータセットからなる。入力は以下の形式で与えられる。
N
Q
s
1
s
2
:
s
N
QUERY
1
QUERY
2
:
QUERY
Q
1行目に生徒の数
N
(1 ≤
N
≤ 1000000)、処理要求の数
Q
(0 ≤
Q
≤ 1000)が与えられる。
続く
N
行に
i
番目の生徒のスコアを示す整数
s
i
(0 ≤
s
i
≤ 1,000,000,000)が与えられる。生徒は1,2,...,
N
で番号付けされているものとする。
続く
Q
行に処理要求
QUERY
i
が与えられる。処理要求は時系列順に与えられる。処理要求はADD, REMOVE, CHECKの3種類あり、各
QUERY
i
は以下のいずれかの形式で与えられる。
ADD
a
または
REMOVE
a
または
CHECK
x
ADD
a
は番号
a
(1 ≤
a
≤
N
)の生徒をリーダーに加えることを表す。
REMOVE
a
は番号
a
(1 ≤
a
≤
N
)の生徒をリーダーから外すことを表す。
CHECK
x
は出力要求を表す。どのグループにも参加できない生徒の数の上限
x
(0 ≤
x
≤
N
)が与えられる。
なお、入力は以下の条件を満たすものとする。
どの時点でも、リーダーの人数が100人を超えることはない。
その時点でリーダーである生徒をリーダーに加えることはない。
その時点でリーダーでない生徒をリーダーから外すことはない。
出力
時系列順に各出力要求の時点で、どのグループにも参加できない生徒がx人以下になるような最小の
r
を1行に出力する。ただし、どのような
r
を選んでも
x
人以下にすることが不可能であればNAと出力する。
入力例 1
5 8
5
10
8
7
3
ADD 1
ADD 3
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
出力例 1
NA
2
1
0
0
0
入力例 2
5 28
5
10
8
7
3
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
ADD 1
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
REMOVE 1
ADD 3
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
ADD 1
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
出力例 2
NA
NA
NA
NA
NA
0
NA
NA
NA
2
0
0
NA
5
3
1
0
0
NA
2
1
0
0
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,Q;
    cin>>N>>Q;
    vector<int> S(N);
    rep(i,N)cin>>S[i];

    vector<int> A=S;
    sort(all(A));

    vector<int> L;
    while(Q--){
        string O;
        int x;
        cin>>O>>x;
        if(O[0]=='A'){
            x--;
            L.push_back(S[x]);
        }
        else if(O[0]=='R'){
            x--;
            L.erase(find(all(L), S[x]));
        }
        else{
            sort(all(L));
            int ok=(int)1e9+1;
            int ng=-1;
            while(ok-ng>1){
                int m=(ok+ng)/2;

                int mae=-1;
                int cnt=0;
                for(int e: L){
                    if(e==mae) continue;
                    auto rt=lower_bound(all(A), e-m);
                    auto lt=upper_bound(all(A), mae);
                    cnt+=max(0l, rt-lt);
                    mae=e;
                }
                cnt+=A.end()-upper_bound(all(A),mae);
                if(cnt<=x) ok=m;
                else ng=m;
            }
            if(ok>1e9)cout<<"NA\n";
            else cout<<ok<<"\n";
        }
    }
}
accuracy: 1
time_ms: 320
memory_kb: 11184
final_rank: 2

code_snippet: //#pragma GCC target ("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>
using namespace std;
using ll = long long;
using ld = long double;
using H = pair<ll, ll>;
using P = pair<ll, H>;
using vi = vector<ll>;
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define Q(i,j,k) mkp((i),mkp((j),(k)))
#define rng(i,s,n) for(ll i = (s) ; i < (n) ; i++)
#define rep(i,n) rng(i, 0, (n))
#define mkp make_pair
#define vec vector
#define pb emplace_back
#define siz(a) int((a).size())
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) (lower_bound(all((b)),(i))-(b).begin())
#define ssp(i,n) (i==(ll)(n)-1?"\n":" ")
#define ctoi(c) (int)((c)-'0')
#define itoc(c) (char)((c)+'0')
#define cyes printf("Yes\n")
#define cno printf("No\n")
#define cdf(n) for(int quetimes_=(n);quetimes_>0;quetimes_--)
#define gcj printf("Case #%lld: ",quetimes_+1)
#define readv(a,n) (a).resize((n),0);rep(i,(n)) (a)[i]=read()
#define found(a,x) (a.find(x)!=a.end())
constexpr ll mod = (ll)1e9 + 7;
constexpr ll Mod = 998244353;
constexpr ld EPS = 1e-10;
constexpr ll inf = (ll)3 * 1e18;
constexpr int Inf = (ll)15 * 1e8;
constexpr int dx[] = { -1,1,0,0 }, dy[] = { 0,0,-1,1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(short g = 0) { H u; int k = scanf("%lld %lld", &u.fs, &u.sc); if (g == 1) u.fs--, u.sc--; if (g == 2) u.fs--; return u; }
bool ina(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
bool ina(int t, int l, int r) { return l <= t && t < r; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll ppc(ll x) {
    int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
    return sum;
}
template<typename T>
void fin(T x) { cout << x << endl; exit(0); }

template<typename T>
class csum {
    vec<T> v;
public:
    csum(vec<T>& a) :v(a) { build(); }
    csum() {}
    csum(int sz) { init(sz); }
    void init(int sz) { v = vector<T>(sz + 1, 0); }
    void init(vec<T>& a) { v = a; build(); }
    void build() {
        for (int i = 1; i < v.size(); i++) v[i] += v[i - 1];
    }
    void add(int l, int r, T x) {
        v[l] += x;
        v[r] -= x;
    }//[l,r)
    void add(int t, T x) {
        v[t] += x;
    }//[l,r)
    //[l,r]
    T a(int l, int r) {
        if (r < l) return 0;
        return v[r] - (l == 0 ? 0 : v[l - 1]);
    }
    //[l,r)
    T b(int l, int r) {
        return a(l, r - 1);
    }
    T a(pair<int, int>t) {
        return a(t.first, t.second);
    }
    T b(pair<int, int>t) {
        return b(t.first, t.second);
    }
    T operator[](int x)const {
        return v[x];
    }
};
class via {
    using H = pair<ll, ll>;
    bool comp;
public:
    vector<H>a;
    via() :comp(true) {}
    via(vector<H>dat) :a(dat), comp(false) { build(); }
    void push(int l, int r) { a.push_back(H{ l,r }); comp = false; }
    void push(H a) { push(a.first, a.second); }
    void build() {
        if (comp) return;
        comp = true;
        sort(a.begin(), a.end());
        int fst = -1; ll lst = -1;
        vector<H>b;
        for (int i = 0; i < a.size(); i++) {
            if (fst < 0) fst = i, lst = a[i].second;
            if (lst < a[i].first) {
                b.push_back(H{ a[fst].first,lst });
                fst = i, lst = a[i].second;
            }
            else if (lst < a[i].second) lst = a[i].second;
        }
        if (~fst) b.push_back(H{ a[fst].first, lst });
        a = b;
    }
    ll size() { return ll(a.size()); }
    ll sum() {
        build();
        ll sum = 0;
        for (auto g : a) sum += g.second - g.first;
        return sum;
    }
    via inter(via& v) {
        build(); v.build();
        via ret;
        for (auto g : a) ret.push(g);
        for (auto g : v.a) ret.push(g);
        ret.build(); return ret;
    }
    via comb(via& v) {
        build(); v.build();
        int s = 0, t = 0;
        via ret;
        while (s < a.size() && t < v.size()) {
            if (v[t].second <= a[s].first)  t++;
            else if (a[s].second <= v[t].first) s++;
            else {
                ret.push(max(a[s].first, v[t].first), min(a[s].second, v[t].second));
                if (a[s].second <= v[t].second) s++;
                else t++;
            }
        }
        return ret;
    }
    H& operator[](int t) { return a[t]; }
};
template<ll mod>
class modint {
public:ll v;
      modint(ll v = 0) { s(v % mod + mod); }
      constexpr static int fn_ = (ll)2e6 + 5;
      static vector<modint>fact, comp;
      modint pow(ll x) const {
          modint b(v), c(1);
          while (x) {
              if (x & 1) c *= b;
              b *= b;
              x >>= 1;
          }
          return c;
      }
      inline modint& s(int vv) {
          v = vv < mod ? vv : vv - mod;
          return *this;
      }
      inline modint inv()const { return pow(mod - 2); }
      inline modint operator-()const { return modint() - *this; }
      inline modint& operator+=(const modint b) { return s(v + b.v); }
      inline modint& operator-=(const modint b) { return s(v + mod - b.v); }
      inline modint& operator*=(const modint b) { v = v * b.v % mod; return *this; }
      inline modint& operator/=(const modint b) { v = v * b.inv().v % mod; return *this; }
      inline modint operator+(const modint& b) const { return modint(v) += b; }
      inline modint operator-(const modint& b) const { return modint(v) -= b; }
      inline modint operator*(const modint& b) const { return modint(v) *= b; }
      inline modint operator/(const modint& b) const { return modint(v) /= b; }
      friend ostream& operator<<(ostream& os, const modint& m) {
          return os << m.v;
      }
      friend istream& operator>>(istream& is, modint& m) {
          int x; is >> x; m = modint(x);
          return is;
      }
      bool operator<(const modint& r)const { return v < r.v; }
      bool operator>(const modint& r)const { return v > r.v; }
      bool operator<=(const modint& r)const { return v <= r.v; }
      bool operator>=(const modint& r)const { return v >= r.v; }
      bool operator==(const modint& r)const { return v == r.v; }
      bool operator!=(const modint& r)const { return v != r.v; }
      explicit operator bool()const { return v; }
      explicit operator int()const { return v; }
      modint comb(modint k) {
          if (k > *this) return modint();
          if (fact.empty()) combinit();
          if (v >= fn_) {
              if (k > *this - k) k = *this - k;
              modint tmp(1);
              for (int i = v; i >= v - k.v + 1; i--) tmp *= modint(i);
              return tmp * comp[k.v];
          }
          return fact[v] * comp[k.v] * comp[v - k.v];
      }//nCk
      modint perm(modint k) {
          if (k > *this) return modint();
          if (fact.empty()) combinit();
          if (v >= fn_) {
              modint tmp(1);
              for (int i = v; i >= v - k.v + 1; i--) tmp *= modint(i);
              return tmp;
          }
          return fact[v] * comp[v - k.v];
      }//nPk
      static void combinit() {
          fact.assign(fn_, modint());
          fact[0] = 1;
          for (int i = 1; i < fn_; i++) fact[i] = fact[i - 1] * modint(i);
          comp.assign(fn_, modint());
          comp[fn_ - 1] = fact[fn_ - 1].inv();
          for (int i = fn_ - 2; i >= 0; i--) comp[i] = comp[i + 1] * modint(i + 1);
      }
};
using mint = modint<ll(1e9 + 7)>; template<>vec<mint> mint::fact = vec<mint>(); template<>vec<mint> mint::comp = vec<mint>();
//--------------------------------------------------------------
//--------------------------------------------------------------
signed main() {
    int n, q; cin >> n >> q;
    vi s; readv(s, n);
    vi a = s; sort(all(a));
    multiset<ll>lead;
    rep(z, q) {
        string st; int t; cin >> st >> t;
        if (st == "ADD") {
            t--;
            lead.insert(s[t]);
        }
        else if (st == "REMOVE") {
            t--;
            lead.erase(lead.find(s[t]));
        }
        else {
            ll ok = 1e9 + 1, ng = -1, mid;
            while (ok - ng > 1) {
                mid = (ok + ng) / 2;
                int cur = 0, score = 0;
                for (auto g : lead) {
                    int t = lower_bound(all(a), g - mid) - a.begin();
                    int r = upper_bound(all(a), g) - a.begin();
                    score += max(0, r - max(t, cur));
                    cur = r;
                }
                if (t + score >= n) ok = mid;
                else ng = mid;
            }
            if (ok == 1e9 + 1) printf("NA\n");
            else printf("%d\n", ok);
        }
    }
}
accuracy: 1
time_ms: 440
memory_kb: 18788
final_rank: 7

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	bool operator<(const struct Info &arg) const{
		return score < arg.score;
	};
	int score,id;
};


int main(){

	int N,Q;
	scanf("%d %d",&N,&Q);

	Info* info = (Info*)malloc(sizeof(Info)*(N+1));

	info[0].score = -1;
	vector<int> SCORE;
	SCORE.push_back(-1);
	for(int i = 1; i <= N; i++){
		scanf("%d",&info[i].score);
		SCORE.push_back(info[i].score);
	}

	sort(SCORE.begin(),SCORE.end());

	int* score_ruisekiwa = new int[N+1];
	int* score = new int[N+1];

	int score_index = 1;

	score[0] = -1;
	score_ruisekiwa[0] = 0;

	for(int i = 1; i <= N; ){

		score[score_index] = SCORE[i];
		score_ruisekiwa[score_index] = 0;

		while(score[score_index] == SCORE[i] && i <= N){
			score_ruisekiwa[score_index]++;
			i++;
		}
 		score_ruisekiwa[score_index] += score_ruisekiwa[score_index-1];
 		score_index++;
	}

	int* loc = new int[N+1];

	int left,right,m;

	for(int i = 1; i <= N; i++){

		left = 1,right = score_index-1,m = (left+right)/2;

		while(left <= right){
			if(score[m] == info[i].score){
				loc[i] = m;
				break;
			}else if(score[m] < info[i].score){
				left = m+1;
			}else{
				right = m-1;
			}
			m = (left+right)/2;
		}
	}

	char order[10];
	int value,current_number,need_number,max_loc;

	bool* check = new bool[score_index];

	vector<int> V;
	vector<Info> work;
	bool FLG;

	int calc_left,calc_right,calc_m;

	for(int loop = 0; loop < Q; loop++){
		scanf("%s %d",order,&value);

		switch(order[0]){
		case 'A':
			V.push_back(value);
			break;
		case 'R':
			for(int i = 0; i < V.size(); i++){
				if(V[i] == value){
					V.erase(V.begin()+i);
					break;
				}
			}
			break;
		case 'C':

			work.clear();

			need_number = N - value;
			max_loc = 0;

			for(int i = 0 ; i <= score_index-1; i++)check[i] = false;

			for(int i = 0; i < V.size(); i++){
				max_loc = max(max_loc,loc[V[i]]);
				FLG = true;
				for(int k = i+1; k < V.size(); k++){
					if(loc[V[i]] == loc[V[k]]){
						FLG = false;
					}
				}
				if(FLG){
					Info new_info;
					new_info.id = V[i];
					new_info.score = score[loc[V[i]]];
					work.push_back(new_info);
					check[loc[V[i]]] = true;
				}
			}

			if(score_ruisekiwa[max_loc] < need_number){
				printf("NA\n");
				continue;
			}

			current_number = 0;
			for(int i = 0; i < work.size(); i++){
				current_number += score_ruisekiwa[loc[work[i].id]] - score_ruisekiwa[loc[work[i].id]-1];
			}

			if(current_number >= need_number){
				printf("0\n");
				continue;
			}

			int ans = 0;
			int left_loc,right_loc;

			sort(work.begin(),work.end());

			left = 0,right = score[max_loc],m = (left+right)/2;

			while(left <= right){

				int tmp = 0;

				for(int i = 0; i < work.size(); i++){

					int left_score = 0;
					if(i > 0){
						left_score = work[i-1].score;

						if(work[i].score-m <= left_score){

							left_loc = loc[work[i-1].id]+1;
							right_loc = loc[work[i].id]-1;

							if(left_loc <= right_loc){
								tmp += score_ruisekiwa[right_loc]-score_ruisekiwa[left_loc-1];
							}
						}else{

							if(work[i].score-m > score[loc[work[i].id]-1]){
								continue;
							}
							right_loc = loc[work[i].id]-1;

							calc_left = loc[work[i-1].id]+1;
							calc_right = right_loc;
							calc_m = (calc_left+calc_right)/2;

							while(calc_left <= calc_right){

								if(score[calc_m] >= work[i].score-m){
									left_loc = calc_m;
									calc_right = calc_m-1;
								}else{
									calc_left = calc_m+1;
								}
								calc_m = (calc_left+calc_right)/2;
							}
							tmp += score_ruisekiwa[right_loc]-score_ruisekiwa[left_loc-1];
						}

					}else{ // i == 0

						if(loc[work[i].id] == 1 || work[i].score-m > score[loc[work[i].id]-1]){
							continue;
						}

						right_loc = loc[work[i].id]-1;

						calc_left = 1;
						calc_right = right_loc;
						calc_m = (calc_left+calc_right)/2;

						while(calc_left <= calc_right){

							if(score[calc_m] >= work[i].score-m){
								left_loc = calc_m;
								calc_right = calc_m-1;
							}else{
								calc_left = calc_m+1;
							}
							calc_m = (calc_left+calc_right)/2;
						}
						tmp += score_ruisekiwa[right_loc]-score_ruisekiwa[left_loc-1];
					}
				}

				if(current_number+tmp >= need_number){
					ans = m;
					right = m-1;
				}else{
					left = m+1;
				}
				m = (left+right)/2;
			}

			printf("%d\n",ans);

			break;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 27712
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(ll i=0;i<N;i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second

bool solve(){
    ll N,Q;cin>>N>>Q;
    vector<ll>S(N);
    rep(i,N)cin>>S[i];
    vector<pair<int,int>>TMP(N);
    vector<int>id(N);
    rep(i,N)TMP[i]={S[i],i};
    sort(all(TMP));
    rep(i,N){
        S[i]=TMP[i].fi;
        id[TMP[i].se]=i;
    }
    vector<int>leader;
    rep(qq,Q){
        string type;cin>>type;
        if(type == "ADD"){
            int a;cin>>a;a--;
            leader.push_back(id[a]);
            sort(all(leader));
        }
        else if(type == "REMOVE"){
            vector<int> tmp;
            int a;cin>>a;a--;a=id[a];
            for(auto e : leader){
                if(e!=a)tmp.push_back(e);
            }
            swap(tmp,leader);
        }
        else {
            int x;cin>>x;
            x = N-x;
            ll ng = -1,ok = 2000000001;
            while(ng+1<ok){
                ll R = (ng+ok)/2;
                int ct = 0;
                int lim = 0;
                rep(i,leader.size()){
                    ll k = S[leader[i]];
                    int l = lower_bound(all(S),k-R)-S.begin();
                    int r = upper_bound(all(S),k)-S.begin();
                    if(r>=S.size())r=S.size()-1;
                    if(S[r]>k)r--;
                    l = max(l,lim);
                    ct += max(0,r-l+1);
                    lim = max(lim,r+1);
                }
                if(ct>=x)ok = R;
                else ng = R;
            }
            if(ok==2000000001)cout<<"NA"<<endl;
            else cout<<ok<<endl;
        }
    }
    return false;
}

int main(){
    while(solve());
}
accuracy: 1
time_ms: 670
memory_kb: 22644
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(ll i=0;i<N;i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second

bool solve(){
    ll N,Q;cin>>N>>Q;
    vector<ll>S(N);
    rep(i,N)cin>>S[i];
    vector<pair<int,int>>TMP(N);
    vector<int>id(N);
    rep(i,N)TMP[i]={S[i],i};
    sort(all(TMP));
    rep(i,N){
        S[i]=TMP[i].fi;
        id[TMP[i].se]=i;
    }
    vector<int>leader;
    rep(qq,Q){
        string type;cin>>type;
        if(type == "ADD"){
            int a;cin>>a;a--;
            leader.push_back(id[a]);
            sort(all(leader));
        }
        else if(type == "REMOVE"){
            vector<int> tmp;
            int a;cin>>a;a--;a=id[a];
            for(auto e : leader){
                if(e!=a)tmp.push_back(a);
            }
            swap(tmp,leader);
        }
        else {
            int x;cin>>x;
            x = N-x;
            ll ng = -1,ok = 2000000001;
            while(ng+1<ok){
                ll R = (ng+ok)/2;
                int ct = 0;
                int lim = 0;
                rep(i,leader.size()){
                    ll k = S[leader[i]];
                    int l = lower_bound(all(S),k-R)-S.begin();
                    int r = upper_bound(all(S),k)-S.begin();
                    if(r>=S.size()||S[r]>k)r--;
                    l = max(l,lim);
                    ct += max(0,r-l+1);
                    lim = max(lim,r+1);
                    
                }
                if(ct>=x)ok = R;
                else ng = R;
            }
            if(ok==2000000001)cout<<"NA"<<endl;
            else cout<<ok<<endl;
        }
    }
    return false;
}

int main(){
    while(solve());
}
accuracy: 0.375
time_ms: 10
memory_kb: 3368
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int N, Q;
  cin >> N >> Q;
  vector<int> s(N);
  for (int i = 0; i < N; i++){
    cin >> s[i];
  }
  vector<int> s2 = s;
  sort(s2.begin(), s2.end());
  set<int> st;
  for (int i = 0; i < Q; i++){
    string S;
    cin >> S;
    if (S == "ADD"){
      int a;
      cin >> a;
      a--;
      st.insert(a);
    }
    if (S == "REMOVE"){
      int a;
      cin >> a;
      a--;
      st.erase(a);
    }
    if (S == "CHECK"){
      int x;
      cin >> x;
      int cnt = st.size();
      int tv = 1000000001, fv = -1;
      while (tv - fv > 1){
        int mid = (tv + fv) / 2;
        vector<int> L, R;
        for (int a : st){
          L.push_back(lower_bound(s2.begin(), s2.end(), s[a] - mid) - s2.begin());
          R.push_back(upper_bound(s2.begin(), s2.end(), s[a]) - s2.begin());
        }
        vector<int> X;
        X.push_back(0);
        X.push_back(N);
        for (int j = 0; j < cnt; j++){
          X.push_back(L[j]);
          X.push_back(R[j]);
        }
        sort(X.begin(), X.end());
        X.erase(unique(X.begin(), X.end()), X.end());
        int cnt2 = X.size();
        vector<int> imos(cnt2, 0);
        for (int j = 0; j < cnt; j++){
          int l = lower_bound(X.begin(), X.end(), L[j]) - X.begin();
          int r = lower_bound(X.begin(), X.end(), R[j]) - X.begin();
          imos[l]++;
          imos[r]--;
        }
        for (int j = 0; j < cnt2 - 1; j++){
          imos[j + 1] += imos[j];
        }
        int sum = 0;
        for (int j = 0; j < cnt2 - 1; j++){
          if (imos[j] == 0){
            sum += X[j + 1] - X[j];
          }
        }
        if (sum <= x){
          tv = mid;
        } else {
          fv = mid;
        }
      }
      if (tv == 1000000001){
        cout << "NA" << endl;
      } else {
        cout << tv << endl;
      }
    }
  }
}
accuracy: 1
time_ms: 970
memory_kb: 10776
final_rank: 10

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main(){
  int n, q;
  cin >> n >> q;
  vector<int> v(n);
  vector<int> s;
  map<int, int> score;
  for(int i = 0;i < n;i++){
    cin >> v[i];
    s.push_back(v[i]);
    score[i] = v[i];
  }

  sort(s.begin(), s.end());
  sort(v.begin(), v.end());
  multiset<int> leader;
  for(int i = 0;i < q;i++){
    string query;
    int x;
    cin >> query >> x;
    if(query == "ADD"){
      leader.insert(score[x - 1]);
    }
    else if(query == "REMOVE"){
      leader.erase(leader.lower_bound(score[x - 1]));
    }
    else{
      int ls = leader.size();
      if(x + ls >= n){
        cout << "0" << endl;
        continue;
      }
      if(ls == 0){
        cout << "NA" << endl;
        continue;
      }
      int l = -1;
      int r = 1001001001;
      while(r - l > 1){
        
		int mid = (l + r) / 2;
        auto si = leader.end();
        int ans = 0;
        for(int j = 0;j < ls;j++){
          si--;
         
		  //cout << *si << endl;
          auto itl = lower_bound(s.begin(), s.end(), (*si) - mid);
          if(j == 0){
            auto dekai = upper_bound(s.begin(), s.end(), *si);
            ans += distance(dekai, s.end());
          }

          if(j != ls - 1){
            auto si_1 = prev(si);
            auto itr = upper_bound(s.begin(), s.end(), *si_1);
            if(itl > itr)ans += distance(itr, itl);
          }
          else{
            if(itl > s.begin()) ans += distance(s.begin(), itl);
          }
        }
        //cout << mid << " " << ans << endl;
        if(ans > x) l = mid;
        else r = mid;
      }
      if(r != 1001001001) cout << r << endl;
      else cout << "NA" << endl;
    }
  }
}
accuracy: 1
time_ms: 950
memory_kb: 57888
final_rank: 16

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

int main(){
  int N,Q;
  cin >> N >> Q;
  vector<long long int> s(N);
  vector<long long int> ss(N);
  for(int i=0;i<N;i++){
    cin >> s[i];
    ss[i] = s[i];
  }
  sort(ss.begin(),ss.end());
  vector<int> leader(0);
  map<int,bool> real_leader;
  map<int,bool> add;
  for(int j=0;j<Q;j++){
    string q;
    cin >> q;
    if(q == "ADD"){
      int a;
      cin >> a;
      a--;
      if(!add[a]){
        add[a] = true;
        leader.push_back(a);
      }
      real_leader[a] = true;
    }
    if(q == "REMOVE"){
      int a;
      cin >> a;
      a--;
      real_leader[a] = false;
    }
    if(q == "CHECK"){
      int x;
      cin >> x;
      vector<int> list(0);
      for(int i=0;i<(int)leader.size();i++){
        int t = leader[i];
        if(real_leader[t]) list.push_back(t);
      }
      if(list.size() == 0){
        if(x != N)cout << "NA" << endl;
        else cout << 0 << endl;
      }
      else if(list.size() == N) cout << 0 << endl;
      else{
        long long int INF = 1000000000000;
        long long int ng=-1,ok=INF;
        int M = list.size();
        while(ok-ng > 1){
          long long int mid = (ok+ng)/2;
          vector<pair<long long int,long long int> > LR(M);
          for(int i=0;i<M;i++){
            long long int L = s[list[i]]-mid;
            long long int R = s[list[i]];
            LR[i] = make_pair(L,R);
          }
          sort(LR.begin(),LR.end());
          vector<pair<long long int,long long int> > sumLR(0);
          for(int i=1;i<M;i++){
            if(LR[i-1].second >= LR[i].first) LR[i].first = LR[i-1].first;
            else sumLR.push_back(make_pair(LR[i-1].first,LR[i-1].second));
          }
          sumLR.push_back(make_pair(LR.back().first,LR.back().second));
          long long int stu = N;
          for(int i=0;i<(int)sumLR.size();i++){
            long long int s = lower_bound(ss.begin(),ss.end(),sumLR[i].first)-ss.begin();
            long long int t = lower_bound(ss.begin(),ss.end(),sumLR[i].second+1)-ss.begin();
            stu -= t-s;
          }
          if(stu <= x) ok = mid;
          else ng = mid;
        }
        if(ok >= INF/10) cout << "NA" << endl;
        else cout << ok << endl;
      }
    }
  }
}
accuracy: 1
time_ms: 540
memory_kb: 18440
final_rank: 8

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<climits>
#include<iomanip>
#include<cctype>
#include<sstream>
#include<regex>
#include<bitset>
#include<random>
#include<complex>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr int mod=1e9+7;
constexpr int Mod=998244353;
constexpr int inf=mod;
constexpr ll linf=(ll)mod*mod;
struct fastio{
	fastio(){
		cin.tie(0);
		ios::sync_with_stdio(0);
		cout<<fixed<<setprecision(20);
	};
}fio;
int s[1000000],ss[1000000];
int main(){
	int n,q;
	string query;
	cin>>n>>q;
	for(int i=0;i<n;i++)
		cin>>s[i];
	copy(s,s+n,ss);
	sort(ss,ss+n);
	vector<int>v;
	while(cin>>query){
		if(query=="ADD"){
			int a;
			cin>>a;
			v.push_back(--a);
		}
		if(query=="REMOVE"){
			int a;
			cin>>a;
			v.erase(find(v.begin(),v.end(),--a));
		}
		if(query=="CHECK"){
			int x;
			cin>>x;
			int ok=inf,ng=-1;
			while(ok-ng>1){
				int mid=(ok+ng)/2;
				vector<pair<int,int>>eve;
				for(auto e:v){
					eve.push_back({s[e]-mid,1});
					eve.push_back({s[e]+1,-1});
				}
				sort(eve.begin(),eve.end());
				int tmp=0;
				vector<int>rg[2];
				for(auto e:eve){
					tmp+=e.second;
					if(tmp==1&&e.second==1)
						rg[0].push_back(e.first);
					if(tmp==0)
						rg[1].push_back(e.first);
				}
				int sum=0;
				for(int i=0;i<rg[0].size();i++)
					sum+=lower_bound(ss,ss+n,rg[1][i])-lower_bound(ss,ss+n,rg[0][i]);
				if(n-sum<=x)
					ok=mid;
				else
					ng=mid;
			}
			if(ok!=inf)
				cout<<ok<<endl;
			else
				cout<<"NA"<<endl;
		}
	}
}
accuracy: 1
time_ms: 350
memory_kb: 11212
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int BIT[1000015];
void add(int p,int v){
  for(;p<1000015;p+=p&-p)BIT[p]+=v;
}
int getsum(int l,int r){
  int ret=0;
  for(l-=1;l>0;l-=l&-l)ret-=BIT[l];
  for(;r>0;r-=r&-r)ret+=BIT[r];
  return ret;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  fill(BIT,BIT+1000015,0);
  int N,Q;
  cin>>N>>Q;
  vector<int>A(N);
  for(int &i:A)cin>>i;
  vector<int>B=A;
  B.push_back(-1);
  sort(B.begin(),B.end()),B.erase(unique(B.begin(),B.end()),B.end());
  for(int i=0;i<N;i++){
    add(lower_bound(B.begin(),B.end(),A[i])-B.begin(),1);
  }
  //for(int i:B)cout<<i<<' ';cout<<'\n';
  multiset<int>rida;
  string op;
  while(Q--){
    cin>>op;
    if(op=="ADD"){
      int x;
      cin>>x;
      rida.insert(A[x-1]);
      add(lower_bound(B.begin(),B.end(),A[x-1])-B.begin(),-1);
      //for(int i=0;i<=N;i++)cout<<getsum(0,i)<<' ';cout<<'\n';
    }else if(op=="REMOVE"){
      int x;cin>>x;
      rida.erase(rida.lower_bound(A[x-1]));
      add(lower_bound(B.begin(),B.end(),A[x-1])-B.begin(),1);
      //for(int i=0;i<=N;i++)cout<<getsum(0,i)<<' ';cout<<'\n';
    }else{
      int x;cin>>x;
      long long l=-1,r=2e9;
      while(r-l>1){
        long long mid=(l+r)/2;
        int t=-1,ok=N-rida.size();
        for(int i:rida){
          if(t>=i-mid){
            ok-=getsum(upper_bound(B.begin(),B.end(),t)-B.begin(),lower_bound(B.begin(),B.end(),i)-B.begin());
          }else{
            ok-=getsum(lower_bound(B.begin(),B.end(),i-mid)-B.begin(),lower_bound(B.begin(),B.end(),i)-B.begin());
          }
          t=i;
        }
        if(ok<=x)r=mid;
        else l=mid;
        //cout<<ok<<' ';
      }
      if(r>1e9){
        cout<<"NA\n";
      }else{
        cout<<r<<'\n';
      }
    }
  }
}
accuracy: 1
time_ms: 590
memory_kb: 20600
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int BIT[1000015];
void add(int p,int v){
  for(;p<1000015;p+=p&-p)BIT[p]+=v;
}
int getsum(int l,int r){
  int ret=0;
  for(l-=1;l>0;l-=l&-l)ret-=BIT[l];
  for(;r>0;r-=r&-r)ret+=BIT[r];
  return ret;
}

int main(){
  fill(BIT,BIT+1000015,0);
  int N,Q;
  cin>>N>>Q;
  vector<int>A(N);
  for(int &i:A)cin>>i;
  vector<int>B=A;
  B.push_back(-1);
  sort(B.begin(),B.end()),B.erase(unique(B.begin(),B.end()),B.end());
  for(int i=0;i<N;i++){
    add(lower_bound(B.begin(),B.end(),A[i])-B.begin(),1);
  }
  //for(int i:B)cout<<i<<' ';cout<<'\n';
  multiset<int>rida;
  string op;
  while(Q--){
    cin>>op;
    if(op=="ADD"){
      int x;
      cin>>x;
      rida.insert(A[x-1]);
      add(lower_bound(B.begin(),B.end(),A[x-1])-B.begin(),-1);
      //for(int i=0;i<=N;i++)cout<<getsum(0,i)<<' ';cout<<'\n';
    }else if(op=="REMOVE"){
      int x;cin>>x;
      rida.erase(rida.lower_bound(A[x-1]));
      add(lower_bound(B.begin(),B.end(),A[x-1])-B.begin(),1);
      //for(int i=0;i<=N;i++)cout<<getsum(0,i)<<' ';cout<<'\n';
    }else{
      int x;cin>>x;
      long long l=-1,r=2e9;
      while(r-l>1){
        long long mid=(l+r)/2;
        int t=-1,ok=N-rida.size();
        for(int i:rida){
          if(t>=i-mid){
            ok-=getsum(upper_bound(B.begin(),B.end(),t)-B.begin(),lower_bound(B.begin(),B.end(),i)-B.begin());
          }else{
            ok-=getsum(lower_bound(B.begin(),B.end(),i-mid)-B.begin(),lower_bound(B.begin(),B.end(),i)-B.begin());
          }
          t=i;
        }
        if(ok<=x)r=mid;
        else l=mid;
        //cout<<ok<<' ';
      }
      if(r>1e9){
        cout<<"NA\n";
      }else{
        cout<<r<<'\n';
      }
    }
  }
}
accuracy: 1
time_ms: 720
memory_kb: 20572
final_rank: 12

code_snippet: #include <bits/stdc++.h>

#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
#define all(x) x.begin(),x.end()
using namespace std;
using pii = pair<int, int>;
template <class A, class B>
inline bool chmax(A& a, const B& b) {
  return b > a && (a = b, true);
}
template <class A, class B>
inline bool chmin(A& a, const B& b) {
  return b < a && (a = b, true);
}
template <class A>
inline A abs(A& a) {
  return (a < 0 ? -a : a);
}
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
template<class T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<class T> using uset=unordered_set<T>;
template<class A,class B> using umap=unordered_map<A,B>;
const lol inf = (1LL << 62);
const int MOD = (1e9) + 7;
const int mod = 998244353;
const int dx[] = {1,  0,  -1, 0, 1, 1, -1, -1, -2, -2, -2, -2,
                  -2, -1, -1, 1, 1, 0, 0,  2,  2,  2,  2,  2};
const int dy[] = {0, 1,  0, -1, 1, -1, 1, -1, -2, -1, 0, 1,
                  2, -2, 2, -2, 2, -2, 2, -2, -1, 0,  1, 2};


signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,q;
  cin >>n>>q;
  vector<int> s(n);
  map<int,int> rui;
  for(int i=0;i<n;i++){
    cin >>s[i];
    rui[s[i]]++;
  }
  rui[-1]=0;
  int tmp = 0;
  for(auto&& p:rui){
    tmp += p.se;
    p.se = tmp;
  }
  map<int,int> reder;
  for(int i=0;i<q;i++){
    string que;
    int a;
    cin >>que>>a;
    if(que=="ADD"){
      reder[s[a-1]]++;
    }else if(que=="REMOVE"){
      reder[s[a-1]]--;
      if(reder[s[a-1]] == 0){
        reder.erase(s[a-1]);
      }
    }else{
      int high=(int)1e9+100,low=-1;
      while(high-low>1){
        int mid=(high+low)/2;
        int sum=0;
        int tmp=-1;
        for(auto se:reder){
          auto w = rui.upper_bound(max(se.fi-mid-1,tmp));
          w--;
          sum += rui[se.fi] - (w->se);
          tmp = se.fi;
        }
        if(n-sum>a){
          low = mid;
        }else{
          high = mid;
        }
      }
      if(high==(int)1e9+100){
        cout <<"NA\n";
      }else{
        cout <<high<<'\n';
      }
    }
  }
  return (0);
}
accuracy: 1
time_ms: 1310
memory_kb: 53816
final_rank: 17

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) begin(x), end(x)
#define Fixed fixed << setprecision(12)
#define int int_fast64_t
using pii = pair<int,int>;
constexpr int32_t INF  = 0x3f3f3f3f;
constexpr int_fast64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7;
constexpr int mod2 = 998244353;

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T> using max_heap = priority_queue<T>;
template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class A, class B> using umap = unordered_map<A,B>;

template <class T> inline void bye(T x) { cout << x << '\n'; exit(0); }

inline int square(int a){ return a * a;}
inline int updiv(int a,int b){ return (a + b - 1) / b; }

constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};


signed main(void){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int n, q;
    cin >> n >> q;

    vector<int> s(n);
    vector<pair<int, int> > ps;

    for (int i = 0; i < n; ++i) {
        cin >> s[i];
        ps.emplace_back(s[i], i);
    }

    sort(all(ps));

    vector<int> idx(n), sorted(n);

    for (int i = 0; i < n; ++i) {
        idx[ps[i].second] = i;
        sorted[i] = ps[i].first;
    }

    auto comp = [&](auto a, auto b) { return (idx[a] < idx[b]); };
    set<int, decltype(comp)> ss(comp);

    auto check = [&](int key) -> int {
        int l = 0, cnt = 0;
        for (auto e: ss) {
            int x = lower_bound(all(sorted), s[e] - key) - sorted.begin();
            cnt += max(l, x) - l;
            l = upper_bound(all(sorted), s[e]) - sorted.begin();
        }
        return (cnt + n - l);
    };

    for (int i = 0; i < q; ++i) {
        string com;
        int a;
        cin >> com >> a;

        if (com == "ADD") {
            ss.emplace(a - 1);
        } else if (com == "REMOVE") {
            ss.erase(a - 1);
        } else {
            int ok = INF, ng = - 1;
            while (ok - ng > 1) {
                int mid = (ok + ng) / 2;
                if (check(mid) <= a) ok = mid;
                else ng = mid;
            }
            if (ok == INF) cout << "NA\n";
            else cout << ok << '\n';
        }
    }

    return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 42600
final_rank: 15

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(long long i = (a)*(strlen(#__VA_ARGS__)!=0);i<(long long)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(long long i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(long long)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

template <class T>
vector<T> make_vector(int n,T init){
    return vector<T>(n,init);
}

template <class... tail>
auto make_vector(int n, tail... args){
    return vector<decltype(make_vector(args...))>(n,make_vector(args...));
}

vector<ll> func(){
    int n = in();
    int q = in();
    vector<int> scores(n);
    foreach(i,scores)i=in();
    vector<int> line = sorted(scores);
    set<int> s;
    vector<int> has;

    method(check,bool,int r,int x){
        int last = -1;
        int cnt = 0;
        foreach(i,has){
            int p = lower_bound(all(line),i-r)-line.begin();
            cnt += max(0,p-last-1);
            int next = upper_bound(all(line),i)-line.begin()-1;
            last = next;
        }
        cnt += n - last - 1;
        return cnt <= x;
    };

    method(calc,ll,int x){
        has.clear();
        foreach(i,s)has.emplace_back(scores[i]);
        sort(all(has));
        int lp = 0;
        int rp = INF;
        while(lp<rp){
            int mid = (lp+rp) / 2;
            if(check(mid,x)){
                rp = mid;
            }else{
                lp = mid+1;
            }
        }
        if(check(lp,x))return lp;
        return -1;
    };

    vector<ll> res;
    rep(i,q){
        string op = in<string>();
        int v = in();
        if(op=="ADD"){
            s.emplace(v-1);
        }else if(op=="REMOVE"){
            s.erase(v-1);
        }else if(op=="CHECK"){
            res.emplace_back(calc(v));
        }
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    foreach(i,func()){
        if(i<0){
            println("NA");
        }else{
            println(i);
        }
    }
    return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 10780
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int N,Q;
int s[1<<20],t[1<<20];
main()
{
	cin>>N>>Q;
	for(int i=0;i<N;i++)
	{
		cin>>s[i];
		t[i]=s[i];
	}
	sort(t,t+N);
	multiset<int>A;
	for(;Q--;)
	{
		string S;int a;
		cin>>S>>a;
		if(S=="ADD"||S=="REMOVE")
		{
			a=upper_bound(t,t+N,s[a-1])-t;
			if(S=="ADD")A.insert(a);
			else A.erase(A.find(a));
		}
		else
		{
			long L=-1,R=1e9+1;
			while(R-L>1)
			{
				int mid=(L+R)/2;
				int cnt=0;
				int pre=0;
				for(int x:A)
				{
					int val=t[x-1];
					cnt+=max(0L,lower_bound(t,t+N,val-mid)-t-pre);
					pre=x;
				}
				cnt+=N-pre;
				if(cnt<=a)R=mid;
				else L=mid;
			}
			if(R>1e9)cout<<"NA"<<endl;
			else cout<<R<<endl;
		}
	}
}
accuracy: 1
time_ms: 550
memory_kb: 11024
final_rank: 5

code_snippet: #line 1 "a.cpp"
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#line 2 "/home/kotatsugame/library/datastructure/lazysegtree.cpp"
template<typename T>
struct lazysegtree{
	int n;
	T defvalue;
	vector<T>dat,lazy;
	vector<bool>lazyflag;
	lazysegtree(int n_=0):defvalue(0)
	{
		n=1;
		while(n<n_)n<<=1;
		dat.assign(2*n-1,defvalue);
		lazy.assign(2*n-1,T());
		lazyflag.assign(2*n-1,false);
	}
	void eval(int i,int l,int r)
	{
		if(lazyflag[i])
		{
			dat[i]=lazy[i]*(r-l);
			if(r-l>1)
			{
				lazy[2*i+1]=lazy[i];
				lazy[2*i+2]=lazy[i];
				lazyflag[2*i+1]=lazyflag[2*i+2]=true;
			}
			lazyflag[i]=false;
		}
	}
	void update(int a,int b,T x,int k=0,int l=0,int r=-1)//[a,b)
	{
		if(r<0)r=n;
		eval(k,l,r);
		if(b<=l||r<=a)return;
		else if(a<=l&&r<=b)
		{
			lazy[k]=x;
			lazyflag[k]=true;
			eval(k,l,r);
		}
		else
		{
			update(a,b,x,2*k+1,l,(l+r)/2);
			update(a,b,x,2*k+2,(l+r)/2,r);
			dat[k]=dat[2*k+1]+dat[2*k+2];
		}
	}
	T query(int a,int b,int k=0,int l=0,int r=-1)//[a,b)
	{
		if(r<0)r=n;
		eval(k,l,r);
		if(b<=l||r<=a)return defvalue;
		else if(a<=l&&r<=b)return dat[k];
		else return query(a,b,2*k+1,l,(l+r)/2)+query(a,b,2*k+2,(l+r)/2,r);
	}
};
#line 7 "a.cpp"
int N,Q;
int s[1<<20];
int ri[1<<20];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin>>N>>Q;
	vector<int>val(N);
	for(int i=0;i<N;i++)
	{
		cin>>s[i];
		val[i]=s[i];
	}
	sort(val.begin(),val.end());
	for(int i=0;i<N;i++)ri[i]=upper_bound(val.begin(),val.end(),s[i])-val.begin();
	lazysegtree<int>P(N);
	set<int>A;
	for(;Q--;)
	{
		string S;int a;
		cin>>S>>a;
		if(S=="ADD")A.insert(a-1);
		else if(S=="REMOVE")A.erase(a-1);
		else
		{
			int L=-1,R=1e9+1;
			while(R-L>1)
			{
				int mid=(L+R)/2;
				P.update(0,N,1);
				for(int id:A)
				{
					P.update(lower_bound(val.begin(),val.end(),s[id]-mid)-val.begin(),
						ri[id],0);
				}
				if(P.query(0,N)<=a)R=mid;
				else L=mid;
			}
			if(R>1e9)cout<<"NA\n";
			else cout<<R<<'\n';
		}
	}
}
accuracy: 1
time_ms: 2250
memory_kb: 31332
final_rank: 18

code_snippet: #include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

// 入力
int N, A[1 << 20], B[1 << 20];
int Q, X[1 << 20];
string TY[1 << 10];

// 状態
set<int> Set;

vector<pair<int, int>> merge(vector<pair<int, int>> D) {
	if (D.size() == 0) return D;

	vector<pair<int, int>> E;
	int maxn = D[0].first, fst = D[0].first;
	for (int i = 0; i < D.size(); i++) {
		maxn = max(maxn, D[i].second);
		if (i + 1 == D.size() || maxn < D[i + 1].first) {
			E.push_back(make_pair(fst, maxn));
			if (i + 1 != D.size()) fst = D[i + 1].first;
		}
	}
	return E;
}

int calc(vector<int> idx, int r) {
	sort(idx.begin(), idx.end());
	vector<pair<int, int>> D;
	for (int i = 0; i < idx.size(); i++) D.push_back(make_pair(idx[i] - r, idx[i]));
	D = merge(D);

	int sum = 0;
	for (int i = 0; i < D.size(); i++) {
		int pos1 = lower_bound(B + 1, B + N + 1, D[i].first) - B;
		int pos2 = lower_bound(B + 1, B + N + 1, D[i].second + 1) - B;
		sum += (pos2 - pos1);
	}
	return sum;
}

void solve(int num) {
	auto itr = Set.begin(); vector<int> C;
	while (itr != Set.end()) {
		C.push_back(A[*itr]);
		itr++;
	}

	int cl = 0, cr = (1 << 30), cm, minx = (1 << 30);
	for (int i = 0; i < 33; i++) {
		cm = (cl + cr) / 2;
		if (calc(C, cm) >= N - num) { minx = min(minx, cm); cr = cm; }
		else { cl = cm; }
	}

	if (minx == (1 << 30)) cout << "NA" << endl;
	else cout << minx << endl;
}

int main() {
	// 入力
	scanf("%d%d", &N, &Q);
	for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
	for (int i = 1; i <= Q; i++) {
		cin >> TY[i];
		scanf("%d", &X[i]);
	}
	
	// ソート
	for (int i = 1; i <= N; i++) B[i] = A[i];
	sort(B + 1, B + N + 1);

	// クエリに答える
	for (int i = 1; i <= Q; i++) {
		if (TY[i] == "ADD") Set.insert(X[i]);
		if (TY[i] == "REMOVE") Set.erase(X[i]);
		if (TY[i] == "CHECK") solve(X[i]);
	}
	return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 11640
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define each(i, a) for(auto &i : a)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
#define elif else if
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }

template<class Op> class SegmentTree {
	using T = typename Op::T;
	int len, n;
	vector<T> dat;
	T unit = Op::unit;
public:
	SegmentTree(const int _n) : n(_n) {
		for (len = 1; len < n; len <<= 1);
		dat.resize(len << 1, unit);
	}
	SegmentTree(const vector<T>& v) : n(v.size()) {
		for (len = 1; len < n; len <<= 1);
		dat.resize(len << 1, unit);
		for (int i = 0; i < n; ++i)dat[i + len] = v[i];
		for (int i = len - 1; 0 < i; --i)
			dat[i] = Op::merge(dat[i << 1], dat[i << 1 | 1]);
	}
	inline void update(int idx, const T a) {
		idx += len;
		dat[idx] = Op::update(dat[idx], a);
		for (idx >>= 1; 0 < idx; idx >>= 1)
			dat[idx] = Op::merge(dat[idx << 1], dat[idx << 1 | 1]);
	}
	inline T value(int l, int r) {
		T vl = unit, vr = unit;
		for (l += len, r += len; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = Op::merge(vl, dat[l++]);
			if (r & 1)vr = Op::merge(dat[--r], vr);
		}
		return Op::merge(vl, vr);
	}
	inline T operator[](int idx) { return dat[idx + len]; }
};
template<class Type> struct intSumAddUpdate {
	using T = Type;
	const static T unit = 0;
	inline static T merge(T vl, T vr) { return vl + vr; }
	inline static T update(T vl, T vr) { return vl + vr; }
};

int main() {
    init();

    int n, q; cin >> n >> q;
    VI s(n); cin >> s;
    VI iton(n), ntoi(n);//index-to-number, and vice versa
    {
        vector<pair<int, int>> stu;
        stu.reserve(n);
        FOR(i, 0, n)stu.emplace_back(s[i], i);
        SORT(stu);
        FOR(i, 0, n) {
            iton[i] = stu[i].second;
            ntoi[stu[i].second] = i;
        }
    }

	SegmentTree<intSumAddUpdate<int>> seg(n);
	set<int> idx;//index of leaders's
	while (q--) {
		string type; int a; cin >> type >> a;
		if (type == "ADD") {
			idx.insert(ntoi[a - 1]);
			continue;
		}
		else if (type == "REMOVE") {
			idx.erase(ntoi[a - 1]);
			continue;
		}
		int ng = -1, ok = INF + 1;
		while (ok - ng > 1) {
			int mid = ok + ng >> 1;
			//---------------
			int cnt = 0, lim = -1;
			for (auto it = idx.begin(); it != idx.end(); ++it) {
				int l = lim, r = n, L = lim, R = n;
				if (++it != idx.end())r = R = *it;
				--it;
				{
					while (r - l > 1) {
						int m = l + r >> 1;
						if (s[iton[m]] < s[iton[*it]] - mid)
							l = m;
						else r = m;
					}
				}
				{
					while (R - L > 1) {
						int m = L + R >> 1;
						if (s[iton[m]] <= s[iton[*it]])
							L = m;
						else R = m;
					}
				}
				cnt += L - l;
				lim = L;
			}
			//---------------
			if (n - cnt <= a)ok = mid;
			else ng = mid;
		}
		if (ok == INF + 1)print("NA");
		else print(ok);
	}

    return 0;
}
accuracy: 1
time_ms: 1160
memory_kb: 22812
final_rank: 14

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<unordered_set>
#include<unordered_map>
using namespace std;
typedef long long ll;
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod
ll n,q;
#define N 1000010
ll s[N];
vector<int> S;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin>>n>>q;
    rep(i,n){
	cin>>s[i];
	S.push_back(s[i]);
    }
    sort(all(S));
    set<int> L;
    while(q--){
	string typ;
	int a;
	cin>>typ>>a;
	if(typ=="ADD"){
	    a--;
	    L.insert(a);
	}
	if(typ=="REMOVE"){
	    a--;
	    L.erase(a);
	}
	if(typ=="CHECK"){
	    vector<ll> ls;
	    for(auto id:L){
		ls.push_back(s[id]);
	    }
	    ls.push_back(-1);
	    sort(all(ls));
	    ls.erase(unique(all(ls)),ls.end());
	    ll l=-1,r=2e9,mid;
	    while(l<r-1){
		mid=(l+r)>>1;
		int cnt=0;
		for(int i=1;i<ls.size();i++){
		    ll l=max(ls[i-1]+1,ls[i]-mid);
		    ll r=ls[i];
		    cnt+=upper_bound(all(S),r)-lower_bound(all(S),l);
		}
		if(n-cnt<=a)r=mid;
		else l=mid;
	    }
	    if(r==2e9)cout<<"NA\n";
	    else cout<<r<<"\n";
	}
    }
}
accuracy: 1
time_ms: 450
memory_kb: 14868
final_rank: 6

code_snippet: #include "bits/stdc++.h"
#pragma GCC optimaze("Ofast")

// Begin Headelr {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

#define all(x) x.begin(), x.end()
#define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i)
#define reps(i, b, e) for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

constexpr size_t    operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t  operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };

constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;

template <class T, class Compare = less<>>
using MaxHeap = priority_queue<T, vector<T>, Compare>;
template <class T, class Compare = greater<>>
using MinHeap = priority_queue<T, vector<T>, Compare>;

inline void input() {}
template <class Head, class... Tail>
inline void input(Head&& head, Tail&&... tail) {
    cin >> head;
    input(forward<Tail>(tail)...);
}

template <class T>
inline istream& operator>>(istream &is, vector<T> &vec) {
    for (auto &e: vec) {
        is >> e;
    }
    return is;
}

inline void output() { cout << "\n"; }
template <class Head, class... Tail>
inline void output(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) {
        cout << " ";
    }
    output(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<<(ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = {" ", ""};
    for (const auto &e: vec) {
        os << e << delim[&e == &vec.back()];
    }
    return os;
}

template <class T>
inline vector<T> makeVector(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeVector(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeVector<T>(initValue, args...))>(sz, makeVector<T>(initValue, args...));
}

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class Container>
struct reverse_t {
    Container &c;
    reverse_t(Container &c) : c(c) {}
    auto begin() { return c.rbegin(); }
    auto end() { return c.rend(); }
};

template <class Container>
auto reversed(Container &c) {
    return reverse_t<Container>(c);
}

template <class T>
inline bool chmax(T &a, const T &b) noexcept {
    return b > a && (a = b, true);
}

template <class T>
inline bool chmin(T &a, const T &b) noexcept {
    return b < a && (a = b, true);
}

template <class T>
inline T diff(const T &a, const T &b) noexcept {
    return a < b ? b - a : a - b;
}
// }}} End Header

constexpr int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

const auto inside = [](int y, int x, int H, int W) -> bool {
    return (y >= 0 && x >= 0 && y < H && x < W);
};

// SegmentTree {{{
template <class Monoid>
struct SegmentTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const size_t sz;
    const Func fn;
    const Monoid unity;
    vector<Monoid> seg;

    SegmentTree(const size_t n, const Monoid &u, Func f)
        : sz(1 << (__lg(n + 5) + 1)), fn(f), unity(u), seg(sz * 2, unity) {}

    void set(size_t k, const Monoid &v) {
        seg[k + sz] = v;
    }

    Monoid& operator[](size_t k) {
        return seg[k + sz];
    }
    const Monoid& operator[](size_t k) const {
        return seg[k + sz];
    }

    void build() {
        for (size_t k = sz - 1; k > 0; --k) {
            seg[k] = fn(seg[2 * k], seg[2 * k + 1]);
        }
    }

    void update(size_t k, const Monoid &x) {
        k += sz;
        seg[k] = x;
        while (k >>= 1) {
            seg[k] = fn(seg[2 * k], seg[2 * k + 1]);
        }
    }

    Monoid query(size_t a, size_t b) const {
        Monoid L = unity, R = unity;
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1) L = fn(L, seg[a++]);
            if (b & 1) R = fn(seg[--b], R);
        }
        return fn(L, R);
    }
};
// }}}

// compress {{{
template <class T> unordered_map<T, int> compress(vector<T> value) {
    unordered_map<T, int> ret;

    sort(value.begin(), value.end());
    value.erase(unique(value.begin(), value.end()), value.end());

    const size_t sz = value.size();
    for (size_t i = 0; i < sz; ++i) {
        ret[value[i]] = i;
    }

    return ret;
}
// }}}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.setf(ios_base::fixed);
    cout.precision(10);
    var(size_t, N, Q);
    vector<intmax_t> s(N); input(s);
    auto ss = s;
    sort(all(ss));
    auto zip = compress(s);
    const size_t M = zip.size();
    vector<pair<intmax_t, intmax_t>> v(M);
    rep(i, 0, N) {
        v[zip[s[i]]] = {s[i], v[zip[s[i]]].second + 1};
    }
    vector<intmax_t> cumsum(M + 1);
    rep(i, 0, M) {
        cumsum[i + 1] = cumsum[i] + v[i].second;
    }
    // {得点, 番号}
    set<pair<intmax_t, int>> leader;
    const auto f = [&](intmax_t r, intmax_t x) -> bool {
        intmax_t ret = N;
        for (auto itr = leader.begin(); itr != leader.end(); itr++) {
            intmax_t L, R;
            if (itr == leader.begin()) {
                L = max(0_jd, s[(*itr).second] - r);
                R = s[(*itr).second];
            } else {
                L = max(s[(*prev(itr)).second] + 1, s[(*itr).second] - r);
                R = s[(*itr).second];
                if (s[(*prev(itr)).second] == s[(*itr).second]) continue;
            }
            const auto pos = zip[*lower_bound(all(ss), L)];
            ret -= cumsum[zip[R] + 1] - cumsum[pos];
        }
        return (ret <= x);
    };
    while (Q--) {
        var(string, com);
        var(intmax_t, x);
        if (com == "ADD") {
            leader.emplace(s[x - 1], x - 1);
        } else if (com == "REMOVE") {
            leader.erase({s[x - 1], x - 1});
        } else {
            intmax_t ng = -1, ok = 1e9 + 1;
            while (ok - ng > 1) {
                const intmax_t mid = (ok + ng) / 2;
                if (f(mid, x)) ok = mid;
                else ng = mid;
            }
            if (ok == 1e9 + 1) {
                output("NA");
            } else {
                output(ok);
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 1210
memory_kb: 81420
final_rank: 19