結果

問題 No.879 Range Mod 2 Query
ユーザー finefine
提出日時 2019-11-09 17:34:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 3,000 ms
コード長 6,413 bytes
コンパイル時間 1,735 ms
コンパイル使用メモリ 172,992 KB
実行使用メモリ 13,040 KB
最終ジャッジ日時 2023-10-13 07:23:07
合計ジャッジ時間 5,320 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,356 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 190 ms
12,600 KB
testcase_12 AC 116 ms
12,600 KB
testcase_13 AC 147 ms
12,600 KB
testcase_14 AC 131 ms
12,864 KB
testcase_15 AC 133 ms
8,260 KB
testcase_16 AC 188 ms
12,868 KB
testcase_17 AC 197 ms
12,852 KB
testcase_18 AC 205 ms
12,976 KB
testcase_19 AC 168 ms
12,860 KB
testcase_20 AC 162 ms
12,992 KB
testcase_21 AC 174 ms
13,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, int>;

template <class MonoidOp>
struct LazySegmentTree {
    using Tm = typename MonoidOp::Tm;
    using To = typename MonoidOp::To;

    int n;
    vector<Tm> data;
    vector<To> lazy;

    LazySegmentTree(int size, Tm initial_data_value = MonoidOp::unit()) {
        n = 1;
        while (n < size) n <<= 1;

        data.assign(2 * n - 1, initial_data_value);
        lazy.assign(2 * n - 1, MonoidOp::op_unit());

        if (initial_data_value != MonoidOp::unit()) {
            for (int i = n - 2; i >= 0; i--) data[i] = MonoidOp::merge(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    LazySegmentTree(const vector<Tm>& v) {
        int size = v.size();
        n = 1;
        while (n < size) n <<= 1;
        data.assign(2 * n - 1, MonoidOp::unit());
        lazy.assign(2 * n - 1, MonoidOp::op_unit());

        for (int i = 0; i < size; i++) data[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) data[i] = MonoidOp::merge(data[i * 2 + 1], data[i * 2 + 2]);
    }

    Tm getLeaf(int k) {
        return query(k, k + 1);
    }

    void push(int k, int l, int r) {
        if (lazy[k] == MonoidOp::op_unit()) return;
        MonoidOp::apply(data[k], lazy[k], r - l);
        if (r - l > 1) {
            MonoidOp::update(lazy[2 * k + 1], lazy[k]);
            MonoidOp::update(lazy[2 * k + 2], lazy[k]);
        }
        lazy[k] = MonoidOp::op_unit();
    }

    //区間[a, b)に対する更新
    //k:節点番号, [l, r):節点に対応する区間
    void update(int a, int b, To x, int k, int l, int r) {
        push(k, l, r);
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) {
            MonoidOp::update(lazy[k], x);
            push(k, l, r);
        } else {
            update(a, b, x, k * 2 + 1, l, (l + r) / 2);
            update(a, b, x, k * 2 + 2, (l + r) / 2, r);
            data[k] = MonoidOp::merge(data[2 * k + 1], data[2 * k + 2]);
        }
    }

    void update(int a, int b, To x) {
        update(a, b, x, 0, 0, n);
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    Tm query(int a, int b, int k, int l, int r) {
        push(k, l, r);
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return MonoidOp::unit();
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            Tm vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            Tm vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return MonoidOp::merge(vl, vr);
        }
    }

    //外から呼ぶ用
    Tm query(int a, int b) {
        return query(a, b, 0, 0, n);
    }
};

// 以下、MonoidOpの例
template<class U = ll, class V = U>
struct RangeSumAdd {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return x + y; }
    static void update(To& target, To x) { target += x; }
    static void apply(Tm& target, To x, int seg_len) { target += x * seg_len; }
    static constexpr Tm unit() { return Tm(0); }
    static constexpr To op_unit() { return To(0); }
};

template<class U = ll, class V = U>
struct RangeMinUpdate {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return min(x, y); }
    static void update(To& target, To x) { target = x; }
    static void apply(Tm& target, To x, int seg_len) { target = x; }
    static constexpr Tm unit() { return numeric_limits<Tm>::max(); }
    static constexpr To op_unit() { return numeric_limits<To>::max(); }
};

// 作用素の初期値は更新クエリで与えられる値の定義域の範囲外の値にする
template<class U = ll, class V = U>
struct RangeSumUpdate {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return x + y; }
    static void update(To& target, To x) { target = x; }
    static void apply(Tm& target, To x, int seg_len) { target = x * seg_len; }
    static constexpr Tm unit() { return Tm(0); }
    static constexpr To op_unit() { return numeric_limits<To>::min(); }
};

// 初期値 != 単位元 の場合に注意
template<class U = ll, class V = U>
struct RangeMinAdd {
    using Tm = U;
    using To = V;
    static Tm merge(Tm x, Tm y) { return min(x, y); }
    static void update(To& target, To x) { target += x; }
    static void apply(Tm& target, To x, int seg_len) { target += x; }
    static constexpr Tm unit() { return numeric_limits<Tm>::max(); }
    static constexpr To op_unit() { return To(0); }
};

template<class U = P, class V = U>
struct RangeMod2 {
    using Tm = U;
    using To = V;
    static Tm merge(const Tm& x, const Tm& y) { return Tm(x.first + y.first, x.second + y.second); }
    static void update(To& target, const To& x) { 
        if ((x.second & 1) > 0) {
            target.first = x.first;
            target.second ^= x.second;
            target.second |= 1;
        } else {
            target.first += x.first;
            target.second ^= x.second;
        }
    }
    static void apply(Tm& target, const To& x, int seg_len) { 
        if (((x.second & 2) > 0) ^ (x.first % 2 == 1)) {
            target.second = seg_len - target.second;
        }

        if ((x.second & 1) > 0) {
            target.first = target.second;
        }
        if ((x.first % 2 == 1)) {
            target.second = seg_len - target.second;
        }
        target.first += x.first * seg_len;
    }
    static constexpr Tm unit() { return Tm(0, 0); }
    static constexpr To op_unit() { return To(0, 0); }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<P> v;
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        v.emplace_back(a, a % 2);
    }

    LazySegmentTree< RangeMod2<> > st(v);
    for (int i = 0; i < q; i++) {
        int c, l, r;
        cin >> c >> l >> r;
        l--;
        if (c == 1) {
            st.update(l, r, P(0, 1));
        } else if (c == 2) {
            ll x;
            cin >> x;
            st.update(l, r, P(x, x % 2 * 2));
        } else {
            cout << st.query(l, r).first << "\n";
            //cerr << st.query(l, r).second << "\n";
        }
    }
    return 0;
}
0