alexjercan/bugnet huggingface dataset:
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;

#define MAX_N 1000000
#define MAX_TEAM 1000000

priority_queue<pair<long long, long long>> Q;
long long d[MAX_TEAM], n, s, L, x[MAX_N], y[MAX_N], t[MAX_N], ti[MAX_TEAM],
    maxn, maxid;

int main() {
  cin >> n >> s >> L;
  Q.push(make_pair(0, -1));
  x[0] = 1;
  t[0] = 0;
  y[0] = 0;
  x[s + 1] = 1;
  t[s + 1] = L;
  y[s + 1] = 0;
  for (int i = 1; i <= s; i++) {
    cin >> x[i] >> t[i] >> y[i];
  }
  for (int i = 0; i <= s; i++) {
    d[x[i]] += y[i];
    Q.push(make_pair(d[x[i]], -x[i]));
    while (true) {
      pair<int, int> p = Q.top();
      if (p.first == d[-p.second]) {
        ti[-p.second] += t[i + 1] - t[i];
        break;
      }
      Q.pop();
    }
  }
  for (int i = 1; i <= n; i++) {
    if (maxn < ti[i]) {
      maxn = ti[i];
      maxid = i;
    }
  }
  cout << maxid << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

#define MAX_N 1000000
#define MAX_TEAM 1000000

priority_queue<pair<long long, long long>> Q;
long long d[MAX_TEAM], n, s, L, x[MAX_N], y[MAX_N], t[MAX_N], ti[MAX_TEAM],
    maxn, maxid;

int main() {
  cin >> n >> s >> L;
  Q.push(make_pair(0, -10000000));
  x[0] = 1;
  t[0] = 0;
  y[0] = 0;
  x[s + 1] = 1;
  t[s + 1] = L;
  y[s + 1] = 0;
  for (int i = 1; i <= s; i++) {
    cin >> x[i] >> t[i] >> y[i];
  }
  for (int i = 0; i <= s; i++) {
    d[x[i]] += y[i];
    Q.push(make_pair(d[x[i]], -x[i]));
    while (!Q.empty()) {
      pair<int, int> p = Q.top();
      if (p.first == d[-p.second]) {
        ti[-p.second] += t[i + 1] - t[i];
        break;
      }
      Q.pop();
    }
  }
  for (int i = 1; i <= n; i++) {
    if (maxn < ti[i]) {
      maxn = ti[i];
      maxid = i;
    }
  }
  cout << maxid << endl;
  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

#define INF 2000000000
typedef pair<int, int> seg;
seg dat[1 << 18 + 1];
int ans[1 << 17];

int n, N, R, L;

void update(int i, seg x) {
  i += n - 1;
  dat[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
}

void init() {
  n = 1;
  while (n < N)
    n *= 2;
  n *= 2;
  for (int i = 0; i < n * 2 - 1; i++)
    dat[i] = make_pair(INF, INF);
  for (int i = 0; i < N; i++)
    update(i, make_pair(0, i));
}

seg query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return make_pair(INF, INF);
  if (a <= l && b >= r)
    return dat[k];
  seg vr = query(a, b, k, (l + r) / 2, r);
  seg vl = query(a, b, k, l, (l + r) / 2);
  return min(vr, vl);
}

void output() {
  int m = 0;
  for (int i = 0; i < N; i++) {
    //    cout << ans[i] << " ";
    m = max(m, ans[i]);
  }
  for (int i = 0; i < N; i++) {
    if (ans[i] == m) {
      cout << i + 1 << endl;
      return;
    }
  }
}

int main() {
  int old_t = 0;

  cin >> N >> R >> L;
  init();
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;
    ans[query(0, N, 0, 0, n).second] += t - old_t;

    seg s = dat[d + n - 1];
    update(d, make_pair(s.first - x, s.second));
    old_t = t;
  }
  ans[query(0, N, 0, 0, n).second] += L - old_t;

  output();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

#define INF 2000000000
typedef pair<int, int> seg;
seg dat[1 << 18];
int ans[1 << 17];

int n, N, R, L;

void update(int i, seg x) {
  i += n - 1;
  dat[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
}

void init() {
  n = 1;
  while (n < N)
    n *= 2;
  n *= 2;
  for (int i = 0; i < n * 2 - 1; i++)
    dat[i] = make_pair(INF, INF);
  for (int i = 0; i < N; i++)
    update(i, make_pair(0, i));
}

seg query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return make_pair(INF, INF);
  if (a <= l && b >= r)
    return dat[k];
  seg vr = query(a, b, k, (l + r) / 2, r);
  seg vl = query(a, b, k, l, (l + r) / 2);
  return min(vr, vl);
}

void output() {
  int m = 0;
  for (int i = 0; i < N; i++) {
    //    cout << ans[i] << " ";
    m = max(m, ans[i]);
  }
  for (int i = 0; i < N; i++) {
    if (ans[i] == m) {
      cout << i + 1 << endl;
      return;
    }
  }
}

int main() {
  int old_t = 0;

  cin >> N >> R >> L;
  init();
  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;
    ans[query(0, N, 0, 0, n).second] += t - old_t;

    seg s = dat[d + n - 1];
    update(d, make_pair(s.first - x, s.second));
    old_t = t;
  }
  ans[query(0, N, 0, 0, n).second] += L - old_t;

  output();
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <utility>
#define rep(i, a) for (int i = 0; i != (a); ++i)

typedef std::pair<int, int> P;

const int MAX_N = 1 << 17, INF = 1 << 30;

int sz;
P dat[2 * MAX_N - 1];

void init(int n) {
  sz = 1;
  while (sz < n)
    sz <<= 1;

  rep(i, 2 * sz - 1) dat[i] = P(INF, INF);

  return;
}

void update(int k, P p) {
  k += sz - 1;
  dat[k] = p;

  while (k > 0) {
    k = (k - 1) >> 1;
    dat[k] = std::min(dat[2 * k + 1], dat[2 * k + 2]);
  }

  return;
}

P query(int a, int b, int k, int l, int r) {
  if (b <= l || r <= a)
    return P(INF, INF);

  if (a <= l && r <= b)
    return dat[k];
  else
    return std::min(query(a, b, 2 * k + 1, l, (l + r) >> 1),
                    query(a, b, 2 * k + 2, (l + r) >> 1, r));
}

int N, R, L;
int cnt[MAX_N];

int main() {
  scanf("%d%d%d", &N, &R, &L);

  init(N);
  rep(i, N) update(i, P(0, i));

  int prvT = 0;
  rep(i, R) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (prvT != t)
      cnt[query(0, N, 0, 0, sz).second] += t - prvT;

    --d;
    P p = query(d, d + 1, 0, 0, sz);
    update(d, P(p.first - x, p.second));
    prvT = t;
  }

  if (L != prvT)
    cnt[query(0, N, 0, 0, sz).second] += L - prvT;

  printf("%d\\n", std::max_element(cnt, cnt + N) - cnt + 1);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <utility>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int MAX_N = 1000000;

typedef std::pair<int, int> P;

int N, R, L;

int sz;
P dat[2 * MAX_N - 1];

void init(int n) {
  sz = 1;
  while (sz < n)
    sz <<= 1;

  rep(i, 2 * sz - 1) dat[i] = P(0, 0);

  return;
}

void update(int k, P p) {
  k += sz - 1;
  dat[k].first += p.first;
  dat[k].second = p.second;
  while (k > 0) {
    k = (k - 1) >> 1;
    dat[k] = std::max(dat[2 * k + 1], dat[2 * k + 2]);
  }

  return;
}

P query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return P(0, 0);

  if (a <= l && r <= b)
    return dat[k];
  else
    return std::max(query(a, b, 2 * k + 1, l, (l + r) >> 1),
                    query(a, b, 2 * k + 2, (l + r) >> 1, r));
}

int cnt[1000001];

int main() {
  scanf("%d%d%d", &N, &R, &L);

  init(N);

  rep(i, N) update(i, P(0, -i));

  int prvT, prvid;
  bool fst = true;

  rep(i, R) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    update(d - 1, P(x, -(d - 1)));

    if (!fst)
      cnt[prvid] += t - prvT;
    prvT = t;
    prvid = -query(0, N, 0, 0, sz).second;
    fst = false;
  }

  cnt[prvid] += L - prvT;

  printf("%d\\n", std::max_element(cnt, cnt + N) - cnt + 1);

  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2013/11/05 Tazoe

#include <iostream>
using namespace std;

int main() {
  int N, R, L;
  cin >> N >> R >> L;

  long long P[100001] = {0};
  int W = 1;

  int T[100001] = {0};
  int tp = 0;

  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;

    T[W] += t - tp;
    tp = t;

    P[d] += x;

    if (x >= 0) {
      if (P[W] < P[d] || (P[W] == P[d] && W > d)) {
        W = d;
      }
    } else {
      if (W == d) {
        W = 1;
        for (int j = 2; j <= N; j++) {
          if (P[W] < P[j]) {
            W = j;
          }
        }
      }
    }
    //		cout << W << ' ' << P[W] << endl;
  }

  T[W] += L - tp;

  int C = 1;
  for (int i = 2; i <= N; i++) {
    if (T[C] < T[i]) {
      C = i;
    }
  }
  cout << C << endl;

  return 0;
}

