#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define bl " "
#define endl "\n"
#define all(v) v.begin(), v.end()
#define comp(v) v.erase(unique(all(v)), v.end())
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1000100/5;
const int MOD = 1e9+7;
struct Query {
string s; int x;
}qry[MAXN];
queue<Query> qq[3];
vector<int> v;
int ans[MAXN];
void solve(int s, int e, int l, int r, int x) {
if(s > e) return;
if(l == r) {
for(int i = s; i <= e; i++) ans[qry[i].x] = v[l];
return;
}
int a = s, mid = l+r>>1;
for(a = s; a <= e; a++) {
if(qry[a].s[x] != '0') break;
}
solve(s, a-1, l, mid, x+1);
s = a;
for(; a <= e; a++) {
if(qry[a].s[x] != '1') break;
}
solve(s, a-1, mid+1, r, x+1);
s = a;
for(int i = l, j = mid+1; j <= r; i++, j++) v[i] += v[j];
solve(s, e, l, mid, x+1);
for(int i = l, j = mid+1; j <= r; i++, j++) v[i] -= v[j];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int l, q; cin >> l >> q; char x;
for(int i = 0; i < (1<<l); i++) {
cin >> x; v.push_back(x-'0');
}
for(int i = 0; i < 5; i++) {
int s = q*i/5+1, e = q*(i+1)/5;
for(int j = s; j <= e; j++) {
cin >> qry[j-s].s; qry[j-s].x = j-s;
}
//sorting
for(int j = l-1; j >= 0; j--) {
for(int k = 0; k <= e-s; k++) {
if(qry[k].s[j] == '0') qq[0].push(qry[k]);
else if(qry[k].s[j] == '1') qq[1].push(qry[k]);
else qq[2].push(qry[k]);
}
int idx = 0;
for(int k = 0; k < 3; k++) {
while(qq[k].size()) {
qry[idx++] = qq[k].front();
qq[k].pop();
}
}
}
solve(0, e-s, 0, (1<<l)-1, 0);
for(int j = 0; j <= e-s; j++) cout << ans[j] << endl;
}
}
Compilation message
snake_escaping.cpp: In function 'void solve(int, int, int, int, int)':
snake_escaping.cpp:33:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int a = s, mid = l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8888 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8952 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8888 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8952 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
395 ms |
20684 KB |
Output is correct |
12 |
Correct |
433 ms |
21076 KB |
Output is correct |
13 |
Correct |
433 ms |
20244 KB |
Output is correct |
14 |
Correct |
434 ms |
20476 KB |
Output is correct |
15 |
Correct |
429 ms |
21752 KB |
Output is correct |
16 |
Correct |
450 ms |
20000 KB |
Output is correct |
17 |
Correct |
463 ms |
20756 KB |
Output is correct |
18 |
Correct |
333 ms |
22244 KB |
Output is correct |
19 |
Correct |
428 ms |
19292 KB |
Output is correct |
20 |
Correct |
430 ms |
20848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8888 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8952 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
395 ms |
20684 KB |
Output is correct |
12 |
Correct |
433 ms |
21076 KB |
Output is correct |
13 |
Correct |
433 ms |
20244 KB |
Output is correct |
14 |
Correct |
434 ms |
20476 KB |
Output is correct |
15 |
Correct |
429 ms |
21752 KB |
Output is correct |
16 |
Correct |
450 ms |
20000 KB |
Output is correct |
17 |
Correct |
463 ms |
20756 KB |
Output is correct |
18 |
Correct |
333 ms |
22244 KB |
Output is correct |
19 |
Correct |
428 ms |
19292 KB |
Output is correct |
20 |
Correct |
430 ms |
20848 KB |
Output is correct |
21 |
Correct |
478 ms |
21684 KB |
Output is correct |
22 |
Correct |
475 ms |
21524 KB |
Output is correct |
23 |
Correct |
551 ms |
20456 KB |
Output is correct |
24 |
Correct |
576 ms |
20408 KB |
Output is correct |
25 |
Correct |
520 ms |
22356 KB |
Output is correct |
26 |
Correct |
603 ms |
21232 KB |
Output is correct |
27 |
Correct |
631 ms |
21128 KB |
Output is correct |
28 |
Correct |
395 ms |
23400 KB |
Output is correct |
29 |
Correct |
475 ms |
19384 KB |
Output is correct |
30 |
Correct |
506 ms |
21588 KB |
Output is correct |
31 |
Correct |
582 ms |
21388 KB |
Output is correct |
32 |
Correct |
536 ms |
21604 KB |
Output is correct |
33 |
Correct |
493 ms |
20252 KB |
Output is correct |
34 |
Correct |
625 ms |
20584 KB |
Output is correct |
35 |
Correct |
596 ms |
20896 KB |
Output is correct |
36 |
Correct |
397 ms |
19320 KB |
Output is correct |
37 |
Correct |
481 ms |
21368 KB |
Output is correct |
38 |
Correct |
470 ms |
19284 KB |
Output is correct |
39 |
Correct |
556 ms |
20556 KB |
Output is correct |
40 |
Correct |
541 ms |
20632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8888 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8952 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
117 ms |
14544 KB |
Output is correct |
12 |
Correct |
128 ms |
14804 KB |
Output is correct |
13 |
Correct |
333 ms |
14272 KB |
Output is correct |
14 |
Correct |
365 ms |
14492 KB |
Output is correct |
15 |
Correct |
250 ms |
14804 KB |
Output is correct |
16 |
Correct |
366 ms |
14404 KB |
Output is correct |
17 |
Correct |
374 ms |
14404 KB |
Output is correct |
18 |
Correct |
61 ms |
14420 KB |
Output is correct |
19 |
Correct |
113 ms |
14256 KB |
Output is correct |
20 |
Correct |
119 ms |
14544 KB |
Output is correct |
21 |
Correct |
328 ms |
14472 KB |
Output is correct |
22 |
Correct |
386 ms |
14560 KB |
Output is correct |
23 |
Correct |
258 ms |
14284 KB |
Output is correct |
24 |
Correct |
377 ms |
14528 KB |
Output is correct |
25 |
Correct |
374 ms |
14472 KB |
Output is correct |
26 |
Correct |
65 ms |
14548 KB |
Output is correct |
27 |
Correct |
114 ms |
14296 KB |
Output is correct |
28 |
Correct |
115 ms |
14288 KB |
Output is correct |
29 |
Correct |
330 ms |
14544 KB |
Output is correct |
30 |
Correct |
367 ms |
14560 KB |
Output is correct |
31 |
Correct |
292 ms |
14300 KB |
Output is correct |
32 |
Correct |
384 ms |
14524 KB |
Output is correct |
33 |
Correct |
391 ms |
14288 KB |
Output is correct |
34 |
Correct |
76 ms |
14524 KB |
Output is correct |
35 |
Correct |
342 ms |
14656 KB |
Output is correct |
36 |
Correct |
338 ms |
14800 KB |
Output is correct |
37 |
Correct |
334 ms |
14524 KB |
Output is correct |
38 |
Correct |
362 ms |
14248 KB |
Output is correct |
39 |
Correct |
352 ms |
14248 KB |
Output is correct |
40 |
Correct |
336 ms |
14544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8888 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8952 KB |
Output is correct |
8 |
Correct |
2 ms |
8792 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
395 ms |
20684 KB |
Output is correct |
12 |
Correct |
433 ms |
21076 KB |
Output is correct |
13 |
Correct |
433 ms |
20244 KB |
Output is correct |
14 |
Correct |
434 ms |
20476 KB |
Output is correct |
15 |
Correct |
429 ms |
21752 KB |
Output is correct |
16 |
Correct |
450 ms |
20000 KB |
Output is correct |
17 |
Correct |
463 ms |
20756 KB |
Output is correct |
18 |
Correct |
333 ms |
22244 KB |
Output is correct |
19 |
Correct |
428 ms |
19292 KB |
Output is correct |
20 |
Correct |
430 ms |
20848 KB |
Output is correct |
21 |
Correct |
478 ms |
21684 KB |
Output is correct |
22 |
Correct |
475 ms |
21524 KB |
Output is correct |
23 |
Correct |
551 ms |
20456 KB |
Output is correct |
24 |
Correct |
576 ms |
20408 KB |
Output is correct |
25 |
Correct |
520 ms |
22356 KB |
Output is correct |
26 |
Correct |
603 ms |
21232 KB |
Output is correct |
27 |
Correct |
631 ms |
21128 KB |
Output is correct |
28 |
Correct |
395 ms |
23400 KB |
Output is correct |
29 |
Correct |
475 ms |
19384 KB |
Output is correct |
30 |
Correct |
506 ms |
21588 KB |
Output is correct |
31 |
Correct |
582 ms |
21388 KB |
Output is correct |
32 |
Correct |
536 ms |
21604 KB |
Output is correct |
33 |
Correct |
493 ms |
20252 KB |
Output is correct |
34 |
Correct |
625 ms |
20584 KB |
Output is correct |
35 |
Correct |
596 ms |
20896 KB |
Output is correct |
36 |
Correct |
397 ms |
19320 KB |
Output is correct |
37 |
Correct |
481 ms |
21368 KB |
Output is correct |
38 |
Correct |
470 ms |
19284 KB |
Output is correct |
39 |
Correct |
556 ms |
20556 KB |
Output is correct |
40 |
Correct |
541 ms |
20632 KB |
Output is correct |
41 |
Correct |
117 ms |
14544 KB |
Output is correct |
42 |
Correct |
128 ms |
14804 KB |
Output is correct |
43 |
Correct |
333 ms |
14272 KB |
Output is correct |
44 |
Correct |
365 ms |
14492 KB |
Output is correct |
45 |
Correct |
250 ms |
14804 KB |
Output is correct |
46 |
Correct |
366 ms |
14404 KB |
Output is correct |
47 |
Correct |
374 ms |
14404 KB |
Output is correct |
48 |
Correct |
61 ms |
14420 KB |
Output is correct |
49 |
Correct |
113 ms |
14256 KB |
Output is correct |
50 |
Correct |
119 ms |
14544 KB |
Output is correct |
51 |
Correct |
328 ms |
14472 KB |
Output is correct |
52 |
Correct |
386 ms |
14560 KB |
Output is correct |
53 |
Correct |
258 ms |
14284 KB |
Output is correct |
54 |
Correct |
377 ms |
14528 KB |
Output is correct |
55 |
Correct |
374 ms |
14472 KB |
Output is correct |
56 |
Correct |
65 ms |
14548 KB |
Output is correct |
57 |
Correct |
114 ms |
14296 KB |
Output is correct |
58 |
Correct |
115 ms |
14288 KB |
Output is correct |
59 |
Correct |
330 ms |
14544 KB |
Output is correct |
60 |
Correct |
367 ms |
14560 KB |
Output is correct |
61 |
Correct |
292 ms |
14300 KB |
Output is correct |
62 |
Correct |
384 ms |
14524 KB |
Output is correct |
63 |
Correct |
391 ms |
14288 KB |
Output is correct |
64 |
Correct |
76 ms |
14524 KB |
Output is correct |
65 |
Correct |
342 ms |
14656 KB |
Output is correct |
66 |
Correct |
338 ms |
14800 KB |
Output is correct |
67 |
Correct |
334 ms |
14524 KB |
Output is correct |
68 |
Correct |
362 ms |
14248 KB |
Output is correct |
69 |
Correct |
352 ms |
14248 KB |
Output is correct |
70 |
Correct |
336 ms |
14544 KB |
Output is correct |
71 |
Execution timed out |
2098 ms |
48636 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |