結果

問題 No.1219 Mancala Combo
ユーザー 沙耶花沙耶花
提出日時 2020-09-04 21:35:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,813 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 211,268 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2024-05-04 21:45:55
合計ジャッジ時間 5,776 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000000	

template <typename T0,typename T1,typename F0,typename F1,typename F2>
struct lazysegtree{
	//元データx[i]はv[n+i]
	//v[i]の親はv[i/2],子はv[i*2]とv[i*2+1]
	F0 func0;
	F1 func1;
	F2 func2;
	vector<T0> v0;
	vector<T1> v1;
	int n;
	int cnt;
	
	T0 init_value0;
	T1 init_value1;
	
	lazysegtree(int sz,F0 f0,F1 f1,F2 f2,T0 iv0,T1 iv1):func0(f0),func1(f1),func2(f2){
		init_value0 = iv0;
		init_value1 = iv1;
		n=1;
		cnt=0;
		while(true){
			if(n>=sz)break;
			n*=2;
			cnt++;
		}
		v0.resize(2*n,init_value0);
		v1.resize(2*n,init_value1);
		
	}
	
	lazysegtree(vector<T0> &x,F0 f0,F1 f1,F2 f2,T0 iv0,T1 iv1):func0(f0),func1(f1),func2(f2){
		init_value0 = iv0;
		init_value1 = iv1;
		n=1;
		cnt=0;
		while(true){
			if(n>=x.size())break;
			n*=2;
			cnt++;
		}
		v0.resize(2*n,init_value0);
		v1.resize(2*n,init_value1);
		for(int i=0;i<x.size();i++){
			v0[n+i]=x[i];
		}
		for(int i=n-1;i>=0;i--){
			v0[i]=func0(v0[i<<1],v0[(i<<1)+1]);
		}	
	}

	//2人の子供に伝える
	void propagate(int ind){
		update(ind<<1,v1[ind]);
		update((ind<<1)+1,v1[ind]);
		v1[ind] = init_value1;
	}
	
	//あるノードに対し先祖から伝播	
	void reflect(int l,int r){
		int j = cnt;
		while(l>>j==r>>j&&j>=1){
			propagate(l>>j);
			j--;
		}
		for(;j>=1;j--){
			propagate(l>>j);
			propagate(r>>j);
		}
	}
	
	//子供の値を親に伝える
	void mergeChildren(int ind){
		v0[ind] = func1(func0(v0[ind<<1],v0[(ind<<1)+1]),v1[ind],n>>(31-__builtin_clz(ind)));
	}
	
	//ある要素について作用させる
	void update(int ind,T1 x){
		v0[ind] = func1(v0[ind],x,n>>(31-__builtin_clz(ind)));
		v1[ind] = func2(v1[ind],x);
	}
	
	//[l,r)に対して作用させる
	void update(int l,int r,T1 x){
		if(l>=r)return;
		int L = l,R = r;
		l+=n;
		r+=n;
		reflect(l,r-1);
		while(true){
			if(l&1){
				update(l,x);
				l++;
			}
			if(r&1){
				update(r-1,x);
				r--;
			}
			if(l>=r)break;
			l>>=1;
			r>>=1;
		}
		
		l = L + n;
		r = R + n-1;
		
		while(true){
			l>>=1;
			r>>=1;
			if(l<=0)break;

			if(l==r){
				while(true){
					mergeChildren(l);
					l>>=1;
					if(!l)return;
				}
			}
			else{
				mergeChildren(l);
				mergeChildren(r);
			}
		}
		
	}
	
	//区間[l,r)におけるクエリ処理
	T0 query(int l,int r){
		T0 res1 = init_value0;
		T0 res2 = init_value0;
		if(l>=r)return res1;
		l+=n;
		r+=n;
		reflect(l,r-1);
		while(true){
			if(l&1){
				res1=func0(res1,v0[l]);
				l++;
			}
			if(r&1){
				res2=func0(v0[r-1],res2);
				r--;
			}
			if(l>=r)break;
			l>>=1;
			r>>=1;
		}
		return func0(res1,res2);
	}

	void show(){
		int n = 1;
		for(int i=1;i<v0.size();i++){
			for(int j=0;j<n;j++){
				if(j!=0)cout<<' ';
				cout<<v0[i+j];
			}
			cout<<endl;
			i+=n-1;
			n*=2;
		}
	}
	
};

int main(){

	int N;
	cin>>N;
	
	vector<pair<long long,int>> A(N+1,make_pair(0LL,0));
	for(int i=1;i<=N;i++){
		cin>>A[i].first;
		A[i].first = i-A[i].first;
		A[i].second = i;
	}
	
	auto f0 = [](pair<long long,int> a,pair<long long,int> b){
		return min(a,b);
	};
	
	auto f1 = [](pair<long long,int> a,int b,long long sz){
		a.first += b;
		return a;
	};
	
	auto f2 = [](int a,int b){
		return a+b;
	};
	pair<long long,int> INF(Inf,0);
	lazysegtree<pair<long long,int>,int,decltype(f0),decltype(f1),decltype(f2)> seg(A,f0,f1,f2,INF,0);
	
	while(true){
		pair<long long,int> P = seg.query(1,N+1);
		if(P.first<0){
			cout<<"No"<<endl;
			return 0;
		}
		if(P.first>0)break;
		seg.update(0,P.second,-1);
		seg.update(P.second,P.second+1,P.second);
	}
	
	for(int i=1;i<=N;i++){
		if(seg.query(i,i+1).first!=i){
			cout<<"No"<<endl;
			return 0;
		}
	}
	
	cout<<"Yes"<<endl;
	
	return 0;
}
0