Problem p00278 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
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
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}
int main() {
  const lint MAXS = 1000000000;
  int n, q;
  multiset<lint> ms;
  vector<lint> a, b;

  cin >> n >> q;
  FOR(i, n) {
    lint x;
    cin >> x;
    a.push_back(x);
    b.push_back(x);
  }
  sort(ALL(a));

  FOR(c, q) {
    string s;
    lint x;
    cin >> s >> x;
    if (s == "ADD") {
      x--;
      ms.insert(b[x]);
    }
    if (s == "REMOVE") {
      x--;
      auto it = ms.lower_bound(b[x]);
      ms.erase(it);
    }
    if (s == "CHECK") {
      lint l = 0, r = MAXS + 1, m;
      while (l != r) {
        m = (l + r) / 2;
        int prev = 0, st = 0;
        for (auto it : ms) {
          int d = lower_bound(ALL(a), it - m) - a.begin();
          st += max(d - prev, 0);
          prev = upper_bound(ALL(a), it) - a.begin();
        }
        st += max(n - prev, 0);
        if (st <= x)
          r = m;
        else
          l = m + 1;
      }
      if (l == MAXS + 1)
        cout << "NA" << endl;
      else
        cout << l << endl;
    }
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
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 = 1100000000;
int ind[MAX_N];
// vi a;
multiset<int> a;
list<int> b;
signed ok[MAX_N + 1];
signed main() {
  int n, q;
  cin >> n >> q;
  FOR(i, n) {
    cin >> ind[i];
    a.insert(ind[i]);
  }
  FOR(c, q) {
    string Q;
    int x;
    cin >> Q >> x;

    if (Q == "ADD") {
      x--;
      b.insert(b.begin(), ind[x]);
      auto it = a.lower_bound(ind[x]);
      a.erase(it);
    } else if (Q == "REMOVE") {
      x--;
      for (auto it = b.begin(); it != b.end(); it++) {
        if (*it == ind[x]) {
          b.erase(it);
          break;
        }
      }
      a.insert(ind[x]);
    } else {
      memset(ok, 0, sizeof ok);
      for (auto it = b.begin(); it != b.end(); it++) {
        auto r = a.upper_bound(*it);
        auto l = a.lower_bound(*it - MAX_S);
        if (r == l)
          continue;
        int L = 0, R = 0;
        for (auto i = a.begin();; i++) {
          if (l == i)
            break;
          L++;
        }
        for (auto i = a.begin();; i++) {
          if (r == i)
            break;
          R++;
        }
        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 = a.upper_bound(*it);
            auto l = a.lower_bound(*it - middle);
            if (r == l)
              continue;
            int L = 0, R = 0;
            for (auto i = a.begin();; i++) {
              if (l == i)
                break;
              L++;
            }
            for (auto i = a.begin();; i++) {
              if (r == i)
                break;
              R++;
            }
            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;
      }
    }
  }
}