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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int n, q, s[1000000], t[1000000];
multiset<int> leader;

bool Check(ll r, int x) {
  int cnt = 0, tail = 0;
  for (auto it = leader.begin(); it != leader.end(); ++it) {
    cnt += max(0, int(lower_bound(t, t + n, *it - r) - t) - tail);
    tail = upper_bound(t, t + n, *it) - t;
  }

  return x >= cnt + n - tail;
}

void Solve() {
  cin >> n >> q;
  ll maxi = 0;
  rep(i, 0, n) {
    cin >> s[i];
    maxi = max(maxi, (ll)s[i]);
    t[i] = s[i];
  }

  sort(t, t + n);

  rep(i, 0, q) {
    string query;
    int a;
    cin >> query >> a;

    if (query == "ADD") {
      --a;
      leader.insert(s[a]);
    } else if (query == "REMOVE") {
      --a;
      leader.erase(leader.find(s[a]));
    } else {
      ll lb = 0, ub = maxi + 1;
      while (ub - lb > 1) {
        ll m = (lb + ub) / 2;
        if (Check(m, a))
          ub = m;
        else
          lb = m;
      }
      if (Check(lb, a))
        ub = lb;

      if (ub == maxi + 1)
        cout << "NA" << endl;
      else
        cout << ub << endl;
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

// map<int,int> leader,normal;
int n, q, s[1000000];
multiset<int> leader, normal;

bool Check(ll r, int x) {
  // map<int,int> tmp=normal;
  // for(auto it=leader.begin(); it!=leader.end(); ++it){
  // tmp.erase(tmp.lower_bound(it->first-r),tmp.upper_bound(it->first));
  //}

  // int cnt=0;
  // for(auto it=tmp.begin(); it!=tmp.end(); ++it) cnt+=it->second;

  // return x>=cnt;

  multiset<int> tmp = normal;
  for (auto it = leader.begin(); it != leader.end(); ++it) {
    tmp.erase(tmp.lower_bound(*it - r), tmp.upper_bound(*it));
  }

  return x >= tmp.size();
}

void Solve() {
  cin >> n >> q;
  ll maxi = 0;
  rep(i, 0, n) {
    cin >> s[i];
    maxi = max(maxi, (ll)s[i]);
    //++normal[s[i]];
    normal.insert(s[i]);
  }

  rep(i, 0, q) {
    string query;
    int a;
    cin >> query >> a;

    if (query == "ADD") {
      --a;
      //++leader[s[a]];
      //--normal[s[a]]; if(normal[s[a]]==0) normal.erase(s[a]);
      leader.insert(s[a]);
    } else if (query == "REMOVE") {
      --a;
      //--leader[s[a]]; if(leader[s[a]]==0) leader.erase(s[a]);
      //++normal[s[a]];
      leader.erase(leader.find(s[a]));
    } else {
      ll lb = 0, ub = maxi + 1;
      while (ub - lb > 1) {
        ll m = (lb + ub) / 2;
        if (Check(m, a))
          ub = m;
        else
          lb = m;
      }
      if (Check(lb, a))
        ub = lb;

      if (ub == maxi + 1)
        cout << "NA" << endl;
      else
        cout << ub << endl;
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}