Submission #944240

#TimeUsernameProblemLanguageResultExecution timeMemory
944240guechotjrhhSnake Escaping (JOI18_snake_escaping)C++14
100 / 100
1746 ms35952 KiB
#include<iostream> #include<string> using namespace std; #include<string> #include<vector> #include<algorithm> #define maxp 1048580 using namespace std; int st[maxp],n,p,zer[maxp], on[maxp], cnt[maxp]; int ab(int x) { return x > 0 ? x : -x; } void init(int N, int Q, string S) { n = N; p = 1 << n; for (int i = 0; i < p; i++) zer[i] = on[i] = st[i] = S[i] - '0'; for (int i = 1; i < p; i<<=1) { for (int j = 0; j < p; j++) { if (j & i) zer[j] += zer[j ^ i]; else on[j] += on[j ^ i]; } } cnt[0] = 0; for (int i = 0; i < p/2; i++) { cnt[i << 1] = cnt[i]; cnt[(i << 1) + 1] = cnt[i] + 1; } } int query(string A) { int a = 0, b = 0, q = 0, res=0; for (int i = 0; i < n; i++) { if (A[i] == '0') a|=1<<(n-1-i); else if (A[i] == '1') b |= 1 << (n - 1 - i); else q |= 1 << (n - 1 - i); } int oc = cnt[b], zc = cnt[a], qc = cnt[q]; if (oc < zc && oc < qc) { res = 0; for (int i = b; i >= 0; i--) { i &= b; if (cnt[i | q] & 1) res += zer[i | q]; else res -= zer[i | q]; } res= ab(res); } else if (qc > zc) { res = 0; for (int i = a; i >= 0; i--) { i &= a; if (cnt[i | b] & 1) res += on[i | b]; else res -= on[i | b]; } res= ab(res); } else { res = 0; for (int i = q; i >= 0; i--) { i &= q; res += st[i | b]; } } return res; } /* 4 8 3141592653589793 0101 ?01? ??1? ?0?? 1?00 01?1 ??10 ???? 3 5 12345678 000 0?? 1?0 ?11 ??? */ int main() { ios::sync_with_stdio(0); cin.tie(0); int N, Q; cin >> N >> Q; string S; cin >> S; init(N, Q, move(S)); for (int i = 0; i < Q; i++) { string A; cin >> A; cout << query(move(A)) << endl; } }
#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...