Problem:
K: トーナメント
問題文
京都大学クスノキ前にて、$2$ 人用対戦ゲームのトーナメントが行われようとしています。
このトーナメントの参加者は $2^N$ 人いて、 $1$ から $2^N$ までの番号がついています。
参加者のうちの $2$ 人が戦った時の勝敗は、$0$ と $1$ からなる長さ $2^N-1$ の文字列 $S$ によって表されます。
人 $x$ と人 $y$ $(1 \le x < y \le 2^N)$ が戦ったとき、
$S_{y-x} = 0$ のとき、人 $x$ が勝ち、
$S_{y-x} = 1$ のとき、人 $y$ が勝つ
ことが分かっています。
トーナメントは参加者が一列に並ぶことで始まり、以下の通りに進行します。
列の先頭から $2$ 人ずつペアを作る。すべてのペアについて、ペア内の $2$ 人が戦う。
1 の対戦で勝った人は列に残り、負けた人は列から抜ける。
残っている人が $2$ 人以上いるときは、列を詰めて 1 に戻る。
残っている人が $1$ 人となったら、その人が優勝者となる。
いま、参加者は初期状態として、先頭から $i$ 番目 $(1 \le i \le 2^N)$ が人 $P_i$ となるように並んでいます。
$0 \le k \le 2^N-1$ を満たすすべての整数 $k$ について、以下の問題を解いてください。
初期状態から先頭 $k$ 人が、その順番を変えずに列の末尾に移動する。
つまり、移動後の列における参加者の番号を先頭から挙げていくと、 $P_{k+1}, P_{k+2}, ..., P_{2^N}, P_1, P_2, ..., P_k$ となる。
移動後の列からトーナメントを始めたときの、優勝者の番号を求めよ。
制約
$1 \leq N \leq 18$
$N$ は整数である。
$S$ は $0$ と $1$ からなる長さ $2^N-1$ の文字列である。
$P$ は $1$ から $2^N$ までの整数を並べ替えた順列である。
入力
入力は以下の形式で標準入力から与えられる。
$N$
$S_1S_2 \ldots S_{2^N-1}$
$P_1$ $P_2$ $\ldots$ $P_{2^N}$
出力
$2^N$ 行出力せよ。
$i$ 行目 $(1 \le i \le 2^N)$ には、$k = i-1$ としたときの上記の問題の答えを出力せよ。
入力例1
2
100
1 4 2 3
出力例1
1
2
1
2
例えば $k = 2$ としたとき、移動後の列における参加者の番号を先頭から挙げていくと、 $2, 3, 1, 4$ となります。
人 $2$ と 人 $3$ が戦うと、 $S_1 = 1$ より人 $3$ が勝ちます。
人 $1$ と 人 $4$ が戦うと、 $S_3 = 0$ より人 $1$ が勝ちます。
人 $3$ と 人 $1$ が戦うと、 $S_2 = 0$ より人 $1$ が勝ちます。
したがって、 $k = 2$ の場合の優勝者は、人 $1$ となります。
入力例2
4
101011100101000
8 15 2 9 12 5 1 7 14 10 11 3 4 6 16 13
出力例2
16
1
16
2
16
12
10
14
16
1
16
2
16
12
10
14
入力例3
1
0
1 2
出力例3
1
1

Submitted solutions:

code_snippet: // AOJ #3147
// Tournament 2025.2.13

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

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

char temp[263000];
string Cins() { // 文字列の入力　スペース以下の文字で入力終了
    char *s = temp;
	do *s = gc();
	while (*s++ > ' ');
	*(s-1) = 0;
    string input(temp); // 文字配列からstringを作成
    return input;
}

void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

string S;
 
inline int match(int a, int b) {
    if(a > b) swap(a, b);
    int diff = b - a;
    return (S[diff - 1] == '0') ? a : b;
}
 
