Submission #287609

# Submission time Handle Problem Language Result Execution time Memory
287609 2020-08-31T21:09:30 Z TadijaSebez Fibonacci representations (CEOI18_fib) C++11
0 / 100
141 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1e9+7;
int add(int x,int y){x+=y;return x>=mod?x-mod:x;}
int mul(int x,int y){return (ll)x*y%mod;}
#define matrix vector<vector<int>>
matrix operator * (matrix a,matrix b){
	matrix ans(2,vector<int>(2,0));
	for(int i=0;i<2;++i)
		for(int j=0;j<2;++j)
			for(int k=0;k<2;++k)
				ans[i][j]=add(ans[i][j],mul(a[i][k],b[k][j]));
	return ans;
}
matrix build(int sz){
	return {{1,1},{(sz-1)/2,sz/2}};
}
const int N=100050;
const int M=30*N*5;
const int lim=1e9+1000;
int ls[M],rs[M],tsz,root;
matrix idx,seg[M];
void Set(int&c,int ss,int se,int qi,matrix m){
	if(!c)c=++tsz,seg[c]=idx;
	if(ss==se){seg[c]=m;return;}
	int mid=ss+se>>1;
	if(qi<=mid)Set(ls[c],ss,mid,qi,m);
	else Set(rs[c],mid+1,se,qi,m);
	seg[c]=seg[rs[c]]*seg[ls[c]];
}
set<int> f;
void Add(int a){
	f.insert(a);
	auto it=f.find(a);
	Set(root,1,lim,a,build(a-*prev(it)));
	if(next(it)!=f.end()){
		Set(root,1,lim,*next(it),build(*next(it)-a));
	}
}
void Del(int a){
	auto it=f.find(a);
	Set(root,1,lim,a,idx);
	if(next(it)!=f.end()){
		Set(root,1,lim,*next(it),build(*next(it)-*prev(it)));
	}
	f.erase(it);
}
map<int,int> cnt;
void Inc(int a){
	cnt[a]++;
	if(cnt[a]==1)Add(a);
}
void Dec(int a){
	cnt[a]--;
	if(cnt[a]==0)Del(a);
}
void Fix(int a){
	if(cnt[a]&&cnt[a-1]){
		Dec(a);
		Dec(a-1);
		Inc(a+1);
		Fix(a+1);
		Fix(a+2);
	}else if(cnt[a]==2){
		if(a==1){
			Dec(a);Dec(a);
			Inc(a+1);
			Fix(a+1);
			Fix(a+2);
		}else{
			Inc(max(a-2,1));
			Dec(a);Dec(a);
			Inc(a+1);
			Fix(max(a-2,1));
			Fix(max(a-2,1)+1);
			Fix(a+1);
			Fix(a+2);
		}
	}
}
int Solve(){
	matrix m=seg[root];
	return add(m[0][0],m[1][0]);
	/*int dp[2]={1,0},las=0;
	for(auto it:cnt){
		int i=it.first;
		if(!cnt[i])continue;
		int d0=add(dp[0],dp[1]);
		int sz=i-las-1;
		int d1=add(mul(dp[0],sz/2),mul(dp[1],(sz+1)/2));
		las=i;dp[0]=d0;dp[1]=d1;
	}
	return add(dp[0],dp[1]);*/
}
int main(){
	f.insert(0);
	idx={{1,0},{0,1}};
	seg[0]=idx;
	int n;
	scanf("%i",&n);
	for(int i=1;i<=n;i++){
		int a;
		scanf("%i",&a);
		Inc(a);
		Fix(a);
		Fix(a+1);
		printf("%i\n",Solve());
	}
	return 0;
}

Compilation message

fib.cpp: In function 'void Set(int&, int, int, int, std::vector<std::vector<int> >)':
fib.cpp:27:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   27 |  int mid=ss+se>>1;
      |          ~~^~~
fib.cpp: In function 'int main()':
fib.cpp:101:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  101 |  scanf("%i",&n);
      |  ~~~~~^~~~~~~~~
fib.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  104 |   scanf("%i",&a);
      |   ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 140 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 141 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -