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 #include using namespace std; const int MAXN = 100010; const int MAXK = 1000010; vector graphIn[MAXK], graphOut[MAXK]; map row[MAXN], col[MAXN]; using ll = long long int; unordered_map f; //mapa ze wpółrzędnych na indeks inline ll key(int x, int y) { return ((ll)x << 32) | y; } bool connA[MAXK]; //connA[x] - istnieje ścieżka z dolnego brzegu do pola x bool connB[MAXK]; //connB[x] - istnieje ścieżka do górnego brzegu z pola x void DFSA(int x) { connA[x] = true; for (int v : graphOut[x]) if (!connA[v]) DFSA(v); } void DFSB(int x) { connB[x] = true; for (int v : graphIn[x]) if (!connB[v]) DFSB(v); } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); int x = 0; for (int $= 1;$ <= k; \$++) { int r, c, z; scanf("%d%d%d", &r, &c, &z); r = (r ^ x) % n; c = (c ^ x) % m; bool connectToA = false; bool connectToB = false; vector newEdgesIn; vector newEdgesOut; for (int rr = max(0, r - 1); rr <= min(n - 1, r + 1); rr++) { for (int cc = max(0, c - 1); cc <= min(m - 1, c + 1); cc++) { if (rr != r || cc != c) { auto it = f.find(key(rr, cc)); if (it != f.end()) { newEdgesIn.push_back(it->second); newEdgesOut.push_back(it->second); if (connA[it->second]) connectToA = true; if (connB[it->second]) connectToB = true; } } } } if (r == n - 1 || c == 0) { connectToA = true; } if (r == 0 || c == m - 1) { connectToB = true; } if (r > 0) { auto it = row[r].lower_bound(c); if (it != row[r].begin()) { it--; newEdgesOut.push_back(it->second); if (connB[it->second]) connectToB = true; } } if (r < n - 1) { auto it = row[r + 1].upper_bound(c); if (it != row[r + 1].end()) { newEdgesIn.push_back(it->second); if (connA[it->second]) connectToA = true; } } if (c > 0) { auto it = col[c].lower_bound(r); if (it != col[c].begin()) { it--; newEdgesIn.push_back(it->second); if (connA[it->second]) connectToA = true; } } if (c < m - 1) { auto it = col[c + 1].upper_bound(r); if (it != col[c + 1].end()) { newEdgesOut.push_back(it->second); if (connB[it->second]) connectToB = true; } } if (connectToA && connectToB) { puts("TAK"); x ^= z; } else { puts("NIE"); int id = f.size() + 1; f.insert(make_pair(key(r, c), id)); row[r].insert(make_pair(c, id)); row[r + 1].insert(make_pair(c, id)); col[c].insert(make_pair(r, id)); col[c + 1].insert(make_pair(r, id)); for (int x : newEdgesIn) { graphOut[x].push_back(id); graphIn[id].push_back(x); } for (int x : newEdgesOut) { graphIn[x].push_back(id); graphOut[id].push_back(x); } if (connectToA) DFSA(id); if (connectToB) DFSB(id); } } }