Submission #130124

#TimeUsernameProblemLanguageResultExecution timeMemory
130124qkxwsmSnake Escaping (JOI18_snake_escaping)C++14
100 / 100
2000 ms43336 KiB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 1100013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, Q;
int arr[MAXN];
int sos[MAXN], sob[MAXN];
vi freq[3];
int ans;

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> Q;
	string temps; cin >> temps;
	FOR(i, 0, (1 << N))
	{
		arr[i] = temps[i] - '0';
		sos[i] = arr[i];
		sob[i] = arr[i];
	}
	//compute answer if there were only 1s and ????s
	FOR(i, 0, N)
	{
		FOR(j, 0, (1 << N))
		{
			if (j & (1 << i))
			{
				sos[j] += sos[j - (1 << i)];
			}
			else
			{
				sob[j] += sob[j + (1 << i)];
			}
		}
	}
	while(Q--)
	{
		cin >> temps; reverse(ALL(temps));
		ans = 0;
		int tot = 0;
		FOR(i, 0, 3) freq[i].clear();
		FOR(i, 0, N)
		{
			int want = temps[i] - '0';
			if (temps[i] == '?') want = 2;
			freq[want].PB(i);
		}
		int mn = 0;
		FOR(i, 0, 3) if (SZ(freq[i]) < SZ(freq[mn])) mn = i;
		//now solve it in O(2^mn)
		if (mn == 2)
		{
			int idx = 0;
			for (int x : freq[1]) idx += (1 << x);
			FOR(i, 0, (1 << SZ(freq[2])))
			{
				int cidx = idx;
				FOR(j, 0, SZ(freq[2]))
				{
					if ((i & (1 << j)))
					{
						cidx += (1 << freq[2][j]);
					}
				}
				ans += arr[cidx];
			}
		}
		if (mn == 1)
		{
			int idx = 0;
			for (int x : freq[1]) idx += (1 << x);
			for (int x : freq[2]) idx += (1 << x);
			FOR(i, 0, (1 << SZ(freq[1])))
			{
				int cidx = idx;
				FOR(j, 0, SZ(freq[1]))
				{
					if ((i & (1 << j))) cidx -= (1 << freq[1][j]);
				}
				ans += ((__builtin_popcount(i) & 1) ? -1 : 1) * sos[cidx];
			}
		}
		if (mn == 0)
		{
			int idx = 0;
			for (int x : freq[1]) idx += (1 << x);
			FOR(i, 0, (1 << SZ(freq[0])))
			{
				int cidx = idx;
				FOR(j, 0, SZ(freq[0]))
				{
					if (i & (1 << j)) cidx += (1 << freq[0][j]);
				}
				ans += ((__builtin_popcount(i) & 1) ? -1 : 1) * sob[cidx];
			}
		}
		cout << ans << '\n';
	}
	return 0;
}

Compilation message (stderr)

snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:75:7: warning: unused variable 'tot' [-Wunused-variable]
   int tot = 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...