Problem p04052 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <vector>
using namespace std;

const int N = 500 * 1000 + 5;

int a[N];
int b[N];
// int outdeg[N];
int indeg[N];
// bool mark[N];
vector<int> vec;
vector<int> adj[N];
// vector <int> adjr[N];
set<int> s1;
set<int> s2;
set<int> s;
int ans[N];

/*void dfs(int v) {
        mark[v] = true;
        for (auto u : adj[v]) {
                if (!mark[u]) {
                        dfs(u);
                }
        }
        vec.push_back(v);
}*/

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]] = i;
  }
  //	if (n >= 5000) {
  //		int x = 0;
  //		n /= x;
  //	}
  s1.insert(-N);
  s1.insert(N);
  s2.insert(-N);
  s2.insert(N);
  /*	for (int i = 1; i <= n; i++) {
                  set <int> :: iterator it = s.lower_bound(b[i]);
                  int r = *it;
                  it--;
                  int l = *it;
                  cout << "73 " << b[i] << " " << l << " " << r << endl;
                  if (r - b[i] < k) {
                          adj[b[i]].push_back(r);
                  }
                  if (b[i] - l < k) {
                          adj[b[i]].push_back(l);
                  }
                  s.insert(b[i]);
          }*/
  for (int i = n; i; i--) {
    set<int>::iterator it = s1.lower_bound(b[i]);
    while (*it - b[i] < k) {
      //	cout << "71 " << b[i] << " " << *it << endl;
      adj[*it].push_back(b[i]);
      s1.erase(it);
      it = s1.lower_bound(b[i]);
    }
    it = s2.lower_bound(b[i]);
    it--;
    while (b[i] - *it < k) {
      //	cout << "72 " << b[i] << " " << *it << endl;
      adj[*it].push_back(b[i]);
      s2.erase(it);
      it = s2.lower_bound(b[i]);
      it--;
    }
    s1.insert(b[i]);
    s2.insert(b[i]);
  }
  while (s1.size()) {
    s1.erase(s1.begin());
  }
  while (s2.size()) {
    s2.erase(s2.begin());
  }
  /*	for (int i = 1; i <= n; i++) {
                  for (int j = i; j; j--) {
                          if (b[j] > b[i] && b[j] - b[i] < k) {
                                  adj[b[i]].push_back(b[j]);
                          //	break;
                          }
                  }
                  for (int j = i; j; j--) {
                          if (b[j] < b[i] && b[i] - b[j] < k) {
                                  adj[b[i]].push_back(b[j]);
                          //	break;
                          }
                  }
          }*/
  for (int i = 1; i <= n; i++) {
    //	outdeg[i] = adj[i].size();
    for (auto u : adj[i]) {
      //	cout << "31 " << i << " " << u << endl;
      //	adjr[u].push_back(i);
      indeg[u]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (indeg[i] == 0) {
      s.insert(-i);
    }
  }
  while (s.size()) {
    int v = -*s.begin();
    s.erase(s.begin());
    //	cout << "32 " << v << endl;
    for (auto u : adj[v]) {
      indeg[u]--;
      if (indeg[u] == 0) {
        s.insert(-u);
      }
    }
    vec.push_back(v);
  }
  /*	for (int i = 1; i <= n; i++) {
                  if (!mark[i]) {
                          dfs(i);
                  }
          }*/
  for (int i = n - 1; i >= 0; i--) {
    //	cout << vec[i] << endl;
    ans[vec[i]] = n - i;
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <set>
#include <vector>
using namespace std;

const int N = 1000 * 1000 + 5;

int a[N];
int b[N];
// int outdeg[N];
int indeg[N];
// bool mark[N];
vector<int> vec;
vector<int> adj[N];
// vector <int> adjr[N];
set<int> s1;
set<int> s2;
set<int> s;
int ans[N];

/*void dfs(int v) {
        mark[v] = true;
        for (auto u : adj[v]) {
                if (!mark[u]) {
                        dfs(u);
                }
        }
        vec.push_back(v);
}*/

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]] = i;
  }
  //	if (n >= 5000) {
  //		int x = 0;
  //		n /= x;
  //	}
  s1.insert(-N);
  s1.insert(N);
  s2.insert(-N);
  s2.insert(N);
  /*	for (int i = 1; i <= n; i++) {
                  set <int> :: iterator it = s.lower_bound(b[i]);
                  int r = *it;
                  it--;
                  int l = *it;
                  cout << "73 " << b[i] << " " << l << " " << r << endl;
                  if (r - b[i] < k) {
                          adj[b[i]].push_back(r);
                  }
                  if (b[i] - l < k) {
                          adj[b[i]].push_back(l);
                  }
                  s.insert(b[i]);
          }*/
  for (int i = n; i; i--) {
    set<int>::iterator it = s1.lower_bound(b[i]);
    while (*it - b[i] < k) {
      //	cout << "71 " << b[i] << " " << *it << endl;
      adj[*it].push_back(b[i]);
      s1.erase(it);
      it = s1.lower_bound(b[i]);
    }
    it = s2.lower_bound(b[i]);
    it--;
    while (b[i] - *it < k) {
      //	cout << "72 " << b[i] << " " << *it << endl;
      adj[*it].push_back(b[i]);
      s2.erase(it);
      it = s2.lower_bound(b[i]);
      it--;
    }
    s1.insert(b[i]);
    s2.insert(b[i]);
  }
  while (s1.size()) {
    s1.erase(s1.begin());
  }
  while (s2.size()) {
    s2.erase(s2.begin());
  }
  /*	for (int i = 1; i <= n; i++) {
                  for (int j = i; j; j--) {
                          if (b[j] > b[i] && b[j] - b[i] < k) {
                                  adj[b[i]].push_back(b[j]);
                          //	break;
                          }
                  }
                  for (int j = i; j; j--) {
                          if (b[j] < b[i] && b[i] - b[j] < k) {
                                  adj[b[i]].push_back(b[j]);
                          //	break;
                          }
                  }
          }*/
  for (int i = 1; i <= n; i++) {
    //	outdeg[i] = adj[i].size();
    for (auto u : adj[i]) {
      //	cout << "31 " << i << " " << u << endl;
      //	adjr[u].push_back(i);
      indeg[u]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (indeg[i] == 0) {
      s.insert(-i);
    }
  }
  while (s.size()) {
    int v = -*s.begin();
    s.erase(s.begin());
    //	cout << "32 " << v << endl;
    for (auto u : adj[v]) {
      indeg[u]--;
      if (indeg[u] == 0) {
        s.insert(-u);
      }
    }
    vec.push_back(v);
  }
  /*	for (int i = 1; i <= n; i++) {
                  if (!mark[i]) {
                          dfs(i);
                  }
          }*/
  for (int i = n - 1; i >= 0; i--) {
    //	cout << vec[i] << endl;
    ans[vec[i]] = n - i;
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}