結果

問題 No.3208 Parse AND OR Affection
コンテスト
ユーザー detteiuu
提出日時 2026-08-23 01:23:38
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 199 ms / 5,000 ms
+ 333µs
コード長 4,152 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,922 ms
コンパイル使用メモリ 395,516 KB
実行使用メモリ 52,436 KB
最終ジャッジ日時 2026-08-23 01:23:51
合計ジャッジ時間 11,746 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

#define pass (void)0
#define INF (1<<30)-1
#define INFLL (1LL<<60)-1
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define YesNo(cond) cout << ((cond) ? "Yes\n" : "No\n")
#define YESNO(cond) cout << ((cond) ? "YES\n" : "NO\n")

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;

template <typename T> void print(const T& value) { cout << value << "\n"; }
template <typename T> void print(const vector<T>& vec) { for (auto& v : vec) cout << v << " "; cout << "\n"; }
template <typename T> void input(vector<T>& vec) { for (auto& v : vec) cin >> v; };
template <typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }

#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using vm = vector<mint>;

using mat = array<array<ll, 4>, 4>;
mat unity = {
    array<ll, 4> {1, 0, 0, 0},
    array<ll, 4> {0, 1, 0, 0},
    array<ll, 4> {0, 0, 1, 0},
    array<ll, 4> {0, 0, 0, 1}
};
mat zero = {
    array<ll, 4> {0, 0, 0, 0},
    array<ll, 4> {0, 0, 0, 0},
    array<ll, 4> {0, 0, 0, 0},
    array<ll, 4> {0, 0, 0, 0}
};
mat operator* (mat& left, mat& right) {
    mat res = zero;
    rep (i, 4) {
        rep (j, 4) {
            rep (k, 4) {
                res[i][j] += left[i][k]*right[k][j];
            }
        }
    }
    return res;
}

mat op(mat left, mat right) {
    return right*left;
}

mat e() {
    return unity;
}

vector<vector<ll>> matrix_mul(const vector<vector<ll>> &a,
                                const vector<vector<ll>> &b) {
    int H1 = a.size();
    int W1 = a[0].size();
    int W2 = b[0].size();

    vector<vector<ll>> ans(H1, vector<ll>(W2, 0));

    for (int i = 0; i < H1; i++) {
        for (int k = 0; k < W1; k++) {
            ll aik = a[i][k];
            for (int j = 0; j < W2; j++) {
                ans[i][j] += aik * b[k][j];
            }
        }
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);

    ll N, Q;
    cin >> N >> Q;
    string S;
    cin >> S;
    S = '+'+S;
    N ++;

    vector<mat> def(N/2);
    for (int i=1; i<N; i+=2) {
        char c;
        c = S[i-1];
        ll n = (S[i] == 'T') ? 1 : 0;
        mat mt = zero;
        if (c == '+') {
            if (n == 0) {
                mt[0][0] = 1;
                mt[1][1] = 1;
            } else {
                mt[1][0] = 1;
                mt[1][1] = 1;
            }
        } else if (c == '*') {
            if (n == 0) {
                mt[0][0] = 1;
                mt[0][1] = 1;
            } else {
                mt[0][0] = 1;
                mt[1][1] = 1;
            }
        } else {
            if (n == 0) {
                mt[0][0] = 1;
                mt[1][1] = 1;
            } else {
                mt[1][0] = 1;
                mt[0][1] = 1;
            }
        }
        // 1
        mt[n][2] = 1;
        mt[2][2] = 1;
        // 合計
        mt[3][1] = 1;
        mt[3][3] = 1;
        def[i/2] = mt;
    }

    segtree<mat, op, e> seg(def);
    while (Q--) {
        ll l, r;
        cin >> l >> r;
        if (l%2 == 0) l ++;
        if (r%2 == 0) r --;
        if (l > r) {
            print(0);
            continue;
        }

        l /= 2;
        r /= 2;
        auto res = seg.prod(l, r+1);

        vvl resv(4, vl(4, -1));
        rep (i, 4) rep (j, 4) resv[i][j] = res[i][j];
        vvl ansv(4, vl(1, 0));
        ansv[2][0] = 1;

        auto ans = matrix_mul(resv, ansv);
        print(ans[1][0]+ans[3][0]);
    }
}
0