結果

問題 No.879 Range Mod 2 Query
ユーザー parukiparuki
提出日時 2019-09-08 23:39:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 417 ms / 3,000 ms
コード長 4,656 bytes
コンパイル時間 1,845 ms
コンパイル使用メモリ 182,280 KB
実行使用メモリ 20,724 KB
最終ジャッジ日時 2023-09-09 22:48:19
合計ジャッジ時間 7,376 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 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 397 ms
19,732 KB
testcase_12 AC 229 ms
19,716 KB
testcase_13 AC 295 ms
19,476 KB
testcase_14 AC 260 ms
20,492 KB
testcase_15 AC 272 ms
12,820 KB
testcase_16 AC 389 ms
20,452 KB
testcase_17 AC 399 ms
20,724 KB
testcase_18 AC 417 ms
20,468 KB
testcase_19 AC 353 ms
20,676 KB
testcase_20 AC 349 ms
20,648 KB
testcase_21 AC 373 ms
20,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class P, class Q>
struct LazySegTree {
    int dataSize;
    vector<P> value;
    vector<Q> lazy;
    function<P(P, P)> f;
    function<P(P, Q)> g;
    function<Q(Q, Q)> h;

    LazySegTree(vector<P> dat,
        function<P(P, P)> f_, function<P(P, Q)> g_, function<Q(Q, Q)> h_) {
        f = f_;
        g = g_;
        h = h_;
        dataSize = 1;
        int n = (int)dat.size();
        while (dataSize < n)dataSize *= 2;
        int treeSize = 2 * dataSize;
        value = vector<P>(treeSize, P());
        lazy = vector<Q>(treeSize, Q());
        for (int i = 0; i < n; ++i) {
            value[dataSize + i] = dat[i];
        }
        for (int i = dataSize - 1; i >= 0; --i) {
            value[i] = f(value[i * 2], value[i * 2 + 1]);
        }
    }

    void propagate(int index, int curL, int curR) {
        if (lazy[index] != Q()) {
            int left = index * 2, right = index * 2 + 1;
            value[index] = g(value[index], lazy[index]);
            if (curR - curL > 1) {
                lazy[left] = h(lazy[left], lazy[index]);
                lazy[right] = h(lazy[right], lazy[index]);
            }
            lazy[index] = Q();
        }
    }

    void update(int index, int curL, int curR, int givenL, int givenR, Q x) {
        propagate(index, curL, curR);

        if (curR <= givenL || givenR <= curL)return;

        if (givenL <= curL && curR <= givenR) {
            lazy[index] = h(lazy[index], x);
            propagate(index, curL, curR);
        } else {
            int mid = (curL + curR) / 2;
            update(index * 2, curL, mid, givenL, givenR, x);
            update(index * 2 + 1, mid, curR, givenL, givenR, x);
            value[index] = f(value[index * 2], value[index * 2 + 1]);
        }
    }

    void update(int l, int r, Q x) {
        update(1, 0, dataSize, l, r, x);
    }

    P query(int l, int r) {
        return query(1, 0, dataSize, l, r);
    }

    P query(int index, int curL, int curR, int givenL, int givenR) {
        if (curR <= givenL || givenR <= curL)return P();

        propagate(index, curL, curR);

        if (givenL <= curL && curR <= givenR) {
            return value[index];
        } else {
            int mid = (curL + curR) / 2;
            P resL = query(index * 2, curL, mid, givenL, givenR);
            P resR = query(index * 2 + 1, mid, curR, givenL, givenR);
            return f(resL, resR);
        }
    }
};

struct P {
    ll x, y, z; // (val, even, odd)
    P() : x(0), y(0), z(0){}
};

struct Q {
    bool x; // query 1?
    ll y, z; // sum1, sum2
    Q() :x(false), y(0), z(0) {}
    Q(bool x_, ll y_, ll z_) :x(x_), y(y_), z(z_) {}
    bool operator!=(Q q) {
        return x != q.x || y != q.y || z != q.z;
    }
};

P f(P a, P b) {
    a.x += b.x;
    a.y += b.y;
    a.z += b.z;
    return a;
}

P g(P p, Q q) {
    if (q.x) {
        if (q.y & 1) {
            swap(p.y, p.z);
        }
        p.x = p.z;
    }
    p.x += (p.y + p.z) * q.z;
    if (q.z & 1) {
        swap(p.y, p.z);
    }
    return p;
}

Q h(Q qa, Q qb) {
    Q res;
    if (qa.x&&qb.x) {
        res = qb;
        res.y += qa.y + qa.z;
    } else if (qa.x) {
        res = qa;
        res.z += qb.z;
    } else if (qb.x) {
        res = qb;
        res.y += qa.z;
    } else {
        res = qa;
        res.z += qb.z;
    }
    return res;
}

void solve() {
    int N, numQuery;
    cin >> N >> numQuery;
    vll A(N);

    vector<P> ps(N);
    rep(i, N) {
        cin >> ps[i].x;
        if (ps[i].x & 1)ps[i].z = 1;
        else ps[i].y = 1;
    }

    LazySegTree<P, Q> lst(ps, f, g, h);

    rep(i, numQuery) {
        int q, l, r;
        cin >> q >> l >> r;
        --l;
        if (q == 1) {
            lst.update(l, r, Q(true, 0ll, 0ll));
        } else if (q == 2) {
            int x;
            cin >> x;
            lst.update(l, r, Q(false, 0ll, x));
        } else {
            ll ans = lst.query(l, r).x;
            cout << ans << endl;
        }
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0