Problem p03531 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define N 500005
#define M 8000005

#define ls (t << 1)
#define rs ((t << 1) | 1)
#define mid ((l + r) >> 1)

#define mk make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

int i, j, m, n, p, k, A[N], L[N], R[N], Q[N], B[N];

vector<pair<int, int>> v[N];

set<pair<int, int>> st;

int main() {
  scanf("%d", &n);
  m = 1 << n;
  for (i = 0; i < m; ++i)
    scanf("%d", &A[i]);
  for (i = m - 1; i >= 0; --i) {
    R[i] = m;
    if (A[i])
      R[i] = A[i];
    for (j = 0; j < n; ++j)
      if ((i ^ (1 << j)) > i)
        R[i] = min(R[i], R[i ^ (1 << j)]);
  }
  for (i = 0; i < m; ++i) {
    L[i] = 1;
    if (A[i])
      L[i] = A[i];
    for (j = 0; j < n; ++j)
      if ((i ^ (1 << j)) < i)
        L[i] = max(L[i], L[i ^ (1 << j)]);
  }
  for (i = 0; i < m; ++i)
    v[L[i]].pb(mk(R[i], i));
  for (i = 1; i <= m; ++i) {
    for (j = 0; j < (int)v[i].size(); ++j)
      st.insert(v[i][j]);
    if (!st.size() || (*st.begin()).fi < i) {
      puts("NO");
      return 0;
    }
    A[(*st.begin()).se] = i;
    st.erase(st.begin());
  }
  puts("YES");
  Q[Q[0] = 1] = 0;
  for (i = 0; i < n; ++i) {
    for (j = 1; j <= (1 << i); ++j)
      Q[++Q[0]] = Q[j] + (1 << (n - 1 - i));
  }
  for (i = 1; i <= m; ++i)
    B[Q[i]] = A[i - 1];
  for (i = 0; i < m; ++i)
    printf("%d ", B[i]);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define N 300005
#define M 19

#define ls (t << 1)
#define rs ((t << 1) | 1)
#define mid ((l + r) >> 1)

#define mk make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

int i, j, m, n, p, k, r, A[1 << M], vis[(1 << M) + 5], Q[(1 << M) + 5],
    deg[1 << M], que[(1 << M) + 5], Qt[(1 << M) + 5], B[1 << M], G[1 << M],
    H[1 << M], F[1 << M];

vector<int> ve[1 << M];

inline int cmp(int a, int b) { return A[a] < A[b]; }

inline int cmp2(int a, int b) {
  if (G[a] != G[b])
    return G[a] < G[b];
  return H[a] > H[b];
}

int ojbk(int x) {
  que[que[0] = 1] = x;
  int i, l;
  for (l = 1; l <= que[0]; ++l) {
    int p = que[l];
    for (i = 0; i < (int)ve[p].size(); ++i) {
      int k = ve[p][i];
      if (A[k]) {
        if (A[k] >= A[x])
          return 0;
      } else
        que[++que[0]] = k, A[k] = -1;
    }
  }
  for (l = 2; l <= que[0]; ++l) {
    int p = que[l];
    for (j = 0; j < (int)ve[p].size(); ++j) {
      int k = ve[p][j];
      if (A[k] == -1)
        deg[k]++;
    }
  }
  Qt[0] = 0;
  for (l = 2; l <= que[0]; ++l) {
    int p = que[l];
    if (!deg[p])
      Qt[++Qt[0]] = p;
  }
  for (l = 1; l <= Qt[0]; ++l) {
    int p = Qt[l];
    for (i = 0; i < (int)ve[p].size(); ++i) {
      int k = ve[p][i];
      if (A[k] == -1) {
        deg[k]--;
        if (!deg[k])
          Qt[++Qt[0]] = k;
      }
    }
  }
  for (i = Qt[0]; i >= 1; --i) {
    int p = Qt[i];
    H[Qt[i]] = i;
    for (j = 0; j < (int)ve[p].size(); ++j) {
      int k = ve[p][j];
      G[p] = max(G[p], G[k]);
    }
  }
  sort(Qt + 1, Qt + Qt[0] + 1, cmp2);
  F[0] = Qt[0] + 1;
  for (i = 1; i <= Qt[0]; ++i)
    F[i + 1] = Qt[i];
  F[1] = x;
  for (i = 1; i <= Qt[0]; ++i) {
    int p = Qt[i];
    for (; vis[r]; ++r)
      ;
    if (r > A[x] || r <= G[p])
      return 0;
    A[p] = r;
    vis[r] = 1;
  }
  for (i = F[0]; i; --i) {
    int p = F[i];
    G[p] = A[p];
    for (j = 0; j < (int)ve[p].size(); ++j) {
      int k = ve[p][j];
      G[p] = max(G[p], G[k]);
    }
  }
  return 1;
}
int ojbkall() {
  int i, l;
  que[0] = 0;
  for (i = 0; i < m; ++i)
    if (!A[i])
      que[++que[0]] = i;
  for (l = 1; l <= que[0]; ++l) {
    int p = que[l];
    for (j = 0; j < (int)ve[p].size(); ++j) {
      int k = ve[p][j];
      if (A[k] == 0)
        deg[k]++;
    }
  }
  Qt[0] = 0;
  for (l = 1; l <= que[0]; ++l) {
    int p = que[l];
    if (!deg[p])
      Qt[++Qt[0]] = p;
  }
  for (l = 1; l <= Qt[0]; ++l) {
    int p = Qt[l];
    for (i = 0; i < (int)ve[p].size(); ++i) {
      int k = ve[p][i];
      if (A[k] == 0) {
        deg[k]--;
        if (!deg[k])
          Qt[++Qt[0]] = k;
      }
    }
  }
  for (i = 1; i <= Qt[0]; ++i)
    H[Qt[i]] = i;
  for (i = 0; i < m; ++i)
    G[i] = A[i];
  for (i = 0; i < m; ++i) {
    int p = i;
    for (j = 0; j < (int)ve[p].size(); ++j)
      G[p] = max(G[p], G[ve[p][j]]);
  }
  sort(Qt + 1, Qt + Qt[0] + 1, cmp2);
  for (i = 1; i <= Qt[0]; ++i) {
    int p = Qt[i];
    for (; vis[r]; ++r)
      ;
    if (G[p] >= r)
      return 0;
    A[p] = r;
    vis[r] = 1;
  }
  return 1;
}

int main() {
  scanf("%d", &n);
  m = 1 << n;
  for (i = 0; i < m; ++i) {
    scanf("%d", &A[i]);
    if (A[i])
      vis[A[i]] = 1, Q[++Q[0]] = i, G[i] = A[i];
  }
  r = 1;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      int p = j ^ (1 << i);
      if (p < j)
        ve[j].pb(p);
    }
  }
  sort(Q + 1, Q + Q[0] + 1, cmp);
  for (i = 1; i <= Q[0]; ++i) {
    if (!ojbk(Q[i])) {
      puts("NO");
      for (;;)
        ;
      return 0;
    }
  }
  //		for (i=0;i<n;++i)
  //		{
  //				for (j=0;j<m;++j)
  //				{
  //						int p=j^(1<<i);
  //						if (p<j&&A[p]&&A[j]&&A[p]>=A[j]) for
  //(;;);
  //				}
  //		}
  if (!ojbkall()) {
    puts("NO");
    for (;;)
      ;
    return 0;
  }
  printf("YES\\n");
  //		for (;vis[r];++r);
  //		if (r!=(1<<n)+1) for (;;);
  /*
                  for (i=0;i<n;++i)
                  {
                                  for (j=0;j<m;++j)
                                  {
                                                  int p=j^(1<<i);
                                                  if (p<j&&A[p]>=A[j]) for (;;);
                                  }
                  }
                  */
  //		sort(A,A+m);
  //		for (i=0;i<m-1;++i) if (A[i]==A[i+1]) for (;;);
  Q[Q[0] = 1] = 0;
  for (i = 0; i < n; ++i) {
    for (j = 1; j <= (1 << i); ++j)
      Q[++Q[0]] = Q[j] + (1 << (n - 1 - i));
  }
  for (i = 1; i <= m; ++i)
    B[Q[i]] = A[i - 1];
  for (i = 0; i < m; ++i)
    printf("%d ", B[i]);
}