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

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

#define MAX 1000000002
#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int N, Q;
iP s[1000000];
set<iP> Leader;
vector<int> sorted;

void ADD(int n) { Leader.insert(s[n - 1]); }

void REMOVE(int n) { Leader.erase(Leader.find(s[n - 1])); }

int CHECK(int x) {

  int l = 0, r = MAX;
  while (l < r) {
    int mid = (l + r) / 2, cnt = 0;
    vector<int>::iterator lb, lu, ll = sorted.end(), rr = sorted.begin();
    for (set<iP>::iterator it = Leader.begin(); it != Leader.end(); it++) {
      lb = lower_bound(sorted.begin(), sorted.end(), (*it).fr - mid);
      lu = upper_bound(sorted.begin(), sorted.end(), (*it).fr);
      // if(lu != sorted.end()) {
      if (rr >= lb)
        lb = rr;
      cnt += lu - lb;
      rr = lu, ll = lb;
      // }
    }
    if (N - cnt > x)
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}

int main() {

  cin >> N >> Q;
  sorted.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> s[i].fr;
    s[i].sc = i;
  }

  for (int i = 0; i < N; i++)
    sorted[i] = s[i].fr;
  sort(sorted.begin(), sorted.end());

  while (Q--) {
    int x;
    string query;
    cin >> query >> x;
    if (query == "ADD")
      ADD(x);
    else if (query == "REMOVE")
      REMOVE(x);
    else {
      int ans = CHECK(x);
      if (ans == MAX)
        cout << "NA\\n";
      else
        cout << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

#define MAX 1000000002
#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int N, Q, s[1000000];
set<int> Leader;

void ADD(int n) { Leader.insert(n - 1); }

void REMOVE(int n) { Leader.erase(Leader.find(n - 1)); }

int CHECK(int x) {

  int l = 0, r = MAX;
  while (l < r) {
    vector<bool> used(N, false);
    int mid = (l + r) / 2;
    for (set<int>::iterator it = Leader.begin(); it != Leader.end(); it++) {
      used[*it] = true;
      for (int i = 0; i < N; i++) {
        if (!(s[*it] < s[i] || s[*it] - mid > s[i]))
          used[i] = true;
      }
    }
    if (count(used.begin(), used.end(), false) > x)
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}

int main() {

  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    cin >> s[i];

  while (Q--) {
    int x;
    string query;
    cin >> query >> x;
    if (query == "ADD")
      ADD(x);
    else if (query == "REMOVE")
      REMOVE(x);
    else {
      int ans = CHECK(x);
      if (ans >= MAX)
        cout << "NA\\n";
      else
        cout << ans << endl;
    }
  }
}