int main(){
    int N = Cin(), L = 1 << N;
    S = Cins();
    
    vector<int> P(L);
    for (int i = 0; i < L; i++) P[i] = Cin();
    
    vector<int> B(2 * L);
    for (int i = 0; i < L; i++){
        B[i] = P[i];
        B[i+L] = P[i];
    }
    
    vector<vector<int>> dp;
    dp.push_back(B);
    for (int m = 1; m <= N; m++){
        int segLen = 1 << m;
        int half = 1 << (m-1);
        int prevSize = dp[m-1].size();
        int newSize = prevSize - half;
        vector<int> cur(newSize);
        for (int i = 0; i < newSize; i++){
            cur[i] = match(dp[m-1][i], dp[m-1][i + half]);
        }
        dp.push_back(move(cur));
    }
    for (int k = 0; k < L; k++) Cout(dp[N][k]);
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 43592
final_rank: 9

code_snippet: // AOJ #3147
// Tournament 2025.2.13

#include <bits/stdc++.h>
using namespace std;
 
string S;
 
inline int match(int a, int b) {
    if(a > b) swap(a, b);
    int diff = b - a;
    return (S[diff - 1] == '0') ? a : b;
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int N;
    cin >> N;
    int L = 1 << N;
    cin >> S;
    
    vector<int> P(L);
    for (int i = 0; i < L; i++) cin >> P[i];
    
    vector<int> B(2 * L);
    for (int i = 0; i < L; i++){
        B[i] = P[i];
        B[i+L] = P[i];
    }
    
    vector<vector<int>> dp;
    dp.push_back(B);
    for (int m = 1; m <= N; m++){
        int segLen = 1 << m;
        int half = 1 << (m-1);
        int prevSize = dp[m-1].size();
        int newSize = prevSize - half;
        vector<int> cur(newSize);
        for (int i = 0; i < newSize; i++){
            cur[i] = match(dp[m-1][i], dp[m-1][i + half]);
        }
        dp.push_back(move(cur));
    }
    
    for (int k = 0; k < L; k++)
        cout << dp[N][k] << endl;
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 43448
final_rank: 15

code_snippet: // competitive-verifier: PROBLEM
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    int n;
    cin >> n;
    int k = 1 << n;
    string s;
    cin >> s;
    vector<int> a(k);
    cin >> a;
    vector dp(n, vector(k, -1));
    auto dfs = [&](auto self, int l, int p) -> int {
        int li = l % k;
        if (p == -1) {
            return a[li];
        }
        if (dp[p][li] != -1)
            return dp[p][li];
        int m = (l + (1 << p)) % k;
        int le = self(self, l, p - 1);
        int re = self(self, m, p - 1);
        if (le > re)
            swap(le, re);
        int d = re - le - 1;
        if (s[d] == '0')
            return dp[p][li] = le;
        else
            return dp[p][li] = re;
    };
    rep (i, k) co(dfs(dfs, i, n - 1));
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 23944
final_rank: 4

code_snippet: // competitive-verifier: PROBLEM
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    int n;
    cin >> n;
    int k = 1 << n;
    string s;
    cin >> s;
    vector<int> a(k);
    cin >> a;
    vector dp(k, vector(n, -1));
    auto dfs = [&](auto self, int l, int p) -> int {
        int li = l % k;
        if (p == -1) {
            return a[li];
        }
        if (dp[li][p] != -1)
            return dp[li][p];
        int m = (l + (1 << p)) % k;
        int le = self(self, l, p - 1);
        int re = self(self, m, p - 1);
        if (le > re)
            swap(le, re);
        int d = re - le - 1;
        if (s[d] == '0')
            return dp[li][p] = le;
        else
            return dp[li][p] = re;
    };
    rep (i, k) co(dfs(dfs, i, n - 1));
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 31316
final_rank: 10

code_snippet: // competitive-verifier: PROBLEM
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    int n;
    cin >> n;
    int k = 1 << n;
    string s;
    cin >> s;
    vector<int> a(k);
    cin >> a;
    vector dp(k, vector(n + 1, -1));
    auto dfs = [&](auto self, int l, int p) -> int {
        int li = l % k;
        if (p == 0) {
            return a[li];
        }
        if (dp[li][p] != -1)
            return dp[li][p];
        int m = (l + (1 << (p - 1))) % k;
        int le = self(self, l, p - 1);
        int re = self(self, m, p - 1);
        if (le > re)
            swap(le, re);
        int d = re - le - 1;
        if (s[d] == '0')
            return dp[li][p] = le;
        else
            return dp[li][p] = re;
    };
    rep (i, k) co(dfs(dfs, i, n));
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 35348
final_rank: 11

code_snippet: // competitive-verifier: PROBLEM
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    int n;
    cin >> n;
    n = 1 << n;
    string s;
    cin >> s;
    vector<int> a(n);
    cin >> a;
    Dec >> a;
    unordered_map<ll, int> mp;
    auto dfs = [&](auto self, int l, int r) -> int {
        ll li = l % n;
        if (r - l == 1) {
            return a[li];
        }
        int ri = r % n;
        if (mp.count(li * n + ri))
            return mp[li * n + ri];
        int m = (l + r) / 2;
        int p = self(self, l, m);
        int q = self(self, m, r);
        if (p > q)
            swap(p, q);
        int d = abs(p - q) - 1;
        if (s[d] == '0')
            return mp[li * n + ri] = p;
        else
            return mp[li * n + ri] = q;
    };
    rep (i, n) co(dfs(dfs, i, n + i) + 1);
    return 0;
}
accuracy: 1
time_ms: 1120
memory_kb: 198728
final_rank: 17

code_snippet: // competitive-verifier: PROBLEM
#ifdef ATCODER
#pragma GCC target("sse4.2,avx512f,avx512dq,avx512ifma,avx512cd,avx512bw,avx512vl,bmi2")
#endif
#pragma GCC optimize("Ofast,fast-math,unroll-all-loops")
#include <bits/stdc++.h>
#ifndef ATCODER
#pragma GCC target("sse4.2,avx2,bmi2")
#endif
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = 3.14159265358979323846;
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }
    constexpr void operator()() const {}
} sonic;
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) os << (it == v.begin() ? "" : " ") << *it;
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
void Yes(bool is_correct = true) { std::cout << (is_correct ? "Yes\n" : "No\n"); }
void No(bool is_not_correct = true) { Yes(!is_not_correct); }
void YES(bool is_correct = true) { std::cout << (is_correct ? "YES\n" : "NO\n"); }
void NO(bool is_not_correct = true) { YES(!is_not_correct); }
void Takahashi(bool is_correct = true) { std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n'; }
void Aoki(bool is_not_correct = true) { Takahashi(!is_not_correct); }
struct increment_impl {
    template <class T>
    const increment_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) ++x;
        return *this;
    }
} Inc;
struct decrement_impl {
    template <class T>
    const decrement_impl &operator>>(std::vector<T> &v) const {
        for (auto &x : v) --x;
        return *this;
    }
} Dec;
struct sort_impl {
    template <class T>
    const sort_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        return *this;
    }
} Sort;
struct unique_impl {
    template <class T>
    const unique_impl &operator>>(std::vector<T> &v) const {
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());
        return *this;
    }
} Uniq;
int main(void) {
    int n;
    cin >> n;
    n = 1 << n;
    string s;
    cin >> s;
    vector<int> a(n);
    cin >> a;
    Dec >> a;
    unordered_map<ll, int> mp;
    auto dfs = [&](auto self, int l, int r) -> int {
        int ll = l % n;
        if (r - l == 1) {
            return a[ll];
        }
        int rr = r % n;
        if (mp.count(ll * n + rr))
            return mp[ll * n + rr];
        int m = (l + r) / 2;
        int p = self(self, l, m);
        int q = self(self, m, r);
        if (p > q)
            swap(p, q);
        int d = abs(p - q) - 1;
        if (s[d] == '0')
            return mp[ll * n + rr] = p;
        else
            return mp[ll * n + rr] = q;
    };
    rep (i, n) co(dfs(dfs, i, n + i) + 1);
    return 0;
}
accuracy: 0.6511627906976745
time_ms: 1050
memory_kb: 166008
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=int64_t;

