結果

問題 No.3208 Parse AND OR Affection
コンテスト
ユーザー zawakasu
提出日時 2026-10-19 16:38:02
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 158 ms / 5,000 ms
+ 805µs
コード長 12,002 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,535 ms
コンパイル使用メモリ 228,888 KB
実行使用メモリ 52,528 KB
最終ジャッジ日時 2026-10-23 20:23:36
合計ジャッジ時間 5,928 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/*
 * Disclaimer: All code enclosed within “namespace zawa” is available at the following link.
 * https://zawa-tin.github.io/cp-documentation/
 */
#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
#include <random>
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"


#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa


#include <concepts>

namespace zawa {

namespace concepts {

template <class T>
concept Semigroup = requires {
    typename T::Element;
    { T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};

} // namespace concepts

} // namespace zawa


namespace zawa {

namespace concepts {

template <class T>
concept Identitiable = requires {
    typename T::Element;
    { T::identity() } -> std::same_as<typename T::Element>;
};

template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;

} // namespace

} // namespace zawa

#include <functional>
#include <type_traits>
#include <ostream>

namespace zawa {

template <concepts::Monoid Monoid>
class SegmentTree {
public:

    using VM = Monoid;

    using V = typename VM::Element;

    using OM = Monoid;

    using O = typename OM::Element;

    SegmentTree() = default;

    explicit SegmentTree(usize n) : m_n{ n }, m_dat(n << 1, VM::identity()) {}

    explicit SegmentTree(const std::vector<V>& dat) : m_n{ dat.size() }, m_dat(dat.size() << 1, VM::identity()) {
        for (usize i{} ; i < m_n ; i++) {
            m_dat[i + m_n] = dat[i];
        }
        for (usize i{m_n} ; i-- ; ) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] inline usize size() const noexcept {
        return m_n;
    }

