結果

問題 No.876 Range Compress Query
ユーザー okok
提出日時 2019-09-07 01:26:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 4,077 bytes
コンパイル時間 1,284 ms
コンパイル使用メモリ 95,804 KB
実行使用メモリ 16,836 KB
最終ジャッジ日時 2023-09-07 05:58:41
合計ジャッジ時間 5,626 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 412 ms
16,596 KB
testcase_12 AC 343 ms
16,184 KB
testcase_13 AC 340 ms
16,520 KB
testcase_14 AC 405 ms
16,676 KB
testcase_15 AC 289 ms
16,616 KB
testcase_16 AC 393 ms
16,776 KB
testcase_17 AC 396 ms
16,836 KB
testcase_18 AC 420 ms
16,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include <functional>
#include <limits>
#include <cassert>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

template<typename T>
class lazy_segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat, lazy;
	
public:
	
	lazy_segment_tree(){
		
	}
	
	lazy_segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}
	
	~lazy_segment_tree(){
		
	}
	
	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;
		
		operation = fun;
		
		n = 1;
		
		while(n < n_) n *= 2;
		
		dat.resize(2 * n - 1);
		lazy.resize(2 * n - 1, fval);
	
		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}
	
	void eval(int k, int l, int r){
		
		if(lazy[k] != fval){
			dat[k] = operation(dat[k], lazy[k]);
			
			if(r - l > 1) {
				lazy[2*k+1] = operation(lazy[2*k+1], lazy[k]/2);
				lazy[2*k+2] = operation(lazy[2*k+2], lazy[k]/2);
			}
			
			lazy[k] = fval;
		}
	}
	
	void update(int a, int b, T x, int k = 0, int l = 0, int r = -1){
		if(r < 0) r = n;
		eval(k, l, r);
		if(b <= l || r <= a) {return ;}
		if(a <= l && r <= b){
			lazy[k] = operation(lazy[k], (r - l) * 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);
			dat[k] = operation(dat[2*k+1], dat[2*k+2]);
		}
	}
	
	T query(int a, int b, int k = 0, int l = 0, int r = -1){
		if(r < 0) r = n;
		eval(k, l, r);
		if(r <= a || b <= l){
			return fval;
		}
		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}
};

template<typename T>
class segment_tree{
	int n;
	T fval;
	function<T(T,T)> operation;
	vector<T> dat;
	
	T _query(int a, int b, int k, int l, int r){
		if(r <= a || b <= l){
			return fval;
		}
		
		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
			return operation(vl, vr);
		}
	}
	
public:
	
	segment_tree(){
		
	}
	
	segment_tree(int n_, T val, function<T(T,T)> fun){
		init(n_, val, fun);
	}
	
	~segment_tree(){
		
	}
	
	void init(int n_, T val, function<T(T,T)> fun){
		fval = val;
		
		operation = fun;
		
		n = 1;
		
		while(n < n_) n *= 2;
		
		dat.resize(2 * n - 1);
	
		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}

	void update(int k, T a){
		k += n - 1;
		dat[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			dat[k] = operation(dat[k*2 + 1],dat[k*2 + 2]);
		}
	}
	
	T query(int a, int b){
		return _query(a, b, 0, 0, n);
	}
	
};

signed main(){
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);
	
	lazy_segment_tree<long long> lseg;
	segment_tree<long long> seg;
	int N, Q;
	vector<int> A, de;
	
	cin>>N>>Q;
	
	lseg.init(N*2,0,[](int first, int second){ return first + second;});
	seg.init(N*2,0, [](int first, int second){ return first + second;});
	A.resize(N);
	de.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>A[i];
		lseg.update(i,i+1,A[i]);
		if(i){
			if(A[i-1] != A[i]){
				seg.update(i, 1);
			}
		}
		de[i] = A[i];
	}
	
	for(int i = 0; i < Q; i++){
		int c, l, r, x;
		
		cin>>c>>l>>r;
		
		l--, r--;
		if(c == 1){
			cin>>x;
			lseg.update(l, r+1, x);
			int l1, l2, r1, r2;
			if(l == 0){
				r1 = lseg.query(r, r+1);
				r2 = lseg.query(r+1, r+2);
				if(r1 == r2) {
					seg.update(r+1, 0);
				} else {
					seg.update(r+1, 1);
				}
			} else {
				l1 = lseg.query(l, l+1);
				l2 = lseg.query(l-1, l);
				r1 = lseg.query(r, r+1);
				r2 = lseg.query(r+1, r+2);
				
				if(l1 == l2) {
					seg.update(l, 0);
				} else {
					seg.update(l, 1);
				}
				
				if(r1 == r2) {
					seg.update(r+1, 0);
				} else {
					seg.update(r+1, 1);
				}
			}
			
			
		} else {
			cout<<seg.query(l+1,r+1) + 1<<endl;
		}
	}
	
	return 0;
}
0