結果

問題 No.3208 Parse AND OR Affection
ユーザー ococonomy1
提出日時 2025-07-18 23:10:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,832 ms / 5,000 ms
コード長 8,976 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 218,364 KB
実行使用メモリ 89,564 KB
最終ジャッジ日時 2025-07-18 23:11:35
合計ジャッジ時間 36,159 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pli = pair<ll,int>;
#define AMARI 998244353
//#define AMARI 1000000007
#define el '\n'
#define El '\n'
#define YESNO(x) ((x) ? "Yes" : "No")
#define YES YESNO(true)
#define NO YESNO(false)
#define REV_PRIORITY_QUEUE(tp) priority_queue<tp,vector<tp>,greater<tp>>
#define EXIT_ANS(x) {cout << (x) << '\n'; return;}
template <typename T> void inline SORT(vector<T> &v){sort(v.begin(),v.end()); return;}
template <typename T> void inline VEC_UNIQ(vector<T> &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return;}
template <typename T> T inline MAX(vector<T> &v){return *max_element(v.begin(),v.end());}
template <typename T> T inline MIN(vector<T> &v){return *min_element(v.begin(),v.end());}
template <typename T> T inline SUM(vector<T> &v){T ans = 0; for(int i = 0; i < (int)v.size(); i++)ans += v[i]; return ans;}
template <typename T> void inline DEC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]--; return;}
template <typename T> void inline INC(vector<T> &v){for(int i = 0; i < (int)v.size(); i++)v[i]++; return;}
void inline TEST(void){cerr << "TEST" << endl; return;}
template <typename T> bool inline chmin(T &x,T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}
template <typename T> bool inline chmax(T &x,T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template <typename T = long long> vector<T> inline get_vec(int n){
    vector<T> ans(n);
    for(int i = 0; i < n; i++)cin >> ans[i];
    return ans;
}
template <typename T> void inline print_vec(vector<T> &vec,bool kaigyou = false){
    int n = (int)vec.size();
    for(int i = 0; i < n; i++){
        cout << vec[i];
        if(kaigyou || i == n - 1)cout << '\n';
        else cout << ' ';
    }
    if(!n)cout << '\n';
    return;
}
template <typename T> void inline debug_vec(vector<T> &vec,bool kaigyou = false){
    int n = (int)vec.size();
    for(int i = 0; i < n; i++){
        cerr << vec[i];
        if(kaigyou || i == n - 1)cerr << '\n';
        else cerr << ' ';
    }
    if(!n)cerr << '\n';
    return;
}
vector<vector<int>> inline get_graph(int n,int m = -1,bool direct = false){
    if(m == -1)m = n - 1;
    vector<vector<int>> g(n);
    while(m--){
        int u,v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        if(!direct)g[v].push_back(u);
    }
    return g;
}

vector<int> make_inv(vector<int> p){
    int n = (int)p.size();
    vector<int> ans(n);
    for(int i = 0; i < n; i++)ans[p[i]] = i;
    return ans;
}

// 足し算・掛け算・べき乗(O(logN)のやつ)しかない。
template <typename T,int n> class ococo_gyouretu {
public:
    int sz;
    // ここに扱うデータが入ってる
    vector<vector<T>> a;
    ococo_gyouretu(void){
        sz = n;
        a.resize(n);
        for(int i = 0; i < n; i++) a[i].resize(n,0LL);
    }
    ococo_gyouretu get_taxnigyouretu(void){
        ococo_gyouretu<T,n> ans;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                ans.a[i][j] = (i == j ? 1 : 0);
            }
        }
        return ans;
    }
    ococo_gyouretu operator+(ococo_gyouretu const &r) const {
        ococo_gyouretu<T,n> ans;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                ans[i][j] += a[i][j] + r.a[i][j];
            }
        }
        return ans;
    }
    ococo_gyouretu operator*(ococo_gyouretu const &r) const {
        ococo_gyouretu<T,n> ans;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                for(int k = 0; k < n; k++) {
                    ans.a[i][j] += a[i][k] * r.a[k][j];
                }
            }
        }
        return ans;
    }
    ococo_gyouretu bekizyou(long long b) {
        ococo_gyouretu<T,n> temp, ans;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                temp.a[i][j] = a[i][j];
                ans.a[i][j] = (i == j ? 1 : 0);
            }
        }
        while(b) {
            if(b % 2) {
                ans = ans * temp;
            }
            temp = temp * temp;
            b /= 2;
        }
        return ans;
    }
    //なぜか friend を外すとコンパイルできなくなる。言語仕様覚えるのしんどすぎ!
    friend bool operator==(const ococo_gyouretu& l,const ococo_gyouretu& r){
        if(l.sz != r.sz)return false;
        for(int i = 0; i < l.sz; i++){
            for(int j = 0; j < l.sz; j++){
                if(l.a[i][j] != r.a[i][j])return false;
            }
        }
        return true;
    }
};


// 抽象再帰セグ木?(抽象セグ木の定義分からんな)(遅延ではない)
template <typename T> class ococo_segtree {
    T e;
    T func(T a, T b) {
        // ここに演算を入れる
        return (b * a);
    }

  public:
    int n;
    T tmax;
    // range minimum query
    vector<T> rmq;

    //(データの大きさ,単位元)
    ococo_segtree(int N) {
        e.a = {{1LL,0LL,0LL,0LL},{0LL,1LL,0LL,0LL},{0LL,0LL,1LL,0LL},{0LL,0LL,0LL,1LL}};
        syokica(N);
    }

    // 配列の初期化 O(N)
    void syokica(int a) {
        n = 1;
        tmax = e;
        while(n < a) n *= 2;
        rmq.resize(2 * n - 1);
        for(int i = 0; i < 2 * n - 1; i++) {
            rmq[i] = tmax;
        }
    }

    // a[i]をxにする O(logN)
    void update_num(int i, T x) {
        i += (n - 1);
        rmq[i] = x;
        while(i) {
            i = (i - 1) / 2;
            rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]);
        }
    }

    // a[i]にxを加える O(logN)
    void add_num(int i, T x) {
        i += (n - 1);
        rmq[i] += x;
        while(i) {
            i = (i - 1) / 2;
            rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]);
        }
    }

    T get_minimum2(int a, int b, int k, int l, int r) {
        if(a <= l && r <= b) return rmq[k];
        else if(r <= a || b <= l) return tmax;
        else return func(get_minimum2(a, b, k * 2 + 1, l, (l + r) / 2), get_minimum2(a, b, k * 2 + 2, (l + r) / 2, r));
    }
    //[l,r]の区間演算の取得 O(logN)
    T get_val(int l, int r) {
        return get_minimum2(l, r + 1, 0, 0, n);
    }
};



#define MULTI_TEST_CASE false
void solve(void){
    //問題を見たらまず「この問題設定から言えること」をいっぱい言う
    //よりシンプルな問題に言い換えられたら、言い換えた先の問題を自然言語ではっきりと書く
    //複数の解法のアイデアを思いついた時は全部メモしておく
    //g++ -D_GLIBCXX_DEBUG -Wall -O2 f.cpp -o o
    int n;
    cin >> n;
    int q;
    cin >> q;
    string s;
    cin >> s;

    ococo_segtree<ococo_gyouretu<ll,4>> sgt(n / 2 + 1);

    ococo_gyouretu<ll,4> kihon;
    kihon.a[0][0] = 1LL;
    kihon.a[3][1] = 1LL;
    kihon.a[3][3] = 1LL;


    for(int i = 1; i < n; i += 2){
        ococo_gyouretu<ll,4> temp = kihon;
        temp.a[(s[i + 1] == 'T' ? 1 : 2)][0] = 1LL;

        if((s[i] == '*' && s[i + 1] == 'T') || (s[i] == '+' && s[i +1] == 'F') || (s[i] == '^' && s[i + 1] == 'F')){
            
            temp.a[1][1] = temp.a[2][2] = 1LL;
        }

        if(s[i] =='*' && s[i + 1] == 'F'){
            temp.a[2][1] = temp.a[2][2] = 1LL;
        }
        if(s[i] == '+' && s[i + 1] == 'T'){
            
            temp.a[1][1] = temp.a[1][2] = 1LL;
        }
        if(s[i] =='^' && s[i + 1] == 'T'){
            
            temp.a[1][2] = temp.a[2][1] = 1LL;
        }
        /*
        for(int j = 0; j < 4; j++){
            for(int l = 0; l <4 ; l++){
                cerr << temp.a[j][l] << ' ' ;
            }
            cerr << el;
        }
        cerr << el;
        */
        //cerr << (i / 2) << el;
        sgt.update_num(i / 2,temp);
    }

    
    sgt.update_num(n / 2,kihon);

    while(q--){
        int l,r;
        cin >> l >> r;
        //cerr << l << ' ' << r << El;
        ococo_gyouretu<ll,4> ans = sgt.get_val(l / 2,r / 2);
        /*
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                cerr << ans.a[i][j] << ' ';
            }
            cerr << el;
        }
        */
        
        
        if(s[l - 1] == 'T'){
            cout << ans.a[3][0] + ans.a[3][1] << el;
            //cout << ans.a[0][3] + ans.a[1][3] << el;
        }
        if(s[l - 1] == 'F'){
            cout << ans.a[3][0] + ans.a[3][2] << el;
            //cout << ans.a[0][3] + ans.a[2][3] << el;
        }
    }

    return;
}

void calc(void){
    return;
}

signed main(void){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE)cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
0