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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  set<int> ldr;
  vector<int> s(n), ss(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    ss[i] = s[i];
  }
  sort(ss.begin(), ss.end());
  for (; q--;) {
    string job;
    int num;
    cin >> job >> num;
    if (job[0] == 'A') {
      ldr.insert(num - 1);
    } else if (job[0] == 'R') {
      ldr.erase(num - 1);
    } else if (job[0] == 'C') {
      int result = -1;
      if (ldr.size()) {
        for (int l = 0, r = 2000000000; r != l;) {
          int c = l + (r - l) / 2;

          priority_queue<pair<int, int>, vector<pair<int, int>>,
                         greater<pair<int, int>>>
              range, mergedRange;
          for (auto it = ldr.begin(); it != ldr.end(); it++) {
            range.push(make_pair(s[(*it)] - c, s[(*it)]));
          }
          while (range.size()) {
            pair<int, int> top = range.top();
            range.pop();
            if (range.size() && top.second >= range.top().first) {
              if (top.second >= range.top().second) {
                range.pop();
              } else {
                top.second = range.top().second;
              }
              range.push(top);
            } else {
              mergedRange.push(top);
            }
          }
          int count = ss.size();
          while (mergedRange.size()) {
            auto b = lower_bound(ss.begin(), ss.end(), mergedRange.top().first);
            auto e =
                upper_bound(ss.begin(), ss.end(), mergedRange.top().second);
            count -= e - b;
            mergedRange.pop();
          }

          if (num >= count) {
            result = r = c;
          } else {
            if (l == c)
              break;
            l = c;
          }
        }
      } else if (num >= s.size()) {
        result = 0;
      }
      if (result < 0)
        cout << "NA" << endl;
      else
        cout << result << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  set<int> ldr;
  vector<int> s(n), ss(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    ss[i] = s[i];
  }
  sort(ss.begin(), ss.end());
  for (; q--;) {
    string job;
    int num;
    cin >> job >> num;
    if (job[0] == 'A')
      ldr.insert(num - 1);
    else if (job[0] == 'R')
      ldr.erase(num - 1);
    else if (job[0] == 'C') {
      int result = -1;
      if (ldr.size()) {
        for (int l = 0, r = 2000000000; r != l;) {
          int i = l + (r - l) / 2;

          set<pair<int, int>> ranges;
          for (auto it = ldr.begin(); it != ldr.end(); it++) {
            ranges.insert(make_pair(s[(*it)] - i, s[(*it)]));
          }

          int count = 0;
          auto it = ranges.begin();
          for (int i = 0; i < ss.size(); i++) {
            if (ss[i] < (*it).first) {
              count++;
            } else if (ss[i] > (*it).second) {
              if (++it == ranges.end()) {
                count += ss.size() - i;
                break;
              }
              i--;
            }
          }

          if (num >= count) {
            if (r == i)
              break;
            result = r = i;
          } else {
            if (l == i)
              break;
            l = i;
          }
        }
      } else if (num >= s.size()) {
        result = 0;
      }
      if (result < 0)
        cout << "NA" << endl;
      else
        cout << result << endl;
    }
  }
}