Problem p03771 (C++)

Buggy submission (status: Time Limit Exceeded):
// Heaplax
// 别让自己后悔
#include <bits/stdc++.h>
#define N 200001
#define LL long long
#define LOG(x) cerr << #x << " = " << x << endl
#define add_edge(u, v) nxt[++cnt] = head[u], head[u] = cnt, to[cnt] = v
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
char ch;
bool fs;
void re(int &x) {
  while (ch = getchar(), ch < 33)
    ;
  if (ch == '-')
    fs = 1, x = 0;
  else
    fs = 0, x = ch - 48;
  while (ch = getchar(), ch > 33)
    x = x * 10 + ch - 48;
  if (fs)
    x = -x;
}
using namespace std;
int n, cnt, v[N], x[N], tl[N][18], tr[N][18];
int fl[1 << 19], fr[1 << 19];
int main() {
  re(n), re(v[0]);
  while (v[cnt]) {
    ++cnt;
    v[cnt] = v[cnt - 1] >> 1;
  }
  for (int i = 1; i <= n; ++i)
    re(x[i]);
  for (int i = 0; i <= cnt; ++i) {
    int last = 1;
    for (int j = 1; j <= n; ++j)
      if (j == n || x[j + 1] - x[j] > v[i]) {
        for (int k = last; k <= j; ++k)
          tl[k][i] = last, tr[k][i] = j;
        last = j + 1;
      }
  }
  for (int i = 0; i < (1 << cnt + 1); ++i) {
    fl[i] = 0;
    fr[i] = n + 1;
    for (int j = 0; j <= cnt; ++j)
      if (i >> j & 1) {
        fl[i] = max(fl[i], tr[min(fl[i ^ (1 << j)] + 1, n)][j]);
        fr[i] = min(fr[i], tl[max(fr[i ^ (1 << j)] - 1, 1)][j]);
      }
  }
  int gg = 0, l = 1;
  while (l <= n) {
    int r = tr[l][0];
    if (gg > cnt) {
      for (int i = l; i <= r; ++i)
        puts("Impossible");
    } else {
      bool ok = 0;
      for (int i = 0; i < (1 << cnt + 1); ++i) {
        int gl = i, gr = ((1 << cnt + 1) - 1) ^ i;
        if (gl & 1)
          gl ^= 1;
        if (gr & 1)
          gr ^= 1;
        if (fl[gl] >= l - 1 && fr[gr] <= r + 1) {
          ok = 1;
          break;
        }
      }
      for (int i = l; i <= r; ++i)
        puts(ok ? "Possible" : "Impossible");
    }
    l = r + 1;
    ++gg;
  }
}


Fixed submission:
// Heaplax
// 别让自己后悔
#include <bits/stdc++.h>
#define N 200001
#define LL long long
#define LOG(x) cerr << #x << " = " << x << endl
#define add_edge(u, v) nxt[++cnt] = head[u], head[u] = cnt, to[cnt] = v
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
char ch;
bool fs;
void re(int &x) {
  while (ch = getchar(), ch < 33)
    ;
  if (ch == '-')
    fs = 1, x = 0;
  else
    fs = 0, x = ch - 48;
  while (ch = getchar(), ch > 33)
    x = x * 10 + ch - 48;
  if (fs)
    x = -x;
}
using namespace std;
int n, cnt, v[N], x[N], tl[N][19], tr[N][19];
int fl[1 << 19], fr[1 << 19];
int main() {
  re(n), re(v[0]);
  while (v[cnt]) {
    ++cnt;
    v[cnt] = v[cnt - 1] >> 1;
  }
  for (int i = 1; i <= n; ++i)
    re(x[i]);
  for (int i = 0; i <= cnt; ++i) {
    int last = 1;
    for (int j = 1; j <= n; ++j)
      if (j == n || x[j + 1] - x[j] > v[i]) {
        for (int k = last; k <= j; ++k)
          tl[k][i] = last, tr[k][i] = j;
        last = j + 1;
      }
  }
  for (int i = 0; i < (1 << cnt + 1); ++i) {
    fl[i] = 0;
    fr[i] = n + 1;
    for (int j = 0; j <= cnt; ++j)
      if (i >> j & 1) {
        fl[i] = max(fl[i], tr[min(fl[i ^ (1 << j)] + 1, n)][j]);
        fr[i] = min(fr[i], tl[max(fr[i ^ (1 << j)] - 1, 1)][j]);
      }
  }
  int gg = 0, l = 1;
  while (l <= n) {
    int r = tr[l][0];
    if (gg > cnt) {
      for (int i = l; i <= r; ++i)
        puts("Impossible");
    } else {
      bool ok = 0;
      for (int i = 0; i < (1 << cnt + 1); ++i) {
        int gl = i, gr = ((1 << cnt + 1) - 1) ^ i;
        if (gl & 1)
          gl ^= 1;
        if (gr & 1)
          gr ^= 1;
        if (fl[gl] >= l - 1 && fr[gr] <= r + 1) {
          ok = 1;
          break;
        }
      }
      for (int i = l; i <= r; ++i)
        puts(ok ? "Possible" : "Impossible");
    }
    l = r + 1;
    ++gg;
  }
}