結果

問題 No.3853 Unique Letters between Equal Letters
コンテスト
ユーザー caz37OwO
提出日時 2026-11-03 18:00:09
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 108 ms / 3,000 ms
+ 418µs
コード長 2,358 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,281 ms
コンパイル使用メモリ 355,288 KB
実行使用メモリ 9,900 KB
最終ジャッジ日時 2026-12-04 20:53:54
合計ジャッジ時間 7,784 ms
ジャッジサーバーID
(参考情報)
judge6_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;

struct Solver {
    int stamp[10] = {};
    int firstPos[10], lastPos[10];
    int epoch = 0;

    // 位置 r の文字を右端に追加する。
    // 追加後も良い文字列なら true。
    bool add(const string &S, int r, int &dup) {
        int c = S[r] - '0';

        if (stamp[c] != epoch) {
            stamp[c] = epoch;
            firstPos[c] = r;
            lastPos[c] = -1;
        }
        else {
            // 同じ文字の最初の出現位置と、
            // それ以降に重複する文字があるかを判定。
            if (firstPos[c] < dup) return false;
        }

        dup = max(dup, lastPos[c]);
        lastPos[c] = r;

        return true;
    }

    // S 全体に含まれる良い部分文字列の個数。
    ll countAll(const string &S) {
        int N = S.size();
        ll res = 0;

        for (int l = 0; l < N; l++) {
            ++epoch;
            int dup = -1;

            for (int r = l; r < N && r < l + 30; r++) {
                if (!add(S, r, dup)) break;

                if (r - l + 1 >= 3) {
                    res++;
                }
            }
        }

        return res;
    }

    // 位置 p を含む良い部分文字列の個数。
    ll countContaining(const string &S, int p) {
        int N = S.size();
        ll res = 0;

        // 良い部分文字列の長さは最大30。
        for (int l = max(0, p - 29); l <= p; l++) {
            ++epoch;
            int dup = -1;

            for (int r = l; r < N && r < l + 30; r++) {
                if (!add(S, r, dup)) break;

                if (r >= p && r - l + 1 >= 3) {
                    res++;
                }
            }
        }

        return res;
    }
};

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

    int N, Q;
    cin >> N >> Q;

    string S;
    cin >> S;

    Solver solver;

    // 初期状態の答え
    ll ans = solver.countAll(S);

    while (Q--) {
        int i;
        char c;
        cin >> i >> c;
        --i;

        // 変更前の寄与を引く
        ans -= solver.countContaining(S, i);

        // 更新
        S[i] = c;

        // 変更後の寄与を足す
        ans += solver.countContaining(S, i);

        cout << ans << '\n';
    }

    return 0;
}
0