답안 #542369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
542369 2022-03-26T09:29:11 Z gs14004 Fibonacci representations (CEOI18_fib) C++17
100 / 100
1305 ms 123532 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int mod = 1e9 + 7;
const int MAXN = 1000005;
 
struct mtrx{
	int a[2][2];
	mtrx operator*(const mtrx &m)const{
		mtrx ret;
		memset(ret.a, 0, sizeof(ret.a));
		for(int i=0; i<2; i++){
			for(int j=0; j<2; j++){
				for(int k=0; k<2; k++){
					ret.a[j][k] += 1ll * a[j][i] * m.a[i][k] % mod;
					if(ret.a[j][k] >= mod) ret.a[j][k] -= mod;
				}
			}
		}
		return ret;
	}
}E, pwr[MAXN];
 
mtrx num(int n){
	mtrx m;
	m.a[1][0] = m.a[1][1] = (n % 2 == 0);
	m.a[0][0] = (n + 1) / 2;
	m.a[0][1] = (n - 1) / 2;
	return m;
}
 
set<pi> s;
 
struct seg{
	vector<mtrx> tree;
	int lim;
	void init(int n){
		for(lim = 1; lim <= n; lim <<= 1);
		tree.resize(2 * lim);
		fill(tree.begin(), tree.end(), E);
	}
	void add(int x, mtrx v){
		x += lim;
		tree[x] = v;
		while(x > 1){
			x >>= 1;
			tree[x] = tree[2*x] * tree[2*x+1];
		}
	}
	mtrx query(){
		return tree[1];
	}
}seg;
 
struct seq{
	int type; 
	int pos;
	mtrx m;
};
 
vector<seq> instr;
vector<int> vect;
 
void batch(){
	sort(vect.begin(), vect.end());
	vect.resize(unique(vect.begin(), vect.end()) - vect.begin());
	seg.init(vect.size());
	for(auto &i : instr){
		if(i.type == 2){
			printf("%d\n", seg.query().a[0][0]);
		}
		else{
			int pos = lower_bound(vect.begin(), vect.end(), i.pos) - vect.begin();
			seg.add(pos, i.m);
		}
	}
}
 
void query(){
	instr.push_back({2, -1, E});
}
 
void add_query(int pos, mtrx m){
	vect.push_back(pos);
	instr.push_back({1, pos, m});
}
 
void rem_query(int pos){
	instr.push_back({1, pos, E});
}
 
void add(int x){ 
	if(x == 0) x = 1;
	if(x == -1) return;
	auto ins = [](pi x){
		auto itr = s.lower_bound(x);
		int pos = 0;
		if(itr != s.begin()) pos = prev(itr)->second;
		if(itr != s.end()) rem_query(pos);
		s.insert(x);
		add_query(pos, num(x.first - pos));
		if(x.first != x.second) add_query(x.first, pwr[(x.second - x.first) / 2]);
		itr = s.upper_bound(x);
		if(itr != s.end()){
			add_query(x.second, num(itr->first - x.second));
		}
	};
	auto rem = [](pi x){
		if(x.first != x.second) rem_query(x.first);
		auto itr = s.lower_bound(x);
        assert(itr != s.end());
		int pos = 0;
		if(itr != s.begin()) pos = prev(itr)->second;
		if(next(itr) != s.end()) rem_query(x.second);
		rem_query(pos);
		s.erase(x);
		itr = s.lower_bound(x);
		if(itr != s.end()){
			add_query(pos, num(itr->first - pos));
		}
	};
	auto it = s.upper_bound(pi(x + 1, -1));
	if(it != s.begin() && prev(it)->second >= x){
		auto intv = *--it;
		rem(intv);
		if(intv.first % 2 == x % 2){
			add(intv.second + 1);
			intv.second = x - 2;
			if(intv.first <= intv.second){
				auto l = s.lower_bound(pi(intv.second + 3, -1));
				auto nxt = pi(intv.first + 1, intv.second + 1);
				if(l != s.end() && l->first == intv.second + 3){
					nxt.second = l->second;
					rem(*l);
				}
				ins(nxt);
			}
			add(intv.first - 2);
		}
		else{
			ins(pi(intv.first, x - 1));
			add(intv.second + 1);
		}
	}
	else{
		auto nxt = it;
		if(nxt != s.begin()){
			nxt = prev(nxt);
			if(nxt->second == x - 1){
				auto prv = *nxt;
				rem(prv);
				if(prv.first != prv.second){
					prv.second -= 2;
					ins(prv);
				}
				add(x + 1);
				return;
			}
			if(nxt->second == x - 2){
				auto l = s.lower_bound(pi(x + 1, -1));
				if(l != s.end() && l->first == x + 1){
					int pos = l->second + 1;
					rem(*l);
					add(pos);
				}
				else if(l != s.end() && l->first == x + 2){
					auto it = *nxt;
					it.second = l->second;
                    auto Q = *nxt;
					rem(*l);
					rem(Q);
					ins(it);
				}
				else{
					auto it = *nxt;
					it.second += 2;
					rem(*nxt);
					ins(it);
				}
				return;
			}
		}
		auto l = s.lower_bound(pi(x + 1, -1));
		if(l->first == x + 1){
			int pos = l->second + 1;
			rem(*l);
			add(pos);
		}
		else if(l->first == x + 2){
			auto nxt = *l; nxt.first -= 2;
			rem(*l);
			ins(nxt);
		}
		else{
			ins(pi(x, x));
		}
	}
}
 
