結果

問題 No.879 Range Mod 2 Query
ユーザー sigma425sigma425
提出日時 2019-09-10 17:07:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 318 ms / 3,000 ms
コード長 4,312 bytes
コンパイル時間 3,175 ms
コンパイル使用メモリ 169,592 KB
実行使用メモリ 21,100 KB
最終ジャッジ日時 2023-09-15 13:05:55
合計ジャッジ時間 9,683 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 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,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 308 ms
20,688 KB
testcase_12 AC 186 ms
20,832 KB
testcase_13 AC 233 ms
20,828 KB
testcase_14 AC 215 ms
21,056 KB
testcase_15 AC 215 ms
12,504 KB
testcase_16 AC 301 ms
21,000 KB
testcase_17 AC 315 ms
21,056 KB
testcase_18 AC 318 ms
20,964 KB
testcase_19 AC 290 ms
20,956 KB
testcase_20 AC 295 ms
21,032 KB
testcase_21 AC 307 ms
21,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
template<class Handler>
struct segtree_lazy{
	using val_t = typename Handler::val_t;
	using opr_t = typename Handler::opr_t;
	int N;
	vector<val_t> val;
	vector<opr_t> lazy;
	segtree_lazy(){}
	segtree_lazy(int n){init(n);}
	segtree_lazy(const vector<val_t>& vc){init(vc);}
	void init(int n){
		N=1;
		while(N<n) N*=2;
		val .assign(N*2,val_t::e());
		lazy.assign(N*2,opr_t::e());
	}
	void init(const vector<val_t>& vc){
		int n = vc.size();
		N=1;
		while(N<n) N*=2;
		val .assign(N*2,val_t::e());
		rep(i,n) val[i+N] = vc[i];
		for(int i=N-1;i>0;i--) val[i] = val[i*2] + val[i*2+1];
		lazy.assign(N*2,opr_t::e());
	}

	val_t query(int a,int b,int l=0,int r=-1,int k=1){	//query_calc
		if(r==-1) r=N;
		if(b<=l||r<=a) return val_t::e();
		if(a<=l&&r<=b) return val[k];
		propagate(l,r,k);
		return query(a,b,l,(l+r)/2,k*2) + query(a,b,(l+r)/2,r,k*2+1);
	}
	void addlazy(int k,const opr_t &f){
		Handler::setg2fg(f,lazy[k]);
		val[k] = Handler::act(f,val[k]);
	}

	void update(int a,int b,const opr_t &f,int l=0,int r=-1,int k=1){	//query_update
		if(r==-1) r=N;
		if(b<=l||r<=a) return;
		if(a<=l&&r<=b){
			addlazy(k,f);
			return;
		}
		propagate(l,r,k);
		update(a,b,f,l,(l+r)/2,k*2);
		update(a,b,f,(l+r)/2,r,k*2+1);
		val[k] = val[k*2] + val[k*2+1];
	}
	void propagate(int l,int r,int k){	//opr_child -> opr_parent * opr_child		parent after child
		addlazy(k*2  ,lazy[k]);
		addlazy(k*2+1,lazy[k]);
		lazy[k] = opr_t::e();
	}
};

struct handler{
	struct val_t{
		ll s;
		int od,ev;
		val_t(){*this = e();}
		val_t(ll s_,int od_,int ev_):s(s_),od(od_),ev(ev_){}

		const static val_t e(){
			return val_t(0,0,0);
		}
		val_t operator+(const val_t &r) const {
			return val_t(s+r.s,od+r.od,ev+r.ev);
		}
//		friend ostream& operator<<(ostream& o,const val_t& d){return o<<d.x;}
	};

	struct opr_t{
		int type;	//0 : no / 1 : e->e 2 : e->o
		ll ad;

		opr_t(){*this = e();}
		opr_t(int t,ll a):type(t),ad(a){}

		const static opr_t e(){
			return opr_t(0,0);
		}
//		friend ostream& operator<<(ostream& o,const opr_t& d){return o<<d.x;}
	};

//	static opr_t getfg(const opr_t &f, const opr_t &g){
//
//	}
	/*
		もしコピーコストとかが気になって,しかも楽に書けるならsetg2fgを直接書く
		そうじゃないなら g = getfg(f,g)
	*/
	static void setg2fg(const opr_t &f, opr_t &g){	//g -> fg		f after g
		if(f.type == 0){
			g.ad += f.ad;
		}else if(f.type == 1){
			if((g.type <= 1) ^ (g.ad % 2 == 0)) g = opr_t(2,f.ad);
			else g = opr_t(1,f.ad);
		}else{
			if((g.type <= 1) ^ (g.ad % 2 == 0)) g = opr_t(1,f.ad);
			else g = opr_t(2,f.ad);
		}
	}
	static val_t act(const opr_t &f, const val_t &v){
		ll n = v.od + v.ev;
		ll O = v.od, E = v.ev;
		if((f.type <= 1) ^ (f.ad % 2 == 0)) swap(O,E);
		ll S = v.s;
		if(f.type == 0){
			S += n * f.ad;
		}else if(f.type == 1){
			S = v.od;
			S += n * f.ad;
		}else{
			S = v.ev;
			S += n * f.ad;
		}
		return val_t(S,O,E);
	}
};


int main(){
	int N,Q;
	cin >> N >> Q;
	segtree_lazy<handler> seg(N);
	using opr_t = handler::opr_t;
	using val_t = handler::val_t;

	{
		V<val_t> v(N);
		rep(i,N){
			ll a;
			cin >> a;
			v[i] = val_t(a,a%2==1,a%2==0);
		}
		seg = segtree_lazy<handler>(v);
	}
	rep(_,Q){
		int t;
		cin >> t;
		if(t == 1){
			int l,r;
			cin >> l >> r;
			l--;
			seg.update(l,r,opr_t(1,0));
		}else if(t == 2){
			int l,r,x;
			cin >> l >> r >> x;
			l--;
			seg.update(l,r,opr_t(0,x));
		}else{
			int l,r;
			cin >> l >> r;
			l--;
			cout << seg.query(l,r).s << endl;
		}
	}
}
0