結果

問題 No.879 Range Mod 2 Query
ユーザー kyort0nkyort0n
提出日時 2019-09-06 23:01:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,850 bytes
コンパイル時間 1,949 ms
コンパイル使用メモリ 177,948 KB
実行使用メモリ 13,568 KB
最終ジャッジ日時 2024-06-24 22:18:51
合計ジャッジ時間 6,301 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
13,440 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const ll INF = 1e9;
//const ll mod = 1000000007;
struct LazySegmentTree {
private:
    int n;
    vector<ll> node, oddnum, evennum, Mod, Add;
 
public:
    LazySegmentTree() {
        int sz = 100500;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        oddnum.resize(2*n-1, 0);
        evennum.resize(2*n-1, 1);
        Add.resize(2*n-1, 0);
        Mod.resize(2*n-1, 0);
        for(int i=0; i<sz; i++) {
            node[i+n-1] = 0;
            evennum[i+n-1] = 1;
            oddnum[i+n-1] = 0;
        }
        for(int i=n-2; i>=0; i--) {
            node[i] = (node[i*2+1] + node[i*2+2]);
            oddnum[i] = (oddnum[i*2+1] + oddnum[i*2+2]);
            evennum[i] = (evennum[i*2+1] + evennum[i*2+2]);
        }
    }
 
    void lazyEvaluate(int k, int l, int r) {
        //cerr << "evaluating: " << k << " " << l << " " << r << endl;
        if(Mod[k] != 0) {
            if(Mod[k] == 1) {
                node[k] = oddnum[k];
            } else if(Mod[k] == 2) {
                node[k] = evennum[k];
                swap(oddnum[k], evennum[k]);
            } else exit(0);
            if(r - l > 1) {
                for(int j = k*2+1; j <= k*2+2; j++) {
                    if(Add[j] % 2 == 1) {
                        Mod[j] = 3 - Mod[k];
                    } else {
                        Mod[j] = Mod[k];
                    }
                    Add[j] = 0;
                }
            }
            Mod[k] = 0;
        }
        if(Add[k]) {
            node[k] += Add[k] * (r - l);
            if(Add[k] % 2 == 1) {
                swap(evennum[k], oddnum[k]);
            }
            if(r - l > 1) {
                Add[k*2+1] = Add[k*2+2] = Add[k];
            }
            Add[k] = 0;
        }
    }
 
    void setmod(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            Mod[k] = 1;
            lazyEvaluate(k, l, r);
        }
        else {
            setmod(a, b, 2*k+1, l, (l+r)/2);
            setmod(a, b, 2*k+2, (l+r)/2, r);
            node[k] = (node[2*k+1] + node[2*k+2]);
            oddnum[k] = oddnum[2*k+1] + oddnum[2*k+2];
            evennum[k] = evennum[2*k+1] + evennum[2*k+2];
        }
    }
 
    void add(int a, int b, int x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            Add[k] += x;
            lazyEvaluate(k, l, r);
        }
        else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = (node[2*k+1] + node[2*k+2]);
            oddnum[k] = oddnum[2*k+1] + oddnum[2*k+2];
            evennum[k] = evennum[2*k+1] + evennum[2*k+2];
        }
    }

    ll getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        lazyEvaluate(k, l, r);
        if(b <= l || r <= a) return 0;
        if(a <= l && r <= b) return node[k];
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return (vl + vr);
    }

    void print(int b) {
        for(int i = 0; i <= b + n - 1; i++) {
            cerr << i << " " << node[i] << " " << Add[i] << " " << oddnum[i] << " " << evennum[i] << " " << Mod[i] << endl;
        }
    }
};


int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, Q;
    cin >> N >> Q;
    LazySegmentTree seg;
    for(int i = 1; i <= N; i++) {
        int a;
        cin >> a;
        seg.add(i, i + 1, a);
        //cerr << "array: " << i << " " << a << endl;
    }
    for(int q = 1; q <= Q; q++) {
        /*
        cerr << "--------------" << endl;
        seg.print(N);
        cerr << "--------------" << endl;
        */
        int ope;
        cin >> ope;
        //cerr << q << " " << ope << endl;
        if(ope == 1) {
            int l, r;
            cin >> l >> r;
            //cerr << "one: " << l << " " << r << endl;
            seg.setmod(l, r + 1);
            continue;
        } else if(ope == 2) {
            int l, r, x;
            cin >> l >> r >> x;
            seg.add(l, r + 1, x);
            continue;
        } else if(ope == 3) {
            //cerr << "A" << endl;
            int l, r;
            cin >> l >> r;
            cout << seg.getsum(l, r + 1) << endl;
        }
    }
    //seg.print(N);
    return 0;
}
0