37b91b57bc 2014-07-10 kinaba: #include <iostream> 37b91b57bc 2014-07-10 kinaba: #include <sstream> 37b91b57bc 2014-07-10 kinaba: #include <iomanip> 37b91b57bc 2014-07-10 kinaba: #include <vector> 37b91b57bc 2014-07-10 kinaba: #include <string> 37b91b57bc 2014-07-10 kinaba: #include <map> 37b91b57bc 2014-07-10 kinaba: #include <set> 37b91b57bc 2014-07-10 kinaba: #include <algorithm> 37b91b57bc 2014-07-10 kinaba: #include <numeric> 37b91b57bc 2014-07-10 kinaba: #include <iterator> 37b91b57bc 2014-07-10 kinaba: #include <functional> 37b91b57bc 2014-07-10 kinaba: #include <complex> 37b91b57bc 2014-07-10 kinaba: #include <queue> 37b91b57bc 2014-07-10 kinaba: #include <stack> 37b91b57bc 2014-07-10 kinaba: #include <cmath> 37b91b57bc 2014-07-10 kinaba: #include <cassert> 37b91b57bc 2014-07-10 kinaba: #include <tuple> 37b91b57bc 2014-07-10 kinaba: using namespace std; 37b91b57bc 2014-07-10 kinaba: typedef long long LL; 37b91b57bc 2014-07-10 kinaba: typedef complex<double> CMP; 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: class InverseRMQ { public: 37b91b57bc 2014-07-10 kinaba: string possible(int n, vector <int> A, vector <int> B, vector <int> ans) 37b91b57bc 2014-07-10 kinaba: { 37b91b57bc 2014-07-10 kinaba: vector<tuple<int,int,int>> E; 37b91b57bc 2014-07-10 kinaba: for(int i=0; i<A.size(); ++i) 37b91b57bc 2014-07-10 kinaba: E.emplace_back(A[i], B[i], ans[i]); 37b91b57bc 2014-07-10 kinaba: sort(E.begin(), E.end(), [&](const tuple<int,int,int>& lhs, const tuple<int,int,int>& rhs){ 37b91b57bc 2014-07-10 kinaba: if(get<2>(lhs) < get<2>(rhs)) 37b91b57bc 2014-07-10 kinaba: return true; 37b91b57bc 2014-07-10 kinaba: return lhs < rhs; 37b91b57bc 2014-07-10 kinaba: }); 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: vector<pair<int,int>> filled; 37b91b57bc 2014-07-10 kinaba: for(int i=0; i<E.size(); ) 37b91b57bc 2014-07-10 kinaba: { 37b91b57bc 2014-07-10 kinaba: int nexti = i+1; 37b91b57bc 2014-07-10 kinaba: for(; nexti<E.size(); ++nexti) 37b91b57bc 2014-07-10 kinaba: if(get<2>(E[i]) != get<2>(E[nexti])) 37b91b57bc 2014-07-10 kinaba: break; 37b91b57bc 2014-07-10 kinaba: // inner 37b91b57bc 2014-07-10 kinaba: int L = get<0>(E[i]); 37b91b57bc 2014-07-10 kinaba: int R = get<1>(E[i]); 37b91b57bc 2014-07-10 kinaba: // outer 37b91b57bc 2014-07-10 kinaba: int l = get<0>(E[i]); 37b91b57bc 2014-07-10 kinaba: int r = get<1>(E[i]); 37b91b57bc 2014-07-10 kinaba: for(int a=i+1; a<nexti; ++a) { 37b91b57bc 2014-07-10 kinaba: if(R<get<0>(E[a]) || get<1>(E[a])<L) 37b91b57bc 2014-07-10 kinaba: return "Impossible"; 37b91b57bc 2014-07-10 kinaba: L = max(L, get<0>(E[a])); 37b91b57bc 2014-07-10 kinaba: R = min(R, get<1>(E[a])); 37b91b57bc 2014-07-10 kinaba: l = min(l, get<0>(E[a])); 37b91b57bc 2014-07-10 kinaba: r = max(r, get<1>(E[a])); 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: bool ok = false; 37b91b57bc 2014-07-10 kinaba: if(filled.empty()) ok = true; 37b91b57bc 2014-07-10 kinaba: if(!ok && has_intersection(L,R,0,filled.front().first-1)) ok = true; 37b91b57bc 2014-07-10 kinaba: if(!ok && has_intersection(L,R,filled.back().second+1,n)) ok = true; 37b91b57bc 2014-07-10 kinaba: if(!ok) { 37b91b57bc 2014-07-10 kinaba: for(int i=0; i+1<filled.size(); ++i) 37b91b57bc 2014-07-10 kinaba: if(!ok && has_intersection(L,R,filled[i].second+1,filled[i+1].first-1)) 37b91b57bc 2014-07-10 kinaba: ok=true; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: if(!ok) 37b91b57bc 2014-07-10 kinaba: return "Impossible"; 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: int C = get<2>(E[i]); 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: // check: enough number of elements for [l,r] 37b91b57bc 2014-07-10 kinaba: int space = 0; 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: // update 37b91b57bc 2014-07-10 kinaba: filled.emplace_back(l, r); 37b91b57bc 2014-07-10 kinaba: sort(filled.begin(), filled.end()); 37b91b57bc 2014-07-10 kinaba: vector<pair<int,int>> f2; 37b91b57bc 2014-07-10 kinaba: filled = f2; 37b91b57bc 2014-07-10 kinaba: for(auto r: filled) { 37b91b57bc 2014-07-10 kinaba: if(f2.empty() || f2.back().second+1 < r.first) 37b91b57bc 2014-07-10 kinaba: f2.push_back(r); 37b91b57bc 2014-07-10 kinaba: else 37b91b57bc 2014-07-10 kinaba: f2.back().second = max(f2.back().second, r.second); 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: i = nexti; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: return "Possible"; 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: static bool has_intersection(int a, int b, int A, int B) 37b91b57bc 2014-07-10 kinaba: { 37b91b57bc 2014-07-10 kinaba: return !(B<a || b<A); 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: }; 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: // BEGIN CUT HERE 37b91b57bc 2014-07-10 kinaba: #include <ctime> 37b91b57bc 2014-07-10 kinaba: double start_time; string timer() 37b91b57bc 2014-07-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 37b91b57bc 2014-07-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 37b91b57bc 2014-07-10 kinaba: { os << "{ "; 37b91b57bc 2014-07-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 37b91b57bc 2014-07-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 37b91b57bc 2014-07-10 kinaba: void verify_case(const string& Expected, const string& Received) { 37b91b57bc 2014-07-10 kinaba: bool ok = (Expected == Received); 37b91b57bc 2014-07-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 37b91b57bc 2014-07-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 37b91b57bc 2014-07-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 37b91b57bc 2014-07-10 kinaba: #define END verify_case(_, InverseRMQ().possible(n, A, B, ans));} 37b91b57bc 2014-07-10 kinaba: int main(){ 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: CASE(0) 37b91b57bc 2014-07-10 kinaba: int n = 5; 37b91b57bc 2014-07-10 kinaba: int A_[] = {1,2,4}; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = {2,4,5}; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = {3,4,5}; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = "Possible"; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(1) 37b91b57bc 2014-07-10 kinaba: int n = 3; 37b91b57bc 2014-07-10 kinaba: int A_[] = {1,2,3}; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = {1,2,3}; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = {3,3,3}; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = "Impossible"; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(2) 37b91b57bc 2014-07-10 kinaba: int n = 600; 37b91b57bc 2014-07-10 kinaba: int A_[] = {1,101,201,301,401,501}; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = {100,200,300,400,500,600}; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = {100,200,300,400,500,600}; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = "Possible"; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(3) 37b91b57bc 2014-07-10 kinaba: int n = 1000000000; 37b91b57bc 2014-07-10 kinaba: int A_[] = {1234,1234}; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = {5678,5678}; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = {10000,20000}; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = "Impossible"; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(4) 37b91b57bc 2014-07-10 kinaba: int n = 8; 37b91b57bc 2014-07-10 kinaba: int A_[] = {1,2,3,4,5,6,7,8}; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = {1,2,3,4,5,6,7,8}; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = {4,8,2,5,6,3,7,1}; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = "Possible"; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(5) 37b91b57bc 2014-07-10 kinaba: int n = 1000000000; 37b91b57bc 2014-07-10 kinaba: int A_[] = {1}; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = {1000000000}; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = {19911120}; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = "Impossible"; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(6) 37b91b57bc 2014-07-10 kinaba: int n = ; 37b91b57bc 2014-07-10 kinaba: int A_[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = ; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: CASE(7) 37b91b57bc 2014-07-10 kinaba: int n = ; 37b91b57bc 2014-07-10 kinaba: int A_[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 37b91b57bc 2014-07-10 kinaba: int B_[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 37b91b57bc 2014-07-10 kinaba: int ans_[] = ; 37b91b57bc 2014-07-10 kinaba: vector <int> ans(ans_, ans_+sizeof(ans_)/sizeof(*ans_)); 37b91b57bc 2014-07-10 kinaba: string _ = ; 37b91b57bc 2014-07-10 kinaba: END 37b91b57bc 2014-07-10 kinaba: 37b91b57bc 2014-07-10 kinaba: } 37b91b57bc 2014-07-10 kinaba: // END CUT HERE