답안 #396894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396894 2021-04-30T23:03:43 Z Benq Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
1130 ms 21424 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 1<<20;
 
int L,Q, dp[2][MX];
short pc[MX], val[MX];
 
void init() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> L >> Q;
    FOR(i,1,MX) pc[i] = pc[i-(i&-i)]+1;
    string S; cin >> S;
    F0R(i,1<<L) dp[1][i] = dp[0][(1<<L)-1-i] = val[i] = S[i]-'0';
    
    F0R(i,L) F0R(j,1<<L) if (j&(1<<i)) 
        F0R(k,2) dp[k][j] += dp[k][j^(1<<i)];
}
 
int solve(const string& T) {
    int a = 0, b = 0, c = 0;
    F0R(i,sz(T)) {
        switch(T[sz(T)-1-i]) {
            case '0':
                a ^= 1<<i;
                break;
            case '1':
                b ^= 1<<i;
                break;
            default:
                c ^= 1<<i;
                break;
        }
    }
    
    int ans = 0;
    int mn = min(min(pc[a],pc[b]),pc[c]);
    if (pc[a] == mn) {
        for (int m = a; ; m = (m-1)&a) {
            if ((pc[a]-pc[m])&1) ans -= dp[0][m|c];
            else ans += dp[0][m|c];
            if (m == 0) break;
        }
    } else if (pc[b] == mn) {
        for (int m = b; ; m = (m-1)&b) {
            if ((pc[b]-pc[m])&1) ans -= dp[1][m|c];
            else ans += dp[1][m|c];
            if (m == 0) break;
        }
    } else {
        for (int m = c; ; m = (m-1)&c) {
            ans += val[m|b];
            if (m == 0) break;
        }
    }
    
    return ans;
}
 
