|
| 1 | +// BEGIN CUT HERE |
| 2 | + |
| 3 | +// END CUT HERE |
| 4 | +#include <algorithm> |
| 5 | +#include <iostream> |
| 6 | +#include <sstream> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | +#include <queue> |
| 10 | +#include <set> |
| 11 | +#include <map> |
| 12 | +#include <cstdio> |
| 13 | +#include <cstdlib> |
| 14 | +#include <cctype> |
| 15 | +#include <cmath> |
| 16 | +#include <string> |
| 17 | +#include <cstring> |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +// BEGIN CUT HERE |
| 21 | +#define ARRSIZE(x) (sizeof(x)/sizeof(x[0])) |
| 22 | + |
| 23 | +template<typename T> void print( T a ) { |
| 24 | + cerr << a; |
| 25 | +} |
| 26 | +static void print( long long a ) { |
| 27 | + cerr << a << "L"; |
| 28 | +} |
| 29 | +static void print( string a ) { |
| 30 | + cerr << '"' << a << '"'; |
| 31 | +} |
| 32 | +template<typename T> void print( vector<T> a ) { |
| 33 | + cerr << "{"; |
| 34 | + for ( int i = 0 ; i != a.size() ; i++ ) { |
| 35 | + if ( i != 0 ) cerr << ", "; |
| 36 | + print( a[i] ); |
| 37 | + } |
| 38 | + cerr << "}" << endl; |
| 39 | +} |
| 40 | +template<typename T> void eq( int n, T have, T need ) { |
| 41 | + if ( have == need ) { |
| 42 | + cerr << "Case " << n << " passed." << endl; |
| 43 | + } else { |
| 44 | + cerr << "Case " << n << " failed: expected "; |
| 45 | + print( need ); |
| 46 | + cerr << " received "; |
| 47 | + print( have ); |
| 48 | + cerr << "." << endl; |
| 49 | + } |
| 50 | +} |
| 51 | +template<typename T> void eq( int n, vector<T> have, vector<T> need ) { |
| 52 | + if( have.size() != need.size() ) { |
| 53 | + cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements."; |
| 54 | + print( have ); |
| 55 | + print( need ); |
| 56 | + return; |
| 57 | + } |
| 58 | + for( int i= 0; i < have.size(); i++ ) { |
| 59 | + if( have[i] != need[i] ) { |
| 60 | + cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << "."; |
| 61 | + print( have ); |
| 62 | + print( need ); |
| 63 | + return; |
| 64 | + } |
| 65 | + } |
| 66 | + cerr << "Case " << n << " passed." << endl; |
| 67 | +} |
| 68 | +static void eq( int n, string have, string need ) { |
| 69 | + if ( have == need ) { |
| 70 | + cerr << "Case " << n << " passed." << endl; |
| 71 | + } else { |
| 72 | + cerr << "Case " << n << " failed: expected "; |
| 73 | + print( need ); |
| 74 | + cerr << " received "; |
| 75 | + print( have ); |
| 76 | + cerr << "." << endl; |
| 77 | + } |
| 78 | +} |
| 79 | +// END CUT HERE |
| 80 | + |
| 81 | +#define REP(i,n) for(int i=0;i<(n);++i) |
| 82 | +#define FOR(i,a,b) for(int i=(a);i<=(b);++i) |
| 83 | +#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) |
| 84 | +#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it) |
| 85 | +#define CLR(x) memset((x),0,sizeof((x))) |
| 86 | +#define SORT(x) sort(x.begin(), x.end()) |
| 87 | +#define REVERSE(x) reverse(x.begin(), x.end()) |
| 88 | +#define SZ(x) x.size() |
| 89 | +#define MP make_pair |
| 90 | +#define MPI make_pair<int, int> |
| 91 | +#define PB push_back |
| 92 | +typedef long long LL; |
| 93 | +typedef vector<bool> VB; |
| 94 | +typedef vector<int> VI; |
| 95 | +typedef vector<vector<int> > VVI; |
| 96 | +typedef vector<string> VS; |
| 97 | +typedef pair<int, int> PI; |
| 98 | +typedef vector<pair<int, int> > VPI; |
| 99 | + |
| 100 | +// BEGIN CUT HERE |
| 101 | +vector<string> split( const string& s, const string& delim =" " ) { |
| 102 | + vector<string> res; |
| 103 | + string t; |
| 104 | + for ( int i = 0 ; i != s.size() ; i++ ) { |
| 105 | + if ( delim.find( s[i] ) != string::npos ) { |
| 106 | + if ( !t.empty() ) { |
| 107 | + res.push_back( t ); |
| 108 | + t = ""; |
| 109 | + } |
| 110 | + } else { |
| 111 | + t += s[i]; |
| 112 | + } |
| 113 | + } |
| 114 | + if ( !t.empty() ) { |
| 115 | + res.push_back(t); |
| 116 | + } |
| 117 | + return res; |
| 118 | +} |
| 119 | + |
| 120 | +vector<int> splitInt( const string& s, const string& delim =" " ) { |
| 121 | + vector<string> tok = split( s, delim ); |
| 122 | + vector<int> res; |
| 123 | + for ( int i = 0 ; i != tok.size(); i++ ) |
| 124 | + res.push_back( atoi( tok[i].c_str() ) ); |
| 125 | + return res; |
| 126 | +} |
| 127 | +// END CUT HERE |
| 128 | + |
| 129 | +// BEGIN CUT HERE |
| 130 | +int s2i(string s) { |
| 131 | + stringstream ss; |
| 132 | + ss << s; |
| 133 | + int res; |
| 134 | + ss >> res; |
| 135 | + return res; |
| 136 | +} |
| 137 | + |
| 138 | +string i2s(int n) { |
| 139 | + stringstream ss; |
| 140 | + ss << n; |
| 141 | + string res; |
| 142 | + ss >> res; |
| 143 | + return res; |
| 144 | +} |
| 145 | +// END CUT HERE |
| 146 | + |
| 147 | +class seg { |
| 148 | +public: |
| 149 | + int val; |
| 150 | + PI uni, ins; |
| 151 | + bool operator<(const seg& rhs) const { |
| 152 | + return val < rhs.val; |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +vector<seg> mm; |
| 157 | + |
| 158 | +int doit(PI p, int cnt) { |
| 159 | + VPI t; |
| 160 | + t.PB(p); |
| 161 | + REP(j,cnt) { |
| 162 | + PI u = mm[j].uni; |
| 163 | + VPI q; |
| 164 | + REP(k,SZ(t)) { |
| 165 | + PI now = t[k]; |
| 166 | + if (u.second < now.first) q.PB(now); |
| 167 | + else if (u.first > now.second) q.PB(now); |
| 168 | + else { |
| 169 | + if (u.first > now.first) q.PB(MP(now.first, u.first - 1)); |
| 170 | + if (u.second < now.second) q.PB(MP(u.second + 1, now.second)); |
| 171 | + } |
| 172 | + } |
| 173 | + t = q; |
| 174 | + } |
| 175 | + |
| 176 | + int sum = 0; |
| 177 | + REP(ii,SZ(t)) { |
| 178 | + sum += t[ii].second - t[ii].first + 1; |
| 179 | + } |
| 180 | + |
| 181 | + return sum; |
| 182 | +} |
| 183 | + |
| 184 | +class InverseRMQ { |
| 185 | +public: |
| 186 | + string possible(int n, vector <int> A, vector <int> B, vector <int> ans) { |
| 187 | + n = n; |
| 188 | + mm.clear(); |
| 189 | + REP(i,SZ(A)) { |
| 190 | + seg s; |
| 191 | + s.val = ans[i]; |
| 192 | + s.uni = MP(A[i], B[i]); |
| 193 | + s.ins = MP(A[i], B[i]); |
| 194 | + mm.PB(s); |
| 195 | + } |
| 196 | + SORT(mm); |
| 197 | + int idx = 0; |
| 198 | + REP(i,SZ(A)) { |
| 199 | + int next = i + 1; |
| 200 | + while (next < SZ(A) && mm[next].val == mm[i].val) { |
| 201 | + int a = mm[next].uni.first, b = mm[next].uni.second; |
| 202 | + mm[i].uni.first = min(mm[i].uni.first, a); |
| 203 | + mm[i].uni.second = max(mm[i].uni.second, b); |
| 204 | + mm[i].ins.first = max(mm[i].ins.first, a); |
| 205 | + mm[i].ins.second = min(mm[i].ins.second, b); |
| 206 | + ++next; |
| 207 | + } |
| 208 | + if (mm[i].ins.first > mm[i].ins.second) return "Impossible"; |
| 209 | + mm[idx++] = mm[i]; |
| 210 | + i = next - 1; |
| 211 | + } |
| 212 | + mm.erase(mm.begin() + idx, mm.end()); |
| 213 | + |
| 214 | + int used = 0; |
| 215 | + REP(i,SZ(mm)) { |
| 216 | + int sum = doit(mm[i].ins, i); |
| 217 | + if (sum == 0) return "Impossible"; |
| 218 | + |
| 219 | + used += doit(mm[i].uni, i); |
| 220 | + if (used > mm[i].val) return "Impossible"; |
| 221 | + } |
| 222 | + |
| 223 | + return "Possible"; |
| 224 | + } |
| 225 | +}; |
| 226 | +// BEGIN CUT HERE |
| 227 | +int main() { |
| 228 | + { |
| 229 | + int AARRAY[] = {2,5}; |
| 230 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 231 | + int BARRAY[] = {6,8}; |
| 232 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 233 | + int ansARRAY[] = {3,3}; |
| 234 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 235 | + InverseRMQ theObject; |
| 236 | + eq(0, theObject.possible(9, A, B, ans),"Impossible"); |
| 237 | + } |
| 238 | + { |
| 239 | + int AARRAY[] = {1,2,4}; |
| 240 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 241 | + int BARRAY[] = {2,4,5}; |
| 242 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 243 | + int ansARRAY[] = {3,4,5}; |
| 244 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 245 | + InverseRMQ theObject; |
| 246 | + eq(0, theObject.possible(5, A, B, ans),"Possible"); |
| 247 | + } |
| 248 | + { |
| 249 | + int AARRAY[] = {1,2,3}; |
| 250 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 251 | + int BARRAY[] = {1,2,3}; |
| 252 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 253 | + int ansARRAY[] = {3,3,3}; |
| 254 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 255 | + InverseRMQ theObject; |
| 256 | + eq(1, theObject.possible(3, A, B, ans),"Impossible"); |
| 257 | + } |
| 258 | + { |
| 259 | + int AARRAY[] = {1,101,201,301,401,501}; |
| 260 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 261 | + int BARRAY[] = {100,200,300,400,500,600}; |
| 262 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 263 | + int ansARRAY[] = {100,200,300,400,500,600}; |
| 264 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 265 | + InverseRMQ theObject; |
| 266 | + eq(2, theObject.possible(600, A, B, ans),"Possible"); |
| 267 | + } |
| 268 | + { |
| 269 | + int AARRAY[] = {1234,1234}; |
| 270 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 271 | + int BARRAY[] = {5678,5678}; |
| 272 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 273 | + int ansARRAY[] = {10000,20000}; |
| 274 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 275 | + InverseRMQ theObject; |
| 276 | + eq(3, theObject.possible(1000000000, A, B, ans),"Impossible"); |
| 277 | + } |
| 278 | + { |
| 279 | + int AARRAY[] = {1,2,3,4,5,6,7,8}; |
| 280 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 281 | + int BARRAY[] = {1,2,3,4,5,6,7,8}; |
| 282 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 283 | + int ansARRAY[] = {4,8,2,5,6,3,7,1}; |
| 284 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 285 | + InverseRMQ theObject; |
| 286 | + eq(4, theObject.possible(8, A, B, ans),"Possible"); |
| 287 | + } |
| 288 | + { |
| 289 | + int AARRAY[] = {1}; |
| 290 | + vector <int> A( AARRAY, AARRAY+ARRSIZE(AARRAY) ); |
| 291 | + int BARRAY[] = {1000000000}; |
| 292 | + vector <int> B( BARRAY, BARRAY+ARRSIZE(BARRAY) ); |
| 293 | + int ansARRAY[] = {19911120}; |
| 294 | + vector <int> ans( ansARRAY, ansARRAY+ARRSIZE(ansARRAY) ); |
| 295 | + InverseRMQ theObject; |
| 296 | + eq(5, theObject.possible(1000000000, A, B, ans),"Impossible"); |
| 297 | + } |
| 298 | + return 0; |
| 299 | +} |
| 300 | +// END CUT HERE |
0 commit comments