Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int k, tot, n, m, p[100001], q[100001], l[400001], r[400001], f[400001],
    lz[400001], dbd, fir[100001], nex[100001], sto[100001], ru[100001];
void addbian(int aa, int bb) {
  tot++;
  nex[tot] = fir[aa];
  fir[aa] = tot;
  sto[tot] = bb;
}
int getmin(int aa, int bb) {
  if (aa < bb)
    return (aa);
  else
    return (bb);
}
void update(int num, int hh) {
  lz[num] = hh;
  f[num] = hh;
}
void built(int num, int x, int y) {
  l[num] = x;
  r[num] = y;
  f[num] = dbd;
  lz[num] = 0;
  if (x == y)
    return;
  int mid = (x + y) / 2;
  built(num * 2, x, mid);
  built(num * 2 + 1, mid + 1, y);
}
int ask(int num, int x) {
  if (l[num] == r[num])
    return (f[num]);
  int mid = (l[num] + r[num]) / 2;
  if (lz[num] > 0) {
    update(num * 2, lz[num]);
    update(num * 2 + 1, lz[num]);
    lz[num] = 0;
  }
  if (x <= mid)
    return (ask(num * 2, x));
  else
    return (ask(num * 2 + 1, x));
  f[num] = getmin(f[num * 2], f[num * 2 + 1]);
}
void gai(int num, int x, int y) {
  if ((x == l[num]) && (y == r[num])) {
    update(num, dbd);
    return;
  }
  int mid = (l[num] + r[num]) / 2;
  if (lz[num] > 0) {
    update(num * 2, lz[num]);
    update(num * 2 + 1, lz[num]);
    lz[num] = 0;
  }
  if (y <= mid)
    gai(num * 2, x, y);
  else {
    if (x > mid)
      gai(num * 2 + 1, x, y);
    else {
      gai(num * 2, x, mid);
      gai(num * 2 + 1, mid + 1, y);
    }
  }
  f[num] = getmin(f[num * 2], f[num * 2 + 1]);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    q[p[i]] = i;
  }
  dbd = 0;
  built(1, 1, n);
  for (int i = 1; i <= n; i++) {
    int bb = ask(1, q[i]);
    if (bb != 0) {
      addbian(bb, q[i]);
      ru[q[i]]++;
    }
    int li = 1, ri = q[i];
    if (q[i] - k + 1 > 1)
      li = q[i] - k + 1;
    dbd = q[i];
    gai(1, li, ri);
  }
  dbd = n + 1;
  built(1, 1, n);
  for (int i = 1; i <= n; i++)
    if (ru[i] == 0) {
      dbd = i;
      gai(1, i, i);
    }
  for (int i = 1; i <= n; i++) {
    int bb = fir[f[1]], cc = f[1];
    while (bb != 0) {
      ru[sto[bb]]--;
      if (ru[sto[bb]] == 0) {
        dbd = sto[bb];
        gai(1, dbd, dbd);
      }
      bb = nex[bb];
    }
    p[cc] = i;
    dbd = n + 1;
    gai(1, cc, cc);
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", p[i]);
}

Fixed submission:
#include <cstdio>
int k, tot, n, m, p[500001], q[500001], l[2000001], r[2000001], f[2000001],
    lz[2000001], dbd, fir[500001], nex[500001], sto[500001], ru[500001];
void addbian(int aa, int bb) {
  tot++;
  nex[tot] = fir[aa];
  fir[aa] = tot;
  sto[tot] = bb;
}
int getmin(int aa, int bb) {
  if (aa < bb)
    return (aa);
  else
    return (bb);
}
void update(int num, int hh) {
  lz[num] = hh;
  f[num] = hh;
}
void built(int num, int x, int y) {
  l[num] = x;
  r[num] = y;
  f[num] = dbd;
  lz[num] = 0;
  if (x == y)
    return;
  int mid = (x + y) / 2;
  built(num * 2, x, mid);
  built(num * 2 + 1, mid + 1, y);
}
int ask(int num, int x) {
  if (l[num] == r[num])
    return (f[num]);
  int mid = (l[num] + r[num]) / 2;
  if (lz[num] > 0) {
    update(num * 2, lz[num]);
    update(num * 2 + 1, lz[num]);
    lz[num] = 0;
  }
  if (x <= mid)
    return (ask(num * 2, x));
  else
    return (ask(num * 2 + 1, x));
  f[num] = getmin(f[num * 2], f[num * 2 + 1]);
}
void gai(int num, int x, int y) {
  if ((x == l[num]) && (y == r[num])) {
    update(num, dbd);
    return;
  }
  int mid = (l[num] + r[num]) / 2;
  if (lz[num] > 0) {
    update(num * 2, lz[num]);
    update(num * 2 + 1, lz[num]);
    lz[num] = 0;
  }
  if (y <= mid)
    gai(num * 2, x, y);
  else {
    if (x > mid)
      gai(num * 2 + 1, x, y);
    else {
      gai(num * 2, x, mid);
      gai(num * 2 + 1, mid + 1, y);
    }
  }
  f[num] = getmin(f[num * 2], f[num * 2 + 1]);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    q[p[i]] = i;
  }
  dbd = 0;
  built(1, 1, n);
  for (int i = 1; i <= n; i++) {
    int bb = ask(1, q[i]);
    if (bb != 0) {
      addbian(bb, q[i]);
      ru[q[i]]++;
    }
    int li = 1, ri = q[i];
    if (q[i] - k + 1 > 1)
      li = q[i] - k + 1;
    dbd = q[i];
    gai(1, li, ri);
  }
  dbd = n + 1;
  built(1, 1, n);
  for (int i = 1; i <= n; i++)
    if (ru[i] == 0) {
      dbd = i;
      gai(1, i, i);
    }
  for (int i = 1; i <= n; i++) {
    int bb = fir[f[1]], cc = f[1];
    while (bb != 0) {
      ru[sto[bb]]--;
      if (ru[sto[bb]] == 0) {
        dbd = sto[bb];
        gai(1, dbd, dbd);
      }
      bb = nex[bb];
    }
    p[cc] = i;
    dbd = n + 1;
    gai(1, cc, cc);
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", p[i]);
}