Problem p04052 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE, LIE)                                                     \
  for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0) ? -(x) : x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif

void solve(vector<vector<int>> E, vector<int> &pos, vector<int> v,
           int pos_add) {
  int N = v.size();
  if (N == 0)
    return;
  vector<int> sub(N, 0);
  queue<int> q;
  q.push(0);
  vector<int> v1, v2;
  while (!q.empty()) {
    ALL_THE(E[q.front()], it) if (sub[*it] == 0) {
      sub[*it] = 1;
      q.push(*it);
    }
    q.pop();
  }
  for (int i = 1; i < N; i++) {
    if (sub[i] == 0)
      v2.push_back(v[i]);
    else
      v1.push_back(v[i]);
  }
  vector<int> num(N);
  int x1 = 0, x2 = 0;
  for (int i = 1; i < N; i++) {
    if (sub[i] == 0)
      num[i] = x2++;
    else
      num[i] = x1++;
  }
  vector<vector<int>> E1, E2;
  for (int i = 1; i < N; i++) {
    if (sub[i] == 1) {
      vector<int> w;
      for (int j = 0; j < (int)E[i].size(); j++)
        if (E[i][j] > 0 && sub[E[i][j]] == 1)
          w.push_back(num[E[i][j]]);
      E1.push_back(w);
    } else {
      vector<int> w;
      for (int j = 0; j < (int)E[i].size(); j++)
        if (E[i][j] > 0 && sub[E[i][j]] == 0)
          w.push_back(num[E[i][j]]);
      E2.push_back(w);
    }
  }
  pos[pos_add + v1.size()] = v[0];
  solve(E1, pos, v1, pos_add);
  solve(E2, pos, v2, pos_add + v1.size() + 1);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> lfte(N, -1), rte(N, -1);
  set<pair<int, int>> S;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      S.insert(make_pair(A[i - 1], i - 1));
    if (i >= K)
      S.erase(make_pair(A[i - K], i - K));
    auto it = S.lower_bound(make_pair(A[i], -1));
    if (it != S.begin()) {
      it--;
      lfte[i] = it->ss;
    }
  }
  S.clear();
  for (int i = N - 1; i >= 0; i--) {
    if (i < N - 1)
      S.insert(make_pair(A[i + 1], i + 1));
    if (i + K < N)
      S.erase(make_pair(A[i + K], i + K));
    auto it = S.lower_bound(make_pair(A[i], -1));
    if (it != S.begin()) {
      it--;
      rte[i] = it->ss;
    }
  }
  vector<vector<int>> E(N);
  for (int i = 0; i < N; i++) {
    //		cout << lfte[i] << " " << rte[i] << " " << i << "\n";
    if (lfte[i] != -1)
      E[i].push_back(lfte[i]);
    if (rte[i] != -1)
      E[i].push_back(rte[i]);
  }
  //	for(int i =0; i < N; i++) ALL_THE(E[i],it) cout << i << " " << *it <<
  //"\n";

  vector<int> pos(N), v;
  for (int i = 0; i < N; i++)
    v.push_back(i);
  solve(E, pos, v, 0);

  vector<int> ans(N);
  for (int i = 0; i < N; i++)
    ans[pos[i]] = i + 1;
  for (int i = 0; i < N; i++)
    cout << ans[i] << "\n";
  return 0;
}

// look at my code
// my code is amazing


Fixed submission:
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE, LIE)                                                     \
  for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0) ? -(x) : x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif

void solve(vector<vector<int>> E, vector<int> &pos, vector<int> v,
           int pos_add) {
  int N = v.size();
  if (N == 0)
    return;
  vector<int> sub(N, 0);
  queue<int> q;
  q.push(0);
  vector<int> v1, v2;
  while (!q.empty()) {
    ALL_THE(E[q.front()], it) if (sub[*it] == 0) {
      sub[*it] = 1;
      q.push(*it);
    }
    q.pop();
  }
  for (int i = 1; i < N; i++) {
    if (sub[i] == 0)
      v2.push_back(v[i]);
    else
      v1.push_back(v[i]);
  }
  vector<int> num(N);
  int x1 = 0, x2 = 0;
  for (int i = 1; i < N; i++) {
    if (sub[i] == 0)
      num[i] = x2++;
    else
      num[i] = x1++;
  }
  vector<vector<int>> E1, E2;
  for (int i = 1; i < N; i++) {
    if (sub[i] == 1) {
      vector<int> w;
      for (int j = 0; j < (int)E[i].size(); j++)
        if (E[i][j] > 0 && sub[E[i][j]] == 1)
          w.push_back(num[E[i][j]]);
      E1.push_back(w);
    } else {
      vector<int> w;
      for (int j = 0; j < (int)E[i].size(); j++)
        if (E[i][j] > 0 && sub[E[i][j]] == 0)
          w.push_back(num[E[i][j]]);
      E2.push_back(w);
    }
  }
  pos[pos_add + v1.size()] = v[0];
  solve(E1, pos, v1, pos_add);
  solve(E2, pos, v2, pos_add + v1.size() + 1);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<int> lfte(N, -1), rte(N, -1);
  set<pair<int, int>> S;
  for (int i = 0; i < N; i++) {
    if (i > 0)
      S.insert(make_pair(A[i - 1], i - 1));
    if (i >= K)
      S.erase(make_pair(A[i - K], i - K));
    auto it = S.lower_bound(make_pair(A[i], -1));
    if (it != S.begin()) {
      it--;
      lfte[i] = it->ss;
    }
  }
  S.clear();
  for (int i = N - 1; i >= 0; i--) {
    if (i < N - 1)
      S.insert(make_pair(A[i + 1], i + 1));
    if (i + K < N)
      S.erase(make_pair(A[i + K], i + K));
    auto it = S.lower_bound(make_pair(A[i], -1));
    if (it != S.begin()) {
      it--;
      rte[i] = it->ss;
    }
  }
  vector<vector<int>> E(N);
  for (int i = 0; i < N; i++) {
    //		cout << lfte[i] << " " << rte[i] << " " << i << "\n";
    if (lfte[i] != -1)
      E[i].push_back(lfte[i]);
    if (rte[i] != -1)
      E[i].push_back(rte[i]);
  }
  //	for(int i =0; i < N; i++) ALL_THE(E[i],it) cout << i << " " << *it <<
  //"\n";

  vector<int> pos(N), v;
  for (int i = 0; i < N; i++)
    v.push_back(i);
  //	solve(E,pos,v,0);

  vector<vector<int>> E2(N);
  vector<int> D(N, 0);
  for (int i = 0; i < N; i++) {
    D[i] = E[i].size();
    ALL_THE(E[i], it) E2[*it].push_back(i);
  }
  set<int> S2;
  for (int i = 0; i < N; i++)
    if (D[i] == 0)
      S2.insert(i);
  for (int i = 0; i < N; i++) {
    int x = *S2.begin();
    pos[i] = x;
    S2.erase(x);
    ALL_THE(E2[x], it) {
      D[*it]--;
      if (D[*it] == 0)
        S2.insert(*it);
    }
  }

  vector<int> ans(N);
  for (int i = 0; i < N; i++)
    ans[pos[i]] = i + 1;
  for (int i = 0; i < N; i++)
    cout << ans[i] << "\n";
  return 0;
}

// look at my code
// my code is amazing