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

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = int(5e2), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];

bool inline upd(int &a, int b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void inline bfs(int sx, int sy) {
  static int qx[msz + 5], qy[msz + 5];
  int ql = 0, qr = 0;
  qx[qr++] = sx, qy[qr++] = sy;
  for (; ql != qr;) {
    int x = qx[ql++], y = qy[ql++];
    loop(dr, 4) {
      int cx = x + dx[dr], cy = y + dy[dr];
      if (cx < 1 || cy < 1 || cx > n || cy > n ||
          !upd(d[cx][cy], d[x][y] + !vis[x][y]))
        continue;
      qx[qr++] = cx, qy[qr++] = cy;
    }
  }
}

int main() {
  scanf("%d", &n);
  m = n * n;
  int s = 0;
  cont(i, n) cont(j, n) {
    fm[++s] = make_pair(i, j);
    d[i][j] = n;
    upd(d[i][j], i - 1);
    upd(d[i][j], j - 1);
    upd(d[i][j], n - i);
    upd(d[i][j], n - j);
  }
  cont(i, m) {
    int id, x, y;
    scanf("%d", &id);
    x = fm[id].first, y = fm[id].second;
    ans += d[x][y];
    vis[x][y] = 1;
    bfs(x, y);
  }
  printf("%lld\n", ans);
}

Fixed submission:

#include <bits/stdc++.h>

#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = int(1e3), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];

bool inline upd(int &a, int b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void inline bfs(int sx, int sy) {
  static int qx[msz + 5], qy[msz + 5];
  int ql = 0, qr = 0;
  qx[qr++] = sx, qy[qr++] = sy;
  for (; ql != qr;) {
    int x = qx[ql++], y = qy[ql++];
    loop(dr, 4) {
      int cx = x + dx[dr], cy = y + dy[dr];
      if (cx < 1 || cy < 1 || cx > n || cy > n ||
          !upd(d[cx][cy], d[x][y] + !vis[x][y]))
        continue;
      qx[qr++] = cx, qy[qr++] = cy;
    }
  }
}

int main() {
  scanf("%d", &n);
  m = n * n;
  int s = 0;
  cont(i, n) cont(j, n) {
    fm[++s] = make_pair(i, j);
    d[i][j] = n;
    upd(d[i][j], i - 1);
    upd(d[i][j], j - 1);
    upd(d[i][j], n - i);
    upd(d[i][j], n - j);
  }
  cont(i, m) {
    int id, x, y;
    scanf("%d", &id);
    x = fm[id].first, y = fm[id].second;
    ans += d[x][y];
    vis[x][y] = 1;
    bfs(x, y);
  }
  printf("%lld\n", ans);
}
Problem p02670 (C++)

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

using namespace std;

int N;
vector<int> P(505);
bool a[505][505];
int d[505][505];

void dfs(int i, int j, int dst) {
  if (dst >= d[i][j])
    return;
  d[i][j] = dst;
  if (i > 0)
    dfs(i - 1, j, dst + (1 - a[i - 1][j]));
  if (i < N - 1)
    dfs(i + 1, j, dst + (1 - a[i + 1][j]));
  if (j > 0)
    dfs(i, j - 1, dst + (1 - a[i][j - 1]));
  if (j < N - 1)
    dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}

void print() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << d[i][j] << " ";
    }
    cout << "\n";
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N * N; i++)
    cin >> P[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
    }
  }

  int s = 0;

  for (int r = 0; r < N * N; r++) {
    int i = (P[r] - 1) / N;
    int j = (P[r] - 1) % N;
    a[i][j] = true;
    int dst = d[i][j] - 1;
    if (i > 0)
      dst = min(dst, d[i - 1][j]);
    if (i < N - 1)
      dst = min(dst, d[i + 1][j]);
    if (j > 0)
      dst = min(dst, d[i][j - 1]);
    if (j < N - 1)
      dst = min(dst, d[i][j + 1]);
    s += dst;
    // cout << r+1 << " " << i << " " << j << " " << dst << "\n";
    dfs(i, j, dst);
    // if (r == 7) print();
  }

  cout << s << "\n";

  return 0;
}

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

using namespace std;

int N;
vector<int> P(500 * 500 + 5);
bool a[505][505];
int d[505][505];

void dfs(int i, int j, int dst) {
  if (dst >= d[i][j])
    return;
  d[i][j] = dst;
  if (i > 0)
    dfs(i - 1, j, dst + (1 - a[i - 1][j]));
  if (i < N - 1)
    dfs(i + 1, j, dst + (1 - a[i + 1][j]));
  if (j > 0)
    dfs(i, j - 1, dst + (1 - a[i][j - 1]));
  if (j < N - 1)
    dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}

void print() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << d[i][j] << " ";
    }
    cout << "\n";
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N * N; i++)
    cin >> P[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
    }
  }

  int s = 0;

  for (int r = 0; r < N * N; r++) {
    int i = (P[r] - 1) / N;
    int j = (P[r] - 1) % N;
    a[i][j] = true;
    int dst = d[i][j] - 1;
    if (i > 0)
      dst = min(dst, d[i - 1][j]);
    if (i < N - 1)
      dst = min(dst, d[i + 1][j]);
    if (j > 0)
      dst = min(dst, d[i][j - 1]);
    if (j < N - 1)
      dst = min(dst, d[i][j + 1]);
    s += dst;
    // cout << r+1 << " " << i << " " << j << " " << dst << "\n";
    dfs(i, j, dst);
    // if (r == 7) print();
  }

  cout << s << "\n";

  return 0;
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 505;
mt19937 rng(time(0));

int n, x, y, a[N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};

