Submission #92374

#TimeUsernameProblemLanguageResultExecution timeMemory
92374Alexa2001Snake Escaping (JOI18_snake_escaping)C++17
75 / 100
2059 ms23160 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...