結果

問題 No.3853 Unique Letters between Equal Letters
コンテスト
ユーザー n_bitand_n_per_3
提出日時 2026-12-02 14:44:27
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 748 ms / 3,000 ms
+ 14µs
コード長 8,260 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,850 ms
コンパイル使用メモリ 367,944 KB
実行使用メモリ 20,864 KB
最終ジャッジ日時 2026-12-04 21:16:29
合計ジャッジ時間 21,739 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;
using Successors = array<int, 3>;
#ifndef UNIQUE_BLOCK_SIZE
#define UNIQUE_BLOCK_SIZE 256
#endif
constexpr int BLOCK = UNIQUE_BLOCK_SIZE;

struct Block {
    int n, left, right;
    int inner_min;
    vector<pair<int, int>> ordered;
    vector<int> local, ends, starts;
    vector<ll> prefix, first_weight, tail_weight;
    vector<int> parent, head, path_position, path;

    Block(int n, int left, int right) : n(n), left(left), right(right) {}

    ll capped_sum(int lo, int hi, int cap, int cut) const {
        int mid = clamp(cut, lo, hi);
        return prefix[mid] - prefix[lo] + 1LL * (hi - mid) * cap;
    }

    void initialize(const vector<Successors>& next) {
        ordered.reserve(3 * (right - left));
        for (int i = left; i < right; ++i) {
            for (int e : next[i]) if (e != n) ordered.emplace_back(e, i - left);
        }
        sort(ordered.begin(), ordered.end());
        rebuild(next);
    }

    void replace(int i, const Successors& before, const Successors& after) {
        int a = i - left;
        for (int e : before) if (e != n && find(after.begin(), after.end(), e) == after.end()) {
            auto entry = make_pair(e, a);
            ordered.erase(lower_bound(ordered.begin(), ordered.end(), entry));
        }
        for (int e : after) if (e != n && find(before.begin(), before.end(), e) == before.end()) {
            auto entry = make_pair(e, a);
            ordered.insert(lower_bound(ordered.begin(), ordered.end(), entry), entry);
        }
    }

    void rebuild(const vector<Successors>& next) {
        int m = right - left;
        local.resize(m);
        prefix.resize(m + 1);
        inner_min = n;
        int suffix_best = n;
        for (int i = right - 1; i >= left; --i) {
            for (int e : next[i]) if (e != n && inner_min < e) {
                suffix_best = min(suffix_best, e);
            }
            local[i - left] = suffix_best;
            inner_min = min(inner_min, next[i][0]);
        }
        prefix[0] = 0;
        for (int i = 0; i < m; ++i) prefix[i + 1] = prefix[i] + local[i];

        ends.clear();
        starts.clear();
        for (auto [d, a] : ordered) if (d < local[a]) {
            if (!ends.empty() && ends.back() == d) {
                starts.back() = a;
            } else {
                ends.push_back(d);
                starts.push_back(a);
            }
        }

        int k = ends.size();
        vector<int> cuts(k);
        first_weight.resize(k);
        tail_weight.resize(k);
        parent.resize(k);
        head.resize(k);
        path_position.resize(k);
        path.clear();
        int cut = 0;
        for (int j = 0; j < k; ++j) {
            while (cut < m && local[cut] <= ends[j]) ++cut;
            cuts[j] = cut;
            first_weight[j] = capped_sum(0, starts[j] + 1, ends[j], cut);
        }

        vector<int> stack;
        stack.reserve(k);
        for (int j = k - 1; j >= 0; --j) {
            while (!stack.empty() && starts[stack.back()] <= starts[j]) stack.pop_back();
            int next_j = stack.empty() ? -1 : stack.back();
            parent[j] = next_j;
            tail_weight[j] = next_j < 0 ? 0 :
                capped_sum(starts[j] + 1, starts[next_j] + 1, ends[next_j], cuts[next_j])
                + tail_weight[next_j];
            stack.push_back(j);
        }

        vector<int> size(k, 1), heavy(k, -1);
        for (int j = 0; j < k; ++j) if (parent[j] >= 0) {
            int p = parent[j];
            size[p] += size[j];
            if (heavy[p] < 0 || size[heavy[p]] < size[j]) heavy[p] = j;
        }
        for (int j = k - 1; j >= 0; --j) {
            if (parent[j] >= 0 && heavy[parent[j]] == j) continue;
            for (int v = j; v >= 0; v = heavy[v]) {
                head[v] = j;
                path_position[v] = path.size();
                path.push_back(v);
            }
        }
    }