Error: TLE

Fixed submission:

// 2013/11/05 Tazoe

#include <iostream>
using namespace std;

int main() {
  int N, R, L;
  cin >> N >> R >> L;

  long long P[100001] = {0};
  int W = 1;

  int T[100001] = {0};
  int tp = 0;

  for (int i = 0; i < R; i++) {
    int d, t, x;
    cin >> d >> t >> x;

    T[W] += t - tp;
    tp = t;

    P[d] += x;

    if (x >= 0) {
      if (P[W] < P[d] || (P[W] == P[d] && W > d)) {
        W = d;
      }
    } else {
      W = 1;
      for (int j = 2; j <= N; j++) {
        if (P[W] < P[j]) {
          W = j;
        }
      }
    }
    //		cout << W << ' ' << P[W] << endl;
  }

  T[W] += L - tp;

  int C = 1;
  for (int i = 2; i <= N; i++) {
    if (T[C] < T[i]) {
      C = i;
    }
  }
  cout << C << endl;

  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <iostream>
#include <utility>

typedef std::pair<int, int> P;

const int MAX_N = 1 << 17;

int n;
// fst: -得点, snd: チームの番号
P dat[MAX_N * 2 - 1];

int cnt[MAX_N];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i != n * 2 - 1; ++i)
    dat[i] = std::make_pair(INT_MAX, INT_MAX);

  return;
}

