結果

問題 No.2992 Range ABCD String Query
コンテスト
ユーザー reinsirk
提出日時 2026-11-30 17:43:45
言語 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
結果
WA  
実行時間 -
コード長 5,880 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,786 ms
コンパイル使用メモリ 413,428 KB
実行使用メモリ 38,760 KB
最終ジャッジ日時 2026-11-30 17:44:23
合計ジャッジ時間 36,127 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 6 WA * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// Library Codes are in: https://github.com/reinsirk/sirkit-cpp-library
// GCC バグ対策 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128217
#pragma GCC optimize("no-tree-loop-vectorize")
#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

#define SKIP_RUNTIME_CHECKS

constexpr int64_t INF = static_cast<int64_t>(1) << 60;

namespace sirkit
{

#ifndef SIRKIT_INF_DEFINED
#define SIRKIT_INF_DEFINED
inline constexpr std::int64_t kInf = std::int64_t{1} << 60;
#endif

#ifndef SIRKIT_UNWEIGHTED_GRAPH_DEFINED
#define SIRKIT_UNWEIGHTED_GRAPH_DEFINED
using Graph = std::vector<std::vector<int>>;
#endif

#ifndef SIRKIT_GRAPH_TYPES_DEFINED
#define SIRKIT_GRAPH_TYPES_DEFINED
struct Edge
{
    int to;
    std::int64_t w;
    Edge(int destination, std::int64_t weight) : to(destination), w(weight)
    {
    }
};
using WGraph = std::vector<std::vector<Edge>>;
#endif

#ifndef SIRKIT_CHMIN_DEFINED
#define SIRKIT_CHMIN_DEFINED
#define SIRKIT_TEMP_DEFINE_CHMIN
template <class T>
constexpr bool chmin(T &a, T b);
#endif

#ifndef SIRKIT_CHMAX_DEFINED
#define SIRKIT_CHMAX_DEFINED
#define SIRKIT_TEMP_DEFINE_CHMAX
template <class T>
constexpr bool chmax(T &a, T b);
#endif

#ifndef SIRKIT_POW64I_DEFINED
#define SIRKIT_POW64I_DEFINED
#define SIRKIT_TEMP_DEFINE_POW64I
constexpr std::uint64_t pow64i(std::uint64_t x, std::uint64_t n);
#endif

#ifndef SIRKIT_IS_MUL_OVERFLOW_DEFINED
#define SIRKIT_IS_MUL_OVERFLOW_DEFINED
#define SIRKIT_TEMP_DEFINE_IS_MUL_OVERFLOW
constexpr bool is_mul_overflow(std::int64_t a, std::int64_t b);
#endif
} // namespace sirkit

using namespace sirkit;

struct S
{
    int s;
    int id;
};
using F = int64_t;

S op(S a, S b)
{
    if (a.s <= b.s)
    {
        return b;
    }
    else
    {
        return a;
    }
}
S e()
{
    return {-10000000, 0};
}
S mapping(F f, S x)
{
    x.s += f;
    return x;
}
F composition(F f, F g)
{
    return f + g;
}
F id()
{
    return 0;
}

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

    int n, q;
    cin >> n >> q;

    vector<vector<S>> v(4, vector<S>(n));
    string s;
    cin >> s;
    vector<fenwick_tree<int>> fx(4, fenwick_tree<int>(n));
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int t = -1;
            if (s.at(j) == 'A' + i)
            {
                t = 1;
                fx.at(i).add(j, 1);
            }
            else if (s.at(j) < 'A' + i)
            {
                t = 0;
            }
            v.at(i).at(j) = {t, j};
        }
        for (int j = 1; j < n; j++)
        {
            v.at(i).at(j).s += v.at(i).at(j - 1).s;
        }
    }
    vector<lazy_segtree<S, op, e, F, mapping, composition, id>> sg(4);
    for (int i = 0; i < 4; i++)
    {
        sg.at(i) = lazy_segtree<S, op, e, F, mapping, composition, id>(v.at(i));
    }
    for (; q > 0; q--)
    {
        int t;
        cin >> t;
        if (t == 1)
        {
            int x;
            char c;
            cin >> x >> c;
            x--;
            sg.at(s.at(x) - 'A').apply(x, n, -2);
            fx.at(s.at(x) - 'A').add(x, -1);
            for (int j = s.at(x) - 'A' + 1; j < 4; j++)
            {
                sg.at(j).apply(x, n, -1);
            }
            sg.at(c - 'A').apply(x, n, +2);
            fx.at(c - 'A').add(x, 1);
            for (int j = c - 'A' + 1; j < 4; j++)
            {
                sg.at(j).apply(x, n, 1);
            }
            s.at(x) = c;
        }
        else
        {
            int l, r;
            cin >> l >> r;

            l--;
            int lx = l;
            int ans = 0;
            for (int i = 0; i < 3; i++)
            {
                auto x = sg.at(i).prod(l, r);
                if (l != 0)
                {
                    x.s -= sg.at(i).get(l - 1).s;
                }
                if (x.s >= 0)
                {
                    ans += fx.at(i).sum(l, x.id + 1);
                    l = x.id + 1;
                }
            }
            ans += fx.back().sum(l, r);
            cout << r - lx - ans << endl;
        }
    }

    return 0;
}

namespace sirkit
{

#ifdef SIRKIT_TEMP_DEFINE_CHMIN
#undef SIRKIT_TEMP_DEFINE_CHMIN
template <class T>
constexpr bool chmin(T &a, T b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}
#endif

#ifdef SIRKIT_TEMP_DEFINE_CHMAX
#undef SIRKIT_TEMP_DEFINE_CHMAX
template <class T>
constexpr bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
#endif

#ifdef SIRKIT_TEMP_DEFINE_POW64I
#undef SIRKIT_TEMP_DEFINE_POW64I
constexpr std::uint64_t pow64i(std::uint64_t x, std::uint64_t n)
{
    std::uint64_t result = 1;
    while (n != 0)
    {
        if ((n & 1) != 0)
        {
            result *= x;
        }
        n >>= 1;
        if (n != 0)
        {
            x *= x;
        }
    }
    return result;
}
#endif

#ifdef SIRKIT_TEMP_DEFINE_IS_MUL_OVERFLOW
#undef SIRKIT_TEMP_DEFINE_IS_MUL_OVERFLOW
constexpr bool is_mul_overflow(std::int64_t a, std::int64_t b)
{
#if (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_MUL_OVERFLOW)
    std::int64_t product;
    return __builtin_mul_overflow(a, b, &product);
#else
    // 最小負数の絶対値も符号なし演算で安全に求める。
    const std::uint64_t ua = a < 0 ? std::uint64_t{0} - static_cast<std::uint64_t>(a) : static_cast<std::uint64_t>(a);
    const std::uint64_t ub = b < 0 ? std::uint64_t{0} - static_cast<std::uint64_t>(b) : static_cast<std::uint64_t>(b);
    const std::uint64_t limit = (a < 0) != (b < 0)
                                    ? std::uint64_t{1} << 63
                                    : static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max());
    return ub != 0 && ua > limit / ub;
#endif
}
#endif

} // namespace sirkit
0