#include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif // end of templates struct Tree { int n; vector<int> t; Tree(int m) { n = m + 1; t.resize(m + 1, -1); } void set_mx(int pos, int val) { ++pos; while(pos > 0) { t[pos] = max(t[pos], val); pos -= pos & (-pos); } } int get(int pos) { ++pos; int ans = -1; while(pos < n) { ans = max(ans, t[pos]); pos += pos & (-pos); } return ans; } }; int n, m; constexpr int sides = 4; struct Solve { Tree prefix_length_left, prefix_length_right; vector<set<int>> alone_column, alone_row; Solve() : prefix_length_left(m), prefix_length_right(m), alone_column(n), alone_row(m) { } int get_last_tree_x(int type, int y) { if(type == +1) return prefix_length_left.get(y); else if(type == -1) return n - 1 - prefix_length_right.get(m - 1 - y); assert(false); } bool valid_xy(int x, int y) { return x >= 0 and y >= 0 and x < n and y < m; } bool inside_tree(int x, int y, int type = 0) { if(not valid_xy(x, y)) return true; bool l = (x <= get_last_tree_x(+1, y)), r = (x >= get_last_tree_x(-1, y)); if(type == 0) return l or r; else if(type == +1) return l; else if(type == -1) return r; assert(false); } int closest(int type, set<int> &s) { if(type == +1) return *s.begin(); else if(type == -1) return *prev(s.end()); assert(false); } int try_remove_alone(int type, int x, int y) { auto rm_alone = [&] { alone_column[x].erase(alone_column[x].find(y)); alone_row[y].erase(alone_row[y].find(x)); }; #define solvedebug debug() << "[" << max(0, type) << "] " solvedebug << "trying to remove " << make_pair(x, y); if(inside_tree(x, y, type)) { rm_alone(); solvedebug << "nvm, already in tree"; return -1; } if(inside_tree(x - type, y - type, type)) { solvedebug << "time for reduction!"; rm_alone(); if(type == +1) prefix_length_left.set_mx(y, x); else if(type == -1) prefix_length_right.set_mx(m - 1 - y, n - 1 - x); while(valid_xy(x + type, y) and size(alone_column[x + type])) { int found = closest(type, alone_column[x + type]); solvedebug << "lets try on the L/R " << make_pair(x + type, found); if(try_remove_alone(type, x + type, found) == 0) break; } while(valid_xy(x, y + type) and size(alone_row[y + type])) { int found = closest(type, alone_row[y + type]); solvedebug << "lets try on the U/D " << make_pair(found, y + type); if(try_remove_alone(type, found, y + type) == 0) break; } solvedebug << "finished reduction of " << make_pair(x, y); return 1; } solvedebug << "nothing interesting"; return 0; } void add(int x, int y) { alone_column[x].emplace(y); alone_row[y].emplace(x); try_remove_alone(+1, x, y); try_remove_alone(-1, x, y); } bool can_add(int x, int y) { if(inside_tree(x - 1, y - 1, +1) and inside_tree(x + 1, y + 1, -1)) return false; return true; } }; /* ostream& operator<<(ostream &os, Solve &s) { os << "board:\n"; for(int y = m - 1; y >= 0; --y) { for(int x = 0; x < n; ++x) { if(s.inside_tree(x, y)) os << 'T'; else if(s.alone_row[y].count(x) == 1) os << 'A'; else os << '-'; } os << '\n'; } return os; } */ int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> m >> k; #ifdef LOCAL set<pair<int, int>> already_considered; #endif Solve solve; int X = 0; while(k --> 0) { int r, c, z; cin >> r >> c >> z; int x = (r ^ X) % n, y = (c ^ X) % m; y = m - 1 - y; debug() << "considering " << make_pair(x, y); #ifdef LOCAL if((x == 0 and y == m - 1) or (x == n - 1 and y == 0)) { debug() << "ignoring (start/end)"; cout << "-\n"; continue; } if(already_considered.count(make_pair(x, y)) == 1) { debug() << "ignoring (already considered)"; cout << "-\n"; continue; } already_considered.emplace(x, y); #endif if(solve.can_add(x, y)) { cout << "NIE\n"; solve.add(x, y); } else { cout << "TAK\n"; X ^= z; } // debug(solve); } }
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 | #include <bits/stdc++.h> // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif // end of templates struct Tree { int n; vector<int> t; Tree(int m) { n = m + 1; t.resize(m + 1, -1); } void set_mx(int pos, int val) { ++pos; while(pos > 0) { t[pos] = max(t[pos], val); pos -= pos & (-pos); } } int get(int pos) { ++pos; int ans = -1; while(pos < n) { ans = max(ans, t[pos]); pos += pos & (-pos); } return ans; } }; int n, m; constexpr int sides = 4; struct Solve { Tree prefix_length_left, prefix_length_right; vector<set<int>> alone_column, alone_row; Solve() : prefix_length_left(m), prefix_length_right(m), alone_column(n), alone_row(m) { } int get_last_tree_x(int type, int y) { if(type == +1) return prefix_length_left.get(y); else if(type == -1) return n - 1 - prefix_length_right.get(m - 1 - y); assert(false); } bool valid_xy(int x, int y) { return x >= 0 and y >= 0 and x < n and y < m; } bool inside_tree(int x, int y, int type = 0) { if(not valid_xy(x, y)) return true; bool l = (x <= get_last_tree_x(+1, y)), r = (x >= get_last_tree_x(-1, y)); if(type == 0) return l or r; else if(type == +1) return l; else if(type == -1) return r; assert(false); } int closest(int type, set<int> &s) { if(type == +1) return *s.begin(); else if(type == -1) return *prev(s.end()); assert(false); } int try_remove_alone(int type, int x, int y) { auto rm_alone = [&] { alone_column[x].erase(alone_column[x].find(y)); alone_row[y].erase(alone_row[y].find(x)); }; #define solvedebug debug() << "[" << max(0, type) << "] " solvedebug << "trying to remove " << make_pair(x, y); if(inside_tree(x, y, type)) { rm_alone(); solvedebug << "nvm, already in tree"; return -1; } if(inside_tree(x - type, y - type, type)) { solvedebug << "time for reduction!"; rm_alone(); if(type == +1) prefix_length_left.set_mx(y, x); else if(type == -1) prefix_length_right.set_mx(m - 1 - y, n - 1 - x); while(valid_xy(x + type, y) and size(alone_column[x + type])) { int found = closest(type, alone_column[x + type]); solvedebug << "lets try on the L/R " << make_pair(x + type, found); if(try_remove_alone(type, x + type, found) == 0) break; } while(valid_xy(x, y + type) and size(alone_row[y + type])) { int found = closest(type, alone_row[y + type]); solvedebug << "lets try on the U/D " << make_pair(found, y + type); if(try_remove_alone(type, found, y + type) == 0) break; } solvedebug << "finished reduction of " << make_pair(x, y); return 1; } solvedebug << "nothing interesting"; return 0; } void add(int x, int y) { alone_column[x].emplace(y); alone_row[y].emplace(x); try_remove_alone(+1, x, y); try_remove_alone(-1, x, y); } bool can_add(int x, int y) { if(inside_tree(x - 1, y - 1, +1) and inside_tree(x + 1, y + 1, -1)) return false; return true; } }; /* ostream& operator<<(ostream &os, Solve &s) { os << "board:\n"; for(int y = m - 1; y >= 0; --y) { for(int x = 0; x < n; ++x) { if(s.inside_tree(x, y)) os << 'T'; else if(s.alone_row[y].count(x) == 1) os << 'A'; else os << '-'; } os << '\n'; } return os; } */ int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> m >> k; #ifdef LOCAL set<pair<int, int>> already_considered; #endif Solve solve; int X = 0; while(k --> 0) { int r, c, z; cin >> r >> c >> z; int x = (r ^ X) % n, y = (c ^ X) % m; y = m - 1 - y; debug() << "considering " << make_pair(x, y); #ifdef LOCAL if((x == 0 and y == m - 1) or (x == n - 1 and y == 0)) { debug() << "ignoring (start/end)"; cout << "-\n"; continue; } if(already_considered.count(make_pair(x, y)) == 1) { debug() << "ignoring (already considered)"; cout << "-\n"; continue; } already_considered.emplace(x, y); #endif if(solve.can_add(x, y)) { cout << "NIE\n"; solve.add(x, y); } else { cout << "TAK\n"; X ^= z; } // debug(solve); } } |