#include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define MAX(dst,src) dst = max(dst, (src)) #define MIN(dst,src) dst = min(dst, (src)) #define pb push_back #define mp make_pair #define st first #define nd second bool debug = true; /** * Intervals are left_bound-closed and right_bound-open: [L, R) */ template<typename Config> class bit { template<typename TConfig = Config> struct range_updates_config { template<class U> static char (&test(typename U::TRangeUpdate const*))[1]; template<class U> static char (&test(...))[2]; template<class U = TConfig> constexpr static typename U::TRangeUpdate _neutral(typename U::TRangeUpdate const*) { return U::neutral_range_update(); } template<class U = TConfig> constexpr static void* _neutral(...) { return 0; } static const bool enabled = (sizeof(test<TConfig>(0)) == 1); typedef decltype(_neutral<TConfig>(0)) Type; constexpr static Type neutral() { return _neutral<TConfig>(0); } }; typedef typename Config::TData TData; typedef typename range_updates_config<Config>::Type TRangeUpdate; typedef const function<bool(const TData&)>& Predicate; int size; /** Interval represented by the node */ vector<pair<int, int>> bounds; vector<TData> data; vector<TRangeUpdate> range_updates; bool __intersects(int L, int R, int idx) { return bounds[idx].first < R && L < bounds[idx].second; } bool __covers(int L, int R, int idx) { return L <= bounds[idx].first && bounds[idx].second <= R; } void __update_range_single(int idx, const TRangeUpdate& op) { Config::apply(op, data[idx], bounds[idx].first, bounds[idx].second); Config::compose_range_updates(op, range_updates[idx]); } template<class T = Config, typename enable_if<!range_updates_config<T>::enabled, int>::type = 0> void __push_range_update(int idx) {} template<class T = Config, typename enable_if<range_updates_config<T>::enabled, int>::type = 0> void __push_range_update(int idx) { if (idx < size) { __update_range_single(2*idx, range_updates[idx]); __update_range_single(2*idx + 1, range_updates[idx]); range_updates[idx] = Config::neutral_range_update(); } } void __update_range(int L, int R, const TRangeUpdate& op, int idx) { if (!__intersects(L, R, idx)) { return; } if (__covers(L, R, idx)) { __update_range_single(idx, op); return; } __push_range_update(idx); __update_range(L, R, op, 2*idx); __update_range(L, R, op, 2*idx+1); data[idx] = Config::merge(data[2*idx], data[2*idx+1]); } TData __query_range(int L, int R, int idx) { if (!__intersects(L, R, idx)) { return Config::neutral(); } if (__covers(L, R, idx)) { return data[idx]; } __push_range_update(idx); return Config::merge( __query_range(L, R, 2*idx), __query_range(L, R, 2*idx+1) ); } /** * If last=1, the last matching is returned. If last=0, the first one. */ int __find(int L, int R, Predicate fn, int idx, int last) { if (!__intersects(L, R, idx) || !fn(data[idx])) { return -1; } if (idx >= size) { return idx - size; } __push_range_update(idx); int preferred = __find(L, R, fn, 2*idx+last, last); if (preferred != -1) { return preferred; } return __find(L, R, fn, 2*idx+(last^1), last); } public: bit(int _size = 0) { size = 1; while (size < _size) { size <<= 1; } data = vector<TData>(2*size, Config::neutral()); range_updates = vector<TRangeUpdate>(2*size, range_updates_config<Config>::neutral()); bounds = vector<pair<int, int>>(2*size); for (int i = 0; i < size; ++i) { bounds[i+size] = {i, i+1}; } for (int i = size - 1; i >= 0; --i) { bounds[i] = {bounds[2*i].first, bounds[2*i+1].second}; } } void update_range(int L, int R, const TRangeUpdate& op) { __update_range(L, R, op, 1); } void update_single(int pos, const TRangeUpdate& op) { update_range(pos, pos+1, op); } TData query_range(int L, int R) { return __query_range(L, R, 1); } TData query_single(int pos) { return query_range(pos, pos+1); } void set(int pos, TData value) { int idx = size + pos; if (range_updates_config<Config>::enabled) { idx = 1; while (idx < size) { __push_range_update(idx); idx = 2*idx + (pos >= bounds[2*idx+1].first); } } // Push pending operations data[idx] = value; idx >>= 1; while (idx > 0) { data[idx] = Config::merge(data[2*idx], data[2*idx+1]); idx >>= 1; } } /** @returns -1 if no element found */ int first_which(int L, int R, Predicate contain_check) { return __find(L, R, contain_check, 1, 0); } int first_which(Predicate contain_check) { return first_which(0, size, contain_check); } /** @returns -1 if no element found */ int last_which(int L, int R, Predicate contain_check) { return __find(L, R, contain_check, 1, 1); } int last_which(Predicate contain_check) { return last_which(0, size, contain_check); } }; struct bit_config { typedef int TData; static TData neutral() { return TData(); } static TData merge(const TData& left, const TData& right) { return max(left, right); } typedef int TRangeUpdate; static void apply(const TRangeUpdate& op, TData& value, int A, int B) { value = max(value, op); } static void compose_range_updates(const TRangeUpdate& outer, TRangeUpdate& inner) { inner = max(inner, outer); } static TRangeUpdate neutral_range_update() { return TRangeUpdate(); } }; struct Fort { int r, c; }; int N, M; struct Path { bit<bit_config> route; priority_queue<int> in_row[111111]; priority_queue<int, vector<int>, greater<int>> in_column[111111]; Path(): route(111111) {} bool collides(Fort f) { int L = f.c == 0 ? 0 : route.query_single(f.c - 1); return L <= f.r && route.query_single(f.c) >= f.r; } void build(Fort newFort) { queue<Fort> colliding; if (collides(newFort)) { colliding.push(newFort); } else { in_row[newFort.r].push(newFort.c); in_column[newFort.c].push(newFort.r); } while (!colliding.empty()) { auto fort = colliding.front(); colliding.pop(); if (route.query_single(fort.c-1) >= fort.r+1) continue; route.update_range(fort.c-1,M,fort.r+1); { int c = fort.c-1; int r = -1; while (!in_column[c].empty() && in_column[c].top() <= fort.r+1) { r = in_column[c].top(); in_column[c].pop(); } if (r > -1) colliding.push({r,c}); } { int r = fort.r+1; auto c = M; while (!in_row[r].empty() && in_row[r].top() >= fort.c-1) { c = in_row[r].top(); in_row[r].pop(); } if (c < M && c != fort.c-1) colliding.push({r,c}); } } } } upper, lower; bool build(int r, int c) { Fort upperFort = { r, c }; Fort lowerFort = { N - 1 - r, M - 1 - c}; bool upperCollides = upper.collides(upperFort), lowerCollides = lower.collides(lowerFort); if (upperCollides && lowerCollides) return true; upper.build(upperFort); lower.build(lowerFort); return false; } int main() { int K; scanf("%d%d%d", &N, &M, &K); upper.route.set(M-1,N-1); lower.route.set(M-1,N-1); int x = 0; REP(i,K) { int r, c, z; scanf("%d%d%d", &r, &c, &z); bool destroyed = build((r^x)%N,(c^x)%M); printf(destroyed ? "TAK\n" : "NIE\n"); if (destroyed) x ^= z; } }
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | #include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define MAX(dst,src) dst = max(dst, (src)) #define MIN(dst,src) dst = min(dst, (src)) #define pb push_back #define mp make_pair #define st first #define nd second bool debug = true; /** * Intervals are left_bound-closed and right_bound-open: [L, R) */ template<typename Config> class bit { template<typename TConfig = Config> struct range_updates_config { template<class U> static char (&test(typename U::TRangeUpdate const*))[1]; template<class U> static char (&test(...))[2]; template<class U = TConfig> constexpr static typename U::TRangeUpdate _neutral(typename U::TRangeUpdate const*) { return U::neutral_range_update(); } template<class U = TConfig> constexpr static void* _neutral(...) { return 0; } static const bool enabled = (sizeof(test<TConfig>(0)) == 1); typedef decltype(_neutral<TConfig>(0)) Type; constexpr static Type neutral() { return _neutral<TConfig>(0); } }; typedef typename Config::TData TData; typedef typename range_updates_config<Config>::Type TRangeUpdate; typedef const function<bool(const TData&)>& Predicate; int size; /** Interval represented by the node */ vector<pair<int, int>> bounds; vector<TData> data; vector<TRangeUpdate> range_updates; bool __intersects(int L, int R, int idx) { return bounds[idx].first < R && L < bounds[idx].second; } bool __covers(int L, int R, int idx) { return L <= bounds[idx].first && bounds[idx].second <= R; } void __update_range_single(int idx, const TRangeUpdate& op) { Config::apply(op, data[idx], bounds[idx].first, bounds[idx].second); Config::compose_range_updates(op, range_updates[idx]); } template<class T = Config, typename enable_if<!range_updates_config<T>::enabled, int>::type = 0> void __push_range_update(int idx) {} template<class T = Config, typename enable_if<range_updates_config<T>::enabled, int>::type = 0> void __push_range_update(int idx) { if (idx < size) { __update_range_single(2*idx, range_updates[idx]); __update_range_single(2*idx + 1, range_updates[idx]); range_updates[idx] = Config::neutral_range_update(); } } void __update_range(int L, int R, const TRangeUpdate& op, int idx) { if (!__intersects(L, R, idx)) { return; } if (__covers(L, R, idx)) { __update_range_single(idx, op); return; } __push_range_update(idx); __update_range(L, R, op, 2*idx); __update_range(L, R, op, 2*idx+1); data[idx] = Config::merge(data[2*idx], data[2*idx+1]); } TData __query_range(int L, int R, int idx) { if (!__intersects(L, R, idx)) { return Config::neutral(); } if (__covers(L, R, idx)) { return data[idx]; } __push_range_update(idx); return Config::merge( __query_range(L, R, 2*idx), __query_range(L, R, 2*idx+1) ); } /** * If last=1, the last matching is returned. If last=0, the first one. */ int __find(int L, int R, Predicate fn, int idx, int last) { if (!__intersects(L, R, idx) || !fn(data[idx])) { return -1; } if (idx >= size) { return idx - size; } __push_range_update(idx); int preferred = __find(L, R, fn, 2*idx+last, last); if (preferred != -1) { return preferred; } return __find(L, R, fn, 2*idx+(last^1), last); } public: bit(int _size = 0) { size = 1; while (size < _size) { size <<= 1; } data = vector<TData>(2*size, Config::neutral()); range_updates = vector<TRangeUpdate>(2*size, range_updates_config<Config>::neutral()); bounds = vector<pair<int, int>>(2*size); for (int i = 0; i < size; ++i) { bounds[i+size] = {i, i+1}; } for (int i = size - 1; i >= 0; --i) { bounds[i] = {bounds[2*i].first, bounds[2*i+1].second}; } } void update_range(int L, int R, const TRangeUpdate& op) { __update_range(L, R, op, 1); } void update_single(int pos, const TRangeUpdate& op) { update_range(pos, pos+1, op); } TData query_range(int L, int R) { return __query_range(L, R, 1); } TData query_single(int pos) { return query_range(pos, pos+1); } void set(int pos, TData value) { int idx = size + pos; if (range_updates_config<Config>::enabled) { idx = 1; while (idx < size) { __push_range_update(idx); idx = 2*idx + (pos >= bounds[2*idx+1].first); } } // Push pending operations data[idx] = value; idx >>= 1; while (idx > 0) { data[idx] = Config::merge(data[2*idx], data[2*idx+1]); idx >>= 1; } } /** @returns -1 if no element found */ int first_which(int L, int R, Predicate contain_check) { return __find(L, R, contain_check, 1, 0); } int first_which(Predicate contain_check) { return first_which(0, size, contain_check); } /** @returns -1 if no element found */ int last_which(int L, int R, Predicate contain_check) { return __find(L, R, contain_check, 1, 1); } int last_which(Predicate contain_check) { return last_which(0, size, contain_check); } }; struct bit_config { typedef int TData; static TData neutral() { return TData(); } static TData merge(const TData& left, const TData& right) { return max(left, right); } typedef int TRangeUpdate; static void apply(const TRangeUpdate& op, TData& value, int A, int B) { value = max(value, op); } static void compose_range_updates(const TRangeUpdate& outer, TRangeUpdate& inner) { inner = max(inner, outer); } static TRangeUpdate neutral_range_update() { return TRangeUpdate(); } }; struct Fort { int r, c; }; int N, M; struct Path { bit<bit_config> route; priority_queue<int> in_row[111111]; priority_queue<int, vector<int>, greater<int>> in_column[111111]; Path(): route(111111) {} bool collides(Fort f) { int L = f.c == 0 ? 0 : route.query_single(f.c - 1); return L <= f.r && route.query_single(f.c) >= f.r; } void build(Fort newFort) { queue<Fort> colliding; if (collides(newFort)) { colliding.push(newFort); } else { in_row[newFort.r].push(newFort.c); in_column[newFort.c].push(newFort.r); } while (!colliding.empty()) { auto fort = colliding.front(); colliding.pop(); if (route.query_single(fort.c-1) >= fort.r+1) continue; route.update_range(fort.c-1,M,fort.r+1); { int c = fort.c-1; int r = -1; while (!in_column[c].empty() && in_column[c].top() <= fort.r+1) { r = in_column[c].top(); in_column[c].pop(); } if (r > -1) colliding.push({r,c}); } { int r = fort.r+1; auto c = M; while (!in_row[r].empty() && in_row[r].top() >= fort.c-1) { c = in_row[r].top(); in_row[r].pop(); } if (c < M && c != fort.c-1) colliding.push({r,c}); } } } } upper, lower; bool build(int r, int c) { Fort upperFort = { r, c }; Fort lowerFort = { N - 1 - r, M - 1 - c}; bool upperCollides = upper.collides(upperFort), lowerCollides = lower.collides(lowerFort); if (upperCollides && lowerCollides) return true; upper.build(upperFort); lower.build(lowerFort); return false; } int main() { int K; scanf("%d%d%d", &N, &M, &K); upper.route.set(M-1,N-1); lower.route.set(M-1,N-1); int x = 0; REP(i,K) { int r, c, z; scanf("%d%d%d", &r, &c, &z); bool destroyed = build((r^x)%N,(c^x)%M); printf(destroyed ? "TAK\n" : "NIE\n"); if (destroyed) x ^= z; } } |