結果

問題 No.3239 Omnibus
ユーザー 00 Sakuda
提出日時 2025-08-15 23:15:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 454 ms / 10,000 ms
コード長 5,196 bytes
コンパイル時間 3,308 ms
コンパイル使用メモリ 297,664 KB
実行使用メモリ 13,132 KB
最終ジャッジ日時 2025-08-15 23:16:59
合計ジャッジ時間 11,937 ms
ジャッジサーバーID
(参考情報)
judge6 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Treap {
    struct Node {
        int key; // index (0-based) where the 3-length substring starts
        uint32_t pri;
        Node *l, *r;
        int sz;
        long long sum; // sum of keys in subtree
        Node(int k, uint32_t p) : key(k), pri(p), l(nullptr), r(nullptr), sz(1), sum(k) {}
    };

    Node* root = nullptr;

    static inline uint64_t splitmix64(uint64_t& x) {
        x += 0x9e3779b97f4a7c15ULL;
        uint64_t z = x;
        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
        z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
        return z ^ (z >> 31);
    }
    static uint32_t rng() {
        static uint64_t seed = chrono::steady_clock::now().time_since_epoch().count();
        return (uint32_t)splitmix64(seed);
    }

    static inline int getsz(Node* t) { return t ? t->sz : 0; }
    static inline long long getsum(Node* t) { return t ? t->sum : 0LL; }

    static inline void pull(Node* t) {
        if (!t) return;
        t->sz = 1 + getsz(t->l) + getsz(t->r);
        t->sum = t->key + getsum(t->l) + getsum(t->r);
    }

    static void split(Node* t, int key, Node*& a, Node*& b) {
        // a: keys < key, b: keys >= key
        if (!t) { a = b = nullptr; return; }
        if (t->key < key) {
            split(t->r, key, t->r, b);
            a = t;
            pull(a);
        } else {
            split(t->l, key, a, t->l);
            b = t;
            pull(b);
        }
    }

    static Node* merge(Node* a, Node* b) {
        if (!a || !b) return a ? a : b;
        if (a->pri < b->pri) {
            a->r = merge(a->r, b);
            pull(a);
            return a;
        } else {
            b->l = merge(a, b->l);
            pull(b);
            return b;
        }
    }

    static Node* insert(Node* t, Node* n) {
        if (!t) return n;
        if (n->pri < t->pri) {
            split(t, n->key, n->l, n->r);
            pull(n);
            return n;
        } else if (n->key < t->key) {
            t->l = insert(t->l, n);
        } else if (n->key > t->key) {
            t->r = insert(t->r, n);
        } else {
            // duplicate key, discard new node
            delete n;
            return t;
        }
        pull(t);
        return t;
    }

    static Node* erase(Node* t, int key) {
        if (!t) return nullptr;
        if (key < t->key) {
            t->l = erase(t->l, key);
            pull(t);
            return t;
        } else if (key > t->key) {
            t->r = erase(t->r, key);
            pull(t);
            return t;
        } else {
            Node* res = merge(t->l, t->r);
            delete t;
            return res;
        }
    }

    // returns {sum, count} in [L, R]
    static pair<long long, int> rangeQuery(Node*& t, int L, int R) {
        if (!t || L > R) return {0LL, 0};
        Node *t1, *t2, *t3;
        split(t, L, t1, t2);
        split(t2, R + 1, t2, t3);
        long long s = getsum(t2);
        int c = getsz(t2);
        t = merge(t1, merge(t2, t3));
        return {s, c};
    }

    inline void insert(int key) { root = insert(root, new Node(key, rng())); }
    inline void erase(int key) { root = erase(root, key); }
    inline pair<long long, int> rangeQuery(int L, int R) { return rangeQuery(root, L, R); }
};

static inline int code3(char a, char b, char c) {
    return ((a - 'a') * 26 + (b - 'a')) * 26 + (c - 'a');
}

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

    int N, Q;
    cin >> N >> Q;
    string S;
    cin >> S;

    const int K = 26 * 26 * 26;
    vector<Treap> treaps(K);

    // 初期構築:全ての3文字開始位置を対応treapに挿入
    for (int i = 0; i + 2 < N; ++i) {
        int code = code3(S[i], S[i + 1], S[i + 2]);
        treaps[code].insert(i);
    }

    auto codeAt = [&](int i) -> int {
        if (i < 0 || i + 2 >= N) return -1;
        return code3(S[i], S[i + 1], S[i + 2]);
    };

    while (Q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int k;
            char x;
            cin >> k >> x;
            --k; // 0-indexed

            // 影響範囲の3箇所を削除
            for (int d = -2; d <= 0; ++d) {
                int idx = k + d;
                int code = codeAt(idx);
                if (code != -1) treaps[code].erase(idx);
            }
            // 更新
            S[k] = x;
            // 新たに挿入
            for (int d = -2; d <= 0; ++d) {
                int idx = k + d;
                int code = codeAt(idx);
                if (code != -1) treaps[code].insert(idx);
            }
        } else {
            int l, r;
            string a;
            cin >> l >> r >> a;
            int L = l - 1;
            int R = r - 1;

            if (R - L + 1 < 3) {
                cout << 0 << '\n';
                continue;
            }
            int left = L;
            int right = R - 2;

            int code = code3(a[0], a[1], a[2]);
            auto [sumPos, cnt] = treaps[code].rangeQuery(left, right);
            long long ans = sumPos - 1LL * cnt * left + cnt; // sum(i - L + 1)
            cout << ans << '\n';
        }
    }
    return 0;
}
0