#include <bits/stdc++.h>
using namespace std;
const int L = 20;
class Node {
public:
vector<pair<int, int>> x;
};
string s;
Node seg[1 << (L + 2)];
bool need[L][3];
void construct(int v, int tl, int tr, int d) {
if (tl == tr) {
seg[v].x.push_back({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);
seg[v].x.reserve(accumulate(need[d], need[d] + 3, 0) * seg[2 * v].x.size());
for (int i = 0; i < 3; ++i) {
if (!need[d][i]) {
continue;
}
for (int j = 0; j < seg[2 * v].x.size(); ++j) {
pair<int, int> p = {j, 0};
if (i == 0) {
seg[v].x.push_back(
{3 * j + i,
lower_bound(seg[2 * v].x.begin(), seg[2 * v].x.end(), p)->second});
} else if (i == 1) {
seg[v].x.push_back({3 * j + i, lower_bound(seg[2 * v + 1].x.begin(),
seg[2 * v + 1].x.end(), p)
->second});
} else {
seg[v].x.push_back(
{3 * j + i,
lower_bound(seg[2 * v].x.begin(), seg[2 * v].x.end(), p)->second +
lower_bound(seg[2 * v + 1].x.begin(), seg[2 * v + 1].x.end(),
p)
->second});
}
}
}
seg[2 * v].x.clear();
seg[2 * v + 1].x.clear();
sort(seg[v].x.begin(), seg[v].x.end());
}
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 (int i = sq.length() - 1; i >= 0; --i) {
char c = sq[i];
if (c == '?') {
c = '2';
}
x = 3 * x + (c - '0');
need[i][c - '0'] = true;
}
queries.push_back(x);
}
construct(1, 0, (1 << l) - 1, 0);
for (auto &i : queries) {
pair<int, int> p = {i, 0};
cout << lower_bound(seg[1].x.begin(), seg[1].x.end(), p)->second << "\n";
}
}
Compilation message
snake_escaping.cpp: In function 'void construct(int, int, int, int)':
snake_escaping.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int j = 0; j < seg[2 * v].x.size(); ++j) {
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
13 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |