Problem p04052 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch)
    ch = getchar();
  while ('0' <= ch && ch <= '9')
    x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}

const int N = 500009;

typedef set<int>::iterator it;

int n, k;
int p[N], q[N], deg[N];
set<int> front, back;
vector<int> g[N];

int main() {
  freopen("permutation.in", "r", stdin);
  freopen("permutation.out", "w", stdout);

  n = read();
  k = read();
  for (int i = 1; i <= n; i++)
    q[p[i] = read()] = i;

  for (int i = 1; i < k; i++)
    front.insert(p[i]);

  for (int i = 1; i <= n; i++) {
    if (i)
      front.erase(p[i - 1]);
    if (i + k - 1 <= n)
      front.insert(p[i + k - 1]);

    back.insert(p[i]);
    if (i - k >= 1)
      back.erase(p[i - k]);

    it tmp = front.upper_bound(p[i]);
    if (tmp != front.end())
      g[q[(*tmp)]].push_back(i), deg[i]++;
    tmp = back.upper_bound(p[i]);
    if (tmp != back.end())
      g[q[(*tmp)]].push_back(i), deg[i]++;
  }

  front.clear();
  for (int i = 1; i <= n; i++)
    if (!deg[i])
      front.insert(-i);

  int top = 0;
  while (!front.empty()) {
    int u = -(*front.begin());
    p[++top] = u;
    front.erase(front.begin());
    for (int i = 0, e = g[u].size(); i < e; i++)
      if (--deg[g[u][i]] == 0)
        front.insert(-g[u][i]);
  }

  for (int i = 1; i <= n; i++)
    q[p[i]] = n - i + 1;
  for (int i = 1; i <= n; i++)
    printf("%d\n", q[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch)
    ch = getchar();
  while ('0' <= ch && ch <= '9')
    x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}

const int N = 500009;

typedef set<int>::iterator it;

int n, k;
int p[N], q[N], deg[N];
set<int> front, back;
vector<int> g[N];

int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; i++)
    q[p[i] = read()] = i;

  for (int i = 1; i < k; i++)
    front.insert(p[i]);

  for (int i = 1; i <= n; i++) {
    if (i)
      front.erase(p[i - 1]);
    if (i + k - 1 <= n)
      front.insert(p[i + k - 1]);

    back.insert(p[i]);
    if (i - k >= 1)
      back.erase(p[i - k]);

    it tmp = front.upper_bound(p[i]);
    if (tmp != front.end())
      g[q[(*tmp)]].push_back(i), deg[i]++;
    tmp = back.upper_bound(p[i]);
    if (tmp != back.end())
      g[q[(*tmp)]].push_back(i), deg[i]++;
  }

  front.clear();
  for (int i = 1; i <= n; i++)
    if (!deg[i])
      front.insert(-i);

  int top = 0;
  while (!front.empty()) {
    int u = -(*front.begin());
    p[++top] = u;
    front.erase(front.begin());
    for (int i = 0, e = g[u].size(); i < e; i++)
      if (--deg[g[u][i]] == 0)
        front.insert(-g[u][i]);
  }

  for (int i = 1; i <= n; i++)
    q[p[i]] = n - i + 1;
  for (int i = 1; i <= n; i++)
    printf("%d\n", q[i]);
  return 0;
}