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 #include #include #include int n, m; struct pt { int x, y; }; bool operator==(pt p, pt q) { return p.x == q.x && p.y == q.y; } struct Cmp { bool operator()(pt p, pt q) const { return p.x + p.y < q.x + q.y; }; }; std::set upper, lower; std::set ver[100001]; std::set hor[100001]; std::vector sack; bool on_lower(pt p) { auto next = lower.lower_bound(p); auto prev = next; prev--; if (p.y == 0) return p.x <= lower.begin()->x; if (p.x == n - 1) return p.y >= lower.rbegin()->y; if (*next == p) return true; if (p.x == prev->x && p.y <= next->y) return true; if (p.y == next->y && p.x >= prev->x) return true; return false; } bool on_upper(pt p) { auto next = upper.lower_bound(p); auto prev = next; prev--; if (p.x == 0) return p.y <= upper.begin()->y; if (p.y == m - 1) { //printf("((%d %d))\n", upper.rbegin()->x, p.x); return p.x >= upper.rbegin()->x; } if (*next == p) return true; if (p.y == prev->y && p.x <= next->x) return true; if (p.x == next->x && p.y >= prev->y) return true; return false; } void put_lower(pt p) { auto next = lower.lower_bound(p); auto prev = next; prev--; if (*next == p) { auto it = next; next++; lower.erase(it); } if (prev->y + 1 == p.y) lower.erase(prev); if (next->x - 1 == p.x) lower.erase(next); p.x++; p.y--; while (!hor[p.y].empty() && *hor[p.y].begin() <= p.x) { sack.push_back(pt{*hor[p.y].begin(), p.y}); hor[p.y].erase(hor[p.y].begin()); } while (!ver[p.x].empty() && *ver[p.x].rbegin() >= p.y) { sack.push_back(pt{p.x, *ver[p.x].rbegin()}); ver[p.x].erase(*ver[p.x].rbegin()); } lower.insert(p); } void put_upper(pt p) { auto next = upper.lower_bound(p); auto prev = next; prev--; if (*next == p) { auto it = next; next++; upper.erase(it); } if (prev->x + 1 == p.x) upper.erase(prev); if (next->y - 1 == p.y) upper.erase(next); p.x--; p.y++; while (!hor[p.y].empty() && *hor[p.y].rbegin() >= p.x) { sack.push_back(pt{*hor[p.y].rbegin(), p.y}); hor[p.y].erase(*hor[p.y].rbegin()); } while (!ver[p.x].empty() && *ver[p.x].begin() <= p.y) { sack.push_back(pt{p.x, *ver[p.x].begin()}); ver[p.x].erase(ver[p.x].begin()); } upper.insert(p); } bool put(pt p) { bool l = on_lower(p); bool u = on_upper(p); //printf("l = %d, u = %d\n", l, u); if (l && u) return false; if (!l && !u) { ver[p.x].insert(p.y); hor[p.y].insert(p.x); return true; } if (l) put_lower(p); if (u) put_upper(p); while (!sack.empty()) { p = sack.back(); sack.pop_back(); //printf("sack: %d %d\n", p.x, p.y); l = on_lower(p); u = on_upper(p); if (l) put_lower(p); if (u) put_upper(p); } return true; } void print(std::set s) { for (auto p : s) printf("[%d %d] ", p.x, p.y); puts(""); } int main() { int k; scanf("%d%d%d", &n, &m, &k); int c = 0; upper.insert({0, 0}); upper.insert({n-1, m-1}); lower.insert({0, 0}); lower.insert({n-1, m-1}); for (int i = 0; i < k; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); x = (x ^ c) % n; y = (y ^ c) % m; //printf("x = %d, y = %d\n", x, y); if (put(pt{x, y})) { puts("NIE"); } else { puts("TAK"); c ^= z; //printf("c = %d\n", c); } //print(lower); //print(upper); } }