結果

問題 No.2611 Count 01
ユーザー rniyarniya
提出日時 2024-01-19 22:46:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 6,000 ms
コード長 3,287 bytes
コンパイル時間 2,602 ms
コンパイル使用メモリ 213,312 KB
実行使用メモリ 32,948 KB
最終ジャッジ日時 2024-01-19 22:46:35
合計ジャッジ時間 8,956 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 232 ms
32,948 KB
testcase_04 AC 235 ms
32,948 KB
testcase_05 AC 230 ms
32,948 KB
testcase_06 AC 225 ms
32,948 KB
testcase_07 AC 223 ms
32,948 KB
testcase_08 AC 262 ms
32,948 KB
testcase_09 AC 239 ms
32,948 KB
testcase_10 AC 236 ms
32,948 KB
testcase_11 AC 233 ms
32,948 KB
testcase_12 AC 236 ms
32,948 KB
testcase_13 AC 242 ms
32,948 KB
testcase_14 AC 223 ms
32,948 KB
testcase_15 AC 258 ms
32,948 KB
testcase_16 AC 241 ms
32,948 KB
testcase_17 AC 234 ms
32,948 KB
testcase_18 AC 231 ms
32,948 KB
testcase_19 AC 228 ms
32,948 KB
testcase_20 AC 232 ms
32,948 KB
testcase_21 AC 231 ms
32,948 KB
testcase_22 AC 253 ms
32,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include "atcoder/modint"
#include "atcoder/segtree"

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

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

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

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

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

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

using mint = atcoder::modint998244353;

struct st {
    mint sum, len;
    array<mint, 2> tot, rui;
    array<array<mint, 2>, 2> cnt;
    st(mint sum, mint len, array<mint, 2> tot, array<mint, 2> rui, array<array<mint, 2>, 2> cnt)
        : sum(sum), len(len), tot(tot), rui(rui), cnt(cnt) {}
};

st op(st l, st r) {
    auto L = l.cnt[1];
    auto R = r.cnt[0];
    array<mint, 2> tot, rui;
    array<array<mint, 2>, 2> cnt;
    mint sum = l.sum + r.sum + l.rui[1] * r.len + r.rui[0] * l.len + L[0] * R[1] + L[1] * R[0], len = l.len + r.len;
    for (int i = 0; i < 2; i++) {
        tot[i] = l.tot[i] + r.tot[i];
        cnt[0][i] = l.cnt[0][i] + l.tot[i] * r.len + r.cnt[0][i];
        cnt[1][i] = r.cnt[1][i] + r.tot[i] * l.len + l.cnt[1][i];
    }
    rui[0] = l.rui[0] + r.rui[0] + l.tot[0] * r.cnt[0][1] + l.tot[1] * r.cnt[0][0] + l.tot[0] * l.tot[1] * r.len;
    rui[1] = l.rui[1] + r.rui[1] + r.tot[0] * l.cnt[1][1] + r.tot[1] * l.cnt[1][0] + r.tot[0] * r.tot[1] * l.len;
    return st(sum, len, tot, rui, cnt);
}

st e() {
    return st(0, 0, array<mint, 2>({0, 0}), array<mint, 2>({0, 0}),
              array<array<mint, 2>, 2>({array<mint, 2>({0, 0}), array<mint, 2>({0, 0})}));
}

st get(int x) {
    mint sum = 0, len = 1;
    array<mint, 2> tot, rui;
    array<array<mint, 2>, 2> cnt;
    for (int i = 0; i < 2; i++) {
        tot[i] = cnt[0][i] = cnt[1][i] = (i == x);
        rui[i] = 0;
    }
    return st(sum, len, tot, rui, cnt);
}

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

    vector<int> v;
    for (int i = 0; i < N; i++) v.emplace_back(S[i] - '0');
    vector<st> init;
    for (int i = 0; i < N; i++) init.emplace_back(get(v[i]));
    atcoder::segtree<st, op, e> seg(init);
    for (; Q--;) {
        int t;
        cin >> t;
        if (t == 1) {
            int i;
            cin >> i;
            i--;
            v[i] ^= 1;
            seg.set(i, get(v[i]));
        } else {
            int l, r;
            cin >> l >> r;
            auto res = seg.prod(--l, r);
            mint ans = res.sum;
            cout << ans.val() << '\n';
        }
    }
    return 0;
}
0