Submission #337278

#TimeUsernameProblemLanguageResultExecution timeMemory
337278super_j6Port Facility (JOI17_port_facility)C++14
0 / 100
1 ms492 KiB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <stack>
#include <deque>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

typedef array<deque<int>, 2> T;

const int mod = 1000000007;
const int mxn = 1000001;
int n;
pi a[mxn];
stack<T> stk;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	
	for(int i = 0; i < n; i++) cin >> a[i].f >> a[i].s;
	a[n] = {2 * n + 1, 2 * n + 1};
	
	sort(a, a + n);
	
	int ret = 1;
	stk.push(T());
	for(int i = 0; i < 2; i++) stk.top()[i].push_front(2 * n + 2);
	for(int i = 0; i <= n; i++){
		while(1){
			for(int j = 0; j < 2; j++){
				while(stk.top()[j].front() < a[i].f) stk.top()[j].pop_front();
			}
			if(stk.top()[0].front() > stk.top()[1].front()){
				swap(stk.top()[0], stk.top()[1]);
			} 
			if(stk.top()[0].front() < 2 * n + 3) break;
			stk.pop(), (ret *= 2) %= mod;
		}
		if(a[i].s < stk.top()[0].front()){
			stk.push(T());
			for(int j = 0; j < 2; j++) stk.top()[j].push_front(2 * n + 3);
			stk.top()[0].push_front(a[i].s);
		}else if(stk.top()[1].front() == 2 * n + 3){
			T x;
			swap(stk.top(), x);
			stk.pop();
			if(a[i].s < stk.top()[0].front()){
				stk.push(T());
				swap(stk.top(), x);
				stk.top()[1].push_front(a[i].s);
			}else if(a[i].s < stk.top()[1].front()){
				x[0].pop_back();
				stk.top()[1].push_front(a[i].s);
				if(x[0].size() < stk.top()[0].size()){
					while(!x[0].empty()){
						stk.top()[0].push_front(x[0].back());
						x[0].pop_back();
					}
				}else{
					swap(x[0], stk.top()[0]);
					while(!x[0].empty()){
						stk.top()[0].push_back(x[0].front());
						x[0].pop_front();
					}
				}
			}else{
				ret = 0;
				break;
			}
		}else if(a[i].s < stk.top()[1].front()){
			stk.top()[1].push_front(a[i].s);
		}else{
			ret = 0;
			break;
		}
	}
	
	cout << ret << endl;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...