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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, Q, t[1000000], w[1000000], x;
string s;
vector<int> leader, ls;
int query(int x1, int x2) {
  return lower_bound(w, w + N, x2) - lower_bound(w, w + N, x1);
}
int check(int v) {
  int ret = 0;
  for (int i = 0; i < (int)ls.size() - 1; i++)
    ret += query(max(ls[i] + 1, ls[i + 1] - v), ls[i + 1] + 1);
  return ret + (ls.size() ? query(ls[0] - v, ls[0] + 1) : 0);
}
int main() {
  scanf("%d%d", &N, &Q);
  for (int i = 0; i < N; i++)
    scanf("%d", &t[i]), w[i] = t[i];
  sort(w, w + N);
  for (int i = 0; i < Q; i++) {
    cin >> s >> x;
    if (s == "ADD")
      leader.push_back(x - 1);
    if (s == "REMOVE") {
      for (int j = 0; j < leader.size(); j++) {
        if (leader[j] == x - 1)
          leader.erase(leader.begin() + j);
      }
    }
    if (s == "CHECK") {
      int l = -1, r = 1000000001;
      ls.clear();
      for (int j = 0; j < leader.size(); j++)
        ls.push_back(t[leader[j]]);
      sort(ls.begin(), ls.end());
      while (r - l > 1) {
        int m = (r + l) / 2, u = check(m);
        if (u >= N - x)
          r = m;
        else
          l = m;
      }
      if (r == 1000000001)
        printf("NA\\n");
      else
        printf("%d\\n", r);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int N, Q, t[1000000], x;
string s;
vector<int> leader, ls, seg[2097152];
int query(int a, int b, int x1, int x2, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return lower_bound(seg[k].begin(), seg[k].end(), x2) -
           lower_bound(seg[k].begin(), seg[k].end(), x1);
  int lc = query(a, b, x1, x2, 2 * k, l, (l + r) / 2);
  int rc = query(a, b, x1, x2, 2 * k + 1, (l + r) / 2, r);
  return lc + rc;
}
int check(int v) {
  int ret = 0;
  for (int i = 0; i < (int)ls.size() - 1; i++)
    ret += query(0, N, max(ls[i] + 1, ls[i + 1] - v), ls[i + 1] + 1, 1, 0,
                 1048576);
  return ret +
         (ls.size() ? query(0, N, ls[0] - v, ls[0] + 1, 1, 0, 1048576) : 0);
}
int main() {
  scanf("%d%d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &t[i]);
    int u = i + 1048576;
    while (u > 0)
      seg[u].push_back(t[i]), u >>= 1;
  }
  for (int i = 1; i < 2097152; i++)
    sort(seg[i].begin(), seg[i].end());
  for (int i = 0; i < Q; i++) {
    cin >> s >> x;
    if (s == "ADD")
      leader.push_back(x - 1);
    if (s == "REMOVE") {
      for (int j = 0; j < leader.size(); j++) {
        if (leader[j] == x - 1)
          leader.erase(leader.begin() + j);
      }
    }
    if (s == "CHECK") {
      int l = -1, r = 1000000001;
      ls.clear();
      for (int j = 0; j < leader.size(); j++)
        ls.push_back(t[leader[j]]);
      sort(ls.begin(), ls.end());
      while (r - l > 1) {
        int m = (r + l) / 2, u = check(m);
        if (u >= N - x)
          r = m;
        else
          l = m;
      }
      if (r == 1000000001)
        printf("NA\\n");
      else
        printf("%d\\n", r);
    }
  }
  return 0;
}