답안 #813376

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
813376 2023-08-07T16:30:55 Z Vladden Fibonacci representations (CEOI18_fib) C++14
50 / 100
4000 ms 12488 KB
#include <iostream>
#include <algorithm>
#include <set>
#include <cstring>
using namespace std;
const int N = 1e5+7;
typedef long long ll;
const ll MOD = 1e9+7;
const int INF = 2e9;
const int MAXA = 1e9+1e6;
int A[N];
ll X(int n){
	if (n<0){
		return 0;
	}
	return (n+2)/2;
}
struct tnode{
	tnode *l,*r;
	int mi, mx, sum;
	ll dp[2][2];
	tnode(int _sum){
		sum = _sum;
		l = nullptr;
		r = nullptr;
		memset(dp,0,sizeof(dp));
		mi = INF, mx = -INF;
	}
	void merge(tnode &pl,tnode &pr){
		sum = pl.sum+pr.sum;
		mi = min(pl.mi,pr.mi);
		mx = max(pl.mx,pr.mx);
		if (pl.sum==0){
			for(int l = 0;l<2;l+=1){
				for(int r = 0;r<2;r+=1){
					dp[l][r] = pr.dp[l][r];
				}
			}
			return;
		}
		if (pr.sum==0){
			for(int l = 0;l<2;l+=1){
				for(int r = 0;r<2;r+=1){
					dp[l][r] = pl.dp[l][r];
				}
			}
			return;
		}
		for(int l = 0;l<2;l+=1){
			for(int r = 0;r<2;r+=1){
				dp[l][r] = (pl.dp[l][0]+pl.dp[l][1])*pr.dp[0][r] +
				 (pl.dp[l][0]*X(pr.mi-2-pl.mx-1)+pl.dp[l][1]*X(pr.mi-2-(pl.mx-1)-1))%MOD*pr.dp[1][r];
				dp[l][r] %= MOD;
			}
		}
	}
	void update(int tl,int tr,int pos,int val){
		if (tl>pos || tr<pos){
			return;
		}
		if (tl==tr){
			sum += val;
			memset(dp,0,sizeof(dp));
			if (sum==0){
				mi = INF, mx = -INF;
			}
			else{
				mi = mx = tl;
				dp[0][0] = 1;
				if (tl>=2){
					dp[1][1] = 1;
				}
			}
			return;
		}
		if (l==nullptr){
			l = new tnode(0);
		}
		if (r==nullptr){
			r = new tnode(0);
		}
		int tm = (tl+tr)/2;
		l->update(tl,tm,pos,val);
		r->update(tm+1,tr,pos,val);
		merge(*l,*r);
		if (l->sum==0){
			delete l;
			l = nullptr;
		}
		if (r->sum==0){
			delete r;
			r = nullptr;
		}
	}
	int get(int tl,int tr,int pos){
		if (tl>pos || tr<pos){
			return 0;
		}
		if (tl==tr){
			return sum;
		}
		int tm = (tl+tr)/2;
		int ret = 0;
		if (l!=nullptr) ret += l->get(tl,tm,pos);
		if (r!=nullptr) ret += r->get(tm+1,tr,pos);
		return ret;
	}
};
multiset<int> S;
tnode *root;
void add(int v,int qnt){
	for(int i = 0;i<-qnt;i+=1){
		S.erase(S.lower_bound(v));
	}
	for(int i = 0;i<qnt;i+=1){
		S.insert(v);
	}
	root->update(0,MAXA,v+1,qnt);
}
void normalize(){
	while(1){
		bool flag = false;
		for(auto it = S.rbegin();it!=S.rend();it = next(it)){
			int v = *it;
			if (S.count(v-1)){
				add(v,-1);
				add(v-1,-1);
				add(v+1,1);
				flag = true;
				break;
			}
			else if (v>=2 && S.count(v)>=2){
				add(v,-2);
				add(v+1,1);
				add(v-2,1);
				flag = true;
				break;
			}
			else if (S.count(v)==2 && v==1){
				add(1,-2);
				add(0,1);
				add(2,1);
				flag = true;
				break;
			}
			else if (S.count(v)==2 && v==0){
				add(0,-2);
				add(1,1);
				flag = true;
				break;
			}
 
		}
		if (!flag){
			break;
		}
	}
}
 
ll solve(int &n){
	normalize();
	ll ret = 0;
	for(int l = 0;l<2;l+=1){
		for(int r = 0;r<2;r+=1){
			ret += root->dp[l][r];
		}
	}
	return ret%MOD;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin>>n;
	root = new tnode(0);
	root->update(0,MAXA,0,1);
	int sz = 0;
	for(int i = 1;i<=n;i+=1){
		int v;
		cin>>v;
		add(v-1,1);
		cout<<solve(sz)<<'\n';
	/*	cout<<"GDB ";
		for(int i = 1;i<=sz;i+=1){
			cout<<A[i]<<' ';
		}
		cout<<'\n';*/
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 452 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 8 ms 340 KB Output is correct
21 Correct 6 ms 340 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Execution timed out 4051 ms 12488 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 452 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 8 ms 340 KB Output is correct
21 Correct 6 ms 340 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Execution timed out 4051 ms 12488 KB Time limit exceeded
26 Halted 0 ms 0 KB -