Submission #128999

# Submission time Handle Problem Language Result Execution time Memory
128999 2019-07-11T12:09:32 Z gs14004 Fibonacci representations (CEOI18_fib) C++17
50 / 100
4000 ms 2204 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 = 200005;
 
set<pi> s;
int n, a[MAXN];
lint dp[MAXN][2];
 
int query(){
	n = 0;
	for(auto &i : s){
		assert(i.first % 2 == i.second % 2);
		for(int j=i.first; j<=i.second; j+=2){
			a[++n] = j;
		}
	}
	dp[0][0] = 1;
	for(int i=1; i<=n; i++){
		int d = a[i] - a[i-1];
	//	printf("G = %d. ", d);
		dp[i][0] = (dp[i-1][1] * (d % 2 == 0) + dp[i-1][0] * ((d + 1) / 2)) % mod;
		dp[i][1] = (dp[i-1][1] * (d % 2 == 0) + dp[i-1][0] * ((d - 1) / 2)) % mod;
	//	printf("(%lld, %lld)\n", dp[i][0], dp[i][1]);
	}
	return dp[n][0];
}
 
int cnt;

void add(int x){ 
	if(x == 0) x = 1;
	if(x == -1) return;
	auto it = s.upper_bound(pi(x + 1, -1));
	if(it != s.begin() && prev(it)->second >= x){
		auto intv = *--it;
		s.erase(it);
		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;
					s.erase(l);
				}
				s.insert(nxt);
			}
			add(intv.first - 2);
		}
		else{
			s.emplace(intv.first, x - 1);
			add(intv.second + 1);
		}
	}
	else{
		auto nxt = it;
		pi ins(x, x);
		if(nxt != s.begin()){
			nxt = prev(nxt);
			if(nxt->second == x - 1){
				auto prv = *nxt;
				s.erase(nxt);
				if(prv.first != prv.second){
					prv.second -= 2;
					s.insert(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;
					s.erase(l);
					add(pos);
				}
				else if(l != s.end() && l->first == x + 2){
					auto it = *nxt;
					it.second = l->second;
					s.erase(l);
					s.erase(nxt);
					s.insert(it);
				}
				else{
					auto it = *nxt;
					it.second += 2;
					s.erase(nxt);
					s.insert(it);
				}
				return;
			}
		}
		auto l = s.lower_bound(pi(x + 1, -1));
		if(l->first == x + 1){
			int pos = l->second + 1;
			s.erase(l);
			add(pos);
		}
		else if(l->first == x + 2){
			auto nxt = *l; nxt.first -= 2;
			s.erase(l); s.insert(nxt);
		}
		else{
			s.emplace(x, x);
		}
	}
}

int main(){
	int n, x;
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		scanf("%d",&x);
		add(x);
		printf("%d\n", query());
	}
}

Compilation message

fib.cpp: In function 'int main()':
fib.cpp:115:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
fib.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x);
   ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 312 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 256 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Execution timed out 4088 ms 2204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 312 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 256 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Execution timed out 4088 ms 2204 KB Time limit exceeded
26 Halted 0 ms 0 KB -