結果

問題 No.2554 MMA文字列2 (Query Version)
ユーザー dyktr_06dyktr_06
提出日時 2023-10-27 02:02:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 462 ms / 5,000 ms
コード長 2,620 bytes
コンパイル時間 2,513 ms
コンパイル使用メモリ 208,232 KB
実行使用メモリ 57,444 KB
最終ジャッジ日時 2023-10-27 02:02:26
合計ジャッジ時間 21,894 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 3 ms
4,372 KB
testcase_03 AC 4 ms
4,372 KB
testcase_04 AC 5 ms
4,372 KB
testcase_05 AC 6 ms
4,372 KB
testcase_06 AC 4 ms
4,372 KB
testcase_07 AC 4 ms
4,372 KB
testcase_08 AC 4 ms
4,372 KB
testcase_09 AC 6 ms
4,372 KB
testcase_10 AC 5 ms
4,372 KB
testcase_11 AC 3 ms
4,372 KB
testcase_12 AC 47 ms
16,828 KB
testcase_13 AC 387 ms
57,444 KB
testcase_14 AC 77 ms
16,828 KB
testcase_15 AC 311 ms
57,444 KB
testcase_16 AC 206 ms
16,828 KB
testcase_17 AC 300 ms
30,344 KB
testcase_18 AC 114 ms
5,024 KB
testcase_19 AC 155 ms
16,828 KB
testcase_20 AC 356 ms
57,444 KB
testcase_21 AC 142 ms
6,608 KB
testcase_22 AC 421 ms
57,444 KB
testcase_23 AC 447 ms
57,444 KB
testcase_24 AC 428 ms
57,444 KB
testcase_25 AC 429 ms
57,444 KB
testcase_26 AC 422 ms
57,444 KB
testcase_27 AC 428 ms
57,444 KB
testcase_28 AC 462 ms
57,444 KB
testcase_29 AC 425 ms
57,444 KB
testcase_30 AC 420 ms
57,444 KB
testcase_31 AC 440 ms
57,444 KB
testcase_32 AC 69 ms
4,372 KB
testcase_33 AC 68 ms
4,372 KB
testcase_34 AC 73 ms
4,372 KB
testcase_35 AC 74 ms
4,372 KB
testcase_36 AC 77 ms
4,372 KB
testcase_37 AC 432 ms
57,444 KB
testcase_38 AC 457 ms
57,444 KB
testcase_39 AC 437 ms
57,444 KB
testcase_40 AC 462 ms
57,444 KB
testcase_41 AC 431 ms
57,444 KB
testcase_42 AC 429 ms
57,444 KB
testcase_43 AC 458 ms
57,444 KB
testcase_44 AC 428 ms
57,444 KB
testcase_45 AC 462 ms
57,444 KB
testcase_46 AC 437 ms
57,444 KB
testcase_47 AC 412 ms
57,444 KB
testcase_48 AC 434 ms
57,444 KB
testcase_49 AC 410 ms
57,444 KB
testcase_50 AC 408 ms
57,444 KB
testcase_51 AC 437 ms
57,444 KB
testcase_52 AC 406 ms
57,444 KB
testcase_53 AC 420 ms
57,444 KB
testcase_54 AC 437 ms
57,444 KB
testcase_55 AC 411 ms
57,444 KB
testcase_56 AC 414 ms
57,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename X>
struct SegTree{
    using FX = function<X(X&, X&)>; // X•X -> X となる関数の型
    int n;
    FX fx;
    const X ex;
    vector<X> dat;

    SegTree(int n_, const FX &fx_, const X &ex_) : n(), fx(fx_), ex(ex_){
        int x = 1;
        while(n_ > x){
            x *= 2;
        }
        n = x;
        dat.assign(n * 2, ex);
    }

    X get(int i) const {
        return dat[i + n];
    }
    
    void set(int i, X &x){ dat[i + n] = x; }

    void build(){
        for(int k = n - 1; k >= 1; k--) dat[k] = fx(dat[k * 2], dat[k * 2 + 1]);
    }

    void update(int i, X &x){
        i += n;
        dat[i] = x;
        while(i > 0){
            i >>= 1;  // parent
            dat[i] = fx(dat[i * 2], dat[i * 2 + 1]);
        }
    }

    X query(int a, int b){
        X vl = ex;
        X vr = ex;
        int l = a + n;
        int r = b + n;
        while(l < r){
            if(l & 1) vl = fx(vl, dat[l++]);
            if(r & 1) vr = fx(dat[--r], vr);
            l >>= 1;
            r >>= 1;
        }
        return fx(vl, vr);
    }
    
    X operator [](int i) const {
        return dat[i + n];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    string s;
    cin >> n >> s >> q;

    using T = array<long long, 53>;

    // A, B, ..., Z, A*, B*, ..., Z*, ans
    T ex;
    ex.fill(0);
    auto fx = [](T &a, T &b){
        T res;
        res.fill(0);
        for(int i = 0; i < 53; i++){
            res[i] = a[i] + b[i];
        }

        long long s = 0;
        for(int i = 0; i < 26; i++){
            s += b[i];
        }

        // M A
        for(int i = 0; i < 26; i++){
            res[26 + i] += a[i] * (s - b[i]);
        }

        // MM A
        for(int i = 0; i < 26; i++){
            long long cnt2 = a[i] * (a[i] - 1) / 2;
            res[52] += cnt2 * (s - b[i]);
        }

        // M MA
        for(int i = 0; i < 26; i++){
            res[52] += a[i] * b[26 + i];
        }
        return res;
    };
    SegTree<T> seg(n, fx, ex);
    for(int i = 0; i < n; i++){
        T cnt;
        cnt.fill(0);
        cnt[s[i] - 'A']++;
        seg.update(i, cnt);
    }
    
    while(q--){
        int t; cin >> t;
        if(t == 1){
            int x;
            char c;
            cin >> x >> c; x--;
            
            T cnt;
            cnt.fill(0);
            cnt[c - 'A']++;
            seg.update(x, cnt);
        }else{
            int l, r;
            cin >> l >> r; l--;
            cout << seg.query(l, r)[52] << endl;
        }
    }
}
0