답안 #445471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445471 2021-07-18T07:43:27 Z Jasiekstrz Fibonacci representations (CEOI18_fib) C++17
100 / 100
1800 ms 205452 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5;
const int T=20*N;
const int MOD=1e9+7;
mt19937 gen(29139);
int rng(int l,int r)
{
	return uniform_int_distribution<int>{l,r}(gen);
}
struct Mat
{
	long long t[2][2];
	long long* operator[](int x)
	{
		return t[x];
	}
};
Mat E={{{1,0},{0,1}}};
Mat operator*(Mat &a,Mat &b)
{
	return {{{(a[0][0]*b[0][0]+a[0][1]*b[1][0])%MOD, (a[0][0]*b[0][1]+a[0][1]*b[1][1])%MOD},
		{(a[1][0]*b[0][0]+a[1][1]*b[1][0])%MOD, (a[1][0]*b[0][1]+a[1][1]*b[1][1])%MOD}}};
}
struct Treap
{
	pair<int,int> val;
	Mat c,m;
	int siz;
	int lazy;
	int pr;
	Treap *l,*r;
};
int treapit=0;
Treap treapmem[T+10];
Treap* newtreap(pair<int,int> val,Mat m)
{
	Treap* x=&treapmem[treapit++];
	x->val=val;
	x->m=x->c=m;
	x->siz=1;
	x->lazy=0;
	x->pr=rng(0,MOD);
	x->l=x->r=nullptr;
	return x;
}
Mat& getm(Treap* x)
{
	if(x==nullptr)
		return E;
	return x->m;
}
int getsiz(Treap* x)
{
	if(x==nullptr)
		return 0;
	return x->siz;
}
void recount(Treap* x)
{
	if(x==nullptr)
		return;
	x->m=getm(x->l)*(x->c);
	x->m=(x->m)*getm(x->r);
	
	x->siz=getsiz(x->l)+getsiz(x->r)+1;
	return;
}
void propagate(Treap* x)
{
	x->val.fi+=x->lazy;
	if(x->l!=nullptr)
		x->l->lazy+=x->lazy;
	if(x->r!=nullptr)
		x->r->lazy+=x->lazy;
	x->lazy=0;
	return;
}
pair<Treap*,Treap*> split(Treap* x,pair<int,int> c)
{
	if(x==nullptr)
		return {nullptr,nullptr};
	propagate(x);
	if(x->val<c)
	{
		auto [a,b]=split(x->r,c);
		x->r=a;
		recount(x);
		return {x,b};
	}
	auto [a,b]=split(x->l,c);
	x->l=b;
	recount(x);
	return {a,x};
}
Treap* merge(Treap* x,Treap* y)
{
	if(x==nullptr)
		return y;
	if(y==nullptr)
		return x;
	propagate(x);
	propagate(y);
	if(x->pr<y->pr)
	{
		x->r=merge(x->r,y);
		recount(x);
		return x;
	}
	y->l=merge(x,y->l);
	recount(y);
	return y;
}
Treap* insert(Treap* x,Treap* c)
{
	if(x==nullptr)
		return c;
	propagate(x);
	if(c->pr<x->pr)
	{
		auto [a,b]=split(x,c->val);
		c->l=a;
		c->r=b;
		recount(c);
		return c;
	}
	if(c->val<x->val)
		x->l=insert(x->l,c);
	else
		x->r=insert(x->r,c);
	recount(x);
	return x;
}
Treap* erase(Treap* x,pair<int,int> c)
{
	if(x==nullptr)
		return nullptr;
	propagate(x);
	if(x->val==c)
		x=merge(x->l,x->r);
	else if(c<x->val)
		x->l=erase(x->l,c);
	else
		x->r=erase(x->r,c);
	recount(x);
	return x;
}
Treap* leq(Treap* x,pair<int,int> c)
{
	if(x==nullptr)
		return nullptr;
	propagate(x);
	if(x->val<=c)
	{
		auto a=leq(x->r,c);
		return (a==nullptr ? x:a);
	}
	return leq(x->l,c);
}
Treap* geq(Treap* x,pair<int,int> c)
{
	if(x==nullptr)
		return nullptr;
	propagate(x);
	if(x->val>=c)
	{
		auto a=geq(x->l,c);
		return (a==nullptr ? x:a);
	}
	return geq(x->r,c);
}
int ord(Treap* x,Treap* c)
{
	propagate(x);
	if(x==c)
		return getsiz(x->l);
	if(x->val<c->val)
		return getsiz(x->l)+1+ord(x->r,c);
	return ord(x->l,c);
}
Treap* cons(Treap* x,int ordx,int ord,int v)
{
	if(x==nullptr)
		return nullptr;
	propagate(x);
	if(x->val.fi>v)
		return cons(x->l,ordx,ord,v);
	if(2*(ord-(ordx+getsiz(x->l)))==v-x->val.fi)
	{
		auto a=cons(x->l,ordx,ord,v);
		return (a==nullptr ? x:a);
	}
	return cons(x->r,ordx+getsiz(x->l)+1,ord,v);
}
Treap *t=nullptr;
void er0(pair<int,int> x)
{
	t=erase(t,x);
	return;
}
void ins0(pair<int,int> x)
{
	auto it=leq(t,x);
	if(it==nullptr)
	{
		t=insert(t,newtreap(x,E));
		return;
	}
	int tmp=it->val.fi;
	Mat m;
	if(tmp==x.fi)
		m={{{0,0},{1,0}}};
	else if(tmp%2==x.fi%2)
		m={{{1,(x.fi-tmp)/2-1},{1,(x.fi-tmp)/2}}};
	else
		m={{{1,(x.fi-tmp)/2},{1,(x.fi-tmp)/2}}};
	t=insert(t,newtreap(x,m));
	return;
}
void er(pair<int,int> x)
{
	er0(x);
	auto it=geq(t,x);
	if(it!=nullptr)
	{
		auto tmp=it->val;
		er0(tmp);
		ins0(tmp);
	}
	return;
}
void ins(pair<int,int> x)
{
	auto it=geq(t,x);
	ins0(x);
	if(it!=nullptr)
	{
		auto tmp=it->val;
		er0(tmp);
		ins0(tmp);
	}
	return;
}
void add(pair<int,int> c)
{
	auto it=geq(t,{c.fi,0});
	if(it!=nullptr && it->val.fi==c.fi)
	{
		auto it2=cons(t,0,ord(t,it),it->val.fi);
		auto cc=it2->val;
		er(it->val);
		auto [t1,t4]=split(t,it2->val);
		auto [t2,t3]=split(t4,it->val);
		if(t2!=nullptr)
			t2->lazy++;
		t4=merge(t2,t3);
		t=merge(t1,t4);
		if(cc.fi==1);
		else if(cc.fi==2)
			add({cc.fi-1,c.se});
		else
			add({cc.fi-2,c.se});
		c.fi++;
		c.se=it->val.se;
	}
	while(true)
	{
		it=leq(t,{c.fi+1,T});
		if(it!=nullptr && it->val.fi==c.fi+1)
		{
			c.fi+=2;
			er(it->val);
			continue;
		}
		it=leq(t,{c.fi-1,T});
		if(it!=nullptr && it->val.fi==c.fi-1)
		{
			c.fi++;
			er(it->val);
			continue;
		}
		break;
	}
	ins(c);
	return;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		pair<int,int> c={0,i};
		cin>>c.fi;
		add(c);
		auto m=getm(t);
		//cerr<<m[0][0]<<" "<<m[0][1]<<" "<<m[1][0]<<" "<<m[1][1]<<"\n";
		int x=geq(t,{1,0})->val.fi;
		pair<long long,long long> tmp={1,(x-1)/2};
		long long ans=(tmp.fi*m[0][0]+tmp.se*m[1][0])%MOD
			+(tmp.fi*m[0][1]+tmp.se*m[1][1])%MOD;
		cout<<ans%MOD<<"\n";
	}
	//for(auto v:st)
	//	cerr<<v.fi<<" "<<v.se<<"\n";
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 203816 KB Output is correct
2 Correct 82 ms 203780 KB Output is correct
3 Correct 85 ms 203804 KB Output is correct
4 Correct 82 ms 203716 KB Output is correct
5 Correct 85 ms 203768 KB Output is correct
6 Correct 81 ms 203704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 203816 KB Output is correct
2 Correct 82 ms 203780 KB Output is correct
3 Correct 85 ms 203804 KB Output is correct
4 Correct 82 ms 203716 KB Output is correct
5 Correct 85 ms 203768 KB Output is correct
6 Correct 81 ms 203704 KB Output is correct
7 Correct 97 ms 203744 KB Output is correct
8 Correct 85 ms 203736 KB Output is correct
9 Correct 90 ms 203716 KB Output is correct
10 Correct 83 ms 203716 KB Output is correct
11 Correct 86 ms 203832 KB Output is correct
12 Correct 83 ms 203796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 203716 KB Output is correct
2 Correct 85 ms 203824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 203816 KB Output is correct
2 Correct 82 ms 203780 KB Output is correct
3 Correct 85 ms 203804 KB Output is correct
4 Correct 82 ms 203716 KB Output is correct
5 Correct 85 ms 203768 KB Output is correct
6 Correct 81 ms 203704 KB Output is correct
7 Correct 97 ms 203744 KB Output is correct
8 Correct 85 ms 203736 KB Output is correct
9 Correct 90 ms 203716 KB Output is correct
10 Correct 83 ms 203716 KB Output is correct
11 Correct 86 ms 203832 KB Output is correct
12 Correct 83 ms 203796 KB Output is correct
13 Correct 81 ms 203716 KB Output is correct
14 Correct 85 ms 203824 KB Output is correct
15 Correct 84 ms 203708 KB Output is correct
16 Correct 84 ms 203752 KB Output is correct
17 Correct 82 ms 203748 KB Output is correct
18 Correct 83 ms 203728 KB Output is correct
19 Correct 85 ms 203716 KB Output is correct
20 Correct 86 ms 203928 KB Output is correct
21 Correct 87 ms 203700 KB Output is correct
22 Correct 83 ms 203712 KB Output is correct
23 Correct 84 ms 203792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 203780 KB Output is correct
2 Correct 609 ms 205128 KB Output is correct
3 Correct 617 ms 205108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 203816 KB Output is correct
2 Correct 82 ms 203780 KB Output is correct
3 Correct 85 ms 203804 KB Output is correct
4 Correct 82 ms 203716 KB Output is correct
5 Correct 85 ms 203768 KB Output is correct
6 Correct 81 ms 203704 KB Output is correct
7 Correct 97 ms 203744 KB Output is correct
8 Correct 85 ms 203736 KB Output is correct
9 Correct 90 ms 203716 KB Output is correct
10 Correct 83 ms 203716 KB Output is correct
11 Correct 86 ms 203832 KB Output is correct
12 Correct 83 ms 203796 KB Output is correct
13 Correct 81 ms 203716 KB Output is correct
14 Correct 85 ms 203824 KB Output is correct
15 Correct 84 ms 203708 KB Output is correct
16 Correct 84 ms 203752 KB Output is correct
17 Correct 82 ms 203748 KB Output is correct
18 Correct 83 ms 203728 KB Output is correct
19 Correct 85 ms 203716 KB Output is correct
20 Correct 86 ms 203928 KB Output is correct
21 Correct 87 ms 203700 KB Output is correct
22 Correct 83 ms 203712 KB Output is correct
23 Correct 84 ms 203792 KB Output is correct
24 Correct 83 ms 203780 KB Output is correct
25 Correct 609 ms 205128 KB Output is correct
26 Correct 617 ms 205108 KB Output is correct
27 Correct 217 ms 204336 KB Output is correct
28 Correct 339 ms 204684 KB Output is correct
29 Correct 182 ms 204072 KB Output is correct
30 Correct 347 ms 204632 KB Output is correct
31 Correct 765 ms 204792 KB Output is correct
32 Correct 545 ms 204580 KB Output is correct
33 Correct 814 ms 204876 KB Output is correct
34 Correct 880 ms 204612 KB Output is correct
35 Correct 823 ms 204812 KB Output is correct
36 Correct 854 ms 204792 KB Output is correct
37 Correct 611 ms 204824 KB Output is correct
38 Correct 619 ms 205080 KB Output is correct
39 Correct 212 ms 204332 KB Output is correct
40 Correct 318 ms 204368 KB Output is correct
41 Correct 907 ms 205300 KB Output is correct
42 Correct 633 ms 205128 KB Output is correct
43 Correct 416 ms 205224 KB Output is correct
44 Correct 1246 ms 205184 KB Output is correct
45 Correct 1583 ms 205220 KB Output is correct
46 Correct 1762 ms 205268 KB Output is correct
47 Correct 1024 ms 205316 KB Output is correct
48 Correct 1800 ms 205452 KB Output is correct
49 Correct 1742 ms 205304 KB Output is correct
50 Correct 811 ms 205236 KB Output is correct