Submission #522322

# Submission time Handle Problem Language Result Execution time Memory
522322 2022-02-04T15:15:51 Z fallingstar Snake Escaping (JOI18_snake_escaping) C++17
75 / 100
277 ms 65540 KB
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1e6 + 2;

int f[1 << 20];
int ans[N];
unsigned char type[N];       // 255 if ?, otherwise 0/1 for majority

int get_mask(const string& s, char target)
{
    int mask = 0;
    for (auto ch : s) {
        mask <<= 1;
        mask |= ch == target;
    }
    return mask;
}

int main()
{
    cin.tie(NULL); ios_base::sync_with_stdio(false);
    int l, q; cin >> l >> q;
    string toxicity; cin >> toxicity;
    vector<string> queries(q);
    for (auto& s : queries) cin >> s;

    for (int i = 0; i < q; ++i) {
        int cnt[3] = {};
        for (auto ch : queries[i])
            ++cnt[ch == '?' ? 2 : ch - '0'];
        int mn = *min_element(cnt, cnt + 3);
        if (cnt[2] == mn) {
            int maskQ = get_mask(queries[i], '?');
            int mask1 = get_mask(queries[i], '1');
            for (int submask = maskQ; submask > 0; submask = (submask - 1) & maskQ)
                ans[i] += toxicity[mask1 | submask] - '0';
            ans[i] += toxicity[mask1] - '0';    // all 0
            type[i] = 255;
        }
        else
            type[i] = cnt[0] > cnt[1];
    }

    transform(toxicity.begin(), toxicity.end(), f, [](char ch) { return ch - '0'; });
    for (int b = 0; b < l; ++b)
        for (int i = 0; i < 1 << l; ++i)
            if ((i >> b) & 1)
                f[i] += f[i ^ (1 << b)];
    for (int i = 0; i < q; ++i)
        if (type[i] == 1) {
            int maskQ = get_mask(queries[i], '?');
            int mask1 = get_mask(queries[i], '1');
            for (int submask = mask1; submask > 0; submask = (submask - 1) & mask1) // mask of 0 within 1
                ans[i] += f[maskQ | (mask1 ^ submask)] * (__builtin_popcount(submask) & 1 ? -1 : 1);
            ans[i] += f[maskQ | mask1];     // base: all 1 are ?
        }

    transform(toxicity.begin(), toxicity.end(), f, [](char ch) { return ch - '0'; });
    for (int b = 0; b < l; ++b)
        for (int i = (1 << l) - 1; i >= 0; --i)
            if (!((i >> b) & 1))
                f[i] += f[i ^ (1 << b)];
    int fullmask = (1 << l) - 1;
    for (int i = 0; i < q; ++i)
        if (type[i] == 0) {
            int maskQ = get_mask(queries[i], '?');
            int mask0 = get_mask(queries[i], '0');
            for (int submask = mask0; submask > 0; submask = (submask - 1) & mask0) // mask of 1 within 0
                ans[i] += f[fullmask ^ (maskQ | (mask0 ^ submask))] * (__builtin_popcount(submask) & 1 ? -1 : 1);
            ans[i] += f[fullmask ^ (maskQ | mask0)];     // base: all 0 are ?
        }

    for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
    return 0;
}
# 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 328 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 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 328 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 207 ms 42184 KB Output is correct
12 Correct 208 ms 41764 KB Output is correct
13 Correct 210 ms 41020 KB Output is correct
14 Correct 203 ms 41120 KB Output is correct
15 Correct 216 ms 42180 KB Output is correct
16 Correct 221 ms 41284 KB Output is correct
17 Correct 223 ms 41284 KB Output is correct
18 Correct 202 ms 43132 KB Output is correct
19 Correct 194 ms 40260 KB Output is correct
20 Correct 216 ms 41804 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 328 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 207 ms 42184 KB Output is correct
12 Correct 208 ms 41764 KB Output is correct
13 Correct 210 ms 41020 KB Output is correct
14 Correct 203 ms 41120 KB Output is correct
15 Correct 216 ms 42180 KB Output is correct
16 Correct 221 ms 41284 KB Output is correct
17 Correct 223 ms 41284 KB Output is correct
18 Correct 202 ms 43132 KB Output is correct
19 Correct 194 ms 40260 KB Output is correct
20 Correct 216 ms 41804 KB Output is correct
21 Correct 226 ms 42216 KB Output is correct
22 Correct 225 ms 42052 KB Output is correct
23 Correct 260 ms 41192 KB Output is correct
24 Correct 244 ms 41052 KB Output is correct
25 Correct 244 ms 42992 KB Output is correct
26 Correct 261 ms 41540 KB Output is correct
27 Correct 246 ms 41408 KB Output is correct
28 Correct 227 ms 43956 KB Output is correct
29 Correct 223 ms 39872 KB Output is correct
30 Correct 227 ms 42180 KB Output is correct
31 Correct 257 ms 41944 KB Output is correct
32 Correct 277 ms 41880 KB Output is correct
33 Correct 244 ms 41008 KB Output is correct
34 Correct 257 ms 40988 KB Output is correct
35 Correct 259 ms 41420 KB Output is correct
36 Correct 225 ms 40136 KB Output is correct
37 Correct 232 ms 41976 KB Output is correct
38 Correct 228 ms 40008 KB Output is correct
39 Correct 255 ms 41392 KB Output is correct
40 Correct 233 ms 41004 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 328 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 56 ms 11812 KB Output is correct
12 Correct 57 ms 12052 KB Output is correct
13 Correct 56 ms 11796 KB Output is correct
14 Correct 69 ms 11752 KB Output is correct
15 Correct 60 ms 11968 KB Output is correct
16 Correct 69 ms 11916 KB Output is correct
17 Correct 61 ms 11792 KB Output is correct
18 Correct 56 ms 12028 KB Output is correct
19 Correct 71 ms 11792 KB Output is correct
20 Correct 73 ms 11848 KB Output is correct
21 Correct 62 ms 11808 KB Output is correct
22 Correct 75 ms 11748 KB Output is correct
23 Correct 56 ms 11748 KB Output is correct
24 Correct 64 ms 11848 KB Output is correct
25 Correct 61 ms 11840 KB Output is correct
26 Correct 58 ms 11740 KB Output is correct
27 Correct 56 ms 11864 KB Output is correct
28 Correct 54 ms 11748 KB Output is correct
29 Correct 56 ms 11812 KB Output is correct
30 Correct 59 ms 11800 KB Output is correct
31 Correct 55 ms 11816 KB Output is correct
32 Correct 64 ms 11856 KB Output is correct
33 Correct 63 ms 11836 KB Output is correct
34 Correct 69 ms 11712 KB Output is correct
35 Correct 60 ms 11800 KB Output is correct
36 Correct 60 ms 11752 KB Output is correct
37 Correct 61 ms 11800 KB Output is correct
38 Correct 62 ms 11748 KB Output is correct
39 Correct 64 ms 11752 KB Output is correct
40 Correct 59 ms 11804 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 328 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 207 ms 42184 KB Output is correct
12 Correct 208 ms 41764 KB Output is correct
13 Correct 210 ms 41020 KB Output is correct
14 Correct 203 ms 41120 KB Output is correct
15 Correct 216 ms 42180 KB Output is correct
16 Correct 221 ms 41284 KB Output is correct
17 Correct 223 ms 41284 KB Output is correct
18 Correct 202 ms 43132 KB Output is correct
19 Correct 194 ms 40260 KB Output is correct
20 Correct 216 ms 41804 KB Output is correct
21 Correct 226 ms 42216 KB Output is correct
22 Correct 225 ms 42052 KB Output is correct
23 Correct 260 ms 41192 KB Output is correct
24 Correct 244 ms 41052 KB Output is correct
25 Correct 244 ms 42992 KB Output is correct
26 Correct 261 ms 41540 KB Output is correct
27 Correct 246 ms 41408 KB Output is correct
28 Correct 227 ms 43956 KB Output is correct
29 Correct 223 ms 39872 KB Output is correct
30 Correct 227 ms 42180 KB Output is correct
31 Correct 257 ms 41944 KB Output is correct
32 Correct 277 ms 41880 KB Output is correct
33 Correct 244 ms 41008 KB Output is correct
34 Correct 257 ms 40988 KB Output is correct
35 Correct 259 ms 41420 KB Output is correct
36 Correct 225 ms 40136 KB Output is correct
37 Correct 232 ms 41976 KB Output is correct
38 Correct 228 ms 40008 KB Output is correct
39 Correct 255 ms 41392 KB Output is correct
40 Correct 233 ms 41004 KB Output is correct
41 Correct 56 ms 11812 KB Output is correct
42 Correct 57 ms 12052 KB Output is correct
43 Correct 56 ms 11796 KB Output is correct
44 Correct 69 ms 11752 KB Output is correct
45 Correct 60 ms 11968 KB Output is correct
46 Correct 69 ms 11916 KB Output is correct
47 Correct 61 ms 11792 KB Output is correct
48 Correct 56 ms 12028 KB Output is correct
49 Correct 71 ms 11792 KB Output is correct
50 Correct 73 ms 11848 KB Output is correct
51 Correct 62 ms 11808 KB Output is correct
52 Correct 75 ms 11748 KB Output is correct
53 Correct 56 ms 11748 KB Output is correct
54 Correct 64 ms 11848 KB Output is correct
55 Correct 61 ms 11840 KB Output is correct
56 Correct 58 ms 11740 KB Output is correct
57 Correct 56 ms 11864 KB Output is correct
58 Correct 54 ms 11748 KB Output is correct
59 Correct 56 ms 11812 KB Output is correct
60 Correct 59 ms 11800 KB Output is correct
61 Correct 55 ms 11816 KB Output is correct
62 Correct 64 ms 11856 KB Output is correct
63 Correct 63 ms 11836 KB Output is correct
64 Correct 69 ms 11712 KB Output is correct
65 Correct 60 ms 11800 KB Output is correct
66 Correct 60 ms 11752 KB Output is correct
67 Correct 61 ms 11800 KB Output is correct
68 Correct 62 ms 11748 KB Output is correct
69 Correct 64 ms 11752 KB Output is correct
70 Correct 59 ms 11804 KB Output is correct
71 Runtime error 109 ms 65540 KB Execution killed with signal 9
72 Halted 0 ms 0 KB -