Problem p04052 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define REP(i, x, y) for (int i = (int)x; i <= (int)y; i++)
#define FOR(i, x, y) for (int i = (int)x; i < (int)y; i++)
#define PER(i, x, y) for (int i = (int)x; i >= (int)y; i--)
#define trace(x) cerr << #x << " " << x << endl;
#define dprintf(...) fprintf(stderr, __VA__ARGS__)
#define dln() fprintf(stderr, "\n")
using namespace std;
typedef long long LL;
typedef long double db;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
const int N = 500005;
const int P = 1e9 + 7;
const int inf = 1e9;
const LL Inf = 1e15;

inline int IN() {
  char ch = getchar();
  int x = 0, f = 0;
  while (ch < '0' || ch > '9')
    ch = getchar(), f = (ch == '-');
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - 48;
    ch = getchar();
  }
  return f ? (-x) : x;
}

inline int Pow(int x, int y, int p) {
  int an = 1;
  for (; y; y >>= 1, x = (LL)x * x % p)
    if (y & 1)
      an = (LL)an * x % p;
  return an;
}

void renew(int &x, int y) {
  x += y;
  if (x < 0)
    x += P;
  else if (x >= P)
    x -= P;
}

template <typename T> inline void chkmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <typename T> inline void chkmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int deg[N];
int n, k, a[N], b[N];
VI e[N];

struct seg {
  int sum[N << 2];

  int ql, qr, qx, qv;

#define ls ((x) << 1)
#define rs ((ls) | 1)
#define md (((L) + (R)) >> 1)

  void M(int x, int L, int R) {
    if (L == R) {
      sum[x] = 1;
      return;
    }
    if (qx <= md)
      M(ls, L, md);
    else
      M(rs, md + 1, R);
    sum[x] = sum[ls] + sum[rs];
  }

  void D(int x, int L, int R) {
    if (!sum[x])
      return;
    if (L == R) {
      e[L].pb(qv);
      deg[qv]++;
      sum[x] = 0;
      return;
    }
    if (ql <= L && R <= qr) {
      D(ls, L, md);
      D(rs, md + 1, R);
      return;
    }
    if (ql <= md)
      D(ls, L, md);
    if (md < qr)
      D(rs, md + 1, R);
    sum[x] = sum[ls] + sum[rs];
  }

  void Modify(int x) {
    qx = x;
    M(1, 1, n);
  }
  void Delete(int L, int R, int v) {
    if (L > R)
      return;
    ql = L;
    qr = R;
    qv = v;
    D(1, 1, n);
  }
} L, R;

set<int> fres;
int lab[N], tim;

int main() {
  scanf("%d%d", &n, &k);
  REP(i, 1, n) scanf("%d", a + i);
  REP(i, 1, n) b[a[i]] = i;
  REP(i, 1, n) {
    int x = b[i];
    L.Delete(max(1, x - k + 1), x - 1, x);
    R.Delete(x + 1, min(n, x + k - 1), x);
    L.Modify(x);
    R.Modify(x);
  }
  REP(i, 1, n) if (!deg[i]) fres.insert(i);

  while (!fres.empty()) {
    int x = *fres.begin();
    lab[x] = ++tim;
    fres.erase(x);
    for (int v : e[x]) {
      --deg[v];
      if (!deg[v])
        fres.insert(v);
    }
  }
  REP(i, 1, n) printf("%d\n", lab[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define REP(i, x, y) for (int i = (int)x; i <= (int)y; i++)
#define FOR(i, x, y) for (int i = (int)x; i < (int)y; i++)
#define PER(i, x, y) for (int i = (int)x; i >= (int)y; i--)
#define trace(x) cerr << #x << " " << x << endl;
#define dprintf(...) fprintf(stderr, __VA__ARGS__)
#define dln() fprintf(stderr, "\n")
using namespace std;
typedef long long LL;
typedef long double db;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
const int N = 500005;
const int P = 1e9 + 7;
const int inf = 1e9;
const LL Inf = 1e15;

inline int IN() {
  char ch = getchar();
  int x = 0, f = 0;
  while (ch < '0' || ch > '9')
    ch = getchar(), f = (ch == '-');
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - 48;
    ch = getchar();
  }
  return f ? (-x) : x;
}

inline int Pow(int x, int y, int p) {
  int an = 1;
  for (; y; y >>= 1, x = (LL)x * x % p)
    if (y & 1)
      an = (LL)an * x % p;
  return an;
}

void renew(int &x, int y) {
  x += y;
  if (x < 0)
    x += P;
  else if (x >= P)
    x -= P;
}

template <typename T> inline void chkmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <typename T> inline void chkmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int deg[N];
int n, k, a[N], b[N];
VI e[N];

struct seg {
  int sum[N << 2];

  int ql, qr, qx, qv;

#define ls ((x) << 1)
#define rs ((ls) | 1)
#define md (((L) + (R)) >> 1)

  void M(int x, int L, int R) {
    if (L == R) {
      sum[x] = 1;
      return;
    }
    if (qx <= md)
      M(ls, L, md);
    else
      M(rs, md + 1, R);
    sum[x] = sum[ls] + sum[rs];
  }

  void D(int x, int L, int R) {
    if (!sum[x])
      return;
    if (L == R) {
      e[L].pb(qv);
      deg[qv]++;
      sum[x] = 0;
      return;
    }
    if (ql <= L && R <= qr) {
      D(ls, L, md);
      D(rs, md + 1, R);
      sum[x] = sum[ls] + sum[rs];
      return;
    }
    if (ql <= md)
      D(ls, L, md);
    if (md < qr)
      D(rs, md + 1, R);
    sum[x] = sum[ls] + sum[rs];
  }

  void Modify(int x) {
    qx = x;
    M(1, 1, n);
  }
  void Delete(int L, int R, int v) {
    if (L > R)
      return;
    ql = L;
    qr = R;
    qv = v;
    D(1, 1, n);
  }
} L, R;

set<int> fres;
int lab[N], tim;

int main() {
  scanf("%d%d", &n, &k);
  REP(i, 1, n) scanf("%d", a + i);
  REP(i, 1, n) b[a[i]] = i;
  REP(i, 1, n) {
    int x = b[i];
    L.Delete(max(1, x - k + 1), x - 1, x);
    R.Delete(x + 1, min(n, x + k - 1), x);
    L.Modify(x);
    R.Modify(x);
  }
  REP(i, 1, n) if (!deg[i]) fres.insert(i);

  while (!fres.empty()) {
    int x = *fres.begin();
    lab[x] = ++tim;
    fres.erase(x);
    for (int v : e[x]) {
      --deg[v];
      if (!deg[v])
        fres.insert(v);
    }
  }
  REP(i, 1, n) printf("%d\n", lab[i]);
  return 0;
}