Submission #536032

#TimeUsernameProblemLanguageResultExecution timeMemory
536032EvangSnake Escaping (JOI18_snake_escaping)C++17
75 / 100
2080 ms21176 KiB
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;

#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a,b) uniform_int_distribution<int>(a,b)(rng)

#define ins insert
#define ssize(x) (int((x).size()))
#define bs(args...) binary_search(args)
#define lb(args...) lower_bound(args)
#define ub(args...) upper_bound(args)
#define all(x) (x).begin(),(x).end()
#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define die exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


const int N = 20;
const int MM = 1<<N;
int n, q, ar[MM], sub[MM], sup[MM];

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cout << fixed; clog << fixed; clog << unitbuf;
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    cin >> n >> q;
    for(int i = 0; i < (1<<n); ++i) {
        char c;
        cin >> c;
        sub[i] = sup[i] = ar[i] = c-'0';
    }

    for(int i = 0; i < n; ++i){
        for(int j = 1; j < (1<<n); ++j)
            if((j>>i)&1)
                sub[j] += sub[j^(1<<i)];
        for(int j = (1<<n)-1; j >= 0; --j)
            if((j>>i)&1^1)
                sup[j] += sup[j^(1<<i)];
    }

#ifdef _DEBUG
    clog << "Line " << __LINE__ << ":\n";
    for(int i = 0; i < (1<<n); ++i)
        clog << bitset<N>(i) << sp << sub[i] << el;
    clog << el;
#endif

#ifdef _DEBUG
    clog << "Line " << __LINE__ << ":\n";
    for(int i = 0; i < (1<<n); ++i)
        clog << bitset<N>(i) << sp << sup[i] << el;
    clog << el;
#endif

    while(q--){
        str s;
        cin >> s;
        reverse(all(s));
        int a = 0, b = 0, c = 0;
        for(int i = 0; i < n; ++i){
            if(s[i]=='0')
                a |= 1<<i;
            else if(s[i]=='1')
                b |= 1<<i;
            else
                c |= 1<<i;
        }

        ll ans = 0;
        if(a==min({a, b, c})){
            for(int x = a;; x = (x-1)&a) {
                if (__builtin_popcount(x) & 1)
                    ans -= sup[b | x];
                else
                    ans += sup[b | x];

                if(x==0)
                    break;
            }
        }else if(b==min({a, b, c})){
            for(int x = b; ; x = (x-1)&b){
                if((__builtin_popcount(b) - __builtin_popcount(x))&1)
                    ans -= sub[c|x];
                else
                    ans += sub[c|x];
                if(x==0)
                    break;
            }
        }else{
            for(int x = c; ; x = (x-1)&c){
                ans += ar[b|x];
                if(x==0)
                    break;
            }
        }
        cout << ans << el;
    }
}

Compilation message (stderr)

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:90:22: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   90 |             if((j>>i)&1^1)
      |                ~~~~~~^~
#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...