Submission #414658

#TimeUsernameProblemLanguageResultExecution timeMemory
414658JerryLiu06Snake Escaping (JOI18_snake_escaping)C++17
100 / 100
1132 ms43312 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = long double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define mp make_pair #define f first #define s second #define sz(x) (int)(x).size() #define bg(x) begin(x) #define all(x) bg(x), end(x) #define sor(x) sort(all(x)) #define ft front() #define bk back() #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define R0F(i, a) ROF(i, 0, a) #define EACH(a, x) for (auto& a : x) const int MOD = 1e9 + 7; const int MX = 20; const ll INF = 1e18; int L, Q; string S; int A, B, C; // A = '0', B = '1', C = '?' int cntA, cntB, cntC; int cnt[(1 << MX)], sup[(1 << MX)], sub[(1 << MX)]; // sup - SUM_{mask \in i}, sub - SUM_{i \in mask} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> L >> Q >> S; F0R(i, (1 << L)) sup[i] = sub[i] = S[i] - '0', cnt[i] = __builtin_popcount(i); F0R(i, L) F0R(j, (1 << L)) { if (!(j & (1 << i))) sup[j] += sup[j ^ (1 << i)]; if ((j & (1 << i))) sub[j] += sub[j ^ (1 << i)]; } F0R(i, Q) { string cur; cin >> cur; A = B = C = cntA = cntB = cntC = 0; ll ans = 0; F0R(j, L) { if (cur[j] == '0') A ^= (1 << (L - j - 1)), cntA++; if (cur[j] == '1') B ^= (1 << (L - j - 1)), cntB++; if (cur[j] == '?') C ^= (1 << (L - j - 1)), cntC++; } if (cntA <= L / 3) { for (int X = A; X > 0; X = (X - 1) & A) { ans += (1 - 2 * (cnt[X] & 1)) * sup[B | X]; } ans += sup[B]; cout << ans << "\n"; } else if (cntB <= L / 3) { for (int X = B; X > 0; X = (X - 1) & B) { ans += (1 - 2 * (cnt[X] & 1)) * sub[C | X]; } ans += sub[C]; cout << abs(ans) << "\n"; } else { for (int X = C; X > 0; X = (X - 1) & C) ans += S[B | X] - '0'; ans += S[B] - '0'; cout << ans << "\n"; } } }
#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...