void update(int k, P x) {
  k += n - 1;
  dat[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = std::min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }

  return;
}

// [a,b)
P query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return std::make_pair(INT_MAX, INT_MAX);

  if (a <= l && r <= b)
    return dat[k];
  else {
    P vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    P vr = query(a, b, k * 2 + 2, (l + r) / 2, r);

    return std::min(vl, vr);
  }
}

int main() {
  int N, R, L;
  std::cin >> N >> R >> L;

  init(N);
  for (int i = 0; i != N; ++i)
    update(i, std::make_pair(0, i));

  int prevT = 0;
  for (int i = 0; i != R; ++i) {
    int d, t, x;
    std::cin >> d >> t >> x;

    --d;

    if (t != prevT) {
      P p = query(0, N, 0, 0, n);
      cnt[p.second] += t - prevT;
    }

    P p = query(d, d + 1, 0, 0, n);
    update(d, std::make_pair(p.first - x, p.second));

    prevT = t;
  }

  if (prevT != L) {
    P p = query(0, N, 0, 0, n);
    cnt[p.second] += L - prevT;
  }

  int t = -1, id;
  for (int i = 0; i != N; ++i) {
    if (t < cnt[i]) {
      t = cnt[i];
      id = i + 1;
    }
  }

  std::cout << id << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>
#include <utility>

typedef std::pair<int, int> P;

const int MAX_N = 100000;

int n;
// fst: -得点, snd: チームの番号
P dat[MAX_N * 2 - 1];

int cnt[MAX_N];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;

  for (int i = 0; i != n * 2 - 1; ++i)
    dat[i] = std::make_pair(INT_MAX, INT_MAX);

  return;
}

void update(int k, P x) {
  k += n - 1;
  dat[k] = x;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = std::min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }

  return;
}

// [a,b)
P query(int a, int b, int k, int l, int r) {
  if (a >= r || b <= l)
    return std::make_pair(INT_MAX, INT_MAX);

  if (a <= l && r <= b)
    return dat[k];
  else {
    P vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    P vr = query(a, b, k * 2 + 2, (l + r) / 2, r);

    return std::min(vl, vr);
  }
}

