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

#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 + 10;
const ll infll = (ll)1e+17 + 10;

int n, q;
int score[(int)1e+7 + 1];
std::vector<int> v;
std::multiset<int> leader;

int main() {
  std::cin >> n >> q;
  rep(i, n) std::cin >> score[i], v.push_back(score[i]);
  std::sort(v.begin(), v.end());
  rep(i, q) {
    char que[10];
    int a;
    std::cin >> que >> a;
    if (que[0] == 'A')
      leader.insert(score[a - 1]);
    else if (que[0] == 'R')
      leader.erase(leader.lower_bound(score[a - 1]));
    else {
      int ans = -1;
      int l = 0, r = infi;
      while (l != r) {
        int m = (l + r) / 2, fail = 0, prev = 0;
        for (std::multiset<int>::iterator it = leader.begin();
             it != leader.end(); ++it) {
          int pos = std::lower_bound(v.begin(), v.end(), *it - m) - v.begin();
          fail += std::max(pos - prev, 0);
          prev = std::upper_bound(v.begin(), v.end(), *it) - v.begin();
        }
        fail += std::max(n - prev, 0);
        if (fail <= a)
          ans = m, r = m;
        else
          l = m + 1;
      }
      if (ans == -1)
        std::cout << "NA" << std::endl;
      else
        std::cout << ans << std::endl;
    }
  }
  return 0;
}

Error: TLE

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 infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

int n, q;
int score[(int)1e+6 + 1];
std::multiset<int> v, leader;

int main() {
  std::cin >> n >> q;
  rep(i, n) std::cin >> score[i], v.insert(score[i]);
  rep(i, q) {
    char que[10];
    int a;
    std::cin >> que >> a;
    if (que[0] == 'A')
      leader.insert(score[a - 1]);
    else if (que[0] == 'R')
      leader.erase(leader.lower_bound(score[a - 1]));
    else {
      int ans = -1;
      int l = 0, r = infi;
      while (l != r) {
        int m = (l + r) / 2, fail = 0, prev = 0;
        for (auto it = leader.begin(); it != leader.end(); ++it) {
          int pos = std::distance(v.begin(), v.lower_bound(*it - m));
          fail += std::max(pos - prev, 0);
          prev = std::distance(v.begin(), v.upper_bound(*it));
        }
        fail += std::max(n - prev, 0);
        if (fail <= a)
          ans = m, r = m;
        else
          l = m + 1;
      }
      if (ans == -1)
        std::cout << "NA" << std::endl;
      else
        std::cout << ans << std::endl;
    }
  }
  return 0;
}