prithivMLmods/Coder-Stat huggingface dataset:
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
// #include <unordered_set>
// #include <boost/container/static_vector.hpp>
// #include <boost/unordered_set.hpp>
// #include <boost/unordered_map.hpp>
// #include <unistd.h>

// #include <cv.h>
// #include <highgui.h>
#include <stdlib.h>
#include <time.h>

#include <string>

struct Node {
  int color;
  std::vector<int> neighbor;
  int lock_dist;
};

const int MAX_N = 100050;
int N, M, Q;
int a[MAX_N], b[MAX_N], v[MAX_N], d[MAX_N], c[MAX_N];

Node graph[MAX_N];

void dfs(int node, int color, int dist) {
  if (graph[node].color == 0) {
    graph[node].color = color;
  }

  if (dist <= graph[node].lock_dist) {
    return;
  }

  for (auto n : graph[node].neighbor) {
    dfs(n, color, dist - 1);
  }
}

int main(int argc, char **argv) {
  std::cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    std::cin >> a[i] >> b[i];
  }

  std::cin >> Q;

  for (int i = 1; i <= Q; i++) {
    std::cin >> v[i] >> d[i] >> c[i];
  }

  for (int i = 1; i <= N; i++) {
    graph[i].color = 0;
    graph[i].lock_dist = 0;
  }

  for (int i = 1; i <= M; i++) {
    graph[a[i]].neighbor.push_back(b[i]);
    graph[b[i]].neighbor.push_back(a[i]);
  }

  for (int i = Q; 1 <= i; i--) {
    dfs(v[i], c[i], d[i]);
  }

  for (int i = 1; i <= N; i++) {
    std::cout << graph[i].color << std::endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
// #include <unordered_set>
// #include <boost/container/static_vector.hpp>
// #include <boost/unordered_set.hpp>
// #include <boost/unordered_map.hpp>
// #include <unistd.h>

// #include <cv.h>
// #include <highgui.h>
#include <stdlib.h>
#include <time.h>

#include <string>

struct Node {
  int color;
  std::vector<int> neighbor;
  int lock_dist;
};

const int MAX_N = 100050;
int N, M, Q;
int a[MAX_N], b[MAX_N], v[MAX_N], d[MAX_N], c[MAX_N];

Node graph[MAX_N];

void dfs(int node, int color, int dist) {
  if (graph[node].color == 0) {
    graph[node].color = color;
  }

  if (dist <= graph[node].lock_dist) {
    return;
  }

  graph[node].lock_dist = dist;

  for (auto n : graph[node].neighbor) {
    dfs(n, color, dist - 1);
  }
}

int main(int argc, char **argv) {
  std::cin >> N >> M;

  for (int i = 1; i <= M; i++) {
    std::cin >> a[i] >> b[i];
  }

  std::cin >> Q;

  for (int i = 1; i <= Q; i++) {
    std::cin >> v[i] >> d[i] >> c[i];
  }

  for (int i = 1; i <= N; i++) {
    graph[i].color = 0;
    graph[i].lock_dist = 0;
  }

  for (int i = 1; i <= M; i++) {
    graph[a[i]].neighbor.push_back(b[i]);
    graph[b[i]].neighbor.push_back(a[i]);
  }

  for (int i = Q; 1 <= i; i--) {
    dfs(v[i], c[i], d[i]);
  }

  for (int i = 1; i <= N; i++) {
    std::cout << graph[i].color << std::endl;
  }

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
/// A drunk man will find his way home, but a drunk bird may get lost forever...
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define f first
#define s second
#define sz(x) (int)x.size()
const int mx = 1000 * 100 + 5;
vector<int> ad[mx];
int col[12][mx], v[2005], d[2005], c[2005];
void dfs(int v, int d, int c) {
  if (col[d][v])
    return;
  col[d][v] = c;
  if (!d)
    return;
  dfs(v, d - 1, c);
  for (auto u : ad[v])
    dfs(u, d - 1, c);
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, q, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    ad[a].pb(b);
    ad[b].pb(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  for (int i = q - 1; i >= 0; i--)
    dfs(v[i], d[i], c[i]);
  for (int i = 0; i < n; i++)
    cout << col[0][i] << endl;
  return 0;
}


Fixed submission:
/// A drunk man will find his way home, but a drunk bird may get lost forever...
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define f first
#define s second
#define sz(x) (int)x.size()
const int mx = 1000 * 100 + 5;
vector<int> ad[mx];
int col[12][mx], v[mx], d[mx], c[mx];
void dfs(int v, int d, int c) {
  if (col[d][v])
    return;
  col[d][v] = c;
  if (!d)
    return;
  dfs(v, d - 1, c);
  for (auto u : ad[v])
    dfs(u, d - 1, c);
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, q, a, b;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    a--, b--;
    ad[a].pb(b);
    ad[b].pb(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  for (int i = q - 1; i >= 0; i--)
    dfs(v[i], d[i], c[i]);
  for (int i = 0; i < n; i++)
    cout << col[0][i] << endl;
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 200010;
int n, m, s[N][11];
PII c[N], que[N];
vector<int> v[N];
int x, y;
struct tri {
  int x, y, z;
  void Input() { scanf("%d%d%d", &x, &y, &z); }
} q[N];

int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++)
    q[i].Input(); // scanf("%d%d",&q[i].X,&q[i].Y);
  for (int i = Q; i >= 1; i--) {
    int head = 0, tail = 0;
    int color = q[i].z;
    que[0] = mp(q[i].x, q[i].y);
    while (head <= tail) {
      PII tmp = que[head++];
      if (i > c[tmp.X].X)
        c[tmp.X] = mp(i, color);
      if (i > s[tmp.X][tmp.Y])
        s[tmp.X][tmp.Y] = i;
      else
        continue;
      if (tmp.Y == 0)
        continue;
      for (int j = 0; j < v[tmp.X].size(); j++) {
        que[++tail] = mp(v[tmp.X][j], tmp.Y - 1);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", c[i].Y);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 200010;
int n, m, s[N][11];
PII c[N], que[N];
vector<int> v[N];
int x, y;
struct tri {
  int x, y, z;
  void Input() { scanf("%d%d%d", &x, &y, &z); }
} q[N];

int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++)
    q[i].Input(); // scanf("%d%d",&q[i].X,&q[i].Y);
  for (int i = Q; i >= 1; i--) {
    int head = 0, tail = 0;
    int color = q[i].z;
    que[0] = mp(q[i].x, q[i].y);
    while (head <= tail) {
      PII tmp = que[head++];
      if (i > c[tmp.X].X)
        c[tmp.X] = mp(i, color);
      // if(i>s[tmp.X][tmp.Y]) s[tmp.X][tmp.Y] = i; else continue;
      bool pd = true;
      for (int j = tmp.Y; j <= 10; j++)
        if (s[tmp.X][j] >= i) {
          pd = false;
          break;
        }
      if (pd)
        s[tmp.X][tmp.Y] = i;
      else
        continue;
      if (tmp.Y == 0)
        continue;
      for (int j = 0; j < v[tmp.X].size(); j++) {
        que[++tail] = mp(v[tmp.X][j], tmp.Y - 1);
      }
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", c[i].Y);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define foru(i, l, r) for (register int i = l; i <= r; i++)
#define ford(i, r, l) for (register int i = r; i >= l; i--)

int read() {
  int res = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    res = res * 10 + c - '0', c = getchar();
  return res;
}

const int maxn = 1e5 + 5;
vector<int> G[maxn];
void link(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
int N, M, Q, mx[maxn], col[maxn];
int v[maxn], d[maxn], c[maxn];

void dfs(int x, int _mx, int _col) {
  if (!col[x])
    col[x] = _col;
  if (_mx <= mx[x] || !_mx)
    return;
  for (auto to : G[x])
    dfs(to, _mx - 1, _col);
}

int main() {
  //  freopen("b.in", "r", stdin);
  //  freopen("b.out", "w", stdout);
  N = read(), M = read();
  foru(i, 1, M) link(read(), read());
  Q = read();
  foru(i, 1, Q) v[i] = read(), d[i] = read(), c[i] = read();
  ford(i, Q, 1) dfs(v[i], d[i], c[i]);
  foru(i, 1, N) printf("%d\n", col[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define foru(i, l, r) for (register int i = l; i <= r; i++)
#define ford(i, r, l) for (register int i = r; i >= l; i--)

int read() {
  int res = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    res = res * 10 + c - '0', c = getchar();
  return res;
}

const int maxn = 1e5 + 5;
vector<int> G[maxn];
void link(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
int N, M, Q, mx[maxn], col[maxn];
int v[maxn], d[maxn], c[maxn];

void dfs(int x, int _mx, int _col) {
  if (!col[x])
    col[x] = _col;
  if (_mx <= mx[x] || !_mx)
    return;
  mx[x] = _mx;
  for (auto to : G[x])
    dfs(to, _mx - 1, _col);
}

int main() {
  //  freopen("b.in", "r", stdin);
  //  freopen("b.out", "w", stdout);
  N = read(), M = read();
  foru(i, 1, M) link(read(), read());
  Q = read();
  foru(i, 1, Q) v[i] = read(), d[i] = read(), c[i] = read();
  ford(i, Q, 1) dfs(v[i], d[i], c[i]);
  foru(i, 1, N) printf("%d\n", col[i]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

vector<vector<int>> g;
vector<int> color;
vector<int> best;

void dfs(int v, int dist, int curr, int c) {
  best[v] = dist - curr;
  // cout << "!" << color[v] << " " << c << "\n";
  if (color[v] == 0) {
    color[v] = c;
  }
  if (dist == curr) {
    return;
  }
  for (int to : g[v]) {
    if (best[to] < dist - (curr - 1)) {
      dfs(to, dist, curr + 1, c);
    }
  }
}

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  // ifstream cin("input.txt");
  // ofstream cout("output.txt");

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;

  g.resize(n);

  while (m--) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].pb(v);
    g[v].pb(u);
  }

  color.assign(n, 0);
  best.assign(n, -1);

  int q;
  cin >> q;
  vector<int> v(q), d(q), c(q);
  for (int i = 0; i < q; ++i) {
    cin >> v[i] >> d[i] >> c[i];
    --v[i];
  }

  for (int i = q - 1; i >= 0; --i) {
    int x = v[i];
    if (best[x] < d[i]) {
      dfs(x, d[i], 0, c[i]);
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << color[i] << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

vector<vector<int>> g;
vector<int> color;
vector<int> best;

void dfs(int v, int dist, int curr, int c) {
  best[v] = dist - curr;
  // cout << "!" << color[v] << " " << c << "\n";
  if (color[v] == 0) {
    color[v] = c;
  }
  if (dist == curr) {
    return;
  }
  for (int to : g[v]) {
    if (best[to] < dist - (curr + 1)) {
      dfs(to, dist, curr + 1, c);
    }
  }
}

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  // ifstream cin("input.txt");
  // ofstream cout("output.txt");

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;

  g.resize(n);

  while (m--) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].pb(v);
    g[v].pb(u);
  }

  color.assign(n, 0);
  best.assign(n, -1);

  int q;
  cin >> q;
  vector<int> v(q), d(q), c(q);
  for (int i = 0; i < q; ++i) {
    cin >> v[i] >> d[i] >> c[i];
    --v[i];
  }

  for (int i = q - 1; i >= 0; --i) {
    int x = v[i];
    if (best[x] < d[i]) {
      dfs(x, d[i], 0, c[i]);
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << color[i] << "\n";
  }
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100010;
const int MAXD = 11;
const int MAXQ = 100010;
int N, M, Q;
vector<int> adj[MAXN];
bool vis[MAXN][MAXD];
int V[MAXQ], D[MAXQ], C[MAXQ];

int ans[MAXN];

void dfs(int cur, int prv, int d, int c) {
  if (vis[cur][d])
    return;
  if (ans[cur] == 0) {
    ans[cur] = c;
  }
  if (d == 0)
    return;
  for (int nxt : adj[cur]) {
    if (nxt == prv)
      continue;
    dfs(nxt, cur, d - 1, c);
  }
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &V[i], &D[i], &C[i]);
  }
  for (int i = Q - 1; i >= 0; i--) {
    dfs(V[i], 0, D[i], C[i]);
  }

  for (int i = 1; i <= N; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100010;
const int MAXD = 11;
const int MAXQ = 100010;
int N, M, Q;
vector<int> adj[MAXN];
bool vis[MAXN][MAXD];
int V[MAXQ], D[MAXQ], C[MAXQ];

int ans[MAXN];

void dfs(int cur, int prv, int d, int c) {
  if (vis[cur][d])
    return;
  vis[cur][d] = true;
  if (ans[cur] == 0) {
    ans[cur] = c;
  }
  if (d == 0)
    return;
  for (int nxt : adj[cur]) {
    if (nxt == prv)
      continue;
    dfs(nxt, cur, d - 1, c);
  }
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &V[i], &D[i], &C[i]);
  }
  for (int i = Q - 1; i >= 0; i--) {
    dfs(V[i], 0, D[i], C[i]);
  }

  for (int i = 1; i <= N; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 7;

struct Edge {
  int to, last;
} E[maxn << 1];
struct Que {
  int s, c, d;
} q[maxn];
int n, m, Q;
int cnt;
int col[maxn];
int Max[maxn];
int head[maxn];
int Read(char c = 0, int re = 0) {
  while (!isdigit(c = getchar()))
    ;
  re = c - '0';
  while (isdigit(c = getchar()))
    re = ((re << 2) + re << 1) + c - '0';
  return re;
}
void Adde(int x, int y) {
  E[++cnt] = (Edge){y, head[x]};
  head[x] = cnt;
}
void Dfs(int x, int d, int c) {
  if (Max[x] >= d)
    return;
  if (!col[x])
    col[x] = c;
  Max[x] = d;
  if (!d)
    return;
  for (int i = head[x], y; y = E[i].to, i; i = E[i].last) {
    Dfs(y, d - 1, c);
  }
}
#define Hermione
int main() {
#ifdef Hermione
  freopen("in", "r", stdin);
#endif
  n = Read();
  m = Read();
  for (int i = 1; i <= m; ++i) {
    int x = Read(), y = Read();
    Adde(x, y);
    Adde(y, x);
  }
  Q = Read();
  for (int i = 1; i <= Q; ++i) {
    q[i].s = Read();
    q[i].d = Read();
    q[i].c = Read();
  }
  memset(Max, -1, sizeof Max);
  for (int i = Q; i; --i) {
    Dfs(q[i].s, q[i].d, q[i].c);
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\n", col[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 7;

struct Edge {
  int to, last;
} E[maxn << 1];
struct Que {
  int s, c, d;
} q[maxn];
int n, m, Q;
int cnt;
int col[maxn];
int Max[maxn];
int head[maxn];
int Read(char c = 0, int re = 0) {
  while (!isdigit(c = getchar()))
    ;
  re = c - '0';
  while (isdigit(c = getchar()))
    re = ((re << 2) + re << 1) + c - '0';
  return re;
}
void Adde(int x, int y) {
  E[++cnt] = (Edge){y, head[x]};
  head[x] = cnt;
}
void Dfs(int x, int d, int c) {
  if (Max[x] >= d)
    return;
  if (!col[x])
    col[x] = c;
  Max[x] = d;
  if (!d)
    return;
  for (int i = head[x], y; y = E[i].to, i; i = E[i].last) {
    Dfs(y, d - 1, c);
  }
}
int main() {
#ifdef Hermione
  freopen("in", "r", stdin);
#endif
  n = Read();
  m = Read();
  for (int i = 1; i <= m; ++i) {
    int x = Read(), y = Read();
    Adde(x, y);
    Adde(y, x);
  }
  Q = Read();
  for (int i = 1; i <= Q; ++i) {
    q[i].s = Read();
    q[i].d = Read();
    q[i].c = Read();
  }
  memset(Max, -1, sizeof Max);
  for (int i = Q; i; --i) {
    Dfs(q[i].s, q[i].d, q[i].c);
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\n", col[i]);
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
}
const int MAX = 1e5 + 5;
vector<int> g[MAX];
pair<int, int> dis[MAX];
int col[MAX];
// int que[MAX] ;
// int dist[MAX] ;
const int INF = 1e9;
struct qry {
  int d, v, c;
  int qno;
};
vector<qry> Q;
int dp[MAX][12];
int c;
void solve(int u, int d) {
  // cout << u << " , " << d << " , c = " << c << endl ;
  int &ret = dp[u][d];
  if (ret != -1)
    return;
  if (d == 0) {
    ret = c;
    return;
  }
  ret = c;
  for (auto v : g[u]) {
    solve(v, d - 1);
  }
  solve(u, d - 1);
}
int main(int argc, char *argv[]) {
  io();
#ifndef ONLINE_JUDGE
  freopen("inp.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  int n, m, qr;
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < MAX; i++) {
    dis[i].first = INF;
    dis[i].second = 0;
  }
  cin >> qr;
  for (int i = 1; i <= qr; i++) {
    int d, v, c;
    cin >> v >> d >> c;
    qry qq;
    qq.v = v;
    qq.d = d;
    qq.c = c;
    Q.push_back(qq);
  }
  reverse(Q.begin(), Q.end());
  memset(dp, -1, sizeof(dp));
  for (auto qq : Q) {
    int d = qq.d;
    int u = qq.v;
    c = qq.c;
    solve(u, d);
  }
  for (int i = 1; i <= n; i++) {
    if (dp[i][0] == -1)
      cout << 0 << endl;
    else
      cout << dp[i][0] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
}
const int MAX = 1e5 + 5;
vector<int> g[MAX];
pair<int, int> dis[MAX];
int col[MAX];
// int que[MAX] ;
// int dist[MAX] ;
const int INF = 1e9;
struct qry {
  int d, v, c;
  int qno;
};
vector<qry> Q;
int dp[MAX][12];
int c;
void solve(int u, int d) {
  // cout << u << " , " << d << " , c = " << c << endl ;
  int &ret = dp[u][d];
  if (ret != -1)
    return;
  if (d == 0) {
    ret = c;
    return;
  }
  ret = c;
  for (auto v : g[u]) {
    solve(v, d - 1);
  }
  solve(u, d - 1);
}
int main(int argc, char *argv[]) {
  io();
#ifndef ONLINE_JUDGE
  // freopen("inp.txt", "r", stdin) ;
  // freopen("out.txt", "w", stdout);
#endif
  int n, m, qr;
  cin >> n >> m;
  int u, v;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < MAX; i++) {
    dis[i].first = INF;
    dis[i].second = 0;
  }
  cin >> qr;
  for (int i = 1; i <= qr; i++) {
    int d, v, c;
    cin >> v >> d >> c;
    qry qq;
    qq.v = v;
    qq.d = d;
    qq.c = c;
    Q.push_back(qq);
  }
  reverse(Q.begin(), Q.end());
  memset(dp, -1, sizeof(dp));
  for (auto qq : Q) {
    int d = qq.d;
    int u = qq.v;
    c = qq.c;
    solve(u, d);
  }
  for (int i = 1; i <= n; i++) {
    if (dp[i][0] == -1)
      cout << 0 << endl;
    else
      cout << dp[i][0] << endl;
  }
  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m, q;
int dp[10][100001];
int d[100001], c[100001], v[100001];
vector<int> G[100001];

void f(int ver, int dist, int color) {
  // printf("%d %d %d\n",ver,dist,color);
  if (dist < 0)
    return;
  if (dp[dist][ver] != 0)
    return;

  dp[dist][ver] = color;

  f(ver, dist - 1, color);
  for (int i = 0; i < G[ver].size(); i++) {
    int u = G[ver][i];
    f(u, dist - 1, color);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  for (int i = q - 1; i >= 0; i--) {
    f(v[i], d[i], c[i]);
  }
  for (int i = 0; i < n; i++)
    cout << dp[0][i] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m, q;
int dp[11][100001];
int d[100001], c[100001], v[100001];
vector<int> G[100001];

void f(int ver, int dist, int color) {
  // printf("%d %d %d\n",ver,dist,color);
  if (dist < 0)
    return;
  if (dp[dist][ver] != 0)
    return;

  dp[dist][ver] = color;

  f(ver, dist - 1, color);
  for (int i = 0; i < G[ver].size(); i++) {
    int u = G[ver][i];
    f(u, dist - 1, color);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> v[i] >> d[i] >> c[i];
    v[i]--;
  }
  for (int i = q - 1; i >= 0; i--) {
    f(v[i], d[i], c[i]);
  }
  for (int i = 0; i < n; i++)
    cout << dp[0][i] << endl;

  return 0;
}
Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<int> ans(n, 0);
  vector<vector<int>> g(n), vis(n, vector<int>(11, 0));
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;

    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  int q, col;
  cin >> q;

  vector<int> v(q), d(q), c(q);
  for (int i = 0; i < q; ++i) {
    cin >> v[i] >> d[i] >> c[i];
    --v[i];
  }

  function<void(int, int)> paint = [&](int x, int dist) {
    if (vis[x][dist])
      return;
    if (dist == 0) {
      ans[x] = col;
      vis[x][dist] = 1;
      return;
    }

    paint(x, dist - 1);
    for (auto to : g[x]) {
      paint(to, dist - 1);
    }
  };

  for (int i = q - 1; i >= 0; --i) {
    col = c[i];
    paint(v[i], d[i]);
  }

  for (int i = 0; i < n; ++i) {
    cout << ans[i] << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<int> ans(n, 0);
  vector<vector<int>> g(n), vis(n, vector<int>(11, 0));
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;

    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  int q, col;
  cin >> q;

  vector<int> v(q), d(q), c(q);
  for (int i = 0; i < q; ++i) {
    cin >> v[i] >> d[i] >> c[i];
    --v[i];
  }

  function<void(int, int)> paint = [&](int x, int dist) {
    if (vis[x][dist])
      return;
    if (dist == 0) {
      ans[x] = col;
      vis[x][dist] = 1;
      return;
    }

    vis[x][dist] = 1;
    paint(x, dist - 1);
    for (auto to : g[x]) {
      paint(to, dist - 1);
    }
  };

  for (int i = q - 1; i >= 0; --i) {
    col = c[i];
    paint(v[i], d[i]);
  }

  for (int i = 0; i < n; ++i) {
    cout << ans[i] << '\n';
  }
}
Problem p03769 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <deque>
using namespace std;
typedef long long ll;

int m, num;
deque<int> s1, s2;
deque<int>::iterator it;

ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

int main() {
  ll n = read() + 1;
  for (int i = 1; i <= n; i <<= 1)
    m++;
  for (int i = m - 2; ~i; i--) {
    s1.push_back(++num), s2.push_back(num);
    if ((n >> i) & 1)
      s1.push_front(++num), s2.push_back(num);
  }
  printf("%d\n", (int)(s1.size() + s2.size()));
  for (it = s1.begin(); it != s1.end(); it++)
    printf("%d ", (*it));
  for (it = s2.begin(); it != s2.end(); it++)
    printf("%d ", (*it));
  return 0;
}


Fixed submission:
#include <cstdio>
#include <deque>
using namespace std;
typedef long long ll;

int m, num;
deque<int> s1, s2;
deque<int>::iterator it;

ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}

int main() {
  ll n = read() + 1;
  for (ll i = 1; i <= n; i <<= 1)
    m++;
  for (int i = m - 2; ~i; i--) {
    s1.push_back(++num), s2.push_back(num);
    if ((n >> i) & 1)
      s1.push_front(++num), s2.push_back(num);
  }
  printf("%d\n", (int)(s1.size() + s2.size()));
  for (it = s1.begin(); it != s1.end(); it++)
    printf("%d ", (*it));
  for (it = s2.begin(); it != s2.end(); it++)
    printf("%d ", (*it));
  return 0;
}
Problem p03769 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 10000000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i, n) for (long long i = 0; i < n; ++i)
long long inv(long long now) {
  long long ans = 1;
  long long te = MAX_MOD - 2LL;
  while (te != 0) {
    if (te % 2 == 1) {
      ans *= now;
      ans %= MAX_MOD;
    }
    te /= 2;
    now *= now;
    now %= MAX_MOD;
  }
  return ans;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> solve;
  solve.push_back(1);
  n--;
  for (long long i = 1; n != 0; ++i) {
    for (long long q = i; q >= 0; --q) {
      if (q < 64 && (1LL << (long long)q) <= n) {
        n -= (1LL << (long long)q);
        solve.insert(solve.begin() + q, i + 1);
        break;
      }
    }
  }
  cout << solve.size() + 100 << endl;
  REP(i, solve.size()) { cout << solve[i] << " "; }
  for (int i = 1; i <= 100; ++i) {
    cout << i << " ";
  }
  cout << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 10000000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i, n) for (long long i = 0; i < n; ++i)
long long inv(long long now) {
  long long ans = 1;
  long long te = MAX_MOD - 2LL;
  while (te != 0) {
    if (te % 2 == 1) {
      ans *= now;
      ans %= MAX_MOD;
    }
    te /= 2;
    now *= now;
    now %= MAX_MOD;
  }
  return ans;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> solve;
  solve.push_back(1);
  n--;
  for (long long i = 1; n != 0; ++i) {
    for (long long q = i; q >= 0; --q) {
      if (q < 50 && (1LL << (long long)q) <= n) {
        n -= (1LL << (long long)q);
        solve.insert(solve.begin() + q, i + 1);
        break;
      }
    }
  }
  cout << solve.size() + 100 << endl;
  REP(i, solve.size()) { cout << solve[i] << " "; }
  for (int i = 1; i <= 100; ++i) {
    cout << i << " ";
  }
  cout << endl;
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Author       : Sun YaoFeng
//============================================================================

// #pragma 	comment(linker, "/STACK:100240000,100240000")
// #include	<cstdio>
// #include	<cstdlib>
// #include	<cstring>
// #include	<algorithm>

#include <bits/stdc++.h>

using namespace std;

#define DB double
#define lf else if
#define I64 long long
#define unt unsigned int
#define Rd() (rand() << 15 | rand())
#define For(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define Rep(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)

#define fi first
#define se second
#define MK make_pair
#define pb push_back
#define PA pair<int, int>

// #define	min(a,b)	((a)<(b)?(a):(b))
// #define	max(a,b)	((a)<(b)?(b):(a))

#define CH (ch = getchar())
int IN() {
  int x = 0, f = 0, ch;
  for (; CH < '0' || ch > '9';)
    f = (ch == '-');
  for (; ch >= '0' && ch <= '9'; CH)
    x = x * 10 + ch - '0';
  return f ? -x : x;
}

#define n 205

I64 M;

int N, A[n], B[n];

int main() {
  cin >> M;

  int t = 0;
  for (; (1ll << t) - 1 <= M; t++)
    ;
  t--;

  M -= (1ll << t) - 1;

  N = t;
  For(i, 1, N) A[i] = B[i] = i;

  Rep(d, t, 1) {
    if ((1ll << d) <= M) {
      M -= (1ll << d);

      N++;
      B[N] = N;

      Rep(i, N, d + 2) A[i] = A[i - 1];
      A[d + 1] = N;
    }
  }

  assert(M == 0);

  printf("%d\n", 2 * N);
  For(i, 1, N) printf("%d ", A[i]);
  For(i, 1, N) printf("%d%c", B[i], " \n"[i == N]);

  return 0;
}

Fixed submission:
//============================================================================
// Author       : Sun YaoFeng
//============================================================================

// #pragma 	comment(linker, "/STACK:100240000,100240000")
// #include	<cstdio>
// #include	<cstdlib>
// #include	<cstring>
// #include	<algorithm>

#include <bits/stdc++.h>

using namespace std;

#define DB double
#define lf else if
#define I64 long long
#define unt unsigned int
#define Rd() (rand() << 15 | rand())
#define For(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define Rep(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)

#define fi first
#define se second
#define MK make_pair
#define pb push_back
#define PA pair<int, int>

// #define	min(a,b)	((a)<(b)?(a):(b))
// #define	max(a,b)	((a)<(b)?(b):(a))

#define CH (ch = getchar())
int IN() {
  int x = 0, f = 0, ch;
  for (; CH < '0' || ch > '9';)
    f = (ch == '-');
  for (; ch >= '0' && ch <= '9'; CH)
    x = x * 10 + ch - '0';
  return f ? -x : x;
}

#define n 205

I64 M;

int N, A[n], B[n];

int main() {
  cin >> M;

  int t = 0;
  for (; (1ll << t) - 1 <= M; t++)
    ;
  t--;

  M -= (1ll << t) - 1;

  N = t;
  For(i, 1, N) A[i] = B[i] = i;

  Rep(d, t, 0) {
    if ((1ll << d) <= M) {
      M -= (1ll << d);

      N++;
      B[N] = N;

      Rep(i, N, d + 2) A[i] = A[i - 1];
      A[d + 1] = N;
    }
  }

  assert(M == 0);

  printf("%d\n", 2 * N);
  For(i, 1, N) printf("%d ", A[i]);
  For(i, 1, N) printf("%d%c", B[i], " \n"[i == N]);

  return 0;
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <deque>
#include <iostream>
using namespace std;
int n, i;
deque<int> solve(long long n) {
  deque<int> ret;
  if (n == 1)
    return ret;
  if (n % 2) {
    ret = solve(n - 1);
    ret.push_front(ret.size() + 1);
  } else {
    ret = solve(n / 2);
    ret.push_back(ret.size() + 1);
  }
  return ret;
}
int main() {
  cin >> n;
  n++;
  deque<int> ans = solve(n);
  int cate = ans.size();
  for (i = 1; i <= cate; i++)
    ans.push_back(i);
  cout << ans.size() << '\n';
  for (i = 0; i < ans.size(); i++)
    cout << ans[i] << ' ';
  return 0;
}


Fixed submission:
#include <deque>
#include <iostream>
using namespace std;
long long n;
int i;
deque<int> solve(long long n) {
  deque<int> ret;
  if (n == 1)
    return ret;
  if (n % 2) {
    ret = solve(n - 1);
    ret.push_front(ret.size() + 1);
  } else {
    ret = solve(n / 2);
    ret.push_back(ret.size() + 1);
  }
  return ret;
}
int main() {
  cin >> n;
  n++;
  deque<int> ans = solve(n);
  int cate = ans.size();
  for (i = 1; i <= cate; i++)
    ans.push_back(i);
  cout << ans.size() << '\n';
  for (i = 0; i < ans.size(); i++)
    cout << ans[i] << ' ';
  return 0;
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
deque<int> a, b;
int n, ly;
void work(ll n) {
  if (n != 1) {
    work(n / 2);
    a.push_back(++ly), b.push_back(ly);
    if (n & 1)
      a.push_front(++ly), b.push_back(ly);
  }
}
int main() {
  scanf("%lld", &n);
  work(n + 1);
  printf("%d\n", a.size() * 2);
  for (int i = 0; i < a.size(); i++)
    printf("%d ", a[i]);
  for (int i = 0; i < b.size(); i++)
    printf("%d ", b[i]);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
deque<int> a, b;
ll n;
int ly;
void work(ll n) {
  if (n != 1) {
    work(n / 2);
    a.push_back(++ly), b.push_back(ly);
    if (n & 1)
      a.push_front(++ly), b.push_back(ly);
  }
}
int main() {
  scanf("%lld", &n);
  work(n + 1);
  printf("%d\n", a.size() * 2);
  for (int i = 0; i < a.size(); i++)
    printf("%d ", a[i]);
  for (int i = 0; i < b.size(); i++)
    printf("%d ", b[i]);
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;

vector<int> solve(int x) {
  if (x == 1)
    return vector<int>();
  if (x & 1) {
    auto ret = solve(x - 1);
    for (auto &i : ret)
      i++;
    ret.push_back(1);
    return ret;
  }
  auto ret = solve(x >> 1);
  ret.push_back(ret.size() + 1);
  return ret;
}

int main() {
  lint n;
  cin >> n;
  n++;
  vector<int> perm = solve(n);
  cout << 2 * perm.size() << endl;
  for (auto &i : perm)
    cout << i << " ";
  for (int i = 1; i <= perm.size(); i++)
    cout << i << " ";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;

vector<int> solve(lint x) {
  if (x == 1)
    return vector<int>();
  if (x & 1) {
    auto ret = solve(x - 1);
    for (auto &i : ret)
      i++;
    ret.push_back(1);
    return ret;
  }
  auto ret = solve(x >> 1);
  ret.push_back(ret.size() + 1);
  return ret;
}

int main() {
  lint n;
  cin >> n;
  n++;
  vector<int> perm = solve(n);
  cout << 2 * perm.size() << endl;
  for (auto &i : perm)
    cout << i << " ";
  for (int i = 1; i <= perm.size(); i++)
    cout << i << " ";
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <array>
#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)

struct Problem {

  UL p = 1;

  deque<UL> ans;
  deque<UL> ans2;

  void Solve2(ULL N) {
    if (N == 1) {
      ans = ans2 = {};
      return;
    }
    Solve2(N / 2);
    ans.push_back(p);
    ans2.push_back(p);
    p++;
    if (N % 2) {
      ans.push_front(p);
      ans2.push_back(p);
      p++;
    }
  }

  void Solve() {
    UL N;
    cin >> N;
    Solve2(N + 1);

    cout << ans.size() + ans2.size() << endl;
    rep(i, ans.size()) {
      if (i)
        cout << " ";
      cout << ans[i];
    }
    rep(i, ans2.size()) {
      cout << " ";
      cout << ans2[i];
    }
    cout << endl;
  }

  Problem();
};
int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}
Problem::Problem() { cout << fixed << setprecision(10); }

Fixed submission:
#include <array>
#include <bits/stdc++.h>
using namespace std;
using ULL = unsigned long long;
using UL = unsigned;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)

struct Problem {

  UL p = 1;

  deque<UL> ans;
  deque<UL> ans2;

  void Solve2(ULL N) {
    if (N == 1) {
      ans = ans2 = {};
      return;
    }
    Solve2(N / 2);
    ans.push_back(p);
    ans2.push_back(p);
    p++;
    if (N % 2) {
      ans.push_front(p);
      ans2.push_back(p);
      p++;
    }
  }

  void Solve() {
    ULL N;
    cin >> N;
    Solve2(N + 1);

    cout << ans.size() + ans2.size() << endl;
    rep(i, ans.size()) {
      if (i)
        cout << " ";
      cout << ans[i];
    }
    rep(i, ans2.size()) {
      cout << " ";
      cout << ans2[i];
    }
    cout << endl;
  }

  Problem();
};
int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}
Problem::Problem() { cout << fixed << setprecision(10); }
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  ++N;
  function<vector<int>(int, int)> go = [&](ll N, int cur) {
    if (N == 1)
      return vector<int>{};
    if (N % 2 == 0) {
      auto ans = go(N / 2, cur + 1);
      ans.insert(ans.begin() + int(ans.size()) / 2, cur);
      ans.emplace_back(cur);
      return ans;
    } else {
      auto ans = go(N - 1, cur + 1);
      ans.insert(ans.begin(), cur);
      ans.emplace_back(cur);
      return ans;
    }
  };
  auto ans = go(N, 1);
  cout << int(ans.size()) << "\n";
  for (int v : ans)
    cout << v << " ";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  ++N;
  function<vector<int>(ll, int)> go = [&](ll N, int cur) {
    if (N == 1)
      return vector<int>{};
    if (N % 2 == 0) {
      auto ans = go(N / 2, cur + 1);
      ans.insert(ans.begin() + int(ans.size()) / 2, cur);
      ans.emplace_back(cur);
      return ans;
    } else {
      auto ans = go(N - 1, cur + 1);
      ans.insert(ans.begin(), cur);
      ans.emplace_back(cur);
      return ans;
    }
  };
  auto ans = go(N, 1);
  cout << int(ans.size()) << "\n";
  for (int v : ans)
    cout << v << " ";
  return 0;
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

// s = (p1, p2, p3, ..., pn, 1, 2, 3, ..., n)
// p is a permutation of (1, 2, 3, ..., n)
// the number of good subsequences of s is the non-empty increasing
// subsequences of (p1, p2, p3, ..., pn)
// f(p) = number of (possibly empty) increasing subsequences of (p1, ..., pn)
// we find a p, such that f(p) = n + 1.
// f(empty-sequence) = 1
// f(q1, ..., qk, k + 1) = 2 * f(q1, ..., qk)
// q(k + 1, q1, ..., qk) = f(q1, ..., qk) + 1

#define ll long long

ll n;
std::deque<int> deque;

void getf(int x) {
  static int k = 0;
  if (x == 1)
    return;
  if (x & 1) {
    getf(x - 1);
    deque.push_front(++k);
  } else {
    getf(x / 2);
    deque.push_back(++k);
  }
}

int main() {
  scanf("%lld", &n);
  getf(n + 1);
  int ans = deque.size();
  printf("%d\n", ans * 2);
  for (auto v : deque) {
    printf("%d ", v);
  }
  for (int i = 1; i <= ans; ++i) {
    printf("%d%c", i, " \n"[i == ans]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

// s = (p1, p2, p3, ..., pn, 1, 2, 3, ..., n)
// p is a permutation of (1, 2, 3, ..., n)
// the number of good subsequences of s is the non-empty increasing
// subsequences of (p1, p2, p3, ..., pn)
// f(p) = number of (possibly empty) increasing subsequences of (p1, ..., pn)
// we find a p, such that f(p) = n + 1.
// f(empty-sequence) = 1
// f(q1, ..., qk, k + 1) = 2 * f(q1, ..., qk)
// q(k + 1, q1, ..., qk) = f(q1, ..., qk) + 1

#define ll long long

ll n;
std::deque<int> deque;

void getf(ll x) {
  static int k = 0;
  if (x == 1)
    return;
  if (x & 1) {
    getf(x - 1);
    deque.push_front(++k);
  } else {
    getf(x / 2);
    deque.push_back(++k);
  }
}

int main() {
  scanf("%lld", &n);
  getf(n + 1);
  int ans = deque.size();
  printf("%d\n", ans * 2);
  for (auto v : deque) {
    printf("%d ", v);
  }
  for (int i = 1; i <= ans; ++i) {
    printf("%d%c", i, " \n"[i == ans]);
  }
  return 0;
}
Problem p03769 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define N 1000005
using namespace std;

typedef long long ll;

ll n, m;
deque<int> q;

void yap(int n) {
  if (n == 1)
    return;
  if (n % 2) {
    yap(n - 1);
    q.push_back(++m);
  } else {
    yap(n / 2);
    q.push_front(++m);
  }
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%lld", &n);
  yap(n + 1);
  printf("%lld\n", m + m);
  while (!q.empty()) {
    printf("%d ", q.back());
    q.pop_back();
  }
  for (int i = 1; i <= m; i++)
    printf("%d ", i);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define N 1000005
using namespace std;

typedef long long ll;

ll n, m;
deque<int> q;

void yap(ll n) {
  if (n == 1)
    return;
  if (n % 2) {
    yap(n - 1);
    q.push_back(++m);
  } else {
    yap(n / 2);
    q.push_front(++m);
  }
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%lld", &n);
  yap(n + 1);
  printf("%lld\n", m + m);
  while (!q.empty()) {
    printf("%d ", q.back());
    q.pop_back();
  }
  for (int i = 1; i <= m; i++)
    printf("%d ", i);
  return 0;
}
Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 100010
#define ll long long
#define mod 1000000007
using namespace std;
int n, x, y;
int c[N], w[N], num[N], minw[N];
ll fac[N], inv[N];
ll ksm(ll a, ll b = mod - 2) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll q = ksm(a, b >> 1);
  if (b & 1)
    return q * q % mod * a % mod;
  return q * q % mod;
}
ll C(int a, int b) {
  if (b > a || b < 0)
    return 0;
  return fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int main() {
  scanf("%d%d%d", &n, &x, &y);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[n] = ksm(fac[n]);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  for (int i = 0; i <= n; i++)
    minw[i] = 100000000;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &c[i], &w[i]), minw[c[i]] = min(minw[c[i]], w[i]);
  int minid = 0, sminid = 0;
  for (int i = 1; i <= n; i++) {
    if (minw[i] <= minw[minid]) {
      sminid = minid;
      minid = i;
    } else if (minw[i] <= minw[sminid])
      sminid = i;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (w[i] + minw[c[i]] <= x)
      w[i] = minw[c[i]];
    int omin = c[i] == minid ? sminid : minid;
    if (minw[omin] + w[i] > y)
      continue;
    num[c[i]]++;
    cnt++;
  }
  ll ans = 1;
  for (int i = 1; i <= n; i++)
    if (num[i])
      ans = ans * C(cnt, num[i]) % mod, cnt -= num[i];
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 400010
#define ll long long
#define mod 1000000007
using namespace std;
int n, x, y;
int c[N], w[N], num[N], minw[N];
ll fac[N], inv[N];
ll ksm(ll a, ll b = mod - 2) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll q = ksm(a, b >> 1);
  if (b & 1)
    return q * q % mod * a % mod;
  return q * q % mod;
}
ll C(int a, int b) {
  if (b > a || b < 0)
    return 0;
  return fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int main() {
  scanf("%d%d%d", &n, &x, &y);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[n] = ksm(fac[n]);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
  for (int i = 0; i <= n; i++)
    minw[i] = 100000000;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &c[i], &w[i]), minw[c[i]] = min(minw[c[i]], w[i]);
  int minid = 0, sminid = 0;
  for (int i = 1; i <= n; i++) {
    if (minw[i] <= minw[minid]) {
      sminid = minid;
      minid = i;
    } else if (minw[i] <= minw[sminid])
      sminid = i;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (w[i] + minw[c[i]] <= x)
      w[i] = minw[c[i]];
    int omin = c[i] == minid ? sminid : minid;
    if (minw[omin] + w[i] > y)
      continue;
    num[c[i]]++;
    cnt++;
  }
  ll ans = 1;
  for (int i = 1; i <= n; i++)
    if (num[i])
      ans = ans * C(cnt, num[i]) % mod, cnt -= num[i];
  printf("%lld", ans);
  return 0;
}
Problem p03770 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i)
using std::cin;
using std::cout;
const int maxn = 200200;
typedef long long ll;
const int mod = 1e9 + 7;
typedef std::vector<int> vec;
vec v[maxn];
int fac[maxn], ifac[maxn], inv[maxn];
int size[maxn], cnt[maxn];
;
int n, x, y, c, w;
int main() {
  fac[0] = ifac[0] = fac[1] = ifac[1] = inv[1] = 1;
  rep(i, 2, maxn - 1) {
    inv[i] = ll(mod - mod / i) * inv[mod % i] % mod;
    fac[i] = (ll)fac[i - 1] * i % mod;
    ifac[i] = (ll)ifac[i - 1] * inv[i] % mod;
  }
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> x >> y;
  rep(i, 1, n) cin >> c >> w, v[c].push_back(w);
  rep(i, 1, n) {
    std::sort(v[i].begin(), v[i].end());
    if (v[i].size() == 0)
      v[i] = {int(1e9 + 1)};
  }
  std::sort(v + 1, v + n + 1,
            [](const vec &x, const vec &y) { return x.front() < y.front(); });
  if (v[1].front() + v[2].front() > y)
    return cout << 1 << '\n', 0;
  int ans = 1;
  rep(i, 1, n) {
    for (auto a : v[i])
      if (a + v[i].front() <= x || a + v[i == 1 ? 2 : 1].front() <= y)
        ++cnt[i];
    if (i == 1 || v[i].front() + v[1].front() <= y)
      size[1] += cnt[i], ans = (ll)ans * ifac[cnt[i]] % mod;
  }
  std::cout << int((ll)fac[size[1]] * ans % mod) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i)
using std::cin;
using std::cout;
const int maxn = 200200;
typedef long long ll;
const int mod = 1e9 + 7;
typedef std::vector<int> vec;
vec v[maxn];
int fac[maxn], ifac[maxn], inv[maxn];
int size[maxn], cnt[maxn];
;
int n, x, y, c, w;
int main() {
  fac[0] = ifac[0] = fac[1] = ifac[1] = inv[1] = 1;
  rep(i, 2, maxn - 1) {
    inv[i] = ll(mod - mod / i) * inv[mod % i] % mod;
    fac[i] = (ll)fac[i - 1] * i % mod;
    ifac[i] = (ll)ifac[i - 1] * inv[i] % mod;
  }
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> x >> y;
  rep(i, 1, n) cin >> c >> w, v[c].push_back(w);
  rep(i, 1, n + 1) {
    std::sort(v[i].begin(), v[i].end());
    if (v[i].size() == 0)
      v[i] = {int(1e9 + 1)};
  }
  std::sort(v + 1, v + n + 1,
            [](const vec &x, const vec &y) { return x.front() < y.front(); });
  if (v[1].front() + v[2].front() > y)
    return cout << 1 << '\n', 0;
  int ans = 1;
  rep(i, 1, n) {
    for (auto a : v[i])
      if (a + v[i].front() <= x || a + v[i == 1 ? 2 : 1].front() <= y)
        ++cnt[i];
    if (i == 1 || v[i].front() + v[1].front() <= y)
      size[1] += cnt[i], ans = (ll)ans * ifac[cnt[i]] % mod;
  }
  std::cout << int((ll)fac[size[1]] * ans % mod) << '\n';
}