int main() {
  int N, R, L;
  std::cin >> N >> R >> L;

  init(N);
  for (int i = 0; i != N; ++i)
    update(i, std::make_pair(0, i));

  int prevT = 0;
  for (int i = 0; i != R; ++i) {
    int d, t, x;
    std::cin >> d >> t >> x;

    --d;

    if (t != prevT) {
      P p = query(0, N, 0, 0, n);
      cnt[p.second] += t - prevT;
    }

    P p = query(d, d + 1, 0, 0, n);
    update(d, std::make_pair(p.first - x, p.second));

    prevT = t;
  }

  if (prevT != L) {
    P p = query(0, N, 0, 0, n);
    cnt[p.second] += L - prevT;
  }

  int t = -1, id;
  for (int i = 0; i != N; ++i) {
    if (t < cnt[i]) {
      t = cnt[i];
      id = i + 1;
    }
  }

  std::cout << id << std::endl;

  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
struct edge {
  int to, time, cost;
};
const double esp = 1e-9;
const int infi = (int)1e+9;
const ll infll = (ll)1e+17;

const int max = (int)1e+6 + 1;

typedef std::pair<ll, int> MP;

int n, r, l;
MP seg[max * 2];
ll point[max];
int d[max], t[max], x[max];

void init() {
  int x = 1;
  while (x < n)
    x *= 2;
  n = x;
  rep(i, max * 2) seg[i].first = -infll, seg[i].second = 0;
  REP(i, n - 1, n * 2 - 1) seg[i].second = i - (n - 1);
}

void update(int k, int e) {
  k += n - 1;
  seg[k].first += e;
  while (k > 0) {
    k = (k - 1) / 2;
    int BigI =
        (seg[k * 2 + 1].first >= seg[k * 2 + 2].first ? k * 2 + 1 : k * 2 + 2);
    seg[k].first = seg[BigI].first;
    seg[k].second = seg[BigI].second;
  }
}

int main() {
  std::cin >> n >> r >> l;
  int before = 0;
  init();
  rep(i, r) std::cin >> d[i] >> t[i] >> x[i];

  rep(i, r) {
    if (i == r - 1 || t[i] != t[i + 1]) {
      point[seg[0].second] += (t[i] - before);
      before = t[i];
    }
    update(d[i] - 1, x[i]);
  }

  point[seg[0].second] += (l - before);
  int maxI = 0;
  REP(i, 1, n) {
    if (point[i] > point[maxI])
      maxI = i;
  }
  std::cout << maxI + 1 << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
struct edge {
  int to, time, cost;
};
const double esp = 1e-9;
const int inf = (int)1e+9;

const int max = (int)1e+5 + 1;

int n, r, l;
P seg[max * 2];
int point[max];

void init() {
  int x = 1;
  while (x < n)
    x *= 2;
  n = x;
  rep(i, max * 2) seg[i].first = -inf, seg[i].second = 0;
  REP(i, n - 1, n * 2 - 1) seg[i].second = i - (n - 1);
}

void update(int k, int e) {
  k += n - 1;
  seg[k].first += e;
  while (k > 0) {
    k = (k - 1) / 2;
    int BigI =
        (seg[k * 2 + 1].first > seg[k * 2 + 2].first ? k * 2 + 1 : k * 2 + 2);
    seg[k].first = seg[BigI].first;
    seg[k].second = seg[BigI].second;
  }
}

int main() {
  std::cin >> n >> r >> l;
  int before = 0;
  init();
  rep(i, r) {
    int d, t, x;
    std::cin >> d >> t >> x;
    --d;
    point[seg[0].second] += (t - before);
    update(d, x);
    before = t;
  }
  point[seg[0].second] += (l - before);

  int maxI = 0;
  REP(i, 1, n) {
    if (point[i] > point[maxI])
      maxI = i;
  }
  std::cout << maxI + 1 << std::endl;
  return 0;
}
Problem p00278 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q;
  int s[1000000];
  int tp[1000000];
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    scanf("%d", &s[i]);
    tp[i] = s[i];
  }
  map<int, int> mp;
  sort(s, s + n);
  for (int i = 0; i < n; i++)
    mp[s[i]] = i;
  vector<int> ld;
  while (q--) {
    string str;
    int a;
    cin >> str >> a;

    if (str[0] == 'A') {
      a--, ld.push_back(mp[tp[a]]);
      sort(ld.begin(), ld.end());
    }

    if (str[0] == 'R') {
      a--;
      int b = mp[tp[a]];
      for (int i = 0; i < ld.size(); i++) {
        if (b == ld[i]) {
          ld.erase(ld.begin() + i);
          break;
        }
      }
    }

    if (str[0] == 'C') {
      int l = 0, r = 1e9 + 10, m;

      while (l < r) {
        m = (l + r) / 2;

        int pr = 1e9, res = 0;

        for (int i = ld.size() - 1; i >= 0; i--) {
          int nld = ld[i];
          if (pr < nld)
            nld = pr;
          pr = lower_bound(s, s + n, s[ld[i]] - m) - s;
          pr--;
          if (nld > pr)
            res += (nld - pr);
        }
        if (n - res <= a)
          r = m;
        else
          l = m + 1;
      }

      if (r == 1e9 + 10)
        cout << "NA" << endl;
      else
        cout << r << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;

vector<P> G[N];
bool used[N], used2[N];

map<int, int> cnt;
set<P> usedP;

void dfs(int x) {

  if (used2[x])
    return;
  used2[x] = 1;
  used[x] = 1;

  for (int i = 0; i < G[x].size(); i++) {

    int nx = G[x][i].first, cost = G[x][i].second;

    if (!usedP.count(P(min(x, nx), max(x, nx)))) {
      usedP.insert(P(min(x, nx), max(x, nx)));
      if (cnt.count(cost))
        cnt[cost]++;
      else
        cnt[cost] = 1;
    }

    dfs(nx);
  }
}

int main() {

  int n;

  while (1) {

    cin >> n;
    if (!n)
      break;

    for (int i = 0; i < n; i++)
      G[i].clear();

    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      G[i].push_back(P(a, b));
      cin >> a >> b;
      G[i].push_back(P(a, b));
    }

    memset(used, 0, sizeof(used));

    int ans = 1;

    for (int i = 0; i < n; i++) {
      if (used[i])
        continue;

      memset(used2, 0, sizeof(used2));
      cnt.clear();
      usedP.clear();

      dfs(i);

      if (cnt.size()) {
        map<int, int>::iterator ite = cnt.end();
        ite--;
        ans = (ans * (*ite).second) % 10007;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00278 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<pair<int, int>> R;
pair<int, int> G[1 << 20];
int I[1 << 20];
int N, Q, P;
string S;
bool check(int M, int J) {
  int sum = 0, cnt = 1200000000;
  if (M < 0)
    return false;
  for (int i = R.size() - 1; i >= 0; i--) {
    int L2 = I[R[i].second] - M, R2 = I[R[i].second] + 1;
    if (R2 >= cnt)
      R2 = cnt;
    if (L2 >= R2)
      continue;
    int pos1 = lower_bound(G, G + N, make_pair(L2, 0)) - G;
    int pos2 = lower_bound(G, G + N, make_pair(R2, 0)) - G;
    sum += pos2 - pos1;
    cnt = L2;
  }
  sum = N - sum;
  if (sum <= J) {
    return true;
  }
  return false;
}
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> I[i];
    G[i] = make_pair(I[i], i);
  }
  sort(G, G + N);
  for (int i = 0; i < Q; i++) {
    cin >> S >> P;
    if (S == "ADD") {
      P--;
      R.push_back(make_pair(I[P], P));
      sort(R.begin(), R.end());
    }
    if (S == "REMOVE") {
      P--;
      for (int j = 0; j < R.size(); j++) {
        if (R[j].second == P) {
          R.erase(R.begin() + j);
          sort(R.begin(), R.end());
          break;
        }
      }
    }
    if (S == "CHECK") {
      int L1 = 0, R1 = 1200000000, M1;
      while (true) {
        M1 = (L1 + R1) / 2;
        bool b1 = check(M1 - 1, P), b2 = check(M1, P);
        if (b1 == false && b2 == true) {
          cout << M1 << endl;
          break;
        }
        if (b1 == true)
          R1 = M1;
        if (b2 == false)
          L1 = M1;
        if (M1 >= 1020000000 && b2 == false) {
          cout << "NA" << endl;
          break;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n, q, s[10000], t[10000], u[10000], b, c, sum, maxn, sumx;
string S;

void add();
void remove();
void check();
void check2();

void add() {
  cin >> b;
  t[b] = 1;
}

void remove() {
  cin >> b;
  t[b] = 0;
}

void check() {
  cin >> b;
  sum = 0;
  maxn = 0;
  for (int i = 1; i <= n; i++) {
    if (t[i] == 1) {
      maxn = max(maxn, s[i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] > maxn) {
      sum++;
    }
  }
  if (sum > b) {
    cout << "NA" << endl;
  } else {
    check2();
  }
}

void check2() {
  c = 0;
  sumx = 0;
  memset(u, 0, sizeof(u));
  while (true) {
    for (int i = 1; i <= n; i++) {
      if (u[i] == 0) {
        for (int j = 1; j <= n; j++) {
          if (t[j] == 1 && s[j] - s[i] <= c && s[j] - s[i] >= 0) {
            u[i] = 1;
            sumx++;
          }
        }
      }
    }
    if ((n - sumx) <= b) {
      cout << c << endl;
      break;
    }
    c++;
  }
}

int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> S;
    if (S == "ADD") {
      add();
    }
    if (S == "REMOVE") {
      remove();
    }
    if (S == "CHECK") {
      check();
    }
  }
}
Problem p00278 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
signed main() {
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  string query[q];
  int a[q];
  for (int i = 0; i < q; i++)
    cin >> query[i] >> a[i];

  vector<int> v = s;
  sort(v.begin(), v.end());
  map<int, int> m;
  for (int i = 0; i < n; i++)
    m[v[i]] = i;

  set<int> si;
  for (int i = 0; i < q; i++) {
    if (query[i] == "ADD") {
      si.insert(a[i] - 1);
      continue;
    }
    if (query[i] == "REMOVE") {
      si.erase(a[i] - 1);
      continue;
    }
    if (a[i] == n) {
      cout << 0 << endl;
      continue;
    }
    vector<int> u;
    for (int j : si)
      u.push_back(m[s[j]]);
    sort(u.begin(), u.end());
    u.erase(unique(u.begin(), u.end()), u.end());
    int sz = u.size();
    // for(int j=0;j<sz;j++) cout<<u[j]<<"-";cout<<endl;

    if (!sz || u[sz - 1] + 1 < n - a[i]) {
      cout << "NA" << endl;
      continue;
    }
    int l = -1, r = (1e9) + 7;
    while (l + 1 < r) {
      int x = (l + r) / 2;
      int tmp = -1, cnt = 0;
      for (int j = 0; j < sz; j++) {
        int pos = lower_bound(v.begin(), v.end(), v[u[j]] - x) - v.begin();
        if (pos < tmp)
          pos = tmp;
        cnt += u[j] + 1 - pos;
        tmp = u[j] + 1;
      }
      // cout<<x<<":"<<cnt<<"-"<<n-a[i]<<endl;
      if (cnt < n - a[i])
        l = x;
      else
        r = x;
    }
    cout << r << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
signed main() {
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  string query[q];
  int a[q];
  for (int i = 0; i < q; i++)
    cin >> query[i] >> a[i];

  vector<int> v = s;
  sort(v.begin(), v.end());
  map<int, int> m;
  for (int i = 0; i < n; i++)
    m[v[i]] = i;

  set<int> si;
  for (int i = 0; i < q; i++) {
    if (query[i] == "ADD") {
      si.insert(a[i] - 1);
      continue;
    }
    if (query[i] == "REMOVE") {
      si.erase(a[i] - 1);
      continue;
    }
    if (a[i] == n) {
      cout << 0 << endl;
      continue;
    }
    vector<int> u;
    for (int j : si)
      u.push_back(m[s[j]]);
    sort(u.begin(), u.end());
    u.erase(unique(u.begin(), u.end()), u.end());
    int sz = u.size();
    // for(int j=0;j<sz;j++) cout<<u[j]<<"-";cout<<endl;

    if (!sz || u[sz - 1] + 1 < n - a[i]) {
      cout << "NA" << endl;
      continue;
    }
    int l = -1, r = 1e10;
    while (l + 1 < r) {
      int x = (l + r) / 2;
      int tmp = -1, cnt = 0;
      for (int j = 0; j < sz; j++) {
        int pos = lower_bound(v.begin(), v.end(), v[u[j]] - x) - v.begin();
        if (pos < tmp)
          pos = tmp;
        cnt += u[j] + 1 - pos;
        tmp = u[j] + 1;
      }
      // cout<<x<<":"<<cnt<<"-"<<n-a[i]<<endl;
      if (cnt < n - a[i])
        l = x;
      else
        r = x;
    }
    cout << r << endl;
  }
  return 0;
}