結果

問題 No.879 Range Mod 2 Query
ユーザー vwxyzvwxyz
提出日時 2024-04-11 01:56:55
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 313 ms / 3,000 ms
コード長 1,836 bytes
コンパイル時間 4,654 ms
コンパイル使用メモリ 286,932 KB
実行使用メモリ 15,584 KB
最終ジャッジ日時 2024-04-11 01:57:05
合計ジャッジ時間 8,365 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 295 ms
14,960 KB
testcase_12 AC 165 ms
14,956 KB
testcase_13 AC 197 ms
14,888 KB
testcase_14 AC 194 ms
15,272 KB
testcase_15 AC 211 ms
10,036 KB
testcase_16 AC 236 ms
15,536 KB
testcase_17 AC 250 ms
15,584 KB
testcase_18 AC 254 ms
15,368 KB
testcase_19 AC 237 ms
15,504 KB
testcase_20 AC 235 ms
15,460 KB
testcase_21 AC 313 ms
15,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <atcoder/lazysegtree>
#include <cstdio>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;


struct S {
    long long s;
    long long ce;
    long long co;
};

struct F {
    long long p;
    long long d;
    long long x;
};

S op(S l, S r) {
	return S{l.s+r.s, l.ce+r.ce, l.co+r.co};
}

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

F composition(F l, F r) {
	if (l.d){
        return F({(r.p+r.x+l.p)%2,1ll,l.x});
	}
	else if (r.d){
        return F({r.p,1ll,r.x+l.x});
	}
    else{
        return F({0ll,0ll,r.x+l.x});
    }
}

S mapping(F l, S r) {
    long long s,ce,co;
    long long p,d,x;
    s=r.s;ce=r.ce;co=r.co;
    p=l.p;d=l.d;x=l.x;
    if (d){
        if(p){
            swap(ce,co);
        }
        s=co+x*(ce+co);
        if(x%2){
            swap(ce,co);
        }
    }
    else{
        s+=x*(ce+co);
        if(x%2){
            swap(ce,co);
        }
    }
    return S({s,ce,co});
}

F id() {
	return F({0ll, 0ll, 0ll});
}

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<long long> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    vector<S> a(N);
    for(int i=0;i<N;i++){
        if(A[i]%2){
            a[i]=S({A[i],0ll,1ll});
        }
        else{
            a[i]=S({A[i],1ll,0ll});
        }
    }
    lazy_segtree<S, op, e, F, mapping, composition, id> seg(a);
    for(int q=0;q<Q;q++){
    	int t,l,r;
    	long long x;
    	cin>>t;
        if(t==1){
            cin>>l>>r;
            l-=1;
            seg.apply(l,r,F({0ll,1ll,0ll}));
        }
        else if(t==2){
            cin>>l>>r>>x;
            l-=1;
            seg.apply(l,r,F({0ll,0ll,x}));
        }
    	else if(t==3){
            cin>>l>>r;
            l-=1;
	    	long long ans=seg.prod(l,r).s;
	    	cout<<ans<<endl;
        }
    }
    return 0;
}
0