結果

問題 No.2308 [Cherry 5th Tune B] もしかして、真?
ユーザー sten_sansten_san
提出日時 2023-05-19 22:17:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 7,618 bytes
コンパイル時間 2,046 ms
コンパイル使用メモリ 204,196 KB
実行使用メモリ 17,144 KB
最終ジャッジ日時 2023-08-23 00:11:09
合計ジャッジ時間 11,859 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 161 ms
7,212 KB
testcase_02 AC 193 ms
10,192 KB
testcase_03 AC 176 ms
10,156 KB
testcase_04 AC 180 ms
10,684 KB
testcase_05 AC 193 ms
11,112 KB
testcase_06 AC 197 ms
13,804 KB
testcase_07 AC 193 ms
13,244 KB
testcase_08 AC 211 ms
14,940 KB
testcase_09 AC 205 ms
14,900 KB
testcase_10 AC 181 ms
10,420 KB
testcase_11 AC 191 ms
10,152 KB
testcase_12 AC 188 ms
10,196 KB
testcase_13 AC 188 ms
10,152 KB
testcase_14 AC 193 ms
10,148 KB
testcase_15 AC 191 ms
10,228 KB
testcase_16 AC 190 ms
10,164 KB
testcase_17 AC 191 ms
10,192 KB
testcase_18 AC 189 ms
10,316 KB
testcase_19 AC 190 ms
10,204 KB
testcase_20 AC 189 ms
10,168 KB
testcase_21 AC 234 ms
17,064 KB
testcase_22 AC 230 ms
17,028 KB
testcase_23 AC 232 ms
16,984 KB
testcase_24 AC 222 ms
17,096 KB
testcase_25 AC 229 ms
17,076 KB
testcase_26 AC 226 ms
17,092 KB
testcase_27 AC 236 ms
16,984 KB
testcase_28 AC 235 ms
17,004 KB
testcase_29 AC 232 ms
17,144 KB
testcase_30 AC 226 ms
17,068 KB
testcase_31 AC 121 ms
17,012 KB
testcase_32 AC 122 ms
17,100 KB
testcase_33 AC 122 ms
17,008 KB
testcase_34 AC 127 ms
17,064 KB
testcase_35 AC 126 ms
17,004 KB
testcase_36 AC 127 ms
17,048 KB
testcase_37 AC 13 ms
4,376 KB
testcase_38 AC 200 ms
10,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return vector(arg, init);
    else return vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename Container>
auto distance(const Container &c, decltype(begin(c)) iter) {
    return distance(begin(c), iter);
}

template <typename RIter, typename Compare = less<typename iterator_traits<RIter>::value_type>>
auto isort(RIter first, RIter last, Compare comp = Compare()) {
    vector<int> i(distance(first, last));
    iota(begin(i), end(i), 0);
    sort(begin(i), end(i), [&](auto x, auto y) {
        return comp(*(first + x), *(first + y));
    });
    return i;
}

template <typename, template <typename> typename, typename = void_t<>>
struct detect : false_type {};
template <typename T, template <typename> typename Check>
struct detect<T, Check, void_t<Check<T>>> : true_type {};
template <typename T, template <typename> typename Check>
constexpr inline bool detect_v = detect<T, Check>::value;

template <typename T>
using has_member_sort = decltype(declval<T>().sort());

template <typename Container, typename Compare = less<typename Container::value_type>>
auto sorted(Container c, Compare comp = Compare()) {
    if constexpr (detect_v<Container, has_member_sort>) {
        c.sort(comp);
        return c;
    }
    else {
        sort(begin(c), end(c), comp);
        return c;
    }
}

