Artifact Content
Not logged in

Artifact aff45afb5a80e9b5614cba94f9198ed62ea88426


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class InverseRMQ { public:
	string possible(int n, vector <int> A, vector <int> B, vector <int> ans)
	{
		vector<tuple<int,int,int>> E;
		for(int i=0; i<A.size(); ++i)
			E.emplace_back(A[i], B[i], ans[i]);
		sort(E.begin(), E.end(), [&](const tuple<int,int,int>& lhs, const tuple<int,int,int>& rhs){
			if(get<2>(lhs) < get<2>(rhs))
				return true;
			return lhs < rhs;
		});

		vector<pair<int,int>> filled;
		for(int i=0; i<E.size(); )
		{
			int nexti = i+1;
			for(; nexti<E.size(); ++nexti)
				if(get<2>(E[i]) != get<2>(E[nexti]))
					break;
			// inner
			int L = get<0>(E[i]);
			int R = get<1>(E[i]);
			// outer
			int l = get<0>(E[i]);
			int r = get<1>(E[i]);
			for(int a=i+1; a<nexti; ++a) {
				if(R<get<0>(E[a]) || get<1>(E[a])<L)
					return "Impossible";
				L = max(L, get<0>(E[a]));
				R = min(R, get<1>(E[a]));
				l = min(l, get<0>(E[a]));
				r = max(r, get<1>(E[a]));
			}

			bool ok = false;
			if(filled.empty()) ok = true;
			if(!ok && has_intersection(L,R,0,filled.front().first-1)) ok = true;
			if(!ok && has_intersection(L,R,filled.back().second+1,n)) ok = true;
			if(!ok) {
				for(int i=0; i+1<filled.size(); ++i)
					if(!ok && has_intersection(L,R,filled[i].second+1,filled[i+1].first-1))
						ok=true;
			}
			if(!ok)
				return "Impossible";

			int C = get<2>(E[i]);

			// check: enough number of elements for [l,r]
			int space = 0;


			// update
			filled.emplace_back(l, r);
			sort(filled.begin(), filled.end());
			vector<pair<int,int>> f2;
			filled = f2;
			for(auto r: filled) {
				if(f2.empty() || f2.back().second+1 < r.first)
					f2.push_back(r);
				else
					f2.back().second = max(f2.back().second, r.second);
			}

			i = nexti;
		}
		return "Possible";
	}

	static bool has_intersection(int a, int b, int A, int B)
	{
		return !(B<a || b<A);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, InverseRMQ().possible(n, A, B, ans));}
int main(){

CASE(0)
	int n = 5; 
	int A_[] = {1,2,4};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2,4,5};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = {3,4,5};
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = "Possible"; 
END
CASE(1)
	int n = 3; 
	int A_[] = {1,2,3};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1,2,3};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = {3,3,3};
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = "Impossible"; 
END
CASE(2)
	int n = 600; 
	int A_[] = {1,101,201,301,401,501};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {100,200,300,400,500,600};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = {100,200,300,400,500,600};
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = "Possible"; 
END
CASE(3)
	int n = 1000000000; 
	int A_[] = {1234,1234};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {5678,5678};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = {10000,20000};
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = "Impossible"; 
END
CASE(4)
	int n = 8; 
	int A_[] = {1,2,3,4,5,6,7,8};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1,2,3,4,5,6,7,8};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = {4,8,2,5,6,3,7,1};
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = "Possible"; 
END
CASE(5)
	int n = 1000000000; 
	int A_[] = {1};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1000000000};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = {19911120};
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = "Impossible"; 
END
CASE(6)
	int n = ; 
	int A_[] = ;
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = ;
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = ; 
END
CASE(7)
	int n = ; 
	int A_[] = ;
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int ans_[] = ;
	  vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 
	string _ = ; 
END

}
// END CUT HERE