int main(){
  int N;
  cin >> N;
  int n=pow(2,N);
  string s;
  cin >> s;
  vector<bool> r(n);
  for(int i=0;i<n-1;i++){
     r[i+1]=(s[i]=='1');
  }
  vector<int> p(n);
  for(int &x:p) cin >> x;
  
  vector w(N,vector<int>(n));
  for(int i=0;i<n/2;i++){
    int i1=i*2,i2=i1+1;
    if(r[abs(p[i1]-p[i2])]){
      w[0][i]=(p[i1]>p[i2] ? p[i1] : p[i2]);
    }else{
      w[0][i]=(p[i1]<p[i2] ? p[i1] : p[i2]);
    }
    i1=((i2+1)%n);
    if(r[abs(p[i1]-p[i2])]){
      w[0][i+n/2]=(p[i1]>p[i2] ? p[i1] : p[i2]);
    }else{
      w[0][i+n/2]=(p[i1]<p[i2] ? p[i1] : p[i2]);
    }
    
  }
  if(N==1){
    cout << w[0][0] << endl;
    cout << w[0][1] << endl;
    return 0;
  }
  
/*  for(int i=0;i<n;i++){
      cout << w[0][i] << " ";
  }
  cout << endl;
*/
  int n1=n,n2,nn=1;
  for(int j=1;j<N;j++){
    n1/=2;n2=n1/2;nn*=2;
    for(int k=0;k<2;k++){
      int k1=k*n/2;
      for(int ii=0;ii<nn;ii++){
        int ii1=n1*ii,ii2=n2*ii+k1;
        for(int i=0;i<n2;i++){
          int i1=i*2+k,i2=(i1+1)%n1;
          i1+=ii1;i2+=ii1;
          if(r[abs(w[j-1][i1]-w[j-1][i2])]){
            w[j][i+ii2]=(w[j-1][i1]>w[j-1][i2] ? w[j-1][i1] : w[j-1][i2]);
          }else{
            w[j][i+ii2]=(w[j-1][i1]<w[j-1][i2] ? w[j-1][i1] : w[j-1][i2]);
          }
          //cout << j << " " << i+ii2 << " " << ii1 << " " << i1 << " " << i2 << endl;
        }
      }
    }
  }
  for(int i=0;i<n;i++) cout << w[N-1][i] << endl;

}
accuracy: 1
time_ms: 280
memory_kb: 24316
final_rank: 12

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

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
//constexpr long long MOD = (ll)1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    ll to, cap, rev;
    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

