제출 #1141351

#제출 시각아이디문제언어결과실행 시간메모리
1141351tfgsSnake Escaping (JOI18_snake_escaping)C++17
5 / 100
2096 ms70828 KiB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("avx2")

#ifdef LOCAL
#include "algo/debug.h"
#else
template <typename... Args>
void dummy(Args&&... args){}
#define ps dummy
#endif

#define f first
#define s second
template<class T> using V = vector<T>; 
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x) 
#define len(x) (int)((x).size())
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ai2 array<int,2>
#define ai3 array<int,3>
#define ai4 array<int,4>
#define ai5 array<int,5>
template<class T> int lwb(const V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(const V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define clz __builtin_clzll
constexpr int p2(int x) { return (int)1 << x; }
constexpr int bits(int x) { return x == 0 ? 0 : 63-clz(x); } // floor(log2(x)) 
template<class T>void UNIQUE(V<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }
template<class T,class U>void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); }
template<class F> struct y_combinator_result {
    F f;
    template<class T> explicit y_combinator_result(T &&f): f(std::forward<T>(f)) {}
    template<class ...Args> decltype(auto) operator()(Args &&...args) { return f(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) yy(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }

void solve() {
    int L, q; cin >> L >> q;
    vi arr(p2(L));
    for (int i = 0; i < p2(L); i++) {
        char c; cin >> c;
        arr[i] = c - '0';
    }

    vi ans(q);
    V<pair<V<char>, int>> qs;
    for (int i = 0; i < q; i++) {
        V<char> pat;
        for (int j = 0; j < L; j++) {
            char c; cin >> c;
            pat.pb(c == '?' ? 2 : c - '0');
        }
        qs.pb({ pat, i });
    }
    sort(all(qs), [&](const pair<V<char>, int>& _a, const pair<V<char>, int>& _b) {
        auto a = _a.f, b = _b.f; 
        for (int i = 0; i < L; i++) {
            bool ba = a[i] == 1;
            bool bb = b[i] == 1;
            if (ba != bb) return ba < bb;
        }
        return false;
    });


    int l = 0, r = p2(L);
    V<char> cur_pre;
    V<ai2> lrs;
    auto descend = [&](const V<char>& pat) {
        while (len(cur_pre) < len(pat)) {
            lrs.pb({ l, r });
            int m = (l + r) / 2;
            if (pat[len(cur_pre)] == 2) {
                for (int i = l; i < m; i++) {
                    arr[i] += arr[i + (r - l) / 2];
                }
            }
            (pat[len(cur_pre)] == 1 ? l : r) = m;
            cur_pre.pb(pat[len(cur_pre)]);
        }
    };
    auto ascend = [&](int new_dep) {
        while (len(cur_pre) > new_dep) {
            l = lrs.bk[0];
            r = lrs.bk[1];
            lrs.pop_back();
            int m = (l + r) / 2;
            if (cur_pre.bk == 2) {
                for (int i = l; i < m; i++) {
                    arr[i] -= arr[i + (r - l) / 2];
                }
            }
            cur_pre.pop_back();
        }
    };
    for (int i = 0; i < q; i++) {
        descend(qs[i].f);
        assert(l == r - 1);
        ans[qs[i].s] = arr[l];
        if (i < q - 1) {
            int lcp = 0;
            while (lcp < L && qs[i].f[lcp] == qs[i + 1].f[lcp]) lcp++;
            ascend(lcp);
        }
    }

    for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    solve();
    return 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...