Problem p00668 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX_N = 1000000;

int num[MAX_N * 2 - 1];
int dat[MAX_N];

struct SegTree {
  int n;
  int size, lim;
  map<int, int> mp;
  SegTree(int _n, int lim) : lim(lim) {
    for (n = 1; n < _n; n *= 2)
      ;
    fill(num, num + 2 * n - 1, 0);
    fill(dat, dat + n, -1);
    size = 0;
  }
  void add(int a) {
    int k = size++;
    dat[k] = a;
    mp[a] = k;
    k += n - 1;
    while (1) {
      num[k]++;
      if (k == 0)
        break;
      k = (k - 1) / 2;
    }
    lim--;
    if (lim < 0) {
      del2(0);
    }
  }
  void del2(
      int index) { // cÁÄ¢ézñÌindexðó¯æÁÄÁ
    del(q(0, index));
  }
  void del(int k) { // datÅÌindexðó¯æÁÄÁ
    dat[k] = -1;
    k += n - 1;
    while (1) {
      num[k]--;
      if (k == 0)
        break;
      k = (k - 1) / 2;
    }
    lim++;
  }
  int query(
      int index) { // cÁÄ¢ézñÌindexÔÚÌf[^ðæ¾
    return dat[q(0, index)];
  }
  int q(
      int a,
      int index) { // cÁÄ¢éÅindexÔÚÌf[^ÌdatÅÌindexðæ¾
    if (a >= n - 1)
      return a - n + 1;
    if (index < num[a * 2 + 1])
      return q(a * 2 + 1, index);
    return q(a * 2 + 2, index - num[a * 2 + 1]);
  }
  void print() {
    REP(i, n) { cout << dat[i] << " "; }
    cout << endl;
    REP(i, n * 2 - 1)
    cout << num[i] << " ";
    cout << endl;
  }
};

int main() {
  int q, lim;
  while (cin >> q >> lim, q || lim) {
    SegTree seg(400000 + 2, lim);
    REP(i, q) {
      int query, x;
      cin >> query >> x;
      if (query == 0) {
        seg.add(x);
      } else if (query == 1) {
        seg.del2(x - 1);
      } else if (query == 2) {
        cout << seg.query(x - 1) << endl;
      } else {
        seg.del(seg.mp[x]);
      }
      // cout << "i = " << i << endl;
      // seg.print();
    }
    cout << "end" << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

const int MAX_N = 1 << 17;

struct SegTree {
  int n;
  int num[MAX_N * 2 - 1];
  int dat[MAX_N];
  int size, lim;
  map<int, int> mp;
  SegTree(int _n, int lim) : lim(lim) {
    for (n = 1; n < _n; n *= 2)
      ;
    fill(num, num + 2 * n - 1, 0);
    fill(dat, dat + n, -1);
    size = 0;
  }
  void add(int a) {
    int k = size++;
    dat[k] = a;
    mp[a] = k;
    k += n - 1;
    while (1) {
      num[k]++;
      if (k == 0)
        break;
      k = (k - 1) / 2;
    }
    lim--;
    if (lim < 0) {
      del2(0);
    }
  }
  void del2(
      int index) { // cÁÄ¢ézñÌindexðó¯æÁÄÁ
    del(q(0, index));
  }
  void del(int k) { // datÅÌindexðó¯æÁÄÁ
    dat[k] = -1;
    k += n - 1;
    while (1) {
      num[k]--;
      if (k == 0)
        break;
      k = (k - 1) / 2;
    }
    lim++;
  }
  int query(
      int index) { // cÁÄ¢ézñÌindexÔÚÌf[^ðæ¾
    return dat[q(0, index)];
  }
  int q(
      int a,
      int index) { // cÁÄ¢éÅindexÔÚÌf[^ÌdatÅÌindexðæ¾
    if (a >= n - 1)
      return a - n + 1;
    if (index < num[a * 2 + 1])
      return q(a * 2 + 1, index);
    return q(a * 2 + 2, index - num[a * 2 + 1]);
  }
  void print() {
    REP(i, n) { cout << dat[i] << " "; }
    cout << endl;
    REP(i, n * 2 - 1)
    cout << num[i] << " ";
    cout << endl;
  }
};

int main() {
  int q, lim;
  while (cin >> q >> lim, q || lim) {
    SegTree seg(q + 2, lim);
    REP(i, q) {
      int query, x;
      cin >> query >> x;
      if (query == 0) {
        seg.add(x);
      } else if (query == 1) {
        seg.del2(x - 1);
      } else if (query == 2) {
        cout << seg.query(x - 1) << endl;
      } else {
        seg.del(seg.mp[x]);
      }
      // cout << "i = " << i << endl;
      // seg.print();
    }
    cout << "end" << endl;
  }
}