#include <bits/stdc++.h>
using namespace std;
const int L = 20;
using Node = map<int, int>;
string s;
Node seg[1 << (L + 2)];
bool need[L][3][3];
string conv(int x) {
string ans;
while (x > 0) {
int d = x % 3;
x /= 3;
ans.push_back(d + '0');
}
for (auto &i : ans) {
if (i == '2') {
i = '?';
}
}
return ans;
}
void construct(int v, int tl, int tr, int d) {
if (tl == tr) {
seg[v][0] = s[tl] - '0';
return;
}
int tm = (tl + tr) / 2;
construct(2 * v, tl, tm, d + 1);
construct(2 * v + 1, tm + 1, tr, d + 1);
for (auto &i : seg[2 * v]) {
if (need[d][0][i.first % 3]) {
seg[v][3 * i.first] = i.second;
}
if (need[d][2][i.first % 3]) {
seg[v][3 * i.first + 2] = seg[2 * v][i.first] + seg[2 * v + 1][i.first];
}
}
for (auto &i : seg[2 * v + 1]) {
if (need[d][1][i.first % 3]) {
seg[v][3 * i.first + 1] = i.second;
}
}
seg[2 * v].clear();
seg[2 * v + 1].clear();
// cout << "seg[" << tl << "-" << tr << "] contains:\n";
// for (auto &i : seg[v]) {
// cout << conv(i.first) << "\n";
// }
// cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int l, q;
cin >> l >> q;
cin >> s;
vector<int> queries;
while (q--) {
string sq;
cin >> sq;
int x = 0;
for (char &c : sq) {
if (c == '?') {
c = '2';
}
}
for (int i = sq.length() - 1; i >= 0; --i) {
x = 3 * x + (sq[i] - '0');
if (i != sq.length() - 1) {
need[i][sq[i] - '0'][sq[i + 1] - '0'] = true;
} else {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
need[i][j][k] = true;
}
}
}
}
queries.push_back(x);
}
construct(1, 0, (1 << l) - 1, 0);
// cout << "sz = " << seg[1].size() << "\n";
for (auto &i : queries) {
// pair<int, int> p = {i, 0};
// cout << get(seg[1]) << "\n";
cout << seg[1][i] << "\n";
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:83:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | if (i != sq.length() - 1) {
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
18 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |