#pragma GCC optimize("Ofast,O3")
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
string S;
void solve(vector<uint>&V, vector<int>&ans, int l, int r, uint akt) {
if(!V.size()) return;
if(l==r) {
ans[0]=S[l]-'0';
return;
}
int ile1=0, ile2=0, ile3=0;
rep(i, V.size()) {
if(V[i]/akt==0) ++ile1;
else if(V[i]/akt==1) ++ile2;
else ++ile3;
V[i]%=akt;
}
vector<uint>zero;
zero.reserve(V.size());
int l1=0, l2=0;
while(l1<ile1 && l2<ile2) {
if(V[l1]<=V[ile1+l2]) {
if(V[l1]==V[ile1+l2]) ++l2;
zero.pb(V[l1]);
++l1;
} else {
zero.pb(V[ile1+l2]);
++l2;
}
}
while(l1<ile1) {
zero.pb(V[l1]); ++l1;
}
while(l2<ile2) {
zero.pb(V[ile1+l2]); ++l2;
}
vector<int>zans(zero.size());
solve(zero, zans, l, (l+r)/2, akt/3);
l1=0;
rep(i, ile1) {
while(zero[l1]<V[i]) ++l1;
ans[i]+=zans[l1];
}
l1=0;
rep(i, ile2) {
while(zero[l1]<V[ile1+i]) ++l1;
ans[ile1+i]+=zans[l1];
}
zero.clear();
l1=0; l2=0;
while(l1<ile1 && l2<ile3) {
if(V[l1]<=V[ile1+ile2+l2]) {
if(V[l1]==V[ile1+ile2+l2]) ++l2;
zero.pb(V[l1]);
++l1;
} else {
zero.pb(V[ile1+ile2+l2]);
++l2;
}
}
while(l1<ile1) {
zero.pb(V[l1]); ++l1;
}
while(l2<ile3) {
zero.pb(V[ile1+ile2+l2]); ++l2;
}
vector<int>jans(zero.size());
solve(zero, jans, (l+r)/2+1, r, akt/3);
l1=0;
rep(i, ile1) {
while(zero[l1]<V[i]) ++l1;
ans[i]+=jans[l1];
}
l1=0;
rep(i, ile3) {
while(zero[l1]<V[ile1+ile2+i]) ++l1;
ans[ile1+ile2+i]+=jans[l1];
}
rep(i, ile2) V[ile1+i]+=akt;
rep(i, ile3) V[ile1+ile2+i]+=2*akt;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int l, q, n=1;
cin >> l >> q >> S;
rep(i, l) n*=2;
pair<uint,int>T[q];
rep(i, q) {
string x;
cin >> x;
ll p=0;
for(auto j : x) {
p*=3;
if(j=='0') ++p;
if(j=='1') p+=2;
}
T[i]={p, i};
}
sort(T, T+q);
uint akt=1;
rep(i, l-1) akt*=3;
vector<uint>V;
V.pb(T[0].st);
for(int i=1; i<q; ++i) if(T[i].st!=T[i-1].st) V.pb(T[i].st);
vector<int>ans(V.size());
solve(V, ans, 0, n-1, akt);
int l1=0;
rep(i, q) {
while(V[l1]<T[i].st) ++l1;
T[i]={T[i].nd, ans[l1]};
}
sort(T, T+q);
rep(i, q) cout << T[i].nd << '\n';
}
Compilation message
snake_escaping.cpp: In function 'void solve(std::vector<unsigned int>&, std::vector<int>&, int, int, uint)':
snake_escaping.cpp:9:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<unsigned int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
snake_escaping.cpp:22:2: note: in expansion of macro 'rep'
22 | rep(i, V.size()) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
313 ms |
12100 KB |
Output is correct |
12 |
Correct |
355 ms |
11844 KB |
Output is correct |
13 |
Correct |
351 ms |
11204 KB |
Output is correct |
14 |
Correct |
354 ms |
11204 KB |
Output is correct |
15 |
Correct |
358 ms |
12228 KB |
Output is correct |
16 |
Correct |
348 ms |
11584 KB |
Output is correct |
17 |
Correct |
364 ms |
11960 KB |
Output is correct |
18 |
Correct |
179 ms |
13104 KB |
Output is correct |
19 |
Correct |
338 ms |
10036 KB |
Output is correct |
20 |
Correct |
319 ms |
11712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
313 ms |
12100 KB |
Output is correct |
12 |
Correct |
355 ms |
11844 KB |
Output is correct |
13 |
Correct |
351 ms |
11204 KB |
Output is correct |
14 |
Correct |
354 ms |
11204 KB |
Output is correct |
15 |
Correct |
358 ms |
12228 KB |
Output is correct |
16 |
Correct |
348 ms |
11584 KB |
Output is correct |
17 |
Correct |
364 ms |
11960 KB |
Output is correct |
18 |
Correct |
179 ms |
13104 KB |
Output is correct |
19 |
Correct |
338 ms |
10036 KB |
Output is correct |
20 |
Correct |
319 ms |
11712 KB |
Output is correct |
21 |
Correct |
324 ms |
12240 KB |
Output is correct |
22 |
Correct |
394 ms |
12504 KB |
Output is correct |
23 |
Correct |
395 ms |
14220 KB |
Output is correct |
24 |
Correct |
392 ms |
13060 KB |
Output is correct |
25 |
Correct |
374 ms |
14056 KB |
Output is correct |
26 |
Correct |
406 ms |
17736 KB |
Output is correct |
27 |
Correct |
439 ms |
24756 KB |
Output is correct |
28 |
Correct |
193 ms |
14160 KB |
Output is correct |
29 |
Correct |
357 ms |
10084 KB |
Output is correct |
30 |
Correct |
336 ms |
12500 KB |
Output is correct |
31 |
Correct |
425 ms |
15708 KB |
Output is correct |
32 |
Correct |
397 ms |
13780 KB |
Output is correct |
33 |
Correct |
361 ms |
11672 KB |
Output is correct |
34 |
Correct |
421 ms |
18500 KB |
Output is correct |
35 |
Correct |
440 ms |
24768 KB |
Output is correct |
36 |
Correct |
176 ms |
10052 KB |
Output is correct |
37 |
Correct |
367 ms |
12228 KB |
Output is correct |
38 |
Correct |
382 ms |
10312 KB |
Output is correct |
39 |
Correct |
399 ms |
14580 KB |
Output is correct |
40 |
Correct |
394 ms |
12600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
182 ms |
4612 KB |
Output is correct |
12 |
Correct |
284 ms |
4360 KB |
Output is correct |
13 |
Correct |
276 ms |
3616 KB |
Output is correct |
14 |
Correct |
533 ms |
3640 KB |
Output is correct |
15 |
Correct |
1309 ms |
4920 KB |
Output is correct |
16 |
Correct |
677 ms |
3700 KB |
Output is correct |
17 |
Correct |
677 ms |
3740 KB |
Output is correct |
18 |
Correct |
109 ms |
2408 KB |
Output is correct |
19 |
Correct |
50 ms |
2752 KB |
Output is correct |
20 |
Correct |
271 ms |
4968 KB |
Output is correct |
21 |
Correct |
1060 ms |
4092 KB |
Output is correct |
22 |
Correct |
536 ms |
3808 KB |
Output is correct |
23 |
Correct |
98 ms |
4324 KB |
Output is correct |
24 |
Correct |
443 ms |
3600 KB |
Output is correct |
25 |
Correct |
672 ms |
3784 KB |
Output is correct |
26 |
Correct |
13 ms |
2408 KB |
Output is correct |
27 |
Correct |
197 ms |
4124 KB |
Output is correct |
28 |
Correct |
54 ms |
2892 KB |
Output is correct |
29 |
Correct |
275 ms |
4008 KB |
Output is correct |
30 |
Correct |
424 ms |
3480 KB |
Output is correct |
31 |
Correct |
100 ms |
3940 KB |
Output is correct |
32 |
Correct |
691 ms |
3560 KB |
Output is correct |
33 |
Correct |
673 ms |
3680 KB |
Output is correct |
34 |
Correct |
13 ms |
2408 KB |
Output is correct |
35 |
Correct |
697 ms |
3660 KB |
Output is correct |
36 |
Correct |
704 ms |
3668 KB |
Output is correct |
37 |
Correct |
693 ms |
3668 KB |
Output is correct |
38 |
Correct |
692 ms |
3624 KB |
Output is correct |
39 |
Correct |
682 ms |
3652 KB |
Output is correct |
40 |
Correct |
706 ms |
3904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
313 ms |
12100 KB |
Output is correct |
12 |
Correct |
355 ms |
11844 KB |
Output is correct |
13 |
Correct |
351 ms |
11204 KB |
Output is correct |
14 |
Correct |
354 ms |
11204 KB |
Output is correct |
15 |
Correct |
358 ms |
12228 KB |
Output is correct |
16 |
Correct |
348 ms |
11584 KB |
Output is correct |
17 |
Correct |
364 ms |
11960 KB |
Output is correct |
18 |
Correct |
179 ms |
13104 KB |
Output is correct |
19 |
Correct |
338 ms |
10036 KB |
Output is correct |
20 |
Correct |
319 ms |
11712 KB |
Output is correct |
21 |
Correct |
324 ms |
12240 KB |
Output is correct |
22 |
Correct |
394 ms |
12504 KB |
Output is correct |
23 |
Correct |
395 ms |
14220 KB |
Output is correct |
24 |
Correct |
392 ms |
13060 KB |
Output is correct |
25 |
Correct |
374 ms |
14056 KB |
Output is correct |
26 |
Correct |
406 ms |
17736 KB |
Output is correct |
27 |
Correct |
439 ms |
24756 KB |
Output is correct |
28 |
Correct |
193 ms |
14160 KB |
Output is correct |
29 |
Correct |
357 ms |
10084 KB |
Output is correct |
30 |
Correct |
336 ms |
12500 KB |
Output is correct |
31 |
Correct |
425 ms |
15708 KB |
Output is correct |
32 |
Correct |
397 ms |
13780 KB |
Output is correct |
33 |
Correct |
361 ms |
11672 KB |
Output is correct |
34 |
Correct |
421 ms |
18500 KB |
Output is correct |
35 |
Correct |
440 ms |
24768 KB |
Output is correct |
36 |
Correct |
176 ms |
10052 KB |
Output is correct |
37 |
Correct |
367 ms |
12228 KB |
Output is correct |
38 |
Correct |
382 ms |
10312 KB |
Output is correct |
39 |
Correct |
399 ms |
14580 KB |
Output is correct |
40 |
Correct |
394 ms |
12600 KB |
Output is correct |
41 |
Correct |
182 ms |
4612 KB |
Output is correct |
42 |
Correct |
284 ms |
4360 KB |
Output is correct |
43 |
Correct |
276 ms |
3616 KB |
Output is correct |
44 |
Correct |
533 ms |
3640 KB |
Output is correct |
45 |
Correct |
1309 ms |
4920 KB |
Output is correct |
46 |
Correct |
677 ms |
3700 KB |
Output is correct |
47 |
Correct |
677 ms |
3740 KB |
Output is correct |
48 |
Correct |
109 ms |
2408 KB |
Output is correct |
49 |
Correct |
50 ms |
2752 KB |
Output is correct |
50 |
Correct |
271 ms |
4968 KB |
Output is correct |
51 |
Correct |
1060 ms |
4092 KB |
Output is correct |
52 |
Correct |
536 ms |
3808 KB |
Output is correct |
53 |
Correct |
98 ms |
4324 KB |
Output is correct |
54 |
Correct |
443 ms |
3600 KB |
Output is correct |
55 |
Correct |
672 ms |
3784 KB |
Output is correct |
56 |
Correct |
13 ms |
2408 KB |
Output is correct |
57 |
Correct |
197 ms |
4124 KB |
Output is correct |
58 |
Correct |
54 ms |
2892 KB |
Output is correct |
59 |
Correct |
275 ms |
4008 KB |
Output is correct |
60 |
Correct |
424 ms |
3480 KB |
Output is correct |
61 |
Correct |
100 ms |
3940 KB |
Output is correct |
62 |
Correct |
691 ms |
3560 KB |
Output is correct |
63 |
Correct |
673 ms |
3680 KB |
Output is correct |
64 |
Correct |
13 ms |
2408 KB |
Output is correct |
65 |
Correct |
697 ms |
3660 KB |
Output is correct |
66 |
Correct |
704 ms |
3668 KB |
Output is correct |
67 |
Correct |
693 ms |
3668 KB |
Output is correct |
68 |
Correct |
692 ms |
3624 KB |
Output is correct |
69 |
Correct |
682 ms |
3652 KB |
Output is correct |
70 |
Correct |
706 ms |
3904 KB |
Output is correct |
71 |
Correct |
586 ms |
19652 KB |
Output is correct |
72 |
Correct |
792 ms |
28416 KB |
Output is correct |
73 |
Correct |
1842 ms |
39600 KB |
Output is correct |
74 |
Execution timed out |
2102 ms |
39784 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |