結果
問題 |
No.3208 Parse AND OR Affection
|
ユーザー |
👑 ![]() |
提出日時 | 2025-07-18 21:38:00 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 179 ms / 5,000 ms |
コード長 | 3,972 bytes |
コンパイル時間 | 1,242 ms |
コンパイル使用メモリ | 98,548 KB |
実行使用メモリ | 40,072 KB |
最終ジャッジ日時 | 2025-07-18 21:38:15 |
合計ジャッジ時間 | 4,728 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 20 |
ソースコード
#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i<i64(n); i++) const i64 INF = 1001001001001001001; template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; } template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; namespace nachia{ template< class S, S op(S l, S r) > struct Segtree { private: int N; std::vector<S> A; int xN; void mergev(int i){ if(i < N) A[i] = op(A[i*2], A[i*2+1]); } template<class E> int minLeft2(int r, E cmp, int a = 0, int b = 0, int i = -1) const { static S x; if(i == -1){ a=0; b=N; i=1; x=A[0]; } if(r <= a) return a; if(b <= r){ S nx = op(A[i], x); if(cmp(nx)){ x = nx; return a; } } if(b - a == 1) return b; int q = minLeft2(r, cmp, (a+b)/2, b, i*2+1); if(q > (a+b)/2) return q; return minLeft2(r, cmp, a, (a+b)/2, i*2); } template<class E> int maxRight2(int l, E cmp, int a = 0, int b = 0, int i = -1) const { static S x; if(i == -1){ a=0; b=N; i=1; x=A[0]; } if(b <= l) return b; if(l <= a){ S nx = op(x, A[i]); if(cmp(nx)){ x = nx; return b; } } if(b - a == 1) return a; int q = maxRight2(l, cmp, a, (a+b)/2, i*2); if(q < (a+b)/2) return q; return maxRight2(l, cmp, (a+b)/2, b, i*2+1); } public: Segtree() : N(0) {} Segtree(int n, S e) : xN(n) { N = 1; while (N < n) N *= 2; A.assign(N * 2, e); } Segtree(const std::vector<S>& a, S e) : Segtree(a.size(), e){ for(int i=0; i<(int)a.size(); i++) A[i + N] = a[i]; for(int i=N-1; i>=1; i--) mergev(i); } S getE() const { return A[0]; } void set(int p, S x){ p += N; A[p] = x; for(int d=1; (1<<d)<=N; d++) mergev(p>>d); } S get(int p) const { return A[N+p]; } S prod(int l, int r) const { l += N; r += N; S ql = A[0], qr = A[0]; while(l<r){ if(l&1) ql = op(ql, A[l++]); if(r&1) qr = op(A[--r], qr); l /= 2; r /= 2; } return op(ql, qr); } S allProd() const { return A[1]; } // bool cmp(S) template<class E> int minLeft(int r, E cmp) const { return minLeft2(r, cmp); } // bool cmp(S) template<class E> int maxRight(int l, E cmp) const { int x = maxRight2(l, cmp); return x > xN ? xN : x; } }; } // namespace nachia struct Node { i64 A[4][3] = {}; }; Node op(Node l, Node r){ Node res; rep(f,4) rep(s,3) rep(t,3) res.A[f][t] += l.A[f][s] * r.A[s][t]; rep(t,3) res.A[3][t] += r.A[3][t]; return res; } void testcase(){ int N, Q; cin >> N >> Q; string s; cin >> s; vector<Node> f(N/2+1); for(i64 i=0; i<N; i+=2){ int c = s[i] == 'T' ? 1 : 0; f[i/2].A[3][c] += 1; } for(i64 i=1; i<N; i+=2){ int c = s[i+1] == 'T' ? 1 : 0; if(s[i] == '+'){ rep(t,2) f[i/2+1].A[t][t|c] += 1; } else if(s[i] == '*'){ rep(t,2) f[i/2+1].A[t][t&c] += 1; } else { rep(t,2) f[i/2+1].A[t][t^c] += 1; } } for(auto& g : f) g.A[2][2] = 1; for(auto& g : f) rep(t,4) g.A[t][2] += g.A[t][1]; Node e; rep(t,3) e.A[t][t] = 1; auto ds = nachia::Segtree<Node,op>(f, e); rep(i,Q){ int l,r; cin >> l >> r; l--; if(l%2 == 1) l++; if(r%2 == 0) r--; if(l>=r){ cout << "0\n"; continue; } l /= 2; r /= 2; r++; i64 ans = ds.prod(l, r).A[3][2]; cout << ans << "\n"; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }