Submission #92374

# Submission time Handle Problem Language Result Execution time Memory
92374 2019-01-02T16:54:09 Z Alexa2001 Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
2000 ms 23160 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = (1<<20);

int n, q;
string Val;
int val[Nmax], up[Nmax], down[Nmax], bits[Nmax];

void precalc()
{
    int i, j;
    for(i=0; i<(1<<n); ++i)
        bits[i] = __builtin_popcount(i), up[i] = down[i] = val[i] = Val[i] - '0';

    for(i=0; i<n; ++i)
        for(j=0; j<(1<<n); ++j)
            if(j & (1<<i))
                up[j ^ (1<<i)] += up[j];
            else down[j ^ (1<<i)] += down[j];
}

int solveQ(int mask, int que)
{
    int i, j, ans = 0;
    for(i=que, j = 0; j < (1<<bits[que]); i = (i-1) & que, ++j)
        ans += val[i ^ mask];
    return ans;
}

int solve0(int mask0, int mask1)
{
    int i, j, ans = 0;
    for(i=mask0, j = 0; j < (1<<bits[mask0]); i = (i-1) & mask0, ++j)
        if(bits[i] & 1) ans -= up[mask1 ^ i];
            else ans += up[mask1 ^ i];
    return ans;
}

int solve1(int mask1, int maskq)
{
    int i, j, ans = 0;
    for(i=mask1, j = 0; j < (1<<bits[mask1]); i = (i-1) & mask1, ++j)
        if(bits[i] & 1) ans -= down[maskq ^ mask1 ^ i];
            else ans += down[maskq ^ mask1 ^ i];
    return ans;
}

int query()
{
    string S;
    cin >> S;
    reverse(S.begin(), S.end());

    int i, mask0 = 0, mask1 = 0, maskq = 0;
    for(i=0; i<n; ++i)
    {
        if(S[i] == '0') mask0 ^= (1<<i);
            else if(S[i] == '1') mask1 ^= (1<<i);
                else maskq ^= (1<<i);
    }

    if(bits[maskq] <= bits[mask0] && bits[maskq] <= bits[mask1]) return solveQ(mask1, maskq);
    else if(bits[mask0] <= bits[mask1]) return solve0(mask0, mask1);
        else return solve1(mask1, maskq);
}

int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false);

    cin >> n >> q;
    cin >> Val;

    precalc();
    while(q--)
        cout << query() << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 380 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 380 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 1418 ms 9588 KB Output is correct
