結果

問題 No.879 Range Mod 2 Query
ユーザー finefine
提出日時 2019-09-07 00:12:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 219 ms / 3,000 ms
コード長 5,071 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 172,660 KB
実行使用メモリ 12,920 KB
最終ジャッジ日時 2023-09-07 03:54:09
合計ジャッジ時間 5,195 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 214 ms
12,516 KB
testcase_12 AC 126 ms
12,636 KB
testcase_13 AC 162 ms
12,544 KB
testcase_14 AC 148 ms
12,816 KB
testcase_15 AC 144 ms
8,368 KB
testcase_16 AC 207 ms
12,908 KB
testcase_17 AC 212 ms
12,920 KB
testcase_18 AC 219 ms
12,784 KB
testcase_19 AC 182 ms
12,884 KB
testcase_20 AC 178 ms
12,780 KB
testcase_21 AC 189 ms
12,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

template <typename T>
struct LazySegmentTree {
    int n;
    vector<T> data;
    vector<T> lazy;
    T INITIAL_DATA_VALUE;
    T INITIAL_LAZY_VALUE;

    //使うときは、この3つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);
    void apply(int k, int seg_len);

    void init(int size, T initial_data_value, T initial_lazy_value) {
        n = 1;
        INITIAL_DATA_VALUE = initial_data_value;
        INITIAL_LAZY_VALUE = initial_lazy_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_DATA_VALUE);
        lazy.resize(2 * n - 1, INITIAL_LAZY_VALUE);
    }

    void init(const vector<T>& v, T initial_data_value, T initial_lazy_value) {
        int size = v.size();
        n = 1;
        INITIAL_DATA_VALUE = initial_data_value;
        INITIAL_LAZY_VALUE = initial_lazy_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_DATA_VALUE);
        lazy.resize(2 * n - 1, INITIAL_LAZY_VALUE);

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

    LazySegmentTree(int size, T initial_data_value, T initial_lazy_value) {
        init(size, initial_data_value, initial_lazy_value);
    }

    LazySegmentTree(int size, T initial_value) {
        init(size, initial_value, initial_value);
    }

    LazySegmentTree(const vector<T>& v, T initial_data_value, T initial_lazy_value) {
        init(v, initial_data_value, initial_lazy_value);
    }

    LazySegmentTree(const vector<T>& v, T initial_value) {
        init(v, initial_value, initial_value);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void eval(int k, int seg_len) {
        if (lazy[k] == INITIAL_LAZY_VALUE) return;
        apply(k, seg_len);
        if (seg_len > 1) {
            updateNode(2 * k + 1, lazy[k]);
            updateNode(2 * k + 2, lazy[k]);
        }
        lazy[k] = INITIAL_LAZY_VALUE;
    }

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

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

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

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

//使うときは以下3つを変更
template <typename T>
T LazySegmentTree<T>::merge(T x, T y) {
    return T(x.first + y.first, x.second + y.second);
}

template <typename T>
void LazySegmentTree<T>::updateNode(int k, T x) {
    if ((x.second & 1) > 0) {
        lazy[k].first = x.first;
        lazy[k].second ^= x.second;
        lazy[k].second |= 1;
    } else {
        lazy[k].first += x.first;
        lazy[k].second ^= x.second;
    }
}

template <typename T>
void LazySegmentTree<T>::apply(int k, int seg_len) {
    if (((lazy[k].second & 2) > 0) ^ (lazy[k].first % 2 == 1)) {
        data[k].second = seg_len - data[k].second;
    }

    if ((lazy[k].second & 1) > 0) {
        data[k].first = data[k].second;
    }
    if ((lazy[k].first % 2 == 1)) {
        data[k].second = seg_len - data[k].second;
    }
    data[k].first += lazy[k].first * seg_len;
}

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<P> st(v, P(0, 0));
    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