結果

問題 No.879 Range Mod 2 Query
ユーザー chocoruskchocorusk
提出日時 2019-09-07 00:09:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 425 ms / 3,000 ms
コード長 3,270 bytes
コンパイル時間 1,803 ms
コンパイル使用メモリ 124,308 KB
実行使用メモリ 16,424 KB
最終ジャッジ日時 2023-09-07 03:34:26
合計ジャッジ時間 7,186 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 416 ms
15,764 KB
testcase_12 AC 244 ms
15,768 KB
testcase_13 AC 316 ms
15,660 KB
testcase_14 AC 283 ms
16,040 KB
testcase_15 AC 281 ms
10,228 KB
testcase_16 AC 392 ms
16,424 KB
testcase_17 AC 406 ms
16,368 KB
testcase_18 AC 424 ms
16,028 KB
testcase_19 AC 395 ms
16,236 KB
testcase_20 AC 397 ms
16,356 KB
testcase_21 AC 425 ms
16,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;
typedef pair<P, ll> Pl;
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	using G=function<Monoid(Monoid, OperatorMonoid, int)>;
	using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
	int sz;
	vector<Monoid> data;
	vector<OperatorMonoid> lazy;
	const F f;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], r-l);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};
int main()
{
	int n, q; cin>>n>>q;
	auto f=[](P a, P b){ return P(a.first+b.first, a.second+b.second);};
	auto g=[](P a, Pl p, int len){
		int c=a.first; ll v=a.second;
		if(p.first.first==0){
			ll x=p.second;
			if(x&1) return P(len-c, v+x*len);
			else return P(c, v+x*len);
		}else{
			ll x=p.first.second, y=p.second;
			ll w=y*len;
			if(x&1) w+=len-c;
			else w+=c;
			if((x+y)&1) return P(len-c, w);
			else return P(c, w);
		}
	};
	auto h=[](Pl p, Pl q){
		if(q.first.first==1){
			return Pl(P(1, (p.first.second+p.second+q.first.second)%2), q.second);
		}else{
			return Pl(p.first, p.second+q.second);
		}
	};
	LazySegmentTree<P, Pl> seg(n, f, g, h, P(0, 0), Pl(P(0, 0), 0));
	vector<P> a(n);
	for(int i=0; i<n; i++){
		ll ai; cin>>ai;
		a[i]=P(ai%2, ai);
	}
	seg.build(a);
	for(int i=0; i<q; i++){
		int t, l, r; cin>>t>>l>>r;
		if(t==1){
			seg.update(l-1, r, Pl(P(1, 0), 0));
		}else if(t==2){
			int x; cin>>x;
			seg.update(l-1, r, Pl(P(0, 0), x));
		}else{
			cout<<seg.find(l-1, r).second<<endl;
		}
	}
	return 0;
}
0