int dp[1LL << 18][20];

void solve() {
    ll n;
    cin >> n;
    ll m = 1LL << n;
    string s;
    cin >> s;
    vec<int> p(m);
    REP(i, m) {
        cin >> p[i];
    }

    auto check = [&](auto &&self, int i, int x) -> int {
        if(dp[i][x] != 0)
            return dp[i][x];
        if(x == 0)
            return dp[i][x] = p[i];
        int L = self(self, i, x - 1);
        int R = self(self, (i + (1 << (x - 1))) % m, x - 1);
        return dp[i][x] = s[abs(R - L) - 1] == '1' ? max(R, L) : min(R, L);
    };
    REP(i, m) {
        cout << check(check, i, n) << en;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    while(t--)*/
    solve();

    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 25088
final_rank: 8

code_snippet: #include <iostream>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <functional>
#include <climits>
#include <numeric>
#include <queue>
#include <cmath>
#include <iomanip>
#include <array>
#include <string>
int winner(const int depth, int from, int until, const std::vector<std::vector<int>>& calculated, const std::string& state) {
	const auto length = 1 << depth;
	if (from + length == until && (calculated.front().size() <= from || until <= calculated.front().size())) 
		return calculated.front().size() <= from ? calculated[depth][from - calculated.front().size()] : calculated[depth][from];
	const auto a = winner(depth - 1, from, from + (length >> 1), calculated, state);
	const auto b = winner(depth - 1, from + (length >> 1), until, calculated, state);
	const auto x = std::min(a, b);
	const auto y = std::max(a, b);
	return state[y - x - 1] == '0' ? x : y;
}
int main() {
	int n; std::cin >> n;
	std::string str; std::cin >> str;
	std::vector<int> permutation(1 << n);
	for (auto& p : permutation) std::cin >> p;
	std::vector<std::vector<int>> calculated;
	calculated.push_back(permutation);
	for (auto d = 0; d < n; ++d) {
		std::vector<int> winner; winner.reserve(calculated.back().size() >> 1);
		const auto length = 1 << calculated.size();
		for (auto i = 0; i + length <= permutation.size(); ++i) {
			const auto x = std::min(calculated.back()[i], calculated.back()[i + (length >> 1)]);
			const auto y = std::max(calculated.back()[i], calculated.back()[i + (length >> 1)]);
			if (str[y - x - 1] == '0') 
				winner.push_back(x);
			else 
				winner.push_back(y);
		}
		calculated.push_back(winner);
	}
	for (auto i = 0; i < permutation.size(); ++i) {
		std::cout << winner(calculated.size() - 1, i, i + permutation.size(), calculated, str) << '\n';
	}
}

//https://onlinejudge.u-aizu.ac.jp/challenges/sources/VPC/KUPC/3147
accuracy: 1
time_ms: 180
memory_kb: 23176
final_rank: 6

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int N;
int POW[22],dp[20][1 << 20];
int P[1 << 20];
string line;

int main(){

	POW[0] = 1;
	for(int i = 1; i < 21; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%d",&N);

	cin >> line;

	for(int i = 0; i < POW[N]; i++){

		scanf("%d",&P[i]);
		P[i+POW[N]] = P[i];
	}

	for(int i = 0; i < 2*POW[N]; i++){

		dp[0][i] = P[i];
	}

	for(int i = 0; i < N; i++){
		for(int k = 0; k+POW[i] < 2*POW[N]; k++){

			int loc = max(dp[i][k],dp[i][k+POW[i]])-min(dp[i][k],dp[i][k+POW[i]]);

			if(line[loc-1] == '1'){

				dp[i+1][k] =  max(dp[i][k],dp[i][k+POW[i]]);

			}else{

				dp[i+1][k] = min(dp[i][k],dp[i][k+POW[i]]);
			}
		}
	}

	for(int i = 0; i < POW[N]; i++){

		printf("%d\n",dp[N][i]);
	}

	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 43476
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=998244353,MAX=1<<18,INF=1<<30;
int ans[MAX];
int N;
string win;

void DFS(vector<int> &S,int bit,int turn){
    if(turn==N){
        ans[bit]=S[0];
        return;
    }
    
    vector<int> T;
    for(int i=0;i<S.size();i+=2){
        int a=S[i],b=S[i+1];
        if(a>b) swap(a,b);
        if(win[b-a-1]=='1'){
            T.push_back(b);
        }else{
            T.push_back(a);
        }
    }
    
    DFS(T,bit,turn+1);
    
    T.clear();
    
    for(int i=1;i<S.size();i+=2){
        int a=S[i],b=S[(i+1)%int(S.size())];
        if(a>b) swap(a,b);
        if(win[b-a-1]=='1'){
            T.push_back(b);
        }else{
            T.push_back(a);
        }
    }
    
    DFS(T,(bit|(1<<turn)),turn+1);
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>N;
    cin>>win;
    
    vector<int> S((1<<N));
    for(int i=0;i<(1<<N);i++){
        cin>>S[i];
        S[i]--;
    }
    
    DFS(S,0,0);
    
    for(int i=0;i<(1<<N);i++){
        cout<<ans[i]+1<<endl;
    }
}
accuracy: 1
time_ms: 310
memory_kb: 6300
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(1); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=(1<<18)+10;
char s[N];
int p[N],n;
int win[N][20];

bool gao(int a,int b) {
	if (b>a) return s[b-a]=='0';
	else return s[a-b]=='1';
}
int main() {
	scanf("%d",&n);
	scanf("%s",s+1);
	rep(i,0,(1<<n)) {
		scanf("%d",p+i);
		win[i][0]=p[i];
	}
	rep(k,0,n) {
		rep(i,0,(1<<n)) {
			int u=win[i][k],v=win[(i+(1<<k))%(1<<n)][k];
			if (gao(u,v)) win[i][k+1]=u; else win[i][k+1]=v;
		}
	}
	rep(i,0,(1<<n)) printf("%d\n",win[i][n]);
}
accuracy: 1
time_ms: 100
memory_kb: 24944
final_rank: 1

code_snippet: #pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 10e8
#define INF 1<<30
#define MOD 1000000007
#define mod 998244353
#define REP(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp(pair<ll,ll>a,pair<ll,ll> b){
        if(a.se!=b.se)
        return a.se<b.se;
        else
        return a.fi<b.fi;
}
//----------------------------------------------------------------------
int n; string s; int p[1 << 19];
int dp[19][1 << 19];
//----------------------------------------------------------------------
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------  
    //ll begin_time=clock();
    //-------------------------------
    cin>>n>>s;
    for(ll i=0;i<pow(2,n);i++)cin>>p[i];
    for(ll i=0;i<pow(2,n);i++)p[i+(1<<n)]=p[i];
    //double link
    for(ll i=0;i<pow(2,n+1);i++){
        dp[0][i]=p[i];
    }
    for(ll p=0;p<n;p++){
        for(ll left=0;left<pow(2,n+1);left++){
            int right=left+(1<<p);
            if(right>=pow(2,n+1))continue;

            int x=dp[p][left];
            int y=dp[p][right];
            if(x>y)swap(x,y);
            if(s[y-x-1]=='0')dp[p+1][left]=x;
            else dp[p+1][left]=y;
        }
    }
    for(int i=0;i<pow(2,n);i++){
        cout<<dp[n][i]<<endl;
    }
    //-------------------------------  
    //ll end_time=clock();cout<<"time="<<end_time-begin_time<<"ms"<<endl;
    //-------------------------------
    return 0;
}
//----------------------------------------------------------------------
accuracy: 1
time_ms: 1240
memory_kb: 43584
final_rank: 16

code_snippet: #include <iostream> // cin, cout, cerr
#include <algorithm> // minmax, sort, swap
#include <numeric> // iota
#include <cstdio> // printf, scanf
#include <string> // string, stoi, to_string
#include <vector> // vector
#include <queue> // queue, priority_queue
#include <deque> // deque
#include <map> // key-value pairs sorted by keys
#include <set> // set
#include <iomanip> // cout<<setprecision(n)
#include <functional> // function<void(int)>
#include <cmath>
#include <cassert>
#include <bitset>

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i,n) for(int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
	int n;
	std::cin >> n;
	std::string s;
	std::cin >> s;
	std::vector<int> p(1<<n);
	rep(i,1<<n) std::cin >> p[i];
	auto f = [&] (int a, int b) -> int {
		if(a>b) std::swap(a,b);
		if(s[b-a-1]=='0') return a;
		return b;
	};
	std::vector<std::vector<int> > dp(n+1,std::vector<int>(1<<n));
	dp[0]=p;
	rep(i,n) {
		int dx=1<<i;
		rep(j,1<<n) {
			dp[i+1][j]=f(dp[i][j],dp[i][(j+dx)%(1<<n)]);
		}
	}
	debug(dp);
	rep(i,1<<n) print(dp[n][i]);

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 46492
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> a;
string s;
vector<vector<int>> dp(18,vector<int> (1<<18,-1));
int n;

int dfs(int k,int l,int r){
    if(k == n) return a[l];
    if(~dp[k][l]) return dp[k][l];
    int N = 1 << n;
    int p = 1 << (n - k - 1);
    int L = dfs(k + 1, l, (r - p + N)%N), R = dfs(k + 1, (l + p)%N, r);
    if(R < L) swap(L, R);
    int ret;
    if(s[R - L - 1] == '1'){
        ret = R;
    }
    else{
        ret = L;
    }
    return dp[k][l] = ret;
}




int main(){
    cin >> n;
    int N = 1 << n;
    cin >> s;
    a.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < N; i++) {
        printf("%d\n",dfs(0, i, i));
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 23096
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> a;
string s;
vector<vector<int>> dp(18,vector<int> (1<<18,-1));
int n;

LL get_hash(LL l, LL r){
    return l * (1 << n) + r;
}

int dfs(int k,int l,int r){
    if(k == n) return a[l];
    int ll = l % (1 << n);
    if(~dp[k][ll]) return dp[k][ll];
    int p = 1 << (n - k - 1);
    int L = dfs(k + 1, l, r - p), R = dfs(k + 1, l + p, r);
    if(R < L) swap(L, R);
    int ret;
    if(s[R - L - 1] == '1'){
        ret = R;
    }
    else{
        ret = L;
    }
    return dp[k][ll] = ret;
}




int main(){
    cin >> n;
    cin >> s;
    a.resize(2 * (1<<n));
    for (int i = 0; i < 1<<n; i++) {
        cin >> a[i];
        a[i+(1<<n)] = a[i];
    }
    for (int i = 0; i < (1<<n); i++) {
        printf("%d\n",dfs(0, i, i + (1<<n)));
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 24204
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> a;
string s;
unordered_map<int, int> dp;
int n;

LL get_hash(LL l, LL r){
    return l * (1 << n) + r;
}

int dfs(int k,int l,int r){
    if(k == n) return a[l];
    LL H = get_hash(l % (1 << n), r % (1 << n));
    if(dp.find(H) != dp.end()) return dp[H];
    int p = 1 << (n - k - 1);
    int L = dfs(k + 1, l, r - p), R = dfs(k + 1, l + p, r);
    if(R < L) swap(L, R);
    int ret;
    if(s[R - L - 1] == '1'){
        ret = R;
    }
    else{
        ret = L;
    }
    return dp[H] = ret;
}




int main(){
    cin >> n;
    cin >> s;
    a.resize(2 * (1<<n));
    for (int i = 0; i < 1<<n; i++) {
        cin >> a[i];
        a[i+(1<<n)] = a[i];
    }
    for (int i = 0; i < (1<<n); i++) {
        printf("%d\n",dfs(0, i, i + (1<<n)));
    }
    return 0;
}
accuracy: 0.6511627906976745
time_ms: 1730
memory_kb: 153948
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

vector<int> a;
string s;
unordered_map<int, int> dp;
int n;

int get_hash(int l, int r){
    return l * (1 << n) + r;
}

int dfs(int k,int l,int r){
    if(k == n) return a[l];
    int H = get_hash(l % (1 << n), r % (1 << n));
    if(dp.find(H) != dp.end()) return dp[H];
    int p = 1 << (n - k - 1);
    int L = dfs(k + 1, l, r - p), R = dfs(k + 1, l + p, r);
    if(R < L) swap(L, R);
    int ret;
    if(s[R - L - 1] == '1'){
        ret = R;
    }
    else{
        ret = L;
    }
    return dp[H] = ret;
}




int main(){
    cin >> n;
    cin >> s;
    a.resize(2 * (1<<n));
    for (int i = 0; i < 1<<n; i++) {
        cin >> a[i];
        a[i+(1<<n)] = a[i];
    }
    for (int i = 0; i < (1<<n); i++) {
        printf("%d\n",dfs(0, i, i + (1<<n)));
    }
    return 0;
}
accuracy: 0.6511627906976745
time_ms: 1700
memory_kb: 153952
final_rank: 19

code_snippet: /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` ``  ` ` ` ` ` ` ` ` `  dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` `` ````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` `` ```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` `  ````````.`````` `` ``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...`````  .,.`  ````....````` ``````` (Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<`` `````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ```````````````` ._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- `````````````````````````````````` ._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.`` OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____..  ````````  ..__~~_((<<!.`.``` .``.`` j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```  jOuWHHqHIdH
// gmmqkW!`(_ J>`    `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__```    zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. `   _ ?&._.I`_`````` .XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}  .`..````jqg@@@@@@
// XHWZ{..<Jk~!.`.. (<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;< ._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~` _-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
// kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
// @@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~``` ~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
// @gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
// mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
// qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~`` (jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ```` d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( `` WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
// XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<`` (v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.`` :.~+=?~.(;_(...jdQQQQQkkk
// H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
// @@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
// @H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
// @@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
// @@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
// MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
// HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
// HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
// rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%`` (Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ ` _``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>` ~````.OXtt>~._<?MM
*/
#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r) //[l, r]
{
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
    return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
    cout<<x;
    if(suc==1) cout<<'\n';
    else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
    for(int i=0;i<v.size();i++)
    print(v[i],i==(int)(v.size())-1?suc:2);
}
int dp[1<<18][19];
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int n2=1<<n;
    vi p(n2);
    auto f=[&](int x,int y)
    {
        if(x>y) swap(x,y);
        return s[y-x-1]=='1'?y:x;
    };
    for(int i=0;i<n2;i++) cin>>p[i];
    for(int i=0;i<n2;i++)
        dp[i][0]=p[i];
    for(int j=1;j<=n;j++)
    {
        int len=1<<(j-1);
        for(int i=0;i<n2;i++)
        {
            int r=(i+len)%n2;
            dp[i][j]=f(dp[i][j-1],dp[r][j-1]);
            //dbg(i,j,dp[i][j]);
        }
    }
    for(int i=0;i<n2;i++) cout<<dp[i][n]<<'\n';
}
accuracy: 1
time_ms: 140
memory_kb: 23792
final_rank: 2