Submission #813384

# Submission time Handle Problem Language Result Execution time Memory
813384 2023-08-07T16:38:56 Z Vladden Fibonacci representations (CEOI18_fib) C++14
65 / 100
4000 ms 119440 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;
set<int> chg;
void add(int v,int qnt){
	if (qnt>0){
		chg.insert(v);
	}
	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(int nn){
	chg.clear();
	chg.insert(nn);
	while(1){
		bool flag = false;
		while(!chg.empty()){
			int vb = *chg.rbegin();
			for(int v = vb+1;v>=vb-1;v-=1){
				if (!S.count(v)){
					continue;
				}
				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;
			}
			if (!flag){
				chg.erase(prev(chg.end()));
			}
		}
		if (!flag){
			break;
		}
	}
}
 
ll solve(int v){
	normalize(v);
	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);
	for(int i = 1;i<=n;i+=1){
		int v;
		cin>>v;
		add(v-1,1);
		cout<<solve(v)<<'\n';
	/*	cout<<"GDB ";
		for(int i = 1;i<=sz;i+=1){
			cout<<A[i]<<' ';
		}
		cout<<'\n';*/
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 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 2 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 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 2 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 0 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 468 KB Output is correct
19 Correct 2 ms 260 KB Output is correct
20 Correct 7 ms 340 KB Output is correct
21 Correct 5 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 418 ms 119440 KB Output is correct
3 Correct 316 ms 44152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 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 2 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 0 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 468 KB Output is correct
19 Correct 2 ms 260 KB Output is correct
20 Correct 7 ms 340 KB Output is correct
21 Correct 5 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 418 ms 119440 KB Output is correct
26 Correct 316 ms 44152 KB Output is correct
27 Correct 101 ms 40436 KB Output is correct
28 Correct 183 ms 63952 KB Output is correct
29 Correct 205 ms 528 KB Output is correct
30 Correct 149 ms 24620 KB Output is correct
31 Execution timed out 4048 ms 800 KB Time limit exceeded
32 Halted 0 ms 0 KB -