int main() {
    init();
    F0R(i,Q) {
        string T; cin >> T;
        cout << solve(T) << "\n";
    }
}
 
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2380 KB Output is correct
2 Correct 3 ms 2380 KB Output is correct
3 Correct 4 ms 2376 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 4 ms 2380 KB Output is correct
6 Correct 3 ms 2372 KB Output is correct
7 Correct 3 ms 2292 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 3 ms 2380 KB Output is correct
10 Correct 4 ms 2340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2380 KB Output is correct
2 Correct 3 ms 2380 KB Output is correct
3 Correct 4 ms 2376 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 4 ms 2380 KB Output is correct
6 Correct 3 ms 2372 KB Output is correct
7 Correct 3 ms 2292 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 3 ms 2380 KB Output is correct
10 Correct 4 ms 2340 KB Output is correct
11 Correct 244 ms 10860 KB Output is correct
12 Correct 258 ms 10576 KB Output is correct
13 Correct 277 ms 9812 KB Output is correct
14 Correct 276 ms 9816 KB Output is correct
15 Correct 255 ms 10808 KB Output is correct
16 Correct 290 ms 10008 KB Output is correct
17 Correct 316 ms 10052 KB Output is correct
18 Correct 211 ms 11848 KB Output is correct
19 Correct 233 ms 8792 KB Output is correct
20 Correct 259 ms 10488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2380 KB Output is correct
2 Correct 3 ms 2380 KB Output is correct
3 Correct 4 ms 2376 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 4 ms 2380 KB Output is correct
6 Correct 3 ms 2372 KB Output is correct
7 Correct 3 ms 2292 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 3 ms 2380 KB Output is correct
10 Correct 4 ms 2340 KB Output is correct
11 Correct 244 ms 10860 KB Output is correct
12 Correct 258 ms 10576 KB Output is correct
13 Correct 277 ms 9812 KB Output is correct
14 Correct 276 ms 9816 KB Output is correct
15 Correct 255 ms 10808 KB Output is correct
16 Correct 290 ms 10008 KB Output is correct
17 Correct 316 ms 10052 KB Output is correct
18 Correct 211 ms 11848 KB Output is correct
19 Correct 233 ms 8792 KB Output is correct
20 Correct 259 ms 10488 KB Output is correct
21 Correct 265 ms 10436 KB Output is correct
22 Correct 306 ms 10652 KB Output is correct
23 Correct 314 ms 9600 KB Output is correct
24 Correct 336 ms 9412 KB Output is correct
25 Correct 285 ms 11492 KB Output is correct
26 Correct 381 ms 9988 KB Output is correct
27 Correct 362 ms 9924 KB Output is correct
28 Correct 242 ms 12484 KB Output is correct
29 Correct 284 ms 8388 KB Output is correct
30 Correct 295 ms 10616 KB Output is correct
31 Correct 332 ms 10564 KB Output is correct
32 Correct 430 ms 10540 KB Output is correct
33 Correct 272 ms 9436 KB Output is correct
34 Correct 331 ms 9532 KB Output is correct
35 Correct 336 ms 10040 KB Output is correct
36 Correct 201 ms 8672 KB Output is correct
37 Correct 263 ms 10640 KB Output is correct
38 Correct 276 ms 8680 KB Output is correct
39 Correct 324 ms 9872 KB Output is correct
40 Correct 326 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2380 KB Output is correct
2 Correct 3 ms 2380 KB Output is correct
3 Correct 4 ms 2376 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 4 ms 2380 KB Output is correct
6 Correct 3 ms 2372 KB Output is correct
7 Correct 3 ms 2292 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 3 ms 2380 KB Output is correct
10 Correct 4 ms 2340 KB Output is correct
11 Correct 55 ms 15052 KB Output is correct
12 Correct 61 ms 15036 KB Output is correct
13 Correct 74 ms 14952 KB Output is correct
14 Correct 73 ms 14960 KB Output is correct
15 Correct 57 ms 15040 KB Output is correct
16 Correct 83 ms 14964 KB Output is correct
17 Correct 79 ms 14960 KB Output is correct
18 Correct 46 ms 15180 KB Output is correct
19 Correct 59 ms 15048 KB Output is correct
20 Correct 56 ms 15040 KB Output is correct
21 Correct 71 ms 14960 KB Output is correct
22 Correct 92 ms 15024 KB Output is correct
23 Correct 59 ms 14960 KB Output is correct
24 Correct 85 ms 14960 KB Output is correct
25 Correct 92 ms 14960 KB Output is correct
26 Correct 45 ms 14960 KB Output is correct
27 Correct 98 ms 14960 KB Output is correct
28 Correct 54 ms 14960 KB Output is correct
29 Correct 64 ms 14960 KB Output is correct
30 Correct 67 ms 15052 KB Output is correct
31 Correct 58 ms 14960 KB Output is correct
32 Correct 87 ms 14960 KB Output is correct
33 Correct 81 ms 14972 KB Output is correct
34 Correct 46 ms 15052 KB Output is correct
35 Correct 70 ms 14964 KB Output is correct
36 Correct 70 ms 14960 KB Output is correct
37 Correct 74 ms 15040 KB Output is correct
38 Correct 87 ms 15024 KB Output is correct
39 Correct 91 ms 14968 KB Output is correct
40 Correct 76 ms 15064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2380 KB Output is correct
2 Correct 3 ms 2380 KB Output is correct
3 Correct 4 ms 2376 KB Output is correct
4 Correct 3 ms 2380 KB Output is correct
5 Correct 4 ms 2380 KB Output is correct
6 Correct 3 ms 2372 KB Output is correct
7 Correct 3 ms 2292 KB Output is correct
8 Correct 3 ms 2380 KB Output is correct
9 Correct 3 ms 2380 KB Output is correct
10 Correct 4 ms 2340 KB Output is correct
11 Correct 244 ms 10860 KB Output is correct
12 Correct 258 ms 10576 KB Output is correct
13 Correct 277 ms 9812 KB Output is correct
14 Correct 276 ms 9816 KB Output is correct
15 Correct 255 ms 10808 KB Output is correct
16 Correct 290 ms 10008 KB Output is correct
17 Correct 316 ms 10052 KB Output is correct
18 Correct 211 ms 11848 KB Output is correct
19 Correct 233 ms 8792 KB Output is correct
20 Correct 259 ms 10488 KB Output is correct
21 Correct 265 ms 10436 KB Output is correct
22 Correct 306 ms 10652 KB Output is correct
23 Correct 314 ms 9600 KB Output is correct
24 Correct 336 ms 9412 KB Output is correct
25 Correct 285 ms 11492 KB Output is correct
26 Correct 381 ms 9988 KB Output is correct
27 Correct 362 ms 9924 KB Output is correct
28 Correct 242 ms 12484 KB Output is correct
29 Correct 284 ms 8388 KB Output is correct
30 Correct 295 ms 10616 KB Output is correct
31 Correct 332 ms 10564 KB Output is correct
32 Correct 430 ms 10540 KB Output is correct
33 Correct 272 ms 9436 KB Output is correct
34 Correct 331 ms 9532 KB Output is correct
35 Correct 336 ms 10040 KB Output is correct
36 Correct 201 ms 8672 KB Output is correct
37 Correct 263 ms 10640 KB Output is correct
38 Correct 276 ms 8680 KB Output is correct
39 Correct 324 ms 9872 KB Output is correct
40 Correct 326 ms 9684 KB Output is correct
41 Correct 55 ms 15052 KB Output is correct
42 Correct 61 ms 15036 KB Output is correct
43 Correct 74 ms 14952 KB Output is correct
44 Correct 73 ms 14960 KB Output is correct
45 Correct 57 ms 15040 KB Output is correct
46 Correct 83 ms 14964 KB Output is correct
47 Correct 79 ms 14960 KB Output is correct
48 Correct 46 ms 15180 KB Output is correct
49 Correct 59 ms 15048 KB Output is correct
50 Correct 56 ms 15040 KB Output is correct
51 Correct 71 ms 14960 KB Output is correct
52 Correct 92 ms 15024 KB Output is correct
53 Correct 59 ms 14960 KB Output is correct
54 Correct 85 ms 14960 KB Output is correct
55 Correct 92 ms 14960 KB Output is correct
56 Correct 45 ms 14960 KB Output is correct
57 Correct 98 ms 14960 KB Output is correct
58 Correct 54 ms 14960 KB Output is correct
59 Correct 64 ms 14960 KB Output is correct
60 Correct 67 ms 15052 KB Output is correct
61 Correct 58 ms 14960 KB Output is correct
62 Correct 87 ms 14960 KB Output is correct
63 Correct 81 ms 14972 KB Output is correct
64 Correct 46 ms 15052 KB Output is correct
65 Correct 70 ms 14964 KB Output is correct
66 Correct 70 ms 14960 KB Output is correct
67 Correct 74 ms 15040 KB Output is correct
68 Correct 87 ms 15024 KB Output is correct
69 Correct 91 ms 14968 KB Output is correct
70 Correct 76 ms 15064 KB Output is correct
71 Correct 460 ms 18696 KB Output is correct
72 Correct 484 ms 18688 KB Output is correct
73 Correct 689 ms 17148 KB Output is correct
74 Correct 901 ms 17404 KB Output is correct
75 Correct 534 ms 19504 KB Output is correct
76 Correct 899 ms 17812 KB Output is correct
77 Correct 996 ms 17644 KB Output is correct
78 Correct 306 ms 21424 KB Output is correct
79 Correct 422 ms 15576 KB Output is correct
80 Correct 477 ms 18552 KB Output is correct
81 Correct 696 ms 18452 KB Output is correct
82 Correct 812 ms 17488 KB Output is correct
83 Correct 444 ms 16592 KB Output is correct
84 Correct 1130 ms 17444 KB Output is correct
85 Correct 1014 ms 17668 KB Output is correct
86 Correct 284 ms 15520 KB Output is correct
87 Correct 456 ms 18436 KB Output is correct
88 Correct 472 ms 15348 KB Output is correct
89 Correct 714 ms 17096 KB Output is correct
90 Correct 579 ms 17404 KB Output is correct
91 Correct 532 ms 16624 KB Output is correct
92 Correct 1107 ms 17788 KB Output is correct
93 Correct 908 ms 17544 KB Output is correct
94 Correct 291 ms 15568 KB Output is correct
95 Correct 807 ms 17616 KB Output is correct
96 Correct 818 ms 17504 KB Output is correct
97 Correct 836 ms 17544 KB Output is correct
98 Correct 803 ms 17424 KB Output is correct
99 Correct 795 ms 17540 KB Output is correct
100 Correct 772 ms 17488 KB Output is correct