답안 #92376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92376 2019-01-02T16:58:01 Z Alexa2001 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1048 ms 47284 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = (1<<20);

int n, q;
char S[Nmax+2], Val[Nmax+2];
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()
{
    scanf("%s\n", S);
    reverse(S, S + n);

    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);

    scanf("%d %d\n", &n, &q);
    scanf("%s\n", Val);

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

    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int query()':
snake_escaping.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s\n", S);
     ~~~~~^~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d\n", &n, &q);
     ~~~~~^~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s\n", Val);
     ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 428 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 428 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 224 ms 8952 KB Output is correct
12 Correct 241 ms 8568 KB Output is correct
13 Correct 255 ms 7932 KB Output is correct
14 Correct 251 ms 7928 KB Output is correct
15 Correct 244 ms 8872 KB Output is correct
16 Correct 265 ms 8116 KB Output is correct
17 Correct 267 ms 8056 KB Output is correct
18 Correct 193 ms 9960 KB Output is correct
19 Correct 218 ms 6980 KB Output is correct
20 Correct 238 ms 8568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 428 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 224 ms 8952 KB Output is correct
12 Correct 241 ms 8568 KB Output is correct
13 Correct 255 ms 7932 KB Output is correct
14 Correct 251 ms 7928 KB Output is correct
15 Correct 244 ms 8872 KB Output is correct
16 Correct 265 ms 8116 KB Output is correct
17 Correct 267 ms 8056 KB Output is correct
18 Correct 193 ms 9960 KB Output is correct
19 Correct 218 ms 6980 KB Output is correct
20 Correct 238 ms 8568 KB Output is correct
21 Correct 276 ms 8824 KB Output is correct
22 Correct 285 ms 8972 KB Output is correct
23 Correct 316 ms 8528 KB Output is correct
24 Correct 322 ms 8500 KB Output is correct
25 Correct 287 ms 10340 KB Output is correct
26 Correct 329 ms 9320 KB Output is correct
27 Correct 324 ms 9388 KB Output is correct
28 Correct 227 ms 12148 KB Output is correct
29 Correct 274 ms 8100 KB Output is correct
30 Correct 282 ms 10772 KB Output is correct
31 Correct 303 ms 10484 KB Output is correct
32 Correct 324 ms 10368 KB Output is correct
33 Correct 282 ms 9208 KB Output is correct
34 Correct 324 ms 9060 KB Output is correct
35 Correct 322 ms 9336 KB Output is correct
36 Correct 200 ms 7928 KB Output is correct
37 Correct 273 ms 9700 KB Output is correct
38 Correct 269 ms 7544 KB Output is correct
39 Correct 301 ms 8568 KB Output is correct
40 Correct 312 ms 8188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 428 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 68 ms 20124 KB Output is correct
12 Correct 67 ms 20088 KB Output is correct
13 Correct 73 ms 19960 KB Output is correct
14 Correct 78 ms 19960 KB Output is correct
15 Correct 64 ms 20088 KB Output is correct
16 Correct 78 ms 20088 KB Output is correct
17 Correct 92 ms 20060 KB Output is correct
18 Correct 60 ms 20344 KB Output is correct
19 Correct 71 ms 20056 KB Output is correct
20 Correct 65 ms 20088 KB Output is correct
21 Correct 79 ms 20088 KB Output is correct
22 Correct 88 ms 20012 KB Output is correct
23 Correct 73 ms 19932 KB Output is correct
24 Correct 93 ms 20076 KB Output is correct
25 Correct 92 ms 20092 KB Output is correct
26 Correct 59 ms 19960 KB Output is correct
27 Correct 71 ms 20116 KB Output is correct
28 Correct 71 ms 19972 KB Output is correct
29 Correct 76 ms 19960 KB Output is correct
30 Correct 78 ms 19960 KB Output is correct
31 Correct 71 ms 20088 KB Output is correct
32 Correct 95 ms 20060 KB Output is correct
33 Correct 89 ms 20100 KB Output is correct
34 Correct 58 ms 19984 KB Output is correct
35 Correct 81 ms 20020 KB Output is correct
36 Correct 85 ms 20112 KB Output is correct
37 Correct 87 ms 20012 KB Output is correct
38 Correct 86 ms 20088 KB Output is correct
39 Correct 84 ms 19960 KB Output is correct
40 Correct 88 ms 20060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 428 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 224 ms 8952 KB Output is correct
12 Correct 241 ms 8568 KB Output is correct
13 Correct 255 ms 7932 KB Output is correct
14 Correct 251 ms 7928 KB Output is correct
15 Correct 244 ms 8872 KB Output is correct
16 Correct 265 ms 8116 KB Output is correct
17 Correct 267 ms 8056 KB Output is correct
18 Correct 193 ms 9960 KB Output is correct
19 Correct 218 ms 6980 KB Output is correct
20 Correct 238 ms 8568 KB Output is correct
21 Correct 276 ms 8824 KB Output is correct
22 Correct 285 ms 8972 KB Output is correct
23 Correct 316 ms 8528 KB Output is correct
24 Correct 322 ms 8500 KB Output is correct
25 Correct 287 ms 10340 KB Output is correct
26 Correct 329 ms 9320 KB Output is correct
27 Correct 324 ms 9388 KB Output is correct
28 Correct 227 ms 12148 KB Output is correct
29 Correct 274 ms 8100 KB Output is correct
30 Correct 282 ms 10772 KB Output is correct
31 Correct 303 ms 10484 KB Output is correct
32 Correct 324 ms 10368 KB Output is correct
33 Correct 282 ms 9208 KB Output is correct
34 Correct 324 ms 9060 KB Output is correct
35 Correct 322 ms 9336 KB Output is correct
36 Correct 200 ms 7928 KB Output is correct
37 Correct 273 ms 9700 KB Output is correct
38 Correct 269 ms 7544 KB Output is correct
39 Correct 301 ms 8568 KB Output is correct
40 Correct 312 ms 8188 KB Output is correct
41 Correct 68 ms 20124 KB Output is correct
42 Correct 67 ms 20088 KB Output is correct
43 Correct 73 ms 19960 KB Output is correct
44 Correct 78 ms 19960 KB Output is correct
45 Correct 64 ms 20088 KB Output is correct
46 Correct 78 ms 20088 KB Output is correct
47 Correct 92 ms 20060 KB Output is correct
48 Correct 60 ms 20344 KB Output is correct
49 Correct 71 ms 20056 KB Output is correct
50 Correct 65 ms 20088 KB Output is correct
51 Correct 79 ms 20088 KB Output is correct
52 Correct 88 ms 20012 KB Output is correct
53 Correct 73 ms 19932 KB Output is correct
54 Correct 93 ms 20076 KB Output is correct
55 Correct 92 ms 20092 KB Output is correct
56 Correct 59 ms 19960 KB Output is correct
57 Correct 71 ms 20116 KB Output is correct
58 Correct 71 ms 19972 KB Output is correct
59 Correct 76 ms 19960 KB Output is correct
60 Correct 78 ms 19960 KB Output is correct
61 Correct 71 ms 20088 KB Output is correct
62 Correct 95 ms 20060 KB Output is correct
63 Correct 89 ms 20100 KB Output is correct
64 Correct 58 ms 19984 KB Output is correct
65 Correct 81 ms 20020 KB Output is correct
66 Correct 85 ms 20112 KB Output is correct
67 Correct 87 ms 20012 KB Output is correct
68 Correct 86 ms 20088 KB Output is correct
69 Correct 84 ms 19960 KB Output is correct
70 Correct 88 ms 20060 KB Output is correct
71 Correct 522 ms 22904 KB Output is correct
72 Correct 537 ms 23024 KB Output is correct
73 Correct 630 ms 21584 KB Output is correct
74 Correct 776 ms 24184 KB Output is correct
75 Correct 481 ms 45288 KB Output is correct
76 Correct 703 ms 43744 KB Output is correct
77 Correct 718 ms 43704 KB Output is correct
78 Correct 299 ms 47284 KB Output is correct
79 Correct 439 ms 41332 KB Output is correct
80 Correct 523 ms 44652 KB Output is correct
81 Correct 580 ms 44336 KB Output is correct
82 Correct 706 ms 43304 KB Output is correct
83 Correct 463 ms 42596 KB Output is correct
84 Correct 667 ms 43324 KB Output is correct
85 Correct 711 ms 43508 KB Output is correct
86 Correct 299 ms 41452 KB Output is correct
87 Correct 453 ms 44456 KB Output is correct
88 Correct 504 ms 41328 KB Output is correct
89 Correct 647 ms 42996 KB Output is correct
90 Correct 646 ms 43280 KB Output is correct
91 Correct 532 ms 42552 KB Output is correct
92 Correct 1048 ms 43580 KB Output is correct
93 Correct 845 ms 43740 KB Output is correct
94 Correct 280 ms 41304 KB Output is correct
95 Correct 758 ms 43488 KB Output is correct
96 Correct 676 ms 43384 KB Output is correct
97 Correct 757 ms 43360 KB Output is correct
98 Correct 675 ms 43512 KB Output is correct
99 Correct 672 ms 43308 KB Output is correct
100 Correct 791 ms 43264 KB Output is correct