Submission #1098554

# Submission time Handle Problem Language Result Execution time Memory
1098554 2024-10-09T15:00:42 Z Tame Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
444 ms 47600 KB
#include <bits/stdc++.h>
#define pu push
#define pf push_front
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define el cout<<"\n"
#define ll long long
#define ld long double
#define MASK(i) (1LL<<(i))
#define BIT(x, i) (((x)>>(i))&1)
#define SET_ON(x, i) ((x)|MASK(i))
#define SET_OFF(x, i) ((x)&~MASK(i))
#define c_bit(i) __builtin_popcount(i)
const int MAX_SIZE = 1100000;
const int inf = (int)1e9+7;
using namespace std;

template<class T1, class T2>
    bool maximize(T1 &x, T2 y){if(x<y){x=y; return true;} return false;}
template<class T1, class T2>
    bool minimize(T1 &x, T2 y){if(x>y){x=y; return true;} return false;}

int L, Q;
int poison[MAX_SIZE], sup[MAX_SIZE], sub[MAX_SIZE], cntbit[MAX_SIZE];
string s;

void init(){
    cin>>L>>Q>>s;
}

void ilovesunshine(){
    init();

    for (int mask=0; mask<MASK(L); mask++){
        poison[mask] = s[mask]-'0';
        sup[mask] = sub[mask] = poison[mask];
        cntbit[mask] = c_bit(mask);
    }

    for (int i=0; i<L; i++){
        for (int mask=0; mask<MASK(L); mask++){
            if (!BIT(mask, i)){
                sup[mask]+=sup[mask^(1<<i)];
            }
            else sub[mask]+=sub[mask^(1<<i)];
        }
    }

    while (Q--){
        cin>>s;
//        cout<<s<<"\n";
        int A=0, B=0, C=0, ca=0, cb=0, cc=0; ll res=0;
        for (int i=0; i<L; i++){
            if (s[i]=='0'){
                A=SET_ON(A, L-i-1);
                ca++;
            }
            else if (s[i]=='1'){
                 B=SET_ON(B, L-i-1);
                cb++;
            }
            else{
                 C=SET_ON(C, L-i-1);
                cc++;
            }
        }

        if (ca<=cb && ca<=cc){
            for (int s=A; s!=0; s=(s-1)&A){
                res += (1-2*((cntbit[s])&1))*sup[B|s];
            }
            res+=sup[B];
        }
        else if (cb<=ca && cb<=cc){
            for (int s=B; s!=0; s=(s-1)&B){
                res += (1-2*((cntbit[s])&1))*sub[C|(B^s)];
            }
            res+=sub[C|B];
        }
        else{
            for (int s=C; s!=0; s=(s-1)&C){
                res+=poison[s|B];
            }
            res+=poison[B];
        }
        cout<<res<<"\n";
    }

//    for (int mask=0; mask<MASK(L); mask++){
//        cout<<poison[mask]<<' '<<sup[mask]<<' '<<sub[mask]<<' '<<cntbit[mask]<<"\n";
//    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    freopen("bdbang.INP", "r", stdin);
//    freopen("bdbang.OUT", "w", stdout);
    ilovesunshine();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 130 ms 15188 KB Output is correct
12 Correct 136 ms 14676 KB Output is correct
13 Correct 153 ms 13908 KB Output is correct
14 Correct 151 ms 14164 KB Output is correct
15 Correct 132 ms 15076 KB Output is correct
16 Correct 154 ms 14416 KB Output is correct
17 Correct 160 ms 14160 KB Output is correct
18 Correct 98 ms 16108 KB Output is correct
19 Correct 121 ms 13136 KB Output is correct
20 Correct 142 ms 14880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 130 ms 15188 KB Output is correct
12 Correct 136 ms 14676 KB Output is correct
13 Correct 153 ms 13908 KB Output is correct
14 Correct 151 ms 14164 KB Output is correct
15 Correct 132 ms 15076 KB Output is correct
16 Correct 154 ms 14416 KB Output is correct
17 Correct 160 ms 14160 KB Output is correct
18 Correct 98 ms 16108 KB Output is correct
19 Correct 121 ms 13136 KB Output is correct
20 Correct 142 ms 14880 KB Output is correct
21 Correct 145 ms 18256 KB Output is correct
22 Correct 157 ms 18520 KB Output is correct
23 Correct 191 ms 17392 KB Output is correct
24 Correct 195 ms 17320 KB Output is correct
25 Correct 158 ms 19284 KB Output is correct
26 Correct 191 ms 17740 KB Output is correct
27 Correct 200 ms 17748 KB Output is correct
28 Correct 103 ms 20308 KB Output is correct
29 Correct 141 ms 16212 KB Output is correct
30 Correct 158 ms 18324 KB Output is correct
31 Correct 169 ms 18260 KB Output is correct
32 Correct 190 ms 18100 KB Output is correct
33 Correct 163 ms 16976 KB Output is correct
34 Correct 211 ms 17236 KB Output is correct
35 Correct 198 ms 17524 KB Output is correct
36 Correct 115 ms 16212 KB Output is correct
37 Correct 146 ms 18192 KB Output is correct
38 Correct 155 ms 16208 KB Output is correct
39 Correct 180 ms 17488 KB Output is correct
40 Correct 180 ms 17220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 36 ms 20212 KB Output is correct
12 Correct 37 ms 20208 KB Output is correct
13 Correct 41 ms 20064 KB Output is correct
14 Correct 46 ms 20204 KB Output is correct
15 Correct 36 ms 20236 KB Output is correct
16 Correct 45 ms 20076 KB Output is correct
17 Correct 45 ms 20208 KB Output is correct
18 Correct 32 ms 20332 KB Output is correct
19 Correct 36 ms 19960 KB Output is correct
20 Correct 37 ms 20212 KB Output is correct
21 Correct 39 ms 20340 KB Output is correct
22 Correct 46 ms 20208 KB Output is correct
23 Correct 37 ms 20076 KB Output is correct
24 Correct 45 ms 20076 KB Output is correct
25 Correct 47 ms 20220 KB Output is correct
26 Correct 33 ms 19956 KB Output is correct
27 Correct 36 ms 20296 KB Output is correct
28 Correct 38 ms 19952 KB Output is correct
29 Correct 44 ms 20260 KB Output is correct
30 Correct 45 ms 20216 KB Output is correct
31 Correct 42 ms 20152 KB Output is correct
32 Correct 52 ms 20212 KB Output is correct
33 Correct 46 ms 20212 KB Output is correct
34 Correct 35 ms 19956 KB Output is correct
35 Correct 42 ms 20076 KB Output is correct
36 Correct 42 ms 20216 KB Output is correct
37 Correct 42 ms 20212 KB Output is correct
38 Correct 42 ms 20208 KB Output is correct
39 Correct 47 ms 20076 KB Output is correct
40 Correct 45 ms 20216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 130 ms 15188 KB Output is correct
12 Correct 136 ms 14676 KB Output is correct
13 Correct 153 ms 13908 KB Output is correct
14 Correct 151 ms 14164 KB Output is correct
15 Correct 132 ms 15076 KB Output is correct
16 Correct 154 ms 14416 KB Output is correct
17 Correct 160 ms 14160 KB Output is correct
18 Correct 98 ms 16108 KB Output is correct
19 Correct 121 ms 13136 KB Output is correct
20 Correct 142 ms 14880 KB Output is correct
21 Correct 145 ms 18256 KB Output is correct
22 Correct 157 ms 18520 KB Output is correct
23 Correct 191 ms 17392 KB Output is correct
24 Correct 195 ms 17320 KB Output is correct
25 Correct 158 ms 19284 KB Output is correct
26 Correct 191 ms 17740 KB Output is correct
27 Correct 200 ms 17748 KB Output is correct
28 Correct 103 ms 20308 KB Output is correct
29 Correct 141 ms 16212 KB Output is correct
30 Correct 158 ms 18324 KB Output is correct
31 Correct 169 ms 18260 KB Output is correct
32 Correct 190 ms 18100 KB Output is correct
33 Correct 163 ms 16976 KB Output is correct
34 Correct 211 ms 17236 KB Output is correct
35 Correct 198 ms 17524 KB Output is correct
36 Correct 115 ms 16212 KB Output is correct
37 Correct 146 ms 18192 KB Output is correct
38 Correct 155 ms 16208 KB Output is correct
39 Correct 180 ms 17488 KB Output is correct
40 Correct 180 ms 17220 KB Output is correct
41 Correct 36 ms 20212 KB Output is correct
42 Correct 37 ms 20208 KB Output is correct
43 Correct 41 ms 20064 KB Output is correct
44 Correct 46 ms 20204 KB Output is correct
45 Correct 36 ms 20236 KB Output is correct
46 Correct 45 ms 20076 KB Output is correct
47 Correct 45 ms 20208 KB Output is correct
48 Correct 32 ms 20332 KB Output is correct
49 Correct 36 ms 19960 KB Output is correct
50 Correct 37 ms 20212 KB Output is correct
51 Correct 39 ms 20340 KB Output is correct
52 Correct 46 ms 20208 KB Output is correct
53 Correct 37 ms 20076 KB Output is correct
54 Correct 45 ms 20076 KB Output is correct
55 Correct 47 ms 20220 KB Output is correct
56 Correct 33 ms 19956 KB Output is correct
57 Correct 36 ms 20296 KB Output is correct
58 Correct 38 ms 19952 KB Output is correct
59 Correct 44 ms 20260 KB Output is correct
60 Correct 45 ms 20216 KB Output is correct
61 Correct 42 ms 20152 KB Output is correct
62 Correct 52 ms 20212 KB Output is correct
63 Correct 46 ms 20212 KB Output is correct
64 Correct 35 ms 19956 KB Output is correct
65 Correct 42 ms 20076 KB Output is correct
66 Correct 42 ms 20216 KB Output is correct
67 Correct 42 ms 20212 KB Output is correct
68 Correct 42 ms 20208 KB Output is correct
69 Correct 47 ms 20076 KB Output is correct
70 Correct 45 ms 20216 KB Output is correct
71 Correct 236 ms 44596 KB Output is correct
72 Correct 235 ms 44528 KB Output is correct
73 Correct 331 ms 42996 KB Output is correct
74 Correct 444 ms 43560 KB Output is correct
75 Correct 247 ms 45556 KB Output is correct
76 Correct 438 ms 43696 KB Output is correct
77 Correct 383 ms 43764 KB Output is correct
78 Correct 165 ms 47600 KB Output is correct
79 Correct 218 ms 41456 KB Output is correct
80 Correct 230 ms 44788 KB Output is correct
81 Correct 303 ms 44408 KB Output is correct
82 Correct 423 ms 43504 KB Output is correct
83 Correct 223 ms 42608 KB Output is correct
84 Correct 396 ms 43500 KB Output is correct
85 Correct 356 ms 43540 KB Output is correct
86 Correct 167 ms 41480 KB Output is correct
87 Correct 239 ms 44528 KB Output is correct
88 Correct 249 ms 41456 KB Output is correct
89 Correct 305 ms 43060 KB Output is correct
90 Correct 376 ms 43508 KB Output is correct
91 Correct 273 ms 42532 KB Output is correct
92 Correct 433 ms 43728 KB Output is correct
93 Correct 382 ms 43584 KB Output is correct
94 Correct 146 ms 41452 KB Output is correct
95 Correct 335 ms 43504 KB Output is correct
96 Correct 344 ms 43620 KB Output is correct
97 Correct 311 ms 43508 KB Output is correct
98 Correct 321 ms 43628 KB Output is correct
99 Correct 313 ms 43504 KB Output is correct
100 Correct 340 ms 43632 KB Output is correct