#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define PII pair <int, int> const int N = 1e5 + 7; int n, m, q; set <int> S[2][N]; map <PII, int> M; int First(int t, int x, int y){ auto it = S[t][x].lower_bound(y); if(t) return M[{*it, x}]; return M[{x, *it}]; } int Last(int t, int x, int y){ auto it = S[t][x].upper_bound(y); --it; if(t) return M[{*it, x}]; return M[{x, *it}]; } int getMask(int x, int y){ int mask = 0; for(int dx = -1; dx <= 1; ++dx) for(int dy = -1; dy <= 1; ++dy){ int nx = x + dx, ny = y + dy; if(M.count({nx, ny})){ mask |= M[{nx, ny}]; // printf("%d %d -> %d\n", nx, ny, M[{nx, ny}]); } } mask |= Last(1, y - 1, x + 1) & 2; mask |= First(0, x + 1, y - 1) & 2; mask |= Last(0, x - 1, y + 1) & 1; mask |= First(1, y + 1, x - 1) & 1; return mask; } bool bad(int x, int y){ return getMask(x, y) == 3; } void propagate1(int x, int y){ M[{x, y}] = 1; for(int dx = -1; dx <= 1; ++dx) for(int dy = -1; dy <= 1; ++dy){ int nx = x + dx, ny = y + dy; if(M.count({nx, ny}) && M[{nx, ny}] == 0) propagate1(nx, ny); } auto it = S[0][x].upper_bound(y); if(M[{x, *it}] == 0) propagate1(x, *it); it = S[1][y].find(x); --it; if(M[{*it, y}] == 0) propagate1(*it, y); it = S[0][x + 1].lower_bound(y - 1); if(M[{x + 1, *it}] == 0) propagate1(x + 1, *it); it = S[1][y - 1].upper_bound(x + 1); --it; if(M[{*it, y - 1}] == 0) propagate1(*it, y - 1); } void propagate2(int x, int y){ M[{x, y}] = 2; for(int dx = -1; dx <= 1; ++dx) for(int dy = -1; dy <= 1; ++dy){ int nx = x + dx, ny = y + dy; if(M.count({nx, ny}) && M[{nx, ny}] == 0) propagate2(nx, ny); } auto it = S[1][y].upper_bound(x); if(M[{*it, y}] == 0) propagate2(*it, y); it = S[0][x].find(y); --it; if(M[{x, *it}] == 0) propagate2(x, *it); it = S[1][y + 1].lower_bound(x - 1); if(M[{*it, y + 1}] == 0) propagate2(*it, y + 1); it = S[0][x - 1].upper_bound(y + 1); --it; if(M[{x - 1, *it}] == 0) propagate2(x - 1, *it); } void add(int x, int y){ S[0][x].insert(y); S[1][y].insert(x); int mask = getMask(x, y); M[{x, y}] = mask; // printf("%d %d -> %d\n", x, y, mask); if(mask == 1) propagate1(x, y); else if(mask == 2) propagate2(x, y); } int main(){ scanf("%d %d %d", &n, &m, &q); for(int i = 1; i <= n; ++i){ S[0][i].insert(0); S[0][i].insert(m + 1); S[1][0].insert(i); S[1][m + 1].insert(i); M[{i, m + 1}] = 1; M[{i, 0}] = 2; } for(int i = 1; i <= m; ++i){ S[0][0].insert(i); S[0][n + 1].insert(i); S[1][i].insert(0); S[1][i].insert(n + 1); M[{0, i}] = 1; M[{n + 1, i}] = 2; } int last = 0; while(q--){ int r, c, z; scanf("%d %d %d", &r, &c, &z); r = (r ^ last) % n + 1; c = (c ^ last) % m + 1; if(bad(r, c)){ last ^= z; puts("TAK"); } else{ add(r, c); puts("NIE"); } } 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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define PII pair <int, int> const int N = 1e5 + 7; int n, m, q; set <int> S[2][N]; map <PII, int> M; int First(int t, int x, int y){ auto it = S[t][x].lower_bound(y); if(t) return M[{*it, x}]; return M[{x, *it}]; } int Last(int t, int x, int y){ auto it = S[t][x].upper_bound(y); --it; if(t) return M[{*it, x}]; return M[{x, *it}]; } int getMask(int x, int y){ int mask = 0; for(int dx = -1; dx <= 1; ++dx) for(int dy = -1; dy <= 1; ++dy){ int nx = x + dx, ny = y + dy; if(M.count({nx, ny})){ mask |= M[{nx, ny}]; // printf("%d %d -> %d\n", nx, ny, M[{nx, ny}]); } } mask |= Last(1, y - 1, x + 1) & 2; mask |= First(0, x + 1, y - 1) & 2; mask |= Last(0, x - 1, y + 1) & 1; mask |= First(1, y + 1, x - 1) & 1; return mask; } bool bad(int x, int y){ return getMask(x, y) == 3; } void propagate1(int x, int y){ M[{x, y}] = 1; for(int dx = -1; dx <= 1; ++dx) for(int dy = -1; dy <= 1; ++dy){ int nx = x + dx, ny = y + dy; if(M.count({nx, ny}) && M[{nx, ny}] == 0) propagate1(nx, ny); } auto it = S[0][x].upper_bound(y); if(M[{x, *it}] == 0) propagate1(x, *it); it = S[1][y].find(x); --it; if(M[{*it, y}] == 0) propagate1(*it, y); it = S[0][x + 1].lower_bound(y - 1); if(M[{x + 1, *it}] == 0) propagate1(x + 1, *it); it = S[1][y - 1].upper_bound(x + 1); --it; if(M[{*it, y - 1}] == 0) propagate1(*it, y - 1); } void propagate2(int x, int y){ M[{x, y}] = 2; for(int dx = -1; dx <= 1; ++dx) for(int dy = -1; dy <= 1; ++dy){ int nx = x + dx, ny = y + dy; if(M.count({nx, ny}) && M[{nx, ny}] == 0) propagate2(nx, ny); } auto it = S[1][y].upper_bound(x); if(M[{*it, y}] == 0) propagate2(*it, y); it = S[0][x].find(y); --it; if(M[{x, *it}] == 0) propagate2(x, *it); it = S[1][y + 1].lower_bound(x - 1); if(M[{*it, y + 1}] == 0) propagate2(*it, y + 1); it = S[0][x - 1].upper_bound(y + 1); --it; if(M[{x - 1, *it}] == 0) propagate2(x - 1, *it); } void add(int x, int y){ S[0][x].insert(y); S[1][y].insert(x); int mask = getMask(x, y); M[{x, y}] = mask; // printf("%d %d -> %d\n", x, y, mask); if(mask == 1) propagate1(x, y); else if(mask == 2) propagate2(x, y); } int main(){ scanf("%d %d %d", &n, &m, &q); for(int i = 1; i <= n; ++i){ S[0][i].insert(0); S[0][i].insert(m + 1); S[1][0].insert(i); S[1][m + 1].insert(i); M[{i, m + 1}] = 1; M[{i, 0}] = 2; } for(int i = 1; i <= m; ++i){ S[0][0].insert(i); S[0][n + 1].insert(i); S[1][i].insert(0); S[1][i].insert(n + 1); M[{0, i}] = 1; M[{n + 1, i}] = 2; } int last = 0; while(q--){ int r, c, z; scanf("%d %d %d", &r, &c, &z); r = (r ^ last) % n + 1; c = (c ^ last) % m + 1; if(bad(r, c)){ last ^= z; puts("TAK"); } else{ add(r, c); puts("NIE"); } } return 0; } |