#include <bits/stdc++.h>
using namespace std;
int ans[1 << 20];
int f(char c){
if(c == '0') return 0;
if(c == '1') return 1;
if(c == '?') return 2;
return -1;
}
vector<pair<long long, int>> Q;
void solve(int L, int* a, int l, int r){
if(l == r) return;
if(L == 0){
for(int i = l; i < r; i++){
ans[Q[i].second] = a[0];
}
return;
}
int mid_0 = l; while(mid_0 < r && (Q[mid_0].first & (3LL << (L - 1) * 2)) == 0) mid_0++;
int mid_1 = mid_0; while(mid_1 < r && ((Q[mid_1].first & (3LL << (L - 1) * 2)) >> (L - 1) * 2) == 1) mid_1++;
// 0
solve(L - 1, a, l, mid_0);
// 1
solve(L - 1, a + (1 << L - 1), mid_0, mid_1);
// ?
for(int i = 0; i < (1 << L - 1); i++) a[i] += a[i + (1 << L - 1)];
solve(L - 1, a, mid_1, r);
for(int i = 0; i < (1 << L - 1); i++) a[i] -= a[i + (1 << L - 1)];
}
int a[1 << 20];
int32_t main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
int L, q; cin >> L >> q; string s; cin >> s;
for(int i = 0; i < (1 << L); i++) a[i] = s[i] - '0';
for(int i = 0; i < q; i++){
string s; cin >> s; reverse(s.begin(), s.end());
long long b = 0;
for(int j = 0; j < L; j++){
b |= f(s[j]) * (1LL << j * 2);
}
Q.push_back({b, i});
}
sort(Q.begin(), Q.end());
solve(L, a, 0, Q.size());
for(int i = 0; i < q; i++){
cout << ans[i] << '\n';
}
}
Compilation message
snake_escaping.cpp: In function 'void solve(int, int*, int, int)':
snake_escaping.cpp:26:30: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
26 | solve(L - 1, a + (1 << L - 1), mid_0, mid_1);
| ~~^~~
snake_escaping.cpp:28:32: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
28 | for(int i = 0; i < (1 << L - 1); i++) a[i] += a[i + (1 << L - 1)];
| ~~^~~
snake_escaping.cpp:28:65: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
28 | for(int i = 0; i < (1 << L - 1); i++) a[i] += a[i + (1 << L - 1)];
| ~~^~~
snake_escaping.cpp:30:32: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
30 | for(int i = 0; i < (1 << L - 1); i++) a[i] -= a[i + (1 << L - 1)];
| ~~^~~
snake_escaping.cpp:30:65: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
30 | for(int i = 0; i < (1 << L - 1); i++) a[i] -= a[i + (1 << L - 1)];
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
261 ms |
23960 KB |
Output is correct |
12 |
Correct |
270 ms |
23596 KB |
Output is correct |
13 |
Correct |
276 ms |
22872 KB |
Output is correct |
14 |
Correct |
282 ms |
22984 KB |
Output is correct |
15 |
Correct |
265 ms |
23976 KB |
Output is correct |
16 |
Correct |
282 ms |
23248 KB |
Output is correct |
17 |
Correct |
293 ms |
23152 KB |
Output is correct |
18 |
Correct |
154 ms |
25060 KB |
Output is correct |
19 |
Correct |
253 ms |
21976 KB |
Output is correct |
20 |
Correct |
271 ms |
23640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
261 ms |
23960 KB |
Output is correct |
12 |
Correct |
270 ms |
23596 KB |
Output is correct |
13 |
Correct |
276 ms |
22872 KB |
Output is correct |
14 |
Correct |
282 ms |
22984 KB |
Output is correct |
15 |
Correct |
265 ms |
23976 KB |
Output is correct |
16 |
Correct |
282 ms |
23248 KB |
Output is correct |
17 |
Correct |
293 ms |
23152 KB |
Output is correct |
18 |
Correct |
154 ms |
25060 KB |
Output is correct |
19 |
Correct |
253 ms |
21976 KB |
Output is correct |
20 |
Correct |
271 ms |
23640 KB |
Output is correct |
21 |
Correct |
306 ms |
24000 KB |
Output is correct |
22 |
Correct |
291 ms |
24264 KB |
Output is correct |
23 |
Correct |
310 ms |
23276 KB |
Output is correct |
24 |
Correct |
313 ms |
23072 KB |
Output is correct |
25 |
Correct |
290 ms |
25000 KB |
Output is correct |
26 |
Correct |
311 ms |
23608 KB |
Output is correct |
27 |
Correct |
317 ms |
23448 KB |
Output is correct |
28 |
Correct |
158 ms |
26044 KB |
Output is correct |
29 |
Correct |
273 ms |
22032 KB |
Output is correct |
30 |
Correct |
299 ms |
24196 KB |
Output is correct |
31 |
Correct |
302 ms |
24080 KB |
Output is correct |
32 |
Correct |
299 ms |
24008 KB |
Output is correct |
33 |
Correct |
288 ms |
22864 KB |
Output is correct |
34 |
Correct |
305 ms |
23108 KB |
Output is correct |
35 |
Correct |
316 ms |
23468 KB |
Output is correct |
36 |
Correct |
161 ms |
22056 KB |
Output is correct |
37 |
Correct |
271 ms |
24100 KB |
Output is correct |
38 |
Correct |
283 ms |
21988 KB |
Output is correct |
39 |
Correct |
305 ms |
23216 KB |
Output is correct |
40 |
Correct |
302 ms |
23108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
31 ms |
7660 KB |
Output is correct |
12 |
Correct |
33 ms |
9724 KB |
Output is correct |
13 |
Correct |
101 ms |
9656 KB |
Output is correct |
14 |
Correct |
114 ms |
9652 KB |
Output is correct |
15 |
Correct |
74 ms |
9784 KB |
Output is correct |
16 |
Correct |
130 ms |
9652 KB |
Output is correct |
17 |
Correct |
120 ms |
9664 KB |
Output is correct |
18 |
Correct |
17 ms |
9884 KB |
Output is correct |
19 |
Correct |
31 ms |
9528 KB |
Output is correct |
20 |
Correct |
32 ms |
9796 KB |
Output is correct |
21 |
Correct |
99 ms |
9708 KB |
Output is correct |
22 |
Correct |
110 ms |
9656 KB |
Output is correct |
23 |
Correct |
74 ms |
9656 KB |
Output is correct |
24 |
Correct |
113 ms |
9648 KB |
Output is correct |
25 |
Correct |
134 ms |
9668 KB |
Output is correct |
26 |
Correct |
16 ms |
9608 KB |
Output is correct |
27 |
Correct |
31 ms |
9780 KB |
Output is correct |
28 |
Correct |
34 ms |
9620 KB |
Output is correct |
29 |
Correct |
105 ms |
9664 KB |
Output is correct |
30 |
Correct |
110 ms |
9652 KB |
Output is correct |
31 |
Correct |
77 ms |
9660 KB |
Output is correct |
32 |
Correct |
115 ms |
9728 KB |
Output is correct |
33 |
Correct |
118 ms |
9660 KB |
Output is correct |
34 |
Correct |
17 ms |
9584 KB |
Output is correct |
35 |
Correct |
103 ms |
9652 KB |
Output is correct |
36 |
Correct |
107 ms |
9656 KB |
Output is correct |
37 |
Correct |
106 ms |
9720 KB |
Output is correct |
38 |
Correct |
107 ms |
9656 KB |
Output is correct |
39 |
Correct |
104 ms |
9660 KB |
Output is correct |
40 |
Correct |
103 ms |
9664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
261 ms |
23960 KB |
Output is correct |
12 |
Correct |
270 ms |
23596 KB |
Output is correct |
13 |
Correct |
276 ms |
22872 KB |
Output is correct |
14 |
Correct |
282 ms |
22984 KB |
Output is correct |
15 |
Correct |
265 ms |
23976 KB |
Output is correct |
16 |
Correct |
282 ms |
23248 KB |
Output is correct |
17 |
Correct |
293 ms |
23152 KB |
Output is correct |
18 |
Correct |
154 ms |
25060 KB |
Output is correct |
19 |
Correct |
253 ms |
21976 KB |
Output is correct |
20 |
Correct |
271 ms |
23640 KB |
Output is correct |
21 |
Correct |
306 ms |
24000 KB |
Output is correct |
22 |
Correct |
291 ms |
24264 KB |
Output is correct |
23 |
Correct |
310 ms |
23276 KB |
Output is correct |
24 |
Correct |
313 ms |
23072 KB |
Output is correct |
25 |
Correct |
290 ms |
25000 KB |
Output is correct |
26 |
Correct |
311 ms |
23608 KB |
Output is correct |
27 |
Correct |
317 ms |
23448 KB |
Output is correct |
28 |
Correct |
158 ms |
26044 KB |
Output is correct |
29 |
Correct |
273 ms |
22032 KB |
Output is correct |
30 |
Correct |
299 ms |
24196 KB |
Output is correct |
31 |
Correct |
302 ms |
24080 KB |
Output is correct |
32 |
Correct |
299 ms |
24008 KB |
Output is correct |
33 |
Correct |
288 ms |
22864 KB |
Output is correct |
34 |
Correct |
305 ms |
23108 KB |
Output is correct |
35 |
Correct |
316 ms |
23468 KB |
Output is correct |
36 |
Correct |
161 ms |
22056 KB |
Output is correct |
37 |
Correct |
271 ms |
24100 KB |
Output is correct |
38 |
Correct |
283 ms |
21988 KB |
Output is correct |
39 |
Correct |
305 ms |
23216 KB |
Output is correct |
40 |
Correct |
302 ms |
23108 KB |
Output is correct |
41 |
Correct |
31 ms |
7660 KB |
Output is correct |
42 |
Correct |
33 ms |
9724 KB |
Output is correct |
43 |
Correct |
101 ms |
9656 KB |
Output is correct |
44 |
Correct |
114 ms |
9652 KB |
Output is correct |
45 |
Correct |
74 ms |
9784 KB |
Output is correct |
46 |
Correct |
130 ms |
9652 KB |
Output is correct |
47 |
Correct |
120 ms |
9664 KB |
Output is correct |
48 |
Correct |
17 ms |
9884 KB |
Output is correct |
49 |
Correct |
31 ms |
9528 KB |
Output is correct |
50 |
Correct |
32 ms |
9796 KB |
Output is correct |
51 |
Correct |
99 ms |
9708 KB |
Output is correct |
52 |
Correct |
110 ms |
9656 KB |
Output is correct |
53 |
Correct |
74 ms |
9656 KB |
Output is correct |
54 |
Correct |
113 ms |
9648 KB |
Output is correct |
55 |
Correct |
134 ms |
9668 KB |
Output is correct |
56 |
Correct |
16 ms |
9608 KB |
Output is correct |
57 |
Correct |
31 ms |
9780 KB |
Output is correct |
58 |
Correct |
34 ms |
9620 KB |
Output is correct |
59 |
Correct |
105 ms |
9664 KB |
Output is correct |
60 |
Correct |
110 ms |
9652 KB |
Output is correct |
61 |
Correct |
77 ms |
9660 KB |
Output is correct |
62 |
Correct |
115 ms |
9728 KB |
Output is correct |
63 |
Correct |
118 ms |
9660 KB |
Output is correct |
64 |
Correct |
17 ms |
9584 KB |
Output is correct |
65 |
Correct |
103 ms |
9652 KB |
Output is correct |
66 |
Correct |
107 ms |
9656 KB |
Output is correct |
67 |
Correct |
106 ms |
9720 KB |
Output is correct |
68 |
Correct |
107 ms |
9656 KB |
Output is correct |
69 |
Correct |
104 ms |
9660 KB |
Output is correct |
70 |
Correct |
103 ms |
9664 KB |
Output is correct |
71 |
Correct |
387 ms |
52560 KB |
Output is correct |
72 |
Correct |
405 ms |
52720 KB |
Output is correct |
73 |
Correct |
697 ms |
51296 KB |
Output is correct |
74 |
Correct |
729 ms |
51608 KB |
Output is correct |
75 |
Correct |
504 ms |
53552 KB |
Output is correct |
76 |
Correct |
767 ms |
51880 KB |
Output is correct |
77 |
Correct |
830 ms |
51736 KB |
Output is correct |
78 |
Correct |
228 ms |
55592 KB |
Output is correct |
79 |
Correct |
388 ms |
49548 KB |
Output is correct |
80 |
Correct |
389 ms |
52716 KB |
Output is correct |
81 |
Correct |
669 ms |
52608 KB |
Output is correct |
82 |
Correct |
773 ms |
51604 KB |
Output is correct |
83 |
Correct |
549 ms |
50880 KB |
Output is correct |
84 |
Correct |
773 ms |
51624 KB |
Output is correct |
85 |
Correct |
814 ms |
51812 KB |
Output is correct |
86 |
Correct |
219 ms |
49624 KB |
Output is correct |
87 |
Correct |
386 ms |
52708 KB |
Output is correct |
88 |
Correct |
404 ms |
49544 KB |
Output is correct |
89 |
Correct |
683 ms |
51344 KB |
Output is correct |
90 |
Correct |
789 ms |
51540 KB |
Output is correct |
91 |
Correct |
542 ms |
50716 KB |
Output is correct |
92 |
Correct |
766 ms |
51848 KB |
Output is correct |
93 |
Correct |
849 ms |
51808 KB |
Output is correct |
94 |
Correct |
243 ms |
49576 KB |
Output is correct |
95 |
Correct |
722 ms |
51620 KB |
Output is correct |
96 |
Correct |
718 ms |
51672 KB |
Output is correct |
97 |
Correct |
731 ms |
51732 KB |
Output is correct |
98 |
Correct |
712 ms |
51736 KB |
Output is correct |
99 |
Correct |
720 ms |
51660 KB |
Output is correct |
100 |
Correct |
698 ms |
51768 KB |
Output is correct |