    int last_before_cap(int j, int cap) const {
        while (true) {
            int h = head[j];
            if (ends[h] >= cap) {
                int lo = path_position[h], hi = path_position[j];
                while (hi - lo > 1) {
                    int mid = (lo + hi) / 2;
                    if (ends[path[mid]] < cap) hi = mid;
                    else lo = mid;
                }
                return path[hi];
            }
            int up = parent[h];
            if (up < 0 || ends[up] >= cap) return h;
            j = up;
        }
    }

    ll consume(int& inner_right, int& bad_right) const {
        int m = right - left;
        int cap = bad_right;
        int j = upper_bound(ends.begin(), ends.end(), inner_right) - ends.begin();
        int cut = upper_bound(local.begin(), local.end(), cap) - local.begin();
        ll sum;
        if (j == (int)ends.size() || ends[j] >= cap) {
            sum = capped_sum(0, m, cap, cut);
        } else {
            int last = last_before_cap(j, cap);
            sum = first_weight[j] + tail_weight[j] - tail_weight[last]
                + capped_sum(starts[last] + 1, m, cap, cut);
            bad_right = ends[j];
        }
        bad_right = min(bad_right, local.front());
        inner_right = min(inner_right, inner_min);
        return sum;
    }
};

struct Counter {
    int n;
    vector<ll> values;
    vector<Successors> next;
    map<ll, set<int>> positions;
    vector<Block> blocks;

    explicit Counter(vector<ll> initial) : n(initial.size()), values(move(initial)), next(n) {
        for (int i = 0; i < n; ++i) positions[values[i]].insert(i);
        for (const auto& [value, group] : positions) {
            for (auto it = group.begin(); it != group.end(); ++it) refresh(group, it);
        }
        for (int l = 0; l < n; l += BLOCK) {
            blocks.emplace_back(n, l, min(n, l + BLOCK));
            blocks.back().initialize(next);
        }
    }

    void refresh(const set<int>& group, set<int>::const_iterator it) {
        auto& following = next[*it];
        following.fill(n);
        for (int k = 0; k < 3; ++k) {
            if (++it == group.end()) break;
            following[k] = *it;
        }
    }

    bool change(int i, ll value) {
        if (value == values[i]) return false;
        vector<int> changed = {i};
        auto add_predecessors = [&](const set<int>& s, set<int>::const_iterator it) {
            for (int t = 0; t < 3 && it != s.begin(); ++t) changed.push_back(*--it);
        };
        auto old = positions.find(values[i]);
        auto old_position = old->second.find(i);
        add_predecessors(old->second, old_position);
        old->second.erase(old_position);
        if (old->second.empty()) positions.erase(old);
        auto& inserted = positions[value];
        add_predecessors(inserted, inserted.insert(i).first);
        values[i] = value;

        sort(changed.begin(), changed.end());
        changed.erase(unique(changed.begin(), changed.end()), changed.end());
        vector<int> rebuild;
        for (int j : changed) {
            auto before = next[j];
            const auto& group = positions.find(values[j])->second;
            refresh(group, group.find(j));
            if (before == next[j]) continue;
            int block = j / BLOCK;
            blocks[block].replace(j, before, next[j]);
            if (rebuild.empty() || rebuild.back() != block) rebuild.push_back(block);
        }
        for (int block : rebuild) blocks[block].rebuild(next);
        return !rebuild.empty();
    }

    ll answer() const {
        int inner_right = n, bad_right = n;
        ll sum = 0;
        for (auto it = blocks.rbegin(); it != blocks.rend(); ++it) {
            sum += it->consume(inner_right, bad_right);
        }
        return sum - 1LL * n * (n - 1) / 2 - (2LL * n - 1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector<ll> a(n);
    for (ll& x : a){
        char tmp;
        cin >> tmp;
        x = (ll)(tmp-'0');
    }
    Counter counter(move(a));
    ll answer = counter.answer();
    while (q--) {
        int i;
        ll c;
        cin >> i >> c;
        if (counter.change(i - 1, c)) answer = counter.answer();
        cout << answer << '\n';
    }
}
0