template <typename Container, typename Compare = equal_to<typename Container::value_type>>
auto uniqued(Container c, Compare comp = Compare()) {
    c.erase(unique(begin(c), end(c), comp), end(c));
    return c;
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

template <typename F>
constexpr auto fix(F &&f) noexcept {
    return [f = std::tuple<F>(std::forward<F>(f))](auto &&...args) mutable {
        return std::get<0>(f)(fix(std::get<0>(f)), std::forward<decltype(args)>(args)...);
    };
}

template <size_t Bits>
struct binary_trie {
    static_assert(0 < Bits);

    constexpr static size_t bits = Bits;

    // O(1)
    binary_trie() noexcept:
        xor_all{ 0 }, root{} {
    }

    // O(1)
    // 0 <= v < 2^Bits
    void apply_xor(size_t v) noexcept {
        xor_all ^= v;
    }

    // O(1)
    size_t size() const noexcept {
        return root.count;
    }

    // O(Bits)
    // 0 <= v < 2^Bits
    size_t count(size_t v) const noexcept {
        return count_(v ^ xor_all);
    }
    // O(Bits)
    // 0 <= v < 2^Bits
    bool exist(size_t v) const noexcept {
        return exist_(v ^ xor_all);
    }

    // O(Bits)
    // 0 <= v < 2^Bits
    void insert(size_t v) {
        insert_(v ^ xor_all);
    }
    // O(Bits)
    // 0 <= v < 2^Bits
    void erase(size_t v) noexcept {
        erase_(v ^ xor_all);
    }

    // O(Bits)
    // 0 <= n < size(binary_trie)
    size_t nth_element(size_t n) const {
        if (size() <= n) {
            throw std::out_of_range("binary_trie");
        }

        size_t path = 0;

        auto b = (size_t(1) << (Bits - 1));

        const node *iter = &root; 
        while (0 < b) {
            auto m = !!(xor_all & b);
            auto c = iter->c[m];
            b >>= 1; path <<= 1;

            if (c != nullptr) {
                if (n < c->count) {
                    iter = c;
                    path |= 0;
                    continue;
                }
                n -= c->count;
            }

            iter = iter->c[!m];
            path |= 1;
        }

        return path;
    }

    // O(Bits)
    // 0 <= v < 2^Bits
    size_t lower_bound(size_t v) const noexcept {
        if (v == 0) {
            return 0;
        }
        return upper_bound(v - 1);
    }

    // O(Bits)
    // 0 <= v < 2^Bits
    size_t upper_bound(size_t v) const noexcept {
        auto b = (size_t(1) << (Bits - 1));

        size_t sum = 0;

        const node *iter = &root;
        while (0 < b) {
            auto m = !!(xor_all & b);
            auto f = !!(v & b);
            auto c = iter->c[m];

            if (c != nullptr && f) {
                sum += c->count;
            }

            auto d = iter->c[m ^ f];
            if (d == nullptr) {
                return sum;
            }
            iter = d;

            b >>= 1;
        }
        sum += iter->count;

        return sum;
    }

private:
    struct node {
        node() noexcept:
            count{ 0 }, c{ nullptr, nullptr } {
        }

        ~node() {
            if (c[0] != nullptr) delete c[0];
            if (c[1] != nullptr) delete c[1];
        }

        node *advance(bool f) {
            if (c[f] == nullptr) {
                c[f] = new node();
            }
            return c[f];
        }

        size_t count = 0; node *c[2];
    };

    size_t count_(size_t n) const noexcept {
        auto b = (size_t(1) << (Bits - 1));

        const node *iter = &root;
        while (0 < b) {
            iter = iter->c[!!(n & b)];
            if (iter == nullptr || iter->count == 0) {
                return 0;
            }
            b >>= 1;
        }

        return iter->count;
    }
    bool exist_(size_t n) const noexcept {
        return 0 < count_(n);
    }

    void insert_(size_t n) {
        auto b = (size_t(1) << (Bits - 1));

        node *iter = &root; ++root.count;
        while (0 < b) {
            iter = iter->advance(!!(n & b)); ++iter->count;
            b >>= 1;
        }
    }
    void erase_(size_t n) noexcept {
        if (exist_(n)) {
            auto b = (size_t(1) << (Bits - 1));

            node *iter = &root; --root.count;
            while (0 < b) {
                iter = iter->c[!!(n & b)]; --iter->count;
                b >>= 1;
            }
        }
    }

    size_t xor_all;
    node root;
};

int main() {
    int t; cin >> t;

    while (t--) {
        int n; cin >> n;

        auto x = vec<int>(uns, n);
        for (auto &v : x) {
            string s; cin >> s;
            v = (s[0] == 'T');
        }

        auto y = vec<int>(uns, n - 1);
        for (auto &v : y) {
            string s; cin >> s;

            if (s[0] == 'a') {
                v = 0;
            }
            if (s[0] == 'o') {
                v = 1;
            }
            if (s[0] == 'x') {
                v = 2;
            }
            if (s[0] == 'i') {
                v = 3;
            }
        }

        binary_trie<18> index;

        for (int i = 0; i < n; ++i) {
            index.insert(i);
        }

        for (int i = 1; i < n; ++i) {
            int s; cin >> s; --s;

            int l = index.nth_element(s);
            int r = index.nth_element(s + 1);

            if (y[l] == 0) {
                x[r] = x[l] & x[r];
            }
            if (y[l] == 1) {
                x[r] = x[l] | x[r];
            }
            if (y[l] == 2) {
                x[r] = x[l] ^ x[r];
            }
            if (y[l] == 3) {
                x[r] = !x[l] | x[r];
            }

            index.erase(l);
        }

        cout << (x[index.nth_element(0)] ? "True" : "False") << '\n';
    }

    cout << flush;
}

0