int main(){
	E.a[0][0] = E.a[1][1] = 1;
	pwr[0] = E;
	pwr[1] = num(2);
	for(int i=2; i<MAXN; i++) pwr[i] = pwr[i-1] * pwr[1];
	int n, x;
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		scanf("%d",&x);
		add(x);
		query();
	}
	batch();
}

Compilation message

fib.cpp: In function 'int main()':
fib.cpp:207:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  207 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
fib.cpp:209:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  209 |   scanf("%d",&x);
      |   ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15844 KB Output is correct
2 Correct 29 ms 15876 KB Output is correct
3 Correct 28 ms 15944 KB Output is correct
4 Correct 30 ms 15848 KB Output is correct
5 Correct 28 ms 15872 KB Output is correct
6 Correct 28 ms 15864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15844 KB Output is correct
2 Correct 29 ms 15876 KB Output is correct
3 Correct 28 ms 15944 KB Output is correct
4 Correct 30 ms 15848 KB Output is correct
5 Correct 28 ms 15872 KB Output is correct
6 Correct 28 ms 15864 KB Output is correct
7 Correct 28 ms 15952 KB Output is correct
8 Correct 29 ms 15912 KB Output is correct
9 Correct 34 ms 15972 KB Output is correct
10 Correct 29 ms 15912 KB Output is correct
11 Correct 36 ms 15956 KB Output is correct
12 Correct 28 ms 15908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15948 KB Output is correct
2 Correct 27 ms 15968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15844 KB Output is correct
2 Correct 29 ms 15876 KB Output is correct
3 Correct 28 ms 15944 KB Output is correct
4 Correct 30 ms 15848 KB Output is correct
5 Correct 28 ms 15872 KB Output is correct
6 Correct 28 ms 15864 KB Output is correct
7 Correct 28 ms 15952 KB Output is correct
8 Correct 29 ms 15912 KB Output is correct
9 Correct 34 ms 15972 KB Output is correct
10 Correct 29 ms 15912 KB Output is correct
11 Correct 36 ms 15956 KB Output is correct
12 Correct 28 ms 15908 KB Output is correct
13 Correct 28 ms 15948 KB Output is correct
14 Correct 27 ms 15968 KB Output is correct
15 Correct 30 ms 15896 KB Output is correct
16 Correct 29 ms 15948 KB Output is correct
17 Correct 33 ms 15888 KB Output is correct
18 Correct 30 ms 15872 KB Output is correct
19 Correct 36 ms 15980 KB Output is correct
20 Correct 29 ms 16044 KB Output is correct
21 Correct 29 ms 16040 KB Output is correct
22 Correct 29 ms 15980 KB Output is correct
23 Correct 31 ms 15920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15948 KB Output is correct
2 Correct 399 ms 36452 KB Output is correct
3 Correct 439 ms 37852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 15844 KB Output is correct
2 Correct 29 ms 15876 KB Output is correct
3 Correct 28 ms 15944 KB Output is correct
4 Correct 30 ms 15848 KB Output is correct
5 Correct 28 ms 15872 KB Output is correct
6 Correct 28 ms 15864 KB Output is correct
7 Correct 28 ms 15952 KB Output is correct
8 Correct 29 ms 15912 KB Output is correct
9 Correct 34 ms 15972 KB Output is correct
10 Correct 29 ms 15912 KB Output is correct
11 Correct 36 ms 15956 KB Output is correct
12 Correct 28 ms 15908 KB Output is correct
13 Correct 28 ms 15948 KB Output is correct
14 Correct 27 ms 15968 KB Output is correct
15 Correct 30 ms 15896 KB Output is correct
16 Correct 29 ms 15948 KB Output is correct
17 Correct 33 ms 15888 KB Output is correct
18 Correct 30 ms 15872 KB Output is correct
19 Correct 36 ms 15980 KB Output is correct
20 Correct 29 ms 16044 KB Output is correct
21 Correct 29 ms 16040 KB Output is correct
22 Correct 29 ms 15980 KB Output is correct
23 Correct 31 ms 15920 KB Output is correct
24 Correct 28 ms 15948 KB Output is correct
25 Correct 399 ms 36452 KB Output is correct
26 Correct 439 ms 37852 KB Output is correct
27 Correct 111 ms 21996 KB Output is correct
28 Correct 184 ms 26420 KB Output is correct
29 Correct 154 ms 30192 KB Output is correct
30 Correct 194 ms 26640 KB Output is correct
31 Correct 634 ms 69756 KB Output is correct
32 Correct 395 ms 43312 KB Output is correct
33 Correct 613 ms 46576 KB Output is correct
34 Correct 709 ms 69532 KB Output is correct
35 Correct 627 ms 44376 KB Output is correct
36 Correct 667 ms 45852 KB Output is correct
37 Correct 453 ms 43748 KB Output is correct
38 Correct 364 ms 36876 KB Output is correct
39 Correct 190 ms 42672 KB Output is correct
40 Correct 282 ms 69320 KB Output is correct
41 Correct 703 ms 69660 KB Output is correct
42 Correct 345 ms 36816 KB Output is correct
43 Correct 382 ms 70180 KB Output is correct
44 Correct 1234 ms 123396 KB Output is correct
45 Correct 1169 ms 77208 KB Output is correct
46 Correct 1296 ms 123232 KB Output is correct
47 Correct 742 ms 70692 KB Output is correct
48 Correct 1305 ms 123532 KB Output is correct
49 Correct 1260 ms 77412 KB Output is correct
50 Correct 465 ms 46388 KB Output is correct