結果

問題 No.879 Range Mod 2 Query
ユーザー milanis48663220milanis48663220
提出日時 2023-08-08 00:47:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 3,000 ms
コード長 2,767 bytes
コンパイル時間 1,574 ms
コンパイル使用メモリ 126,908 KB
実行使用メモリ 14,908 KB
最終ジャッジ日時 2024-04-25 18:51:37
合計ジャッジ時間 4,053 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,948 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 122 ms
14,388 KB
testcase_12 AC 75 ms
14,372 KB
testcase_13 AC 96 ms
14,572 KB
testcase_14 AC 87 ms
14,868 KB
testcase_15 AC 88 ms
9,448 KB
testcase_16 AC 121 ms
14,900 KB
testcase_17 AC 127 ms
14,896 KB
testcase_18 AC 128 ms
14,852 KB
testcase_19 AC 103 ms
14,908 KB
testcase_20 AC 106 ms
14,892 KB
testcase_21 AC 116 ms
14,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/lazysegtree>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

struct S{
    ll len, sum, odd;
    S(ll len, ll sum, ll odd): len(len), sum(sum), odd(odd) {}
};

struct T{
    int p;
    ll add1;
    bool mod2;
    T(int p, bool mod2, ll add1): p(p), mod2(mod2), add1(add1) {}
};

S op(S x, S y){
    return S(x.len+y.len, x.sum+y.sum, x.odd+y.odd);
}

S e(){
    return S(0, 0, 0);
}

T id(){
    return T(0, false, 0);
}

S mapping(T f, S x){
    if(f.mod2){
        x.sum += f.p*x.len;
        if(f.p == 1){
            x.odd = x.len-x.odd;
        }
        x.sum = x.odd;
    }
    x.sum += f.add1*x.len;
    if(f.add1%2 == 1){
        x.odd = x.len-x.odd;
    }
    return x;
}

T composition(T f, T g){
    if(f.mod2){
        g.add1 += f.p;
        g.p ^= g.add1%2;
        g.mod2 = true;
        g.add1 = 0;
    }
    g.add1 += f.add1;
    return g;
}

using Seg = atcoder::lazy_segtree<S, op, e, T, mapping, composition, id>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, q; cin >> n >> q;
    vector<S> s;
    for(int i = 0; i < n; i++){
        int a; cin >> a;
        s.push_back(S(1, a, a%2));
    }
    Seg seg(s);
    while(q--){
        int t, l, r; cin >> t >> l >> r; l--;
        if(t == 1){
            seg.apply(l, r, T(0, true, 0));
        }else if(t == 2){
            int x; cin >> x;
            seg.apply(l, r, T(0, false, x));
        }else{
            auto s = seg.prod(l, r);
            cout << s.sum << '\n';
        }
        // for(int i = 0; i < n; i++) cout << seg.get(i).sum << ' ';
        // cout << endl;
    }
}
0