結果

問題 No.2762 Counting and Deleting
ユーザー pitPpitP
提出日時 2024-05-18 15:29:21
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 897 ms / 4,000 ms
コード長 6,665 bytes
コンパイル時間 6,098 ms
コンパイル使用メモリ 331,584 KB
実行使用メモリ 45,048 KB
最終ジャッジ日時 2024-05-18 15:29:38
合計ジャッジ時間 14,343 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 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,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 759 ms
44,972 KB
testcase_08 AC 760 ms
45,048 KB
testcase_09 AC 800 ms
45,028 KB
testcase_10 AC 785 ms
45,012 KB
testcase_11 AC 839 ms
44,864 KB
testcase_12 AC 857 ms
45,040 KB
testcase_13 AC 897 ms
45,040 KB
testcase_14 AC 833 ms
44,860 KB
testcase_15 AC 610 ms
44,908 KB
testcase_16 AC 636 ms
44,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
istream &operator>>(istream &is, modint &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint &a) { return os << a.val(); }
istream &operator>>(istream &is, modint998244353 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint998244353 &a) { return os << a.val(); }
istream &operator>>(istream &is, modint1000000007 &a) { long long v; is >> v; a = v; return is; }
ostream &operator<<(ostream &os, const modint1000000007 &a) { return os << a.val(); } 

typedef long long ll;
typedef vector<vector<int>> Graph;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define FOR(i,l,r) for (int i = l;i < (int)(r); i++)
#define rep(i,n) for (int i = 0;i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define my_sort(x) sort(x.begin(), x.end())
#define my_max(x) *max_element(all(x))
#define my_min(x) *min_element(all(x))
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
const int MOD = 998244353;
const int MOD2 = 1e9+7;
const double PI = acos(-1);
vector<int> di = {1,0,-1,0};
vector<int> dj = {0,1,0,-1};

#ifdef LOCAL
#  include <debug_print.hpp>
#  define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define debug(...) (static_cast<void>(0))
#endif

// https://youtu.be/ylWYSurx10A?t=2352
template<typename T>
struct Matrix{
    int h, w;
    vector<vector<T>> d;
    Matrix() {}
    Matrix(int h, int w, T val=0): h(h), w(w), d(h, vector<T>(w,val)) {}
    Matrix(int n, T val=0): h(n), w(n), d(n, vector<T>(n,val)) {}

    Matrix& unit(){
        assert(h == w);
        rep(i,h) d[i][i] = 1;
        return *this;
    }

    const vector<T>& operator[](int i) const { return d[i];}
    vector<T>& operator[](int i) { return d[i];}

    Matrix operator+(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        Matrix r(h, w);
        rep(i,h)rep(j,w){
            r[i][j] = d[i][j] + a[i][j];
        }
        return r;
    }

    Matrix &operator+=(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        rep(i,h)rep(j,w){
            (*this)[i][j] += a[i][j];
        }
        return (*this);
    }

    Matrix operator-(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        Matrix r(h, w);
        rep(i,h)rep(j,w){
            r[i][j] = d[i][j] - a[i][j];
        }
        return r;
    }

    Matrix &operator-=(const Matrix& a) const {
        assert(h == a.h && w == a.w);
        rep(i,h)rep(j,w){
            (*this)[i][j] -= a[i][j];
        }
        return (*this);
    }

    Matrix operator*(const Matrix& a) const {
        assert(w == a.h);
        Matrix r(h, a.w);
        rep(i,h)rep(k,w)rep(j,a.w){
            r[i][j] += d[i][k] * a[k][j];
        }
        return r;
    }

    Matrix &operator*=(const Matrix& a) const {
        assert(w == a.h);
        vector<vector<T>> nd(h, vector<T>(w));
        rep(i,h)rep(k,w)rep(j,a.w){
            nd[i][j] += (*this)[i][k] * a[k][j];
        }
        d = move(nd);
        return (*this);
    }

    vector<T> operator*(const vector<T> &a) const {
        // res[i] = sum{ M[i][j] * x[j] } (j = 0 ... h-1)
        assert(w == (int)a.size());
        vector<T> r(h);
        rep(i,h)rep(j,w){
            r[i] += (*this)[i][j] * a[j];
        }
        return r;
    }

    Matrix operator*(const T &a) const {
        Matrix r(h, w);
        rep(i,h)rep(j,w){
            r[i][j] = (*this)[i][j] * a;
        }
        return r;
    }

    Matrix &operator*=(const T &a) const {
        vector<vector<T>> nd(h, vector<T>(w));
        rep(i,h)rep(j,w){
            nd[i][j] = (*this)[i][j] * a;
        }
        d = move(nd);
        return (*this);
    }

    bool operator==(const Matrix &a){
        if(h != a.h || w != a.w) return false;
        rep(i,h)rep(j,w){
            if((*this)[i][j] != a[i][j]) return false;
        }
        return true;
    }

    friend ostream &operator<<(ostream &os, Matrix &a){
        rep(i,a.h){
            os << (i == 0 ? '[' : ' ');
            rep(j,a.w) os << (j == 0 ? '[' : ' ') << a[i][j] << (j == a.w - 1 ? "]" : ",");
            os << (i == a.h - 1 ? "]" : ",") << '\n';
        }
        return os;
    }


    // pii hw = mat.shape();
    pair<int, int> shape() {return {this->h, this->w};}

    // auto eye = Matrix<mint>::eye(3);
    static Matrix eye(int n){
        Matrix mat(n, n);
        mat.unit();
        return mat;
    }

    // auto AK = A.pow(K);
    Matrix pow(long long t) const {
        assert(h == w);
        if(!t) Matrix(h, h).unit();
        if(t == 1) return *this;
        Matrix r = pow(t >> 1);
        r = r * r;
        if(t & 1) r = r * (*this);
        return r;
    }

    T det(){
        assert(h == w);
        T res = 1;
        rep(k,h){
            for(int i = k; i < h; i++){
                if(d[i][k] == 0) continue;
                if(i != k){
                    swap(d[i], d[k]);
                    res = -res;
                }
            }
            if(d[k][k] == 0) return 0;
            res *= d[k][k];
            T inv = T(1) / d[k][k];
            rep(j,h) d[k][j] *= inv;
            for(int i = k + 1; i < h; i++){
                T c = d[i][k];
                for(int j = k; j < h; j++) d[i][j] -= d[k][j] * c;
            }
        }
        return res;
    }
};

const int D = 3;
using mint = modint998244353;
using S = Matrix<mint>;
S op(S a, S b){return a * b;}
S e(){ return Matrix<mint>::eye(D);}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int N, Q; cin >> N >> Q;
    string T; cin >> T;

    S eye = e();

    S zero = e();
    zero[0][1] = 1;

    S one = e();
    one[1][0] = 1;
    one[1][2] = 1;


    vector<S> dat(N);
    rep(i, N){
        if(T[i] == '0') dat[N - 1 - i] = zero;
        if(T[i] == '1') dat[N - 1 - i] = one;
    }
    segtree<S,op,e> seg(dat);

    set<int> rest;
    rep(i, N) rest.insert(i);

    while(Q--){
        int f, l, r; cin >> f >> l >> r;
        l--;
        if(f == 1){
            auto itr = rest.lower_bound(l);
            while(itr != rest.end() && *itr < r){
                seg.set(N - 1 - *itr, eye);
                itr = rest.erase(itr);
            }
        }
        else{
            S res = seg.prod((N - 1) - r + 1, N - 1 - l + 1);
            cout << res[0][2] + res[1][2] << endl;
        }
    }
}
0