結果

問題 No.2762 Counting and Deleting
ユーザー rniyarniya
提出日時 2024-05-17 23:00:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,970 bytes
コンパイル時間 3,630 ms
コンパイル使用メモリ 262,404 KB
実行使用メモリ 14,008 KB
最終ジャッジ日時 2024-05-17 23:00:49
合計ジャッジ時間 8,673 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
    for (auto& e : v) {
        is >> e;
    }
    return is;
}

template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    for (std::string_view sep = ""; const auto& e : v) {
        os << std::exchange(sep, " ") << e;
    }
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) {
    return y < x and (x = std::forward<U>(y), true);
}

template <class T, class U = T> bool chmax(T& x, U&& y) {
    return x < y and (x = std::forward<U>(y), true);
}

template <class T> void mkuni(std::vector<T>& v) {
    std::ranges::sort(v);
    auto result = std::ranges::unique(v);
    v.erase(result.begin(), result.end());
}

template <class T> int lwb(const std::vector<T>& v, const T& x) {
    return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}

#include <atcoder/lazysegtree>
#include <atcoder/modint>

template <typename T, size_t N> struct SquareMatrix {
    std::array<std::array<T, N>, N> A;

    SquareMatrix() : A{{}} {}

    size_t size() const { return N; }

    inline const std::array<T, N>& operator[](int k) const { return A[k]; }

    inline std::array<T, N>& operator[](int k) { return A[k]; }

    static SquareMatrix I() {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++) res[i][i] = 1;
        return res;
    }

    SquareMatrix& operator+=(const SquareMatrix& B) {
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] += B[i][j];
            }
        }
        return *this;
    }

    SquareMatrix& operator-=(const SquareMatrix& B) {
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] -= B[i][j];
            }
        }
        return *this;
    }

    SquareMatrix& operator*=(const SquareMatrix& B) {
        std::array<std::array<T, N>, N> C = {};
        for (size_t i = 0; i < N; i++) {
            for (size_t k = 0; k < N; k++) {
                for (size_t j = 0; j < N; j++) {
                    C[i][j] += (*this)[i][k] * B[k][j];
                }
            }
        }
        A.swap(C);
        return *this;
    }

    SquareMatrix& operator*=(const T& v) {
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] *= v;
            }
        }
        return *this;
    }

    SquareMatrix& operator/=(const T& v) {
        T inv = T(1) / v;
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                (*this)[i][j] *= inv;
            }
        }
        return *this;
    }

    SquareMatrix& operator^=(long long k) {
        assert(0 <= k);
        SquareMatrix B = SquareMatrix::I();
        while (k > 0) {
            if (k & 1) B *= *this;
            *this *= *this;
            k >>= 1;
        }
        A.swap(B.A);
        return *this;
    }

    SquareMatrix operator-() const {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                res[i][j] = -(*this)[i][j];
            }
        }
        return res;
    }

    SquareMatrix operator+(const SquareMatrix& B) const { return SquareMatrix(*this) += B; }

    SquareMatrix operator-(const SquareMatrix& B) const { return SquareMatrix(*this) -= B; }

    SquareMatrix operator*(const SquareMatrix& B) const { return SquareMatrix(*this) *= B; }

    SquareMatrix operator*(const T& v) const { return SquareMatrix(*this) *= v; }

    SquareMatrix operator/(const T& v) const { return SquareMatrix(*this) /= v; }

    SquareMatrix operator^(const long long k) const { return SquareMatrix(*this) ^= k; }

    bool operator==(const SquareMatrix& B) const { return A == B.A; }

    bool operator!=(const SquareMatrix& B) const { return A != B.A; }

    SquareMatrix transpose() const {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++) {
            for (size_t j = 0; j < N; j++) {
                res[j][i] = (*this)[i][j];
            }
        }
        return res;
    }

    T determinant() const {
        SquareMatrix B(*this);
        T res = 1;
        for (size_t i = 0; i < N; i++) {
            int pivot = -1;
            for (size_t j = i; j < N; j++) {
                if (B[j][i] != 0) {
                    pivot = j;
                    break;
                }
            }
            if (pivot == -1) return 0;
            if (pivot != (int)i) {
                res *= -1;
                std::swap(B[i], B[pivot]);
            }
            res *= B[i][i];
            T inv = T(1) / B[i][i];
            for (size_t j = 0; j < N; j++) B[i][j] *= inv;
            for (size_t j = i + 1; j < N; j++) {
                T a = B[j][i];
                for (size_t k = 0; k < N; k++) {
                    B[j][k] -= B[i][k] * a;
                }
            }
        }
    }

    SquareMatrix inv() const {
        SquareMatrix B(*this), C = SquareMatrix::I();
        for (size_t i = 0; i < N; i++) {
            int pivot = -1;
            for (size_t j = i; j < N; j++) {
                if (B[j][i] != 0) {
                    pivot = j;
                    break;
                }
            }
            if (pivot == -1) return {};
            if (pivot != (int)i) {
                std::swap(B[i], B[pivot]);
                std::swap(C[i], C[pivot]);
            }
            T inv = T(1) / B[i][i];
            for (size_t j = 0; j < N; j++) {
                B[i][j] *= inv;
                C[i][j] *= inv;
            }
            for (size_t j = 0; j < N; j++) {
                if (j == i) continue;
                T a = B[j][i];
                for (size_t k = 0; k < N; k++) {
                    B[j][k] -= B[i][k] * a;
                    C[j][k] -= C[i][k] * a;
                }
            }
        }
        return C;
    }

    friend std::ostream& operator<<(std::ostream& os, const SquareMatrix& p) {
        os << "[(" << N << " * " << N << " Matrix)";
        os << "\n[columun sums: ";
        for (size_t j = 0; j < N; j++) {
            T sum = 0;
            for (size_t i = 0; i < N; i++) sum += p[i][j];
            ;
            os << sum << (j + 1 < N ? "," : "");
        }
        os << "]";
        for (size_t i = 0; i < N; i++) {
            os << "\n[";
            for (size_t j = 0; j < N; j++) os << p[i][j] << (j + 1 < N ? "," : "");
            os << "]";
        }
        os << "]\n";
        return os;
    }
};

using ll = long long;

using namespace std;

using mint = atcoder::modint998244353;
constexpr int SIZE = 10;
using SM = SquareMatrix<mint, SIZE + 1>;  // 末尾が i の整数の個数

SM op(SM l, SM r) { return r * l; }

SM e() { return SM::I(); }

SM mapping(pair<SM, bool> l, SM r) { return l.second ? l.first : r; }

pair<SM, bool> composition(pair<SM, bool> l, pair<SM, bool> r) { return l.second ? l : r; }

pair<SM, bool> id() { return {SM::I(), false}; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    int N, Q;
    cin >> N >> Q;
    string S;
    cin >> S;

    vector<SM> init;
    for (int i = 0; i < N; i++) {
        int x = S[i] - '0';
        auto m = SM::I();
        for (int j = 0; j < SIZE + (x != 0); j++) m[x][j] = 1;
        init.emplace_back(m);
    }
    atcoder::lazy_segtree<SM, op, e, pair<SM, bool>, mapping, composition, id> seg(init);

    for (; Q--;) {
        int t, l, r;
        cin >> t >> l >> r;
        l--;
        if (t == 1) {
            seg.apply(l, r, {SM::I(), true});
        } else {
            auto res = seg.prod(l, r);
            mint ans = 0;
            for (int j = 0; j < SIZE; j++) ans += res[j][SIZE];
            cout << ans.val() << '\n';
        }
    }
    return 0;
}
0