Problem p00668 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX

int n, ln;

int bn = 0;
int non = 0;
int bit[400005] = {};
vector<int> dats;

map<int, int> poss;

int bget(int p) {
  int res = 0;
  while (p) {
    res += bit[p];
    p -= (p & (-p));
  }
  return res;
}

void bset(int p, int q) { // 1.indexd
  while (p <= bn) {
    bit[p] += q;
    p += (p & (-p));
  }
}

int ordtopos(int p) {
  int l = 0, r = dats.size();
  r--;

  while (l + 1 < r) {
    int tyu = (l + r) / 2;
    int no = bget(tyu);
    if (no >= p)
      r = tyu;
    else
      l = tyu;
  }
  return r;
}

void enkanbypos(int p) {
  bset(p, -1);
  non--;
}

int main(void) {
  /*
  bn = 10000;
  bset(1,1);
  bset(2,3);
  printf("%d\n",bget(2));
  printf("%d\n",bget(1));
  bset(1,-5);
  printf("%d\n",bget(2));
  printf("%d\n",bget(1));
  */

  while (1) {
    scanf("%d%d", &n, &ln);
    if (n == 0)
      break;
    poss.clear();
    dats.clear();
    memset(bit, 0, sizeof(bit));
    bn = 1;
    non = 0;
    while (bn <= n + 5)
      bn *= 2;
    dats.push_back(-10000);

    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b); // input .. 1.indexed
      // printf("%d %d\n",a,b);
      switch (a) {
      case 0: {
        int no = ((int)dats.size());
        poss[b] = no;
        dats.push_back(b);
        bset(no, 1);
        non++;
        if (non > ln)
          enkanbypos(ordtopos(1));
      } break;
      case 1:
        enkanbypos(ordtopos(b));
        break;
      case 2:
        printf("%d\n", dats[ordtopos(b)]);
        break;
      case 3:
        enkanbypos(poss[b]);
        break;
      }
      /*
      if(a==0){
              printf("ordpos .. %d is %d\n",1,ordtopos(1));
      }
      else if(a==1 || a==2){
              printf("ordpos .. %d is %d\n",b,ordtopos(b));
      }
      printf("non .. %d\n",non);
      rep(k,dats.size()){
              printf("(%d %d) ",dats[k],bget(k));
      }
      printf("\n\n");*/
    }
    printf("end\n");
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reg(i, a, b) for (int i = (a); i <= (b); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = (b); i >= (a); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX

int n, ln;

int bn = 0;
int non = 0;
int bit[800005] = {};
vector<int> dats;

map<int, int> poss;

int bget(int p) {
  int res = 0;
  while (p) {
    res += bit[p];
    p -= (p & (-p));
  }
  return res;
}

void bset(int p, int q) { // 1.indexd
  while (p <= bn) {
    bit[p] += q;
    p += (p & (-p));
  }
}

int ordtopos(int p) {
  int l = 0, r = dats.size();
  r--;

  while (l + 1 < r) {
    int tyu = (l + r) / 2;
    int no = bget(tyu);
    if (no >= p)
      r = tyu;
    else
      l = tyu;
  }
  return r;
}

void enkanbypos(int p) {
  bset(p, -1);
  non--;
}

int main(void) {
  /*
  bn = 10000;
  bset(1,1);
  bset(2,3);
  printf("%d\n",bget(2));
  printf("%d\n",bget(1));
  bset(1,-5);
  printf("%d\n",bget(2));
  printf("%d\n",bget(1));
  */

  while (1) {
    scanf("%d%d", &n, &ln);
    if (n == 0)
      break;
    poss.clear();
    dats.clear();
    memset(bit, 0, sizeof(bit));
    bn = 1;
    non = 0;
    while (bn <= n + 5)
      bn *= 2;
    dats.push_back(-10000);

    rep(i, n) {
      int a, b;
      scanf("%d%d", &a, &b); // input .. 1.indexed
      // printf("%d %d\n",a,b);
      switch (a) {
      case 0: {
        int no = ((int)dats.size());
        poss[b] = no;
        dats.push_back(b);
        bset(no, 1);
        non++;
        if (non > ln)
          enkanbypos(ordtopos(1));
      } break;
      case 1:
        enkanbypos(ordtopos(b));
        break;
      case 2:
        printf("%d\n", dats[ordtopos(b)]);
        break;
      case 3:
        enkanbypos(poss[b]);
        break;
      }
      /*
      if(a==0){
              printf("ordpos .. %d is %d\n",1,ordtopos(1));
      }
      else if(a==1 || a==2){
              printf("ordpos .. %d is %d\n",b,ordtopos(b));
      }
      printf("non .. %d\n",non);
      rep(k,dats.size()){
              printf("(%d %d) ",dats[k],bget(k));
      }
      printf("\n\n");*/
    }
    printf("end\n");
  }

  return 0;
}