/* Author: Dominik Wójt */ /* Problem: Bohater, Potyczki Algorytmiczne 2014 */ #include <iostream> #include <cstdlib> #include <vector> #include <algorithm> #define MY_ASSERT(condition) \ do \ { \ if(!(condition)) \ { \ std::cerr << "assert failed: " << #condition << std::endl; \ std::abort(); \ } \ } \ while(false) //------------------------------------------------------------------------------ typedef long int lint; typedef long int llint; //------------------------------------------------------------------------------ template<typename T> T checked_read(const T min, const T max) { T n; std::cin >> n; MY_ASSERT(std::cin); MY_ASSERT(min<=n && n<=max); return n; } //------------------------------------------------------------------------------ template<typename T> T round_up(const T a, const T b) { return a%b ? a+b : a; } //------------------------------------------------------------------------------ template<typename T> T round_down(const T a, const T b) { return a - a%b; } //------------------------------------------------------------------------------ template<typename T> T divide_ceil(const T a, const T b) { return (a+b-1)/b; } //------------------------------------------------------------------------------ template<typename T> T log2_ceil(const T x) { T base = 1; T log2 = 0; while(x>base) { log2++; base = 1<<log2; } return log2; } //------------------------------------------------------------------------------ template<typename TElement, typename TCompare> void merge_sort(std::vector<TElement> &vector, TCompare compare) { typedef std::vector<TElement> Vector; typedef typename Vector::iterator Iterator; Vector buffer(vector.size()); Iterator source_begin = vector.begin(), source_end = vector.end(); Iterator target_begin = buffer.begin(), target_end = buffer.end(); for(size_t chunk_size=1; chunk_size<vector.size(); chunk_size*=2) { size_t partition_size = round_down(round_up(vector.size(), chunk_size), 2*chunk_size)/2; Iterator target = target_begin; Iterator b0=source_begin, b1=source_begin+partition_size; while(b0 != source_begin+partition_size) { Iterator next_b1 = std::min(b1+chunk_size, source_end); target = std::merge(b0, b0+chunk_size, b1, next_b1, target, compare); b0+=chunk_size; b1=next_b1; } if(target!=target_end) target = std::copy(b1, source_end, target); MY_ASSERT(target==target_end); std::swap(source_begin, target_begin); std::swap(source_end, target_end); } if(source_begin!=vector.begin()) std::swap(vector, buffer); } //------------------------------------------------------------------------------ struct Car { lint source_x; lint target_x; lint source_index; lint target_index; lint height; }; //------------------------------------------------------------------------------ bool less_source_x(const Car &a, const Car &b) { return a.source_x < b.source_x; } //------------------------------------------------------------------------------ bool less_target_x(const Car &a, const Car &b) { return a.target_x < b.target_x; } //------------------------------------------------------------------------------ template<typename TElement, typename TCompare> class Compare_index_adaptor { const std::vector<TElement> &m_vector; TCompare m_compare; public: Compare_index_adaptor(const std::vector<TElement> &vector, TCompare compare) : m_vector(vector), m_compare(compare) { } bool operator()(size_t i, size_t j) { return m_compare(m_vector[i], m_vector[j]); } }; //------------------------------------------------------------------------------ template<typename TElement, typename TCompare> Compare_index_adaptor<TElement, TCompare> make_compare_index_adaptor( const std::vector<TElement> &vector, TCompare compare) { return Compare_index_adaptor<TElement, TCompare>(vector, compare); } //------------------------------------------------------------------------------ class Height_tree { const lint m_n; std::vector<lint> m_data; public: Height_tree(lint n) : m_n(1<<log2_ceil(n)), m_data(2*m_n, 0) { } void put(lint i, lint height) { lint j = i+m_n; m_data[j] = height; j = get_parrent(j); while(j > 0) { if(m_data[j] < m_data[get_left_child(j)]) m_data[j] = m_data[get_left_child(j)]; else if(m_data[j] < m_data[get_right_child(j)]) m_data[j] = m_data[get_right_child(j)]; else break; j = get_parrent(j); } } lint get_highest(lint begin, lint end) { lint top = 0; // naive implementation: //for(lint i=begin; i<end; i++) // top = std::max(top, m_data[m_n+i]); lint i = begin; while(i < end) { lint range = 1; lint j = i+m_n; while(is_left_child(j) && i+range*2<end) { j = get_parrent(j); range *= 2; MY_ASSERT(j > 0); } top = std::max(top, m_data[j]); i+=range; } return top; } private: lint get_parrent(lint j) { return j/2; } lint get_left_child(lint j) { return j*2; } lint get_right_child(lint j) { return j*2 + 1; } bool is_left_child(lint j) { return j%2 == 0; } }; //------------------------------------------------------------------------------ void process_case() { const lint n = checked_read<lint>(1, 50000); const lint w = checked_read<lint>(1, 1000000000); std::vector<Car> cars(n); for(lint i=0; i<n; i++) { Car &car = cars[i]; const lint x0 = checked_read<lint>(0,1000000000); const lint y0 = checked_read<lint>(0,w); const lint x1 = checked_read<lint>(0,1000000000); const lint y1 = checked_read<lint>(0,w); car.source_x = x0; MY_ASSERT(x0 < x1); MY_ASSERT(y0 < y1); car.height = y1-y0; } for(lint i=0; i<n; i++) { Car &car = cars[i]; const lint x0 = checked_read<lint>(0,1000000000); const lint y0 = checked_read<lint>(0,w); const lint x1 = checked_read<lint>(0,1000000000); const lint y1 = checked_read<lint>(0,w); car.target_x = x0; MY_ASSERT(x0 < x1); MY_ASSERT(y0 < y1); MY_ASSERT(car.height == y1-y0); } std::vector<lint> source_order(n); std::vector<lint> target_order(n); for(lint i=0; i<n; i++) { source_order[i] = target_order[i] = i; } merge_sort(source_order, make_compare_index_adaptor(cars, less_source_x)); merge_sort(target_order, make_compare_index_adaptor(cars, less_target_x)); for(lint i=0; i<n; i++) { cars[source_order[i]].source_index = i; cars[target_order[i]].target_index = i; MY_ASSERT(i==0 || cars[source_order[i-1]].source_x<=cars[source_order[i]].source_x); MY_ASSERT(i==0 || cars[target_order[i-1]].target_x<=cars[target_order[i]].target_x); } // this part can be tought as of some cheaty kind of insertion sort Height_tree height_tree(n); for(lint i=0; i<n; i++) { int car_index = source_order[i]; Car &car = cars[car_index]; if(car.height <= w-height_tree.get_highest(car.target_index, n)) { height_tree.put(car.target_index, car.height); } else { std::cout << "NIE\n"; return; } } std::cout << "TAK\n"; } //------------------------------------------------------------------------------ int main() { const int t = checked_read<int>(1, 20); for(int i=0; i<t; i++) process_case(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | /* Author: Dominik Wójt */ /* Problem: Bohater, Potyczki Algorytmiczne 2014 */ #include <iostream> #include <cstdlib> #include <vector> #include <algorithm> #define MY_ASSERT(condition) \ do \ { \ if(!(condition)) \ { \ std::cerr << "assert failed: " << #condition << std::endl; \ std::abort(); \ } \ } \ while(false) //------------------------------------------------------------------------------ typedef long int lint; typedef long int llint; //------------------------------------------------------------------------------ template<typename T> T checked_read(const T min, const T max) { T n; std::cin >> n; MY_ASSERT(std::cin); MY_ASSERT(min<=n && n<=max); return n; } //------------------------------------------------------------------------------ template<typename T> T round_up(const T a, const T b) { return a%b ? a+b : a; } //------------------------------------------------------------------------------ template<typename T> T round_down(const T a, const T b) { return a - a%b; } //------------------------------------------------------------------------------ template<typename T> T divide_ceil(const T a, const T b) { return (a+b-1)/b; } //------------------------------------------------------------------------------ template<typename T> T log2_ceil(const T x) { T base = 1; T log2 = 0; while(x>base) { log2++; base = 1<<log2; } return log2; } //------------------------------------------------------------------------------ template<typename TElement, typename TCompare> void merge_sort(std::vector<TElement> &vector, TCompare compare) { typedef std::vector<TElement> Vector; typedef typename Vector::iterator Iterator; Vector buffer(vector.size()); Iterator source_begin = vector.begin(), source_end = vector.end(); Iterator target_begin = buffer.begin(), target_end = buffer.end(); for(size_t chunk_size=1; chunk_size<vector.size(); chunk_size*=2) { size_t partition_size = round_down(round_up(vector.size(), chunk_size), 2*chunk_size)/2; Iterator target = target_begin; Iterator b0=source_begin, b1=source_begin+partition_size; while(b0 != source_begin+partition_size) { Iterator next_b1 = std::min(b1+chunk_size, source_end); target = std::merge(b0, b0+chunk_size, b1, next_b1, target, compare); b0+=chunk_size; b1=next_b1; } if(target!=target_end) target = std::copy(b1, source_end, target); MY_ASSERT(target==target_end); std::swap(source_begin, target_begin); std::swap(source_end, target_end); } if(source_begin!=vector.begin()) std::swap(vector, buffer); } //------------------------------------------------------------------------------ struct Car { lint source_x; lint target_x; lint source_index; lint target_index; lint height; }; //------------------------------------------------------------------------------ bool less_source_x(const Car &a, const Car &b) { return a.source_x < b.source_x; } //------------------------------------------------------------------------------ bool less_target_x(const Car &a, const Car &b) { return a.target_x < b.target_x; } //------------------------------------------------------------------------------ template<typename TElement, typename TCompare> class Compare_index_adaptor { const std::vector<TElement> &m_vector; TCompare m_compare; public: Compare_index_adaptor(const std::vector<TElement> &vector, TCompare compare) : m_vector(vector), m_compare(compare) { } bool operator()(size_t i, size_t j) { return m_compare(m_vector[i], m_vector[j]); } }; //------------------------------------------------------------------------------ template<typename TElement, typename TCompare> Compare_index_adaptor<TElement, TCompare> make_compare_index_adaptor( const std::vector<TElement> &vector, TCompare compare) { return Compare_index_adaptor<TElement, TCompare>(vector, compare); } //------------------------------------------------------------------------------ class Height_tree { const lint m_n; std::vector<lint> m_data; public: Height_tree(lint n) : m_n(1<<log2_ceil(n)), m_data(2*m_n, 0) { } void put(lint i, lint height) { lint j = i+m_n; m_data[j] = height; j = get_parrent(j); while(j > 0) { if(m_data[j] < m_data[get_left_child(j)]) m_data[j] = m_data[get_left_child(j)]; else if(m_data[j] < m_data[get_right_child(j)]) m_data[j] = m_data[get_right_child(j)]; else break; j = get_parrent(j); } } lint get_highest(lint begin, lint end) { lint top = 0; // naive implementation: //for(lint i=begin; i<end; i++) // top = std::max(top, m_data[m_n+i]); lint i = begin; while(i < end) { lint range = 1; lint j = i+m_n; while(is_left_child(j) && i+range*2<end) { j = get_parrent(j); range *= 2; MY_ASSERT(j > 0); } top = std::max(top, m_data[j]); i+=range; } return top; } private: lint get_parrent(lint j) { return j/2; } lint get_left_child(lint j) { return j*2; } lint get_right_child(lint j) { return j*2 + 1; } bool is_left_child(lint j) { return j%2 == 0; } }; //------------------------------------------------------------------------------ void process_case() { const lint n = checked_read<lint>(1, 50000); const lint w = checked_read<lint>(1, 1000000000); std::vector<Car> cars(n); for(lint i=0; i<n; i++) { Car &car = cars[i]; const lint x0 = checked_read<lint>(0,1000000000); const lint y0 = checked_read<lint>(0,w); const lint x1 = checked_read<lint>(0,1000000000); const lint y1 = checked_read<lint>(0,w); car.source_x = x0; MY_ASSERT(x0 < x1); MY_ASSERT(y0 < y1); car.height = y1-y0; } for(lint i=0; i<n; i++) { Car &car = cars[i]; const lint x0 = checked_read<lint>(0,1000000000); const lint y0 = checked_read<lint>(0,w); const lint x1 = checked_read<lint>(0,1000000000); const lint y1 = checked_read<lint>(0,w); car.target_x = x0; MY_ASSERT(x0 < x1); MY_ASSERT(y0 < y1); MY_ASSERT(car.height == y1-y0); } std::vector<lint> source_order(n); std::vector<lint> target_order(n); for(lint i=0; i<n; i++) { source_order[i] = target_order[i] = i; } merge_sort(source_order, make_compare_index_adaptor(cars, less_source_x)); merge_sort(target_order, make_compare_index_adaptor(cars, less_target_x)); for(lint i=0; i<n; i++) { cars[source_order[i]].source_index = i; cars[target_order[i]].target_index = i; MY_ASSERT(i==0 || cars[source_order[i-1]].source_x<=cars[source_order[i]].source_x); MY_ASSERT(i==0 || cars[target_order[i-1]].target_x<=cars[target_order[i]].target_x); } // this part can be tought as of some cheaty kind of insertion sort Height_tree height_tree(n); for(lint i=0; i<n; i++) { int car_index = source_order[i]; Car &car = cars[car_index]; if(car.height <= w-height_tree.get_highest(car.target_index, n)) { height_tree.put(car.target_index, car.height); } else { std::cout << "NIE\n"; return; } } std::cout << "TAK\n"; } //------------------------------------------------------------------------------ int main() { const int t = checked_read<int>(1, 20); for(int i=0; i<t; i++) process_case(); return 0; } |