void update(int x, int y) {
  v[x][y] = 0;
  queue<ar2> Q;
  Q.push({x, y});
  while (!Q.empty()) {
    ar2 ele = Q.front();
    Q.pop();
    int x = ele[0], y = ele[1];
    rep(i, 0, 3) {
      int x1 = x + dx[i], y1 = y + dy[i];
      if (dp[x][y] + v[x][y] < dp[x1][y1]) {
        dp[x1][y1] = dp[x][y] + v[x][y];
        Q.push({x1, y1});
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
#endif
  scanf("%d", &n);
  rep(i, 1, n * n) scanf("%d", a + i);

  rep(i, 1, n) rep(j, 1, n) dp[i][j] =
      min(min(i - 1, n - i), min(j - 1, n - j)),
                            v[i][j] = 1;

  int ans = 0;
  rep(q, 1, n * n) {
    x = a[q] - 1;
    y = x % n + 1, x = x / n + 1;

    ans += dp[x][y];
    update(x, y);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 505;
mt19937 rng(time(0));

int n, x, y, a[N * N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};

void update(int x, int y) {
  v[x][y] = 0;
  queue<ar2> Q;
  Q.push({x, y});
  while (!Q.empty()) {
    ar2 ele = Q.front();
    Q.pop();
    int x = ele[0], y = ele[1];
    rep(i, 0, 3) {
      int x1 = x + dx[i], y1 = y + dy[i];
      if (dp[x][y] + v[x][y] < dp[x1][y1]) {
        dp[x1][y1] = dp[x][y] + v[x][y];
        Q.push({x1, y1});
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
#endif
  scanf("%d", &n);
  rep(i, 1, n * n) scanf("%d", a + i);

  rep(i, 1, n) rep(j, 1, n) dp[i][j] =
      min(min(i - 1, n - i), min(j - 1, n - j)),
                            v[i][j] = 1;

  int ans = 0;
  rep(q, 1, n * n) {
    x = a[q] - 1;
    y = x % n + 1, x = x / n + 1;

    ans += dp[x][y];
    update(x, y);
  }
  cout << ans << endl;
  return 0;
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 505
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int a[N][N], n, ans, p[N], vis[N][N];
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; }
void dfs(int x, int y) {
  for (int i = 0; i < 4; ++i) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (check(nx, ny) && a[nx][ny] > a[x][y] + (!vis[x][y])) {
      a[nx][ny] = a[x][y] + (!vis[x][y]);
      dfs(nx, ny);
    }
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n * n; ++i) {
    p[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      a[i][j] = min(min(n - i, i - 1), min(n - j, j - 1));
    }
  }
  for (int i = 1; i <= n * n; ++i) {
    int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
    ans += a[x][y];
    vis[x][y] = 1;
    for (int k = 0; k < 4; ++k) {
      int nx = x + d[k][0], ny = y + d[k][1];
      if (check(nx, ny) && a[nx][ny] > a[x][y]) {
        a[nx][ny] = a[x][y], dfs(nx, ny);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 505
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int a[N][N], n, ans, p[N * N], vis[N][N];
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; }
void dfs(int x, int y) {
  for (int i = 0; i < 4; ++i) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (check(nx, ny) && a[nx][ny] > a[x][y] + (!vis[x][y])) {
      a[nx][ny] = a[x][y] + (!vis[x][y]);
      dfs(nx, ny);
    }
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n * n; ++i) {
    p[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      a[i][j] = min(min(n - i, i - 1), min(n - j, j - 1));
    }
  }
  for (int i = 1; i <= n * n; ++i) {
    int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
    ans += a[x][y];
    vis[x][y] = 1;
    for (int k = 0; k < 4; ++k) {
      int nx = x + d[k][0], ny = y + d[k][1];
      if (check(nx, ny) && a[nx][ny] > a[x][y]) {
        a[nx][ny] = a[x][y], dfs(nx, ny);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

const int N = 507;
int dist[N][N];

vector<ii> v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];

signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
      bad[i][j] = 1;
    }
  }

  int ans = 0;
  for (int t = 0; t < n * n; ++t) {
    int who;
    cin >> who;
    --who;

    int i = who / n;
    int j = who % n;

    bad[i][j] = 0;
    ans += dist[i][j];
    queue<ii> q;
    q.push(mp(i, j));
    while (q.size()) {
      auto p = q.front();
      q.pop();
      int i = p.f, j = p.s;
      for (auto e : v) {
        int x = i + e.f, y = j + e.s;
        if (dist[i][j] + bad[i][j] < dist[x][y]) {
          dist[x][y] = dist[i][j] + bad[i][j];
          q.push(mp(x, y));
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

const int N = 507;
int dist[N][N];

vector<ii> v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];

signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
      bad[i][j] = 1;
    }
  }

  int ans = 0;
  for (int t = 0; t < n * n; ++t) {
    int who;
    cin >> who;
    --who;

    int i = who / n;
    int j = who % n;

    bad[i][j] = 0;
    ans += dist[i][j];
    queue<ii> q;
    q.push(mp(i, j));
    while (q.size()) {
      auto p = q.front();
      q.pop();
      int i = p.f, j = p.s;
      for (auto e : v) {
        int x = i + e.f, y = j + e.s;
        if (0 <= x && x < n && 0 <= y && y < n &&
            dist[i][j] + bad[i][j] < dist[x][y]) {
          dist[x][y] = dist[i][j] + bad[i][j];
          q.push(mp(x, y));
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02670 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];

void update(int x, int y) {
  int xx, yy, t;
  head = 0;
  tail = 1;
  memset(B, 0, sizeof(B));
  list[1][1] = x;
  list[1][2] = y;
  B[x][y] = 1;
  while (head < tail) {
    head++;
    x = list[head][1];
    y = list[head][2];
    for (t = 1; t <= 4; t++) {
      xx = x + fx1[t];
      yy = y + fx2[t];
      if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
        dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
        if (B[xx][yy] == 0) {
          B[xx][yy] = 1;
          tail++;
          list[tail][1] = xx;
          list[tail][2] = yy;
        }
      }
    }
    B[x][y] = 0;
  }
}

int main() {
  //	freopen("read.in","r",stdin);
  fx1[1] = 1;
  fx1[2] = -1;
  fx1[3] = 0;
  fx1[4] = 0;
  fx2[1] = 0;
  fx2[2] = 0;
  fx2[3] = 1;
  fx2[4] = -1;
  scanf("%d", &n);
  for (i = 1; i <= n * n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    }
  }
  for (i = 1; i <= n * n; i++) {
    x = (p[i] - 1) / n + 1;
    y = (p[i] - 1) % n + 1;
    bz[x][y] = 1;
    dis[x][y]--;
    ans += dis[x][y];
    update(x, y);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];

void update(int x, int y) {
  int xx, yy, t;
  head = 0;
  tail = 1;
  list[1][1] = x;
  list[1][2] = y;
  B[x][y] = 1;
  while (head < tail) {
    head++;
    x = list[head][1];
    y = list[head][2];
    for (t = 1; t <= 4; t++) {
      xx = x + fx1[t];
      yy = y + fx2[t];
      if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
        dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
        if (B[xx][yy] == 0) {
          B[xx][yy] = 1;
          tail++;
          list[tail][1] = xx;
          list[tail][2] = yy;
        }
      }
    }
    B[x][y] = 0;
  }
}

int main() {
  //	freopen("read.in","r",stdin);
  fx1[1] = 1;
  fx1[2] = -1;
  fx1[3] = 0;
  fx1[4] = 0;
  fx2[1] = 0;
  fx2[2] = 0;
  fx2[3] = 1;
  fx2[4] = -1;
  scanf("%d", &n);
  for (i = 1; i <= n * n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    }
  }
  for (i = 1; i <= n * n; i++) {
    x = (p[i] - 1) / n + 1;
    y = (p[i] - 1) % n + 1;
    bz[x][y] = 1;
    dis[x][y]--;
    ans += dis[x][y];
    update(x, y);
  }
  printf("%lld", ans);
  return 0;
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 500 + 7;

int x[M], y[M];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int vs[M][M]; // i,j位置当前时刻 是否还有人
int d[M][M];  // i,j位置的人，最快离开剧场所需要的时间
struct node {
  int x, y;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    int z;
    cin >> z;
    x[i] = (z - 1) / n + 1;
    y[i] = (z - 1) % n + 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), vs[i][j] = 1;
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans += d[x[i]][y[i]];
    vs[x[i]][y[i]] = 0;
    queue<node> q;
    q.push(node{x[i], y[i]});
    while (q.size()) {
      node tp = q.front();
      q.pop();
      int tx = tp.x, ty = tp.y;
      for (int j = 0; j < 4; j++) {
        int px = tx + dx[j], py = ty + dy[j];
        if (px < 1 || px > n || py < 1 || py > n ||
            d[tx][ty] + vs[tx][ty] >= d[px][py])
          continue; // px,py 先到 tx,ty 是否更优，如果不优没必要拓展下去
        d[px][py] = d[tx][ty] + vs[tx][ty];
        q.push(node{px, py});
      }
    }
    //		cout<<i<<"   "<<x[i]<<" "<<y[i]<<"-----"<<endl;
    //		for(int j=1;j<=n;j++)
    //		for(int k=1;k<=n;k++)
    //		{
    //			cout<<d[j][k]<<" ";
    //			if(k==n)cout<<endl;
    //		}
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 500 + 7;

int x[M * M], y[M * M];
int dx[10] = {-1, 1, 0, 0}, dy[10] = {0, 0, -1, 1};
int vs[M][M]; // i,j位置当前时刻 是否还有人
int d[M][M];  // i,j位置的人，最快离开剧场所需要的时间
struct node {
  int x, y;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    int z;
    cin >> z;
    x[i] = (z - 1) / n + 1;
    y[i] = (z - 1) % n + 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), vs[i][j] = 1;
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans += d[x[i]][y[i]];
    vs[x[i]][y[i]] = 0;
    queue<node> q;
    q.push(node{x[i], y[i]});
    while (q.size()) {
      node tp = q.front();
      q.pop();
      int tx = tp.x, ty = tp.y;
      for (int j = 0; j < 4; j++) {
        int px = tx + dx[j], py = ty + dy[j];
        if (px < 1 || px > n || py < 1 || py > n ||
            d[tx][ty] + vs[tx][ty] >= d[px][py])
          continue; // px,py 先到 tx,ty 是否更优，如果不优没必要拓展下去
        d[px][py] = d[tx][ty] + vs[tx][ty];
        q.push(node{px, py});
      }
    }
    //		cout<<i<<"   "<<x[i]<<" "<<y[i]<<"-----"<<endl;
    //		for(int j=1;j<=n;j++)
    //		for(int k=1;k<=n;k++)
    //		{
    //			cout<<d[j][k]<<" ";
    //			if(k==n)cout<<endl;
    //		}
  }
  cout << ans << endl;
  return 0;
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100100
#define ll long long
ll n, m, a[510][510], dis[510][510], p[50100], ans;
void dfs(ll x, ll y) {
  if (dis[x - 1][y] > dis[x][y] + a[x][y]) {
    dis[x - 1][y] = dis[x][y] + a[x][y];
    dfs(x - 1, y);
  }
  if (dis[x][y - 1] > dis[x][y] + a[x][y]) {
    dis[x][y - 1] = dis[x][y] + a[x][y];
    dfs(x, y - 1);
  }
  if (dis[x + 1][y] > dis[x][y] + a[x][y]) {
    dis[x + 1][y] = dis[x][y] + a[x][y];
    dfs(x + 1, y);
  }
  if (dis[x][y + 1] > dis[x][y] + a[x][y]) {
    dis[x][y + 1] = dis[x][y] + a[x][y];
    dfs(x, y + 1);
  }
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n * n; i++)
    scanf("%lld", &p[i]);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      a[i][j] = 1;
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j)));
    }
  }
  int x, y;
  for (ll i = 1; i <= n * n; i++) {
    y = p[i] % n;
    if (y == 0)
      y = n;
    x = (p[i] - y) / n + 1;
    ans += dis[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%lld\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100100
#define ll long long
ll n, m, a[510][510], dis[510][510], p[500100], ans;
void dfs(ll x, ll y) {
  if (dis[x - 1][y] > dis[x][y] + a[x][y]) {
    dis[x - 1][y] = dis[x][y] + a[x][y];
    dfs(x - 1, y);
  }
  if (dis[x][y - 1] > dis[x][y] + a[x][y]) {
    dis[x][y - 1] = dis[x][y] + a[x][y];
    dfs(x, y - 1);
  }
  if (dis[x + 1][y] > dis[x][y] + a[x][y]) {
    dis[x + 1][y] = dis[x][y] + a[x][y];
    dfs(x + 1, y);
  }
  if (dis[x][y + 1] > dis[x][y] + a[x][y]) {
    dis[x][y + 1] = dis[x][y] + a[x][y];
    dfs(x, y + 1);
  }
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n * n; i++)
    scanf("%lld", &p[i]);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      a[i][j] = 1;
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j)));
    }
  }
  int x, y;
  for (ll i = 1; i <= n * n; i++) {
    y = p[i] % n;
    if (y == 0)
      y = n;
    x = (p[i] - y) / n + 1;
    ans += dis[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 600005;
int n, rt, cnt, ind, pw[N], ch[N][3], tg[N], val[N], ans[N];
char s[N];
void bud(int &k1, int k2, int k3) {
  k1 = ++cnt;
  if (k2 == n) {
    val[k1] = k3;
    return;
  }
  rep(i, 0, 2) bud(ch[k1][i], k2 + 1, k3 + pw[k2] * i);
}
void pst(int k1) {
  if (!ch[k1][0])
    return;
  swap(ch[k1][1], ch[k1][2]);
  tg[k1] ^= 1;
}
void psd(int k1) {
  if (!ch[k1][0])
    return;
  if (tg[k1]) {
    pst(ch[k1][0]);
    pst(ch[k1][1]);
    pst(ch[k1][2]);
    tg[k1] ^= 1;
  }
}
void go(int k1) {
  if (!ch[k1][0])
    return;
  psd(k1);
  int t = ch[k1][2];
  ch[k1][2] = ch[k1][1], ch[k1][1] = ch[k1][0], ch[k1][0] = t;
  go(ch[k1][0]);
}
void dfs(int k1, int k2, int k3) {
  if (k2 == n) {
    ans[val[k1]] = k3;
    return;
  }
  psd(k1);
  rep(i, 0, 2) dfs(ch[k1][i], k2 + 1, k3 + pw[k2] * i);
}
int main() {
  scanf("%d%s", &n, s);
  pw[0] = 1;
  rep(i, 1, n) pw[i] = pw[i - 1] * 3;
  bud(rt, 0, 0);
  for (int i = 0; s[i]; ++i) {
    if (s[i] == 'S')
      pst(rt);
    else
      go(rt);
  }
  dfs(rt, 0, 0);
  rep(i, 0, pw[n] - 1) printf("%d ", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 1800005;
int n, rt, cnt, ind, pw[N], ch[N][3], tg[N], val[N], ans[N];
char s[N];
void bud(int &k1, int k2, int k3) {
  k1 = ++cnt;
  if (k2 == n) {
    val[k1] = k3;
    return;
  }
  rep(i, 0, 2) bud(ch[k1][i], k2 + 1, k3 + pw[k2] * i);
}
void pst(int k1) {
  if (!ch[k1][0])
    return;
  swap(ch[k1][1], ch[k1][2]);
  tg[k1] ^= 1;
}
void psd(int k1) {
  if (!ch[k1][0])
    return;
  if (tg[k1]) {
    pst(ch[k1][0]);
    pst(ch[k1][1]);
    pst(ch[k1][2]);
    tg[k1] ^= 1;
  }
}
void go(int k1) {
  if (!ch[k1][0])
    return;
  psd(k1);
  int t = ch[k1][2];
  ch[k1][2] = ch[k1][1], ch[k1][1] = ch[k1][0], ch[k1][0] = t;
  go(ch[k1][0]);
}
void dfs(int k1, int k2, int k3) {
  if (k2 == n) {
    ans[val[k1]] = k3;
    return;
  }
  psd(k1);
  rep(i, 0, 2) dfs(ch[k1][i], k2 + 1, k3 + pw[k2] * i);
}
int main() {
  scanf("%d%s", &n, s);
  pw[0] = 1;
  rep(i, 1, n) pw[i] = pw[i - 1] * 3;
  bud(rt, 0, 0);
  for (int i = 0; s[i]; ++i) {
    if (s[i] == 'S')
      pst(rt);
    else
      go(rt);
  }
  dfs(rt, 0, 0);
  rep(i, 0, pw[n] - 1) printf("%d ", ans[i]);
  return 0;
}
Problem p02671 (C++)

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

const int maxN = 6e5 + 10;

int n, len, cnt, root;
int ch[maxN + 1][3], a[maxN + 1];
int rev[maxN + 1], pw[15], ans[maxN + 1];
char s[maxN + 1];

inline void reverse(int x) {
  if (!x)
    return;
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}

inline void pushdown(int x) {
  if (!rev[x])
    return;
  reverse(ch[x][0]);
  reverse(ch[x][1]);
  reverse(ch[x][2]);
  rev[x] = 0;
}

inline void insert(int &node, int x, int k) {
  if (!node)
    node = ++cnt;
  if (k == n) {
    a[node] = x;
    return;
  }
  int d = x / pw[k] % 3;
  insert(ch[node][d], x, k + 1);
}

inline void change(int node) {
  if (!node)
    return;
  pushdown(node);
  change(ch[node][2]);
  swap(ch[node][1], ch[node][2]);
  swap(ch[node][0], ch[node][1]);
}

inline void dfs(int node, int k, int x) {
  if (!node)
    return;
  if (k == n) {
    ans[a[node]] = x;
    return;
  }
  pushdown(node);
  dfs(ch[node][0], k + 1, x);
  dfs(ch[node][1], k + 1, x + pw[k]);
  dfs(ch[node][2], k + 1, x + 2 * pw[k]);
}

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  len = strlen(s + 1);

  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;

  for (int i = 0; i < pw[n]; i++)
    insert(root, i, 0);

  for (int i = 1; i <= len; i++)
    if (s[i] == 'S')
      reverse(root);
    else
      change(root);

  dfs(root, 0, 0);

  for (int i = 0; i < pw[n]; i++)
    printf("%d ", ans[i]);
  puts("");
  return 0;
}


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

const int maxN = 2e6 + 10;

int n, len, cnt, root;
int ch[maxN + 1][3], a[maxN + 1];
int rev[maxN + 1], pw[15], ans[maxN + 1];
char s[maxN + 1];

inline void reverse(int x) {
  if (!x)
    return;
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}

inline void pushdown(int x) {
  if (!rev[x])
    return;
  reverse(ch[x][0]);
  reverse(ch[x][1]);
  reverse(ch[x][2]);
  rev[x] = 0;
}

inline void insert(int &node, int x, int k) {
  if (!node)
    node = ++cnt;
  if (k == n) {
    a[node] = x;
    return;
  }
  int d = x / pw[k] % 3;
  insert(ch[node][d], x, k + 1);
}

inline void change(int node) {
  if (!node)
    return;
  pushdown(node);
  change(ch[node][2]);
  swap(ch[node][1], ch[node][2]);
  swap(ch[node][0], ch[node][1]);
}

inline void dfs(int node, int k, int x) {
  if (!node)
    return;
  if (k == n) {
    ans[a[node]] = x;
    return;
  }
  pushdown(node);
  dfs(ch[node][0], k + 1, x);
  dfs(ch[node][1], k + 1, x + pw[k]);
  dfs(ch[node][2], k + 1, x + 2 * pw[k]);
}

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  len = strlen(s + 1);

  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;

  for (int i = 0; i < pw[n]; i++)
    insert(root, i, 0);

  for (int i = 1; i <= len; i++)
    if (s[i] == 'S')
      reverse(root);
    else
      change(root);

  dfs(root, 0, 0);

  for (int i = 0; i < pw[n]; i++)
    printf("%d ", ans[i]);
  puts("");
  return 0;
}
Problem p02671 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
using namespace std;

const int maxn = 6e5;
int n, m, tot, val[maxn + 5], ch[maxn + 5][3], cnt, res[maxn + 5];
bool sal[maxn + 5];
char s[maxn + 5];

int dfs(int k, int t, int s) {
  int x = ++tot;
  if (!k)
    return val[x] = s, x;
  ch[x][0] = dfs(k - 1, t * 3, s);
  ch[x][1] = dfs(k - 1, t * 3, s + t);
  ch[x][2] = dfs(k - 1, t * 3, s + t * 2);
  return x;
}

void salsa(int x) { sal[x] ^= 1, swap(ch[x][1], ch[x][2]); }

void push_down(int x) {
  if (sal[x]) {
    rep(i, 0, 2) salsa(ch[x][i]);
    sal[x] = false;
  }
}

void rumba(int x) {
  if (!ch[x][0])
    return;
  push_down(x);
  int t = ch[x][0];
  ch[x][0] = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = t;
  rumba(ch[x][0]);
}

void work(int x, int t, int s) {
  if (!ch[x][0]) {
    res[val[x]] = s, cnt++;
    return;
  }
  push_down(x);
  rep(i, 0, 2) work(ch[x][i], t * 3, s + i * t);
}

int main() {
  scanf("%d %s", &n, s + 1);
  m = strlen(s + 1);
  dfs(n, 1, 0);
  rep(i, 1, m) {
    if (s[i] == 'S')
      salsa(1);
    else
      rumba(1);
  }
  work(1, 1, 0);
  rep(i, 0, cnt - 1) printf("%d%c", res[i], " \n"[i == cnt - 1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
using namespace std;

const int maxn = 8e5;
int n, m, tot, val[maxn + 5], ch[maxn + 5][3], cnt, res[maxn + 5];
bool sal[maxn + 5];
char s[maxn + 5];

int dfs(int k, int t, int s) {
  int x = ++tot;
  if (!k)
    return val[x] = s, x;
  ch[x][0] = dfs(k - 1, t * 3, s);
  ch[x][1] = dfs(k - 1, t * 3, s + t);
  ch[x][2] = dfs(k - 1, t * 3, s + t * 2);
  return x;
}

void salsa(int x) { sal[x] ^= 1, swap(ch[x][1], ch[x][2]); }

void push_down(int x) {
  if (sal[x]) {
    rep(i, 0, 2) salsa(ch[x][i]);
    sal[x] = false;
  }
}

void rumba(int x) {
  if (!ch[x][0])
    return;
  push_down(x);
  int t = ch[x][0];
  ch[x][0] = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = t;
  rumba(ch[x][0]);
}

void work(int x, int t, int s) {
  if (!ch[x][0]) {
    res[val[x]] = s, cnt++;
    return;
  }
  push_down(x);
  rep(i, 0, 2) work(ch[x][i], t * 3, s + i * t);
}

int main() {
  scanf("%d %s", &n, s + 1);
  m = strlen(s + 1);
  dfs(n, 1, 0);
  rep(i, 1, m) {
    if (s[i] == 'S')
      salsa(1);
    else
      rumba(1);
  }
  work(1, 1, 0);
  rep(i, 0, cnt - 1) printf("%d%c", res[i], " \n"[i == cnt - 1]);
  return 0;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 13
#define MAXM 500005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n;
string str;
int ans[MAXM];
int curdigit[MAXN], curres[MAXN];
string reduce(string &t) {
  vector<char> st;
  string ret = "";
  for (auto ch : t) {
    st.push_back(ch);
    if (st.size() >= 2 && st[(int)st.size() - 1] == 'S' &&
        st[(int)st.size() - 2] == 'S')
      st.pop_back(), st.pop_back();
  }
  for (auto ch : st)
    ret += ch;
  return ret;
}
void update_ans() {
  int x = 0, y = 0;
  for (int i = n - 1; i >= 0; i--) {
    x = x * 3 + curdigit[i];
    y = y * 3 + curres[i];
  }
  ans[x] = y;
}
void solve(int cur, string t) {
  if (cur == n) {
    update_ans();
    return;
  }
  for (int d = 0; d < 3; d++) {
    string curt = "";
    int curd = d;
    for (auto ch : t) {
      if (ch == 'S') {
        if (curd)
          curd = 3 - curd;
        curt += 'S';
      }
      if (ch == 'R') {
        curd++;
        if (curd == 3) {
          curd = 0;
          curt += 'R';
        }
      }
    }
    curdigit[cur] = d;
    curres[cur] = curd;
    solve(cur + 1, reduce(curt));
  }
}
int main() {
  scanf("%d", &n);
  cin >> str;
  solve(0, reduce(str));
  int p = 1;
  for (int i = 0; i < n; i++)
    p = p * 3;
  for (int i = 0; i < p; i++)
    printf("%d ", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 13
#define MAXM 550005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n;
string str;
int ans[MAXM];
int curdigit[MAXN], curres[MAXN];
string reduce(string &t) {
  vector<char> st;
  string ret = "";
  for (auto ch : t) {
    st.push_back(ch);
    if (st.size() >= 2 && st[(int)st.size() - 1] == 'S' &&
        st[(int)st.size() - 2] == 'S')
      st.pop_back(), st.pop_back();
  }
  for (auto ch : st)
    ret += ch;
  return ret;
}
void update_ans() {
  int x = 0, y = 0;
  for (int i = n - 1; i >= 0; i--) {
    x = x * 3 + curdigit[i];
    y = y * 3 + curres[i];
  }
  ans[x] = y;
}
void solve(int cur, string t) {
  if (cur == n) {
    update_ans();
    return;
  }
  for (int d = 0; d < 3; d++) {
    string curt = "";
    int curd = d;
    for (auto ch : t) {
      if (ch == 'S') {
        if (curd)
          curd = 3 - curd;
        curt += 'S';
      }
      if (ch == 'R') {
        curd++;
        if (curd == 3) {
          curd = 0;
          curt += 'R';
        }
      }
    }
    curdigit[cur] = d;
    curres[cur] = curd;
    solve(cur + 1, reduce(curt));
  }
}
int main() {
  scanf("%d", &n);
  cin >> str;
  solve(0, reduce(str));
  int p = 1;
  for (int i = 0; i < n; i++)
    p = p * 3;
  for (int i = 0; i < p; i++)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef vector<bool> vi;
const int maxn = 2e5 + 10;
char s[maxn];
int n, ans[maxn], len, p3[13];
void solve(int x, int v, vi &a) {
  int res[3];
  vi tmp[3];
  for (int i = 0; i < 3; i++) {
    tmp[i].clear();
    int now = i;
    for (int j = 0; j < a.size(); j++) {
      if (a[j]) {
        if (now > 0)
          now = 3 - now;
        tmp[i].pb(1);
        int m = tmp[i].size();
        if (m >= 2 && tmp[i][m - 1] && tmp[i][m - 2])
          tmp[i].resize(m - 2);
      } else {
        now++;
        if (now == 3) {
          now = 0;
          tmp[i].pb(0);
        }
      }
    }
    res[i] = now;
  }
  for (int i = 0; i < p3[n] / p3[x]; i++) {
    ans[i * p3[x] + v] += p3[x] * res[i % 3];
  }
  if (x < n - 1) {
    for (int i = 0; i < 3; i++)
      solve(x + 1, v + i * p3[x], tmp[i]);
  }
}
int main() {
  cin >> n >> s;
  len = strlen(s);
  vector<vi> dp;
  p3[0] = 1;
  for (int i = 1; i <= n; i++)
    p3[i] = p3[i - 1] * 3;
  vi res;
  res.clear();
  for (int i = 0; i < len; i++) {
    res.pb(s[i] == 'S');
    int m = res.size();
    if (m >= 2 && res[m - 1] && res[m - 2])
      res.resize(m - 2);
  }
  solve(0, 0, res);
  for (int i = 0; i < p3[n]; i++)
    cout << ans[i] << ' ';
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef vector<bool> vi;
const int maxn = 1e6 + 10;
char s[maxn];
int n, ans[maxn], len, p3[13];
void solve(int x, int v, vi &a) {
  int res[3];
  vi tmp[3];
  for (int i = 0; i < 3; i++) {
    tmp[i].clear();
    int now = i;
    for (int j = 0; j < a.size(); j++) {
      if (a[j]) {
        if (now > 0)
          now = 3 - now;
        tmp[i].pb(1);
        int m = tmp[i].size();
        if (m >= 2 && tmp[i][m - 1] && tmp[i][m - 2])
          tmp[i].resize(m - 2);
      } else {
        now++;
        if (now == 3) {
          now = 0;
          tmp[i].pb(0);
        }
      }
    }
    res[i] = now;
  }
  for (int i = 0; i < p3[n] / p3[x]; i++) {
    ans[i * p3[x] + v] += p3[x] * res[i % 3];
  }
  if (x < n - 1) {
    for (int i = 0; i < 3; i++)
      solve(x + 1, v + i * p3[x], tmp[i]);
  }
}
int main() {
  cin >> n >> s;
  len = strlen(s);
  vector<vi> dp;
  p3[0] = 1;
  for (int i = 1; i <= n; i++)
    p3[i] = p3[i - 1] * 3;
  vi res;
  res.clear();
  for (int i = 0; i < len; i++) {
    res.pb(s[i] == 'S');
    int m = res.size();
    if (m >= 2 && res[m - 1] && res[m - 2])
      res.resize(m - 2);
  }
  solve(0, 0, res);
  for (int i = 0; i < p3[n]; i++)
    cout << ans[i] << ' ';
  cout << endl;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int ch[600005][3], ed[600005], cnt = 1, ans[600005];
bool rev[600005];
char s[200005];
void pushr(int x) {
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}
void pushdown(int x) {
  if (!rev[x])
    return;
  for (int i = 0; i <= 2; i++)
    if (ch[x][i])
      pushr(ch[x][i]);
  rev[x] = 0;
}
void dfs(int x) {
  pushdown(x);
  swap(ch[x][0], ch[x][2]), swap(ch[x][1], ch[x][2]);
  if (ch[x][0])
    dfs(ch[x][0]);
}
void get(int x, int now, int pw) {
  pushdown(x);
  if (ed[x] != 0xffffffff)
    ans[ed[x]] = now;
  if (ch[x][0])
    get(ch[x][0], now, pw * 3);
  if (ch[x][1])
    get(ch[x][1], now + pw, pw * 3);
  if (ch[x][2])
    get(ch[x][2], now + 2 * pw, pw * 3);
}
int main() {
  int n, m = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    m *= 3;
  memset(ed, 0xff, sizeof(ed));
  for (int i = 0; i < m; i++) {
    int now = 1, tmp = i;
    for (int k = 0; k < n; k++) {
      int t = tmp % 3;
      tmp /= 3;
      if (!ch[now][t])
        ch[now][t] = ++cnt;
      now = ch[now][t];
    }
    ed[now] = i;
  }
  scanf("%s", s + 1);
  int q = strlen(s + 1);
  for (int i = 1; i <= q; i++) {
    if (s[i] == 'S')
      pushr(1);
    else
      dfs(1);
  }
  get(1, 0, 1);
  for (int i = 0; i < m; i++)
    printf("%d ", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int ch[10000005][3], ed[10000005], cnt = 1, ans[10000005];
bool rev[10000005];
char s[200005];
void pushr(int x) {
  swap(ch[x][1], ch[x][2]);
  rev[x] ^= 1;
}
void pushdown(int x) {
  if (!rev[x])
    return;
  for (int i = 0; i <= 2; i++)
    if (ch[x][i])
      pushr(ch[x][i]);
  rev[x] = 0;
}
void dfs(int x) {
  pushdown(x);
  swap(ch[x][0], ch[x][2]), swap(ch[x][1], ch[x][2]);
  if (ch[x][0])
    dfs(ch[x][0]);
}
void get(int x, int now, int pw) {
  pushdown(x);
  if (ed[x] != 0xffffffff)
    ans[ed[x]] = now;
  if (ch[x][0])
    get(ch[x][0], now, pw * 3);
  if (ch[x][1])
    get(ch[x][1], now + pw, pw * 3);
  if (ch[x][2])
    get(ch[x][2], now + 2 * pw, pw * 3);
}
int main() {
  int n, m = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    m *= 3;
  memset(ed, 0xff, sizeof(ed));
  for (int i = 0; i < m; i++) {
    int now = 1, tmp = i;
    for (int k = 0; k < n; k++) {
      int t = tmp % 3;
      tmp /= 3;
      if (!ch[now][t])
        ch[now][t] = ++cnt;
      now = ch[now][t];
    }
    ed[now] = i;
  }
  scanf("%s", s + 1);
  int q = strlen(s + 1);
  for (int i = 1; i <= q; i++) {
    if (s[i] == 'S')
      pushr(1);
    else
      dfs(1);
  }
  get(1, 0, 1);
  for (int i = 0; i < m; i++)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define SZ(x) (int)x.size() - 1
#define F(i, a, b) for (int i = a; i <= b; ++i)
#define DF(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define ms(a, b) memset(a, b, sizeof a)
using namespace std;
int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      w = -1;
  for (; isdigit(ch); ch = getchar())
    c = (c << 3) + (c << 1) + (ch ^ 48);
  return w * c;
}
const int M = 6e5 + 10;
int t[M][3], n, pw[M];
int val[M], rt, tot, ans[M];
bool tg[M];
char ch[M];
void rev(int x) {
  tg[x] ^= 1;
  swap(t[x][1], t[x][2]);
}
void pd(int x) {
  if (tg[x]) {
    F(i, 0, 2)
    rev(t[x][i]);
    tg[x] = 0;
  }
}
void build(int &x, int dep, int o) {
  x = ++tot;
  val[x] = o;
  if (dep == n)
    return;
  F(i, 0, 2) build(t[x][i], dep + 1, o + pw[dep] * i);
}
void add(int x, int dep) {
  if (dep == n)
    return;
  pd(x);
  int t0 = t[x][0], t1 = t[x][1], t2 = t[x][2];
  t[x][0] = t2;
  t[x][1] = t0;
  t[x][2] = t1;
  add(t[x][0], dep + 1);
}
void calc(int x, int dep, int o) {
  if (dep == n) {
    ans[val[x]] = o;
    return;
  }
  pd(x);
  F(i, 0, 2) calc(t[x][i], dep + 1, o + pw[dep] * i);
}
int main() {
  n = read();
  scanf("%s", ch + 1);
  int m = strlen(ch + 1);
  pw[0] = 1;
  F(i, 1, 20) pw[i] = pw[i - 1] * 3;
  build(rt, 0, 0);
  F(i, 1, m) {
    if (ch[i] == 'S')
      rev(rt);
    else
      add(rt, 0);
  }
  calc(1, 0, 0);

  F(i, 0, pw[n] - 1) { cout << ans[i] << " "; }
  cout << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define SZ(x) (int)x.size() - 1
#define F(i, a, b) for (int i = a; i <= b; ++i)
#define DF(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define ms(a, b) memset(a, b, sizeof a)
using namespace std;
int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      w = -1;
  for (; isdigit(ch); ch = getchar())
    c = (c << 3) + (c << 1) + (ch ^ 48);
  return w * c;
}
const int M = 5e6 + 10;
int t[M][3], n, pw[M];
int val[M], rt, tot, ans[M];
bool tg[M];
char ch[M];
void rev(int x) {
  tg[x] ^= 1;
  swap(t[x][1], t[x][2]);
}
void pd(int x) {
  if (tg[x]) {
    F(i, 0, 2)
    rev(t[x][i]);
    tg[x] = 0;
  }
}
void build(int &x, int dep, int o) {
  x = ++tot;
  val[x] = o;
  if (dep == n)
    return;
  F(i, 0, 2) build(t[x][i], dep + 1, o + pw[dep] * i);
}
void add(int x, int dep) {
  if (dep == n)
    return;
  pd(x);
  int t0 = t[x][0], t1 = t[x][1], t2 = t[x][2];
  t[x][0] = t2;
  t[x][1] = t0;
  t[x][2] = t1;
  add(t[x][0], dep + 1);
}
void calc(int x, int dep, int o) {
  if (dep == n) {
    ans[val[x]] = o;
    return;
  }
  pd(x);
  F(i, 0, 2) calc(t[x][i], dep + 1, o + pw[dep] * i);
}
int main() {
  n = read();
  scanf("%s", ch + 1);
  int m = strlen(ch + 1);
  pw[0] = 1;
  F(i, 1, 20) pw[i] = pw[i - 1] * 3;
  build(rt, 0, 0);
  F(i, 1, m) {
    if (ch[i] == 'S')
      rev(rt);
    else
      add(rt, 0);
  }
  calc(1, 0, 0);

  F(i, 0, pw[n] - 1) { cout << ans[i] << " "; }
  cout << "\n";
  return 0;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline int Pow(int a, int b) {
  return b ? Pow(a * a, b >> 1) * (b & 1 ? a : 1) : 1;
}
int main() {
  int N, now, len = 0, A[200001], B[531441];
  char T[200001], S[200001];
  now = scanf("%d %s", &N, T);
  len = strlen(T);
  fflush(stdin);
  int pw3 = Pow(3, N), x = Pow(3, N >> 1), y = Pow(3, N - (N >> 1));
  rep(i, pw3) B[i] = B[i / 3] * 3 + (3 - i % 3) % 3;
  rep(i, x) {
    int temp = i;
    now = 0;
    rep(j, len) {
      if (T[j] == 'S') {
        temp = B[temp];
        if (S[now] == 'S')
          --now;
        else
          S[++now] = 'S';
      } else {
        ++temp;
        if (temp == x)
          S[++now] = 'R', temp = 0;
      }
    }
    rep(j, y) {
      int keep = j;
      rep(k, now) {
        if (S[k + 1] == 'S')
          keep = B[keep];
        else if (S[k + 1] == 'R')
          if ((++keep) == y)
            keep = 0;
      }
      A[i + j * x] = temp + keep * x;
    }
  }
  rep(i, pw3) printf("%d%c", A[i], i + 1 == pw3 ? '\n' : ' ');
  return fflush(stdout), 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline int Pow(int a, int b) {
  return b ? Pow(a * a, b >> 1) * (b & 1 ? a : 1) : 1;
}
int main() {
  int N, now, len = 0, A[531441], B[531441];
  char T[200001], S[200001];
  now = scanf("%d %s", &N, T);
  len = strlen(T);
  fflush(stdin);
  int pw3 = Pow(3, N), x = Pow(3, N >> 1), y = Pow(3, N - (N >> 1));
  rep(i, pw3) B[i] = B[i / 3] * 3 + (3 - i % 3) % 3;
  rep(i, x) {
    int temp = i;
    now = 0;
    rep(j, len) {
      if (T[j] == 'S') {
        temp = B[temp];
        if (S[now] == 'S')
          --now;
        else
          S[++now] = 'S';
      } else {
        ++temp;
        if (temp == x)
          S[++now] = 'R', temp = 0;
      }
    }
    rep(j, y) {
      int keep = j;
      rep(k, now) {
        if (S[k + 1] == 'S')
          keep = B[keep];
        else if (S[k + 1] == 'R')
          if ((++keep) == y)
            keep = 0;
      }
      A[i + j * x] = temp + keep * x;
    }
  }
  rep(i, pw3) printf("%d%c", A[i], i + 1 == pw3 ? '\n' : ' ');
  return fflush(stdout), 0;
}
Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 6e5 + 50;
int n, p[N];
char S[N];
int ch[N][3], tag[N], vl[N], nd, pw[20];
int build(int d, int v) {
  if (!d)
    return vl[++nd] = v, nd;
  int x = ++nd;
  ch[x][0] = build(d - 1, v);
  ch[x][1] = build(d - 1, v + pw[n - d]);
  ch[x][2] = build(d - 1, v + 2 * pw[n - d]);
  return x;
}
void put(int x) {
  if (!x)
    return;
  tag[x] ^= 1;
  swap(ch[x][1], ch[x][2]);
}
void down(int x) {
  if (tag[x])
    put(ch[x][0]), put(ch[x][1]), put(ch[x][2]), tag[x] = 0;
}
void work(int x, int d) {
  if (!d)
    return;
  down(x);
  int v = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = ch[x][0];
  ch[x][0] = v;
  work(v, d - 1);
}
void dfs(int x, int d, int v = 0) {
  if (!d)
    return p[vl[x]] = v, void();
  down(x);
  dfs(ch[x][0], d - 1, v);
  dfs(ch[x][1], d - 1, v + pw[n - d]);
  dfs(ch[x][2], d - 1, v + 2 * pw[n - d]);
}
int main() {
#ifdef FSYolanda
  freopen("1.in", "r", stdin);
#endif
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  scanf("%s", S);
  int m = strlen(S);
  build(n, 0);
  for (int i = 0; i < m; i++) {
    if (S[i] == 'R')
      work(1, n);
    if (S[i] == 'S')
      put(1);
  }
  dfs(1, n);
  for (int i = 0; i < pw[n]; i++)
    cout << p[i] << " ";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 2e6 + 50;
int n, p[N];
char S[N];
int ch[N][3], tag[N], vl[N], nd, pw[20];
int build(int d, int v) {
  if (!d)
    return vl[++nd] = v, nd;
  int x = ++nd;
  ch[x][0] = build(d - 1, v);
  ch[x][1] = build(d - 1, v + pw[n - d]);
  ch[x][2] = build(d - 1, v + 2 * pw[n - d]);
  return x;
}
void put(int x) {
  if (!x)
    return;
  tag[x] ^= 1;
  swap(ch[x][1], ch[x][2]);
}
void down(int x) {
  if (tag[x])
    put(ch[x][0]), put(ch[x][1]), put(ch[x][2]), tag[x] = 0;
}
void work(int x, int d) {
  if (!d)
    return;
  down(x);
  int v = ch[x][2];
  ch[x][2] = ch[x][1];
  ch[x][1] = ch[x][0];
  ch[x][0] = v;
  work(v, d - 1);
}
void dfs(int x, int d, int v = 0) {
  if (!d)
    return p[vl[x]] = v, void();
  down(x);
  dfs(ch[x][0], d - 1, v);
  dfs(ch[x][1], d - 1, v + pw[n - d]);
  dfs(ch[x][2], d - 1, v + 2 * pw[n - d]);
}
int main() {
#ifdef FSYolanda
  freopen("1.in", "r", stdin);
#endif
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * 3;
  scanf("%s", S);
  int m = strlen(S);
  build(n, 0);
  for (int i = 0; i < m; i++) {
    if (S[i] == 'R')
      work(1, n);
    if (S[i] == 'S')
      put(1);
  }
  dfs(1, n);
  for (int i = 0; i < pw[n]; i++)
    cout << p[i] << " ";
  return 0;
}