Submission #1141382

#TimeUsernameProblemLanguageResultExecution timeMemory
1141382tfgsSnake Escaping (JOI18_snake_escaping)C++17
75 / 100
2094 ms46760 KiB
// folding tries technique
// the bottleneck here is reading all the queries and keeping them in memory, and sorting.
// the folding tries is a lot better than the worst case in practice

#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)); }

const int K = 5e5;
pair<string, int> qs[K];

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';
    }

    int times = (Q + K - 1) / K;
    for (int _ = 0; _ < times; _++) {
        int q = min(K, Q);
        Q -= K;
        vi ans(q);
        for (int i = 0; i < q; i++) {
            string pat(L, 0);
            for (int j = 0; j < L; j++) {
                char c; cin >> c;
                pat[j] = c == '?' ? 2 : c - '0';
            }
            qs[i] = { pat, i };
        }
        sort(begin(qs), begin(qs) + q, [&](const pair<string, int>& _a, const pair<string, int>& _b) {
            const auto& a = _a.f, b = _b.f; 
            int key[3];
            key[0] = 0;
            key[1] = 2;
            key[2] = 1;
            for (int i = 0; i < L; i++) {
                if (a[i] != b[i]) return key[a[i]] < key[b[i]];
            }
            return false;
        });
        cerr << "sort done" << endl;

        int l = 0, r = p2(L);
        V<char> cur_pre;
        V<ai2> lrs;
        auto descend = [&](const string& pat) {
            while (len(cur_pre) < L) {
                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];
            int lcp = 0;
            if (i < q - 1) {
                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...