    [[nodiscard]] V get(usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    [[nodiscard]] V operator[](usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    void operation(usize i, const O& value) {
        assert(i < size());
        i += size();
        m_dat[i] = OM::operation(m_dat[i], value);
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    void assign(usize i, const V& value) {
        assert(i < size());
        i += size();
        m_dat[i] = value;
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] V product(u32 l, u32 r) const {
        assert(l <= r and r <= size());
        V L{ VM::identity() }, R{ VM::identity() };
        for (l += size(), r += size() ; l < r ; l = parent(l), r = parent(r)) {
            if (l & 1) {
                L = VM::operation(L, m_dat[l++]);
            }
            if (r & 1) {
                R = VM::operation(m_dat[--r], R);
            }
        }
        return VM::operation(L, R);
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize maxRight(usize l, const F& f) {
        assert(l < size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{l}, width{1};
        V prod{ VM::identity() };
        for (l += size() ; res + width <= size() ; l = parent(l), width <<= 1) if (l & 1) {
            if (not f(VM::operation(prod, m_dat[l]))) break; 
            res += width;
            prod = VM::operation(prod, m_dat[l++]);
        }
        while (l = left(l), width >>= 1) {
            if (res + width <= size() and f(VM::operation(prod, m_dat[l]))) {
                res += width;
                prod = VM::operation(prod, m_dat[l++]);
            } 
        }
        return res;
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize minLeft(usize r, const F& f) const {
        assert(r <= size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{r}, width{1};
        V prod{ VM::identity() };
        for (r += size() ; res >= width ; r = parent(r), width <<= 1) if (r & 1) {
            if (not f(VM::operation(m_dat[r - 1], prod))) break;
            res -= width;
            prod = VM::operation(prod, m_dat[--r]);
        }
        while (r = left(r), width >>= 1) {
            if (res >= width and f(VM::operation(m_dat[r - 1], prod))) {
                res -= width;
                prod = VM::operation(m_dat[--r], prod);
            }
        }
        return res;
    }

    friend std::ostream& operator<<(std::ostream& os, const SegmentTree& st) {
        for (usize i{1} ; i < 2 * st.size() ; i++) {
            os << st.m_dat[i] << (i + 1 == 2 * st.size() ? "" : " ");
        }
        return os;
    }

private:

    constexpr u32 left(u32 v) const {
        return v << 1;
    }

    constexpr u32 right(u32 v) const {
        return v << 1 | 1;
    }

    constexpr u32 parent(u32 v) const {
        return v >> 1;
    }

    usize m_n;

    std::vector<V> m_dat;
};

} // namespace zawa
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// #include "Src/DataStructure/Heap/BinaryHeap.hpp"


#include <array>
#include <initializer_list>
#include <span>

namespace zawa {

template <class Semiring,usize N>
class ConstantSquareMatrix {

    static_assert(N > 0,"ConstantSquareMatrix::N must be positive");

public:

    using T = typename Semiring::Element;

    using A = typename Semiring::Addition;

    using M = typename Semiring::Multiplication;

    using Element = ConstantSquareMatrix;

    constexpr ConstantSquareMatrix() {
        m_data.fill(A::identity());
    }

    constexpr explicit ConstantSquareMatrix(const std::array<T,N*N>& data) : m_data{data} {}

    constexpr explicit ConstantSquareMatrix(const std::array<std::array<T,N>,N>& data) {
        for (usize i = 0 ; i < N ; i++)
            for (usize j = 0 ; j < N ; j++)
                m_data[i*N+j] = data[i][j];
    }

    constexpr explicit ConstantSquareMatrix(std::initializer_list<std::initializer_list<T>> data) {
        assert(data.size() == N);
        for (usize i = 0 ; const auto& row : data) {
            assert(row.size() == N);
            for (usize j = 0 ; const auto& x : row)
                m_data[i*N+j++]=x;
            i++;
        }
    }

    constexpr std::span<T,N> operator[](usize i) & {
        return std::span<T,N>{m_data.data()+i*N,N};
    }

    constexpr std::span<const T,N> operator[](usize i) const& {
        return std::span<const T,N>{m_data.data()+i*N,N};
    }

    constexpr usize size() const noexcept {
        return N;
    }

    static constexpr Element zero() {
        return ConstantSquareMatrix();
    }

    static constexpr Element identity() {
        auto res = ConstantSquareMatrix();
        for (usize i = 0 ; i < N ; i++)
            res[i][i] = M::identity();
        return res;
    }

    static constexpr Element operation(const Element& lhs,const Element& rhs) {
        Element res = zero();
        for (usize i = 0 ; i < N ; i++) {
            for (usize k = 0 ; k < N ; k++) {
                const T& x = lhs[i][k];
                for (usize j = 0 ; j < N ; j++)
                    res[i][j] = A::operation(res[i][j],M::operation(x,rhs[k][j]));
            }
        }
        return res;
    }

    static constexpr std::array<T,N> leftAction(const Element& mat,const std::array<T,N>& vec) {
        std::array<T,N> res;
        for (usize i = 0 ; i < N ; i++) {
            res[i] = A::identity();
            for (usize j = 0 ; j < N ; j++)
                res[i] = A::operation(res[i],M::operation(mat[i][j],vec[j]));
        }
        return res;
    }

private:

    std::array<T,N*N> m_data;
};

} // namespace zawa



namespace zawa {

template <class T>
class AdditiveGroup {
public:
    using Element = T;
    static constexpr T identity() noexcept {
        return T{};
    }
    static constexpr T operation(T l,T r) noexcept {
        return l + r;
    }
    static constexpr T inverse(T v) noexcept {
        return -v;
    }
    template <class U>
    static constexpr T power(T v,U exp) noexcept {
        return v * static_cast<T>(exp);
    }
};

} // namespace zawa

namespace zawa {

namespace internal {

template <class T>
struct Multiplication {

    using Element = T;

    static constexpr Element identity() {
        return static_cast<Element>(1);
    }

    static constexpr Element operation(const Element& lhs, const Element& rhs) {
        return lhs * rhs;
    }

    static constexpr Element inverse(const Element& value) {
        return identity() / value;
    }
};

} // namespace internal

template <class T>
struct UsualRing {

    using Element = T;

    using Addition = AdditiveGroup<T>;

    using Multiplication = typename internal::Multiplication<T>;

};

} // namespace zawa
namespace zawa {}
using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
// #include <array>
// #include <bit>
// #include <bitset>
// #include <climits>
// #include <cmath>
// #include <set>
// #include <unordered_set>
// #include <map>
// #include <unordered_map>
// #include <optional>
// #include <queue>
// #include <stack>
// #include <deque>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0 ; i < ssize(v) ; i++)
        os << v[i] << (i + 1 == ssize(v) ? "" : " ");
    return os;
}
/*
 *
 */
using M=ConstantSquareMatrix<UsualRing<long long>,4>;
// 0...今0
// 1...今1
// 2...全部の1
// 3...定数項
M gen(char c,int t) {
    M res=M::zero();
    res[2][2]=1;
    res[3][3]=1;
    res[3][t]++;
    if (t)
        res[3][2]++;
    if (c!='_') 
        for (int i : {0,1}) {
            int to;
            if (c=='+')
                to=i|t;
            else if (c=='*')
                to=i&t;
            else if (c=='^')
                to=i^t;
            else
                assert(0);
            res[i][to]++;
            if (to)
                res[i][2]++;
        }
    return res;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
#if !defined DEBUG
    int N,Q;
    cin >> N >> Q;
    string S;
    cin >> S;
    S.insert(S.begin(),'_');
    vector<M> A;
    A.reserve(N/2);
    for (int i = 1 ; i < ssize(S) ; i += 2)
        A.push_back(gen(S[i-1],S[i]=='T'));
    SegmentTree<M> seg(move(A));
    while (Q--) {
        int l,r;
        cin >> l >> r;
        l/=2;
        r=(r+1)/2;
        auto pd=seg.product(l,r);
        cout << pd[3][2] << '\n';
    }
#else
    mt19937_64 mt{random_device{}()};
    for (int testcase = 0 ; ; ) {
        cerr << "----------" << ++testcase << "----------" << endl;
        
        auto a = solve(), b = naive();
        if (a != b) {
            // print testcase

            cerr << "you: " << a << endl;
            cout << "correct: " << b << endl;
            exit(0);
        }
    }
#endif
}
0