12 Correct 1424 ms 9224 KB Output is correct
13 Correct 1463 ms 8568 KB Output is correct
14 Correct 1447 ms 8568 KB Output is correct
15 Correct 1461 ms 9448 KB Output is correct
16 Correct 1468 ms 8836 KB Output is correct
17 Correct 1464 ms 8696 KB Output is correct
18 Correct 1394 ms 10592 KB Output is correct
19 Correct 1408 ms 7460 KB Output is correct
20 Correct 1442 ms 9336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 380 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 1418 ms 9588 KB Output is correct
12 Correct 1424 ms 9224 KB Output is correct
13 Correct 1463 ms 8568 KB Output is correct
14 Correct 1447 ms 8568 KB Output is correct
15 Correct 1461 ms 9448 KB Output is correct
16 Correct 1468 ms 8836 KB Output is correct
17 Correct 1464 ms 8696 KB Output is correct
18 Correct 1394 ms 10592 KB Output is correct
19 Correct 1408 ms 7460 KB Output is correct
20 Correct 1442 ms 9336 KB Output is correct
21 Correct 1451 ms 9208 KB Output is correct
22 Correct 1466 ms 9208 KB Output is correct
23 Correct 1505 ms 8312 KB Output is correct
24 Correct 1499 ms 7948 KB Output is correct
25 Correct 1477 ms 9720 KB Output is correct
26 Correct 1522 ms 8312 KB Output is correct
27 Correct 1529 ms 8060 KB Output is correct
28 Correct 1395 ms 10272 KB Output is correct
29 Correct 1422 ms 6392 KB Output is correct
30 Correct 1577 ms 8412 KB Output is correct
31 Correct 1509 ms 8168 KB Output is correct
32 Correct 1541 ms 8048 KB Output is correct
33 Correct 1464 ms 6984 KB Output is correct
34 Correct 1511 ms 6904 KB Output is correct
35 Correct 1521 ms 7152 KB Output is correct
36 Correct 1401 ms 5752 KB Output is correct
37 Correct 1455 ms 7416 KB Output is correct
38 Correct 1447 ms 5328 KB Output is correct
39 Correct 1510 ms 6340 KB Output is correct
40 Correct 1492 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 380 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 132 ms 20252 KB Output is correct
12 Correct 139 ms 20260 KB Output is correct
13 Correct 144 ms 20092 KB Output is correct
14 Correct 151 ms 20120 KB Output is correct
15 Correct 129 ms 20400 KB Output is correct
16 Correct 150 ms 20152 KB Output is correct
17 Correct 147 ms 20252 KB Output is correct
18 Correct 122 ms 20376 KB Output is correct
19 Correct 133 ms 19992 KB Output is correct
20 Correct 130 ms 20248 KB Output is correct
21 Correct 147 ms 20176 KB Output is correct
22 Correct 155 ms 20160 KB Output is correct
23 Correct 130 ms 20248 KB Output is correct
24 Correct 156 ms 20164 KB Output is correct
25 Correct 155 ms 20248 KB Output is correct
26 Correct 136 ms 20284 KB Output is correct
27 Correct 126 ms 20180 KB Output is correct
28 Correct 126 ms 20120 KB Output is correct
29 Correct 142 ms 20120 KB Output is correct
30 Correct 142 ms 20180 KB Output is correct
31 Correct 130 ms 20176 KB Output is correct
32 Correct 167 ms 20248 KB Output is correct
33 Correct 160 ms 20120 KB Output is correct
34 Correct 118 ms 19996 KB Output is correct
35 Correct 148 ms 20104 KB Output is correct
36 Correct 149 ms 20252 KB Output is correct
37 Correct 150 ms 20120 KB Output is correct
38 Correct 148 ms 20120 KB Output is correct
39 Correct 148 ms 20168 KB Output is correct
40 Correct 147 ms 20096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 380 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 1418 ms 9588 KB Output is correct
12 Correct 1424 ms 9224 KB Output is correct
13 Correct 1463 ms 8568 KB Output is correct
14 Correct 1447 ms 8568 KB Output is correct
15 Correct 1461 ms 9448 KB Output is correct
16 Correct 1468 ms 8836 KB Output is correct
17 Correct 1464 ms 8696 KB Output is correct
18 Correct 1394 ms 10592 KB Output is correct
19 Correct 1408 ms 7460 KB Output is correct
20 Correct 1442 ms 9336 KB Output is correct
21 Correct 1451 ms 9208 KB Output is correct
22 Correct 1466 ms 9208 KB Output is correct
23 Correct 1505 ms 8312 KB Output is correct
24 Correct 1499 ms 7948 KB Output is correct
25 Correct 1477 ms 9720 KB Output is correct
26 Correct 1522 ms 8312 KB Output is correct
27 Correct 1529 ms 8060 KB Output is correct
28 Correct 1395 ms 10272 KB Output is correct
29 Correct 1422 ms 6392 KB Output is correct
30 Correct 1577 ms 8412 KB Output is correct
31 Correct 1509 ms 8168 KB Output is correct
32 Correct 1541 ms 8048 KB Output is correct
33 Correct 1464 ms 6984 KB Output is correct
34 Correct 1511 ms 6904 KB Output is correct
35 Correct 1521 ms 7152 KB Output is correct
36 Correct 1401 ms 5752 KB Output is correct
37 Correct 1455 ms 7416 KB Output is correct
38 Correct 1447 ms 5328 KB Output is correct
39 Correct 1510 ms 6340 KB Output is correct
40 Correct 1492 ms 6264 KB Output is correct
41 Correct 132 ms 20252 KB Output is correct
42 Correct 139 ms 20260 KB Output is correct
43 Correct 144 ms 20092 KB Output is correct
44 Correct 151 ms 20120 KB Output is correct
45 Correct 129 ms 20400 KB Output is correct
46 Correct 150 ms 20152 KB Output is correct
47 Correct 147 ms 20252 KB Output is correct
48 Correct 122 ms 20376 KB Output is correct
49 Correct 133 ms 19992 KB Output is correct
50 Correct 130 ms 20248 KB Output is correct
51 Correct 147 ms 20176 KB Output is correct
52 Correct 155 ms 20160 KB Output is correct
53 Correct 130 ms 20248 KB Output is correct
54 Correct 156 ms 20164 KB Output is correct
55 Correct 155 ms 20248 KB Output is correct
56 Correct 136 ms 20284 KB Output is correct
57 Correct 126 ms 20180 KB Output is correct
58 Correct 126 ms 20120 KB Output is correct
59 Correct 142 ms 20120 KB Output is correct
60 Correct 142 ms 20180 KB Output is correct
61 Correct 130 ms 20176 KB Output is correct
62 Correct 167 ms 20248 KB Output is correct
63 Correct 160 ms 20120 KB Output is correct
64 Correct 118 ms 19996 KB Output is correct
65 Correct 148 ms 20104 KB Output is correct
66 Correct 149 ms 20252 KB Output is correct
67 Correct 150 ms 20120 KB Output is correct
68 Correct 148 ms 20120 KB Output is correct
69 Correct 148 ms 20168 KB Output is correct
70 Correct 147 ms 20096 KB Output is correct
71 Correct 1774 ms 23064 KB Output is correct
72 Correct 1797 ms 23160 KB Output is correct
73 Correct 1934 ms 21908 KB Output is correct
74 Execution timed out 2059 ms 22312 KB Time limit exceeded
75 Halted 0 ms 0 KB -