# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
535016 | pooyashams | Snake Escaping (JOI18_snake_escaping) | C++17 | 1895 ms | 42068 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int lgmx = 20;
const int maxn = (1<<lgmx);
int val[maxn];
int sub[maxn];
int sup[maxn];
int32_t main()
{
int L, q;
scanf("%d %d", &L, &q);
const int n = (1<<L);
for(int i = 0; i < n; i++)
{
char c;
scanf(" %c ", &c);
val[i] = c-'0';
sub[i] = val[i];
sup[i] = val[i];
}
for(int j = 0; j < L; j++)
for(int i = 0; i < n; i++)
if((i>>j) & 1)
sub[i] += sub[i^(1<<j)];
for(int j = L-1; j >= 0; j--)
for(int i = n-1; i >= 0; i--)
if((i>>j) & 1)
sup[i^(1<<j)] += sup[i];
vector<int> vec[3];
for(int i = 0; i < 3; i++) vec[i].reserve(20);
const int l3 = L/3;
while(q--)
{
for(int i = 0; i < 3; i++) vec[i].clear();
for(int i = L-1; i >= 0; i--)
{
char c; scanf(" %c ", &c);
if(c == '?')
vec[2].push_back(i);
else
vec[c-'0'].push_back(i);
}
int ans = 0;
if((int)(vec[0].size()) <= l3)
{
int a = vec[0].size();
int k = 0;
for(int j: vec[1]) k ^= (1<<j);
for(int i = 0; i < (1<<a); i++)
{
int x = k;
for(int j = i; j > 0; j -= j&-j)
x ^= (1<<vec[0][__builtin_ctz(j)]);
if(__builtin_popcount(i) & 1)
ans -= sup[x];
else
ans += sup[x];
}
}
else if((int)(vec[1].size()) <= l3)
{
int a = vec[1].size();
int k = 0;
for(int j: vec[2]) k ^= (1<<j);
for(int i = 0; i < (1<<a); i++)
{
int x = k;
for(int j = i; j > 0; j -= j&-j)
x ^= (1<<vec[1][__builtin_ctz(j)]);
if( (a-__builtin_popcount(i)) & 1)
ans -= sub[x];
else
ans += sub[x];
}
}
else
{
int a = vec[2].size();
int k = 0;
for(int j: vec[1]) k ^= (1<<j);
for(int i = 0; i < (1<<a); i++)
{
int x = k;
for(int j = i; j > 0; j -= j&-j)
x ^= (1<<vec[2][__builtin_ctz(j)]);
ans += val[x];
}
}
printf("%d\n", ans);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |