#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbl(k, x) fixed << setprecision(k) << (x) template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e5 + 7; const int K = 1e6 + 7; const int MAX = 1 << 17; int lt[2 * MAX], rt[2 * MAX]; int n, m, k; void update_left(int i, int j) { int a = i + MAX, b = n + 1 + MAX; lt[a] = max(lt[a], j); lt[b] = max(lt[b], j); while(a / 2 != b / 2) { if(a % 2 == 0) lt[a + 1] = max(lt[a + 1], j); if(b % 2 == 1) lt[b - 1] = max(lt[b - 1], j); a /= 2, b /= 2; } } int query_left(int i) { int res = 0; for(int e = i + MAX; e; e /= 2) res = max(res, lt[e]); return res; } void update_right(int i, int j) { int a = 0 + MAX, b = i + MAX; rt[a] = min(rt[a], j); rt[b] = min(rt[b], j); while(a / 2 != b / 2) { if(a % 2 == 0) rt[a + 1] = min(rt[a + 1], j); if(b % 2 == 1) rt[b - 1] = min(rt[b - 1], j); a /= 2, b /= 2; } } int query_right(int i) { int res = m + 1; for(int e = i + MAX; e; e /= 2) res = min(res, rt[e]); return res; } int da[] = {1, 1, 1, 0, 0, -1, -1, -1}; int db[] = {1, 0, -1, 1, -1, 1, 0, -1}; set < PII > S, R; void dfs1(int a, int b) { S.erase({a, b}); R.erase({b, a}); update_left(a, b); auto it = S.lower_bound({a - 1, 0}); while(it != S.end() && it->first == a - 1 && it->second <= b + 1) { dfs1(it->first, it->second); it = S.lower_bound({a - 1, 0}); } auto it2 = R.lower_bound({b + 1, n + 2}); if(it2 != R.begin()) { it2--; while(it2->first == b + 1 && it2->second >= a - 1) { dfs1(it2->second, it2->first); it2 = R.lower_bound({b + 1, n + 2}); if(it2 == R.begin()) break; it2--; } } for(int i = 0; i < 8; i++) { int ua = a + da[i], ub = b + db[i]; if(S.find({ua, ub}) != S.end()) dfs1(ua, ub); } } void dfs2(int a, int b) { S.erase({a, b}); R.erase({b, a}); update_right(a, b); auto it = S.lower_bound({a + 1, m + 2}); if(it != S.begin()) { it--; while(it->first == a + 1 && it->second >= b - 1) { dfs2(it->first, it->second); it = S.lower_bound({a + 1, m + 2}); if(it == S.begin()) break; it--; } } auto it2 = R.lower_bound({b - 1, 0}); while(it2 != R.end() && it2->first == b - 1 && it2->second <= a + 1) { dfs2(it2->second, it2->first); it2 = R.lower_bound({b - 1, 0}); } for(int i = 0; i < 8; i++) { int ua = a + da[i], ub = b + db[i]; if(S.find({ua, ub}) != S.end()) dfs2(ua, ub); } } int main() { _upgrade cin >> n >> m >> k; for(int i = 0; i < 2 * MAX; i++) rt[i] = m + 1; update_left(n + 1, m); update_right(0, 1); int last = 0; for(int t = 1; t <= k; t++) { //for(int i = 1; i <= n; i++) // DBG(query_left(i), query_right(i)); int a, b, c; cin >> a >> b >> c; a = (a ^ last) % n; b = (b ^ last) % m; a++, b++; int l1 = query_left(a), l2 = query_left(a + 1); int r0 = query_right(a - 1), r1 = query_right(a); if(b <= l1 || r1 <= b) { cout << "NIE\n"; continue; } bool n1 = (l2 + 1 >= b); bool n2 = (r0 - 1 <= b); //DBG(a, b, n1, n2); if(n1 && n2) { cout << "TAK\n"; last ^= c; continue; } S.insert({a, b}); R.insert({b, a}); if(n1) dfs1(a, b); else if(n2) dfs2(a, b); cout << "NIE\n"; } 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 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define dbl(k, x) fixed << setprecision(k) << (x) template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 1e5 + 7; const int K = 1e6 + 7; const int MAX = 1 << 17; int lt[2 * MAX], rt[2 * MAX]; int n, m, k; void update_left(int i, int j) { int a = i + MAX, b = n + 1 + MAX; lt[a] = max(lt[a], j); lt[b] = max(lt[b], j); while(a / 2 != b / 2) { if(a % 2 == 0) lt[a + 1] = max(lt[a + 1], j); if(b % 2 == 1) lt[b - 1] = max(lt[b - 1], j); a /= 2, b /= 2; } } int query_left(int i) { int res = 0; for(int e = i + MAX; e; e /= 2) res = max(res, lt[e]); return res; } void update_right(int i, int j) { int a = 0 + MAX, b = i + MAX; rt[a] = min(rt[a], j); rt[b] = min(rt[b], j); while(a / 2 != b / 2) { if(a % 2 == 0) rt[a + 1] = min(rt[a + 1], j); if(b % 2 == 1) rt[b - 1] = min(rt[b - 1], j); a /= 2, b /= 2; } } int query_right(int i) { int res = m + 1; for(int e = i + MAX; e; e /= 2) res = min(res, rt[e]); return res; } int da[] = {1, 1, 1, 0, 0, -1, -1, -1}; int db[] = {1, 0, -1, 1, -1, 1, 0, -1}; set < PII > S, R; void dfs1(int a, int b) { S.erase({a, b}); R.erase({b, a}); update_left(a, b); auto it = S.lower_bound({a - 1, 0}); while(it != S.end() && it->first == a - 1 && it->second <= b + 1) { dfs1(it->first, it->second); it = S.lower_bound({a - 1, 0}); } auto it2 = R.lower_bound({b + 1, n + 2}); if(it2 != R.begin()) { it2--; while(it2->first == b + 1 && it2->second >= a - 1) { dfs1(it2->second, it2->first); it2 = R.lower_bound({b + 1, n + 2}); if(it2 == R.begin()) break; it2--; } } for(int i = 0; i < 8; i++) { int ua = a + da[i], ub = b + db[i]; if(S.find({ua, ub}) != S.end()) dfs1(ua, ub); } } void dfs2(int a, int b) { S.erase({a, b}); R.erase({b, a}); update_right(a, b); auto it = S.lower_bound({a + 1, m + 2}); if(it != S.begin()) { it--; while(it->first == a + 1 && it->second >= b - 1) { dfs2(it->first, it->second); it = S.lower_bound({a + 1, m + 2}); if(it == S.begin()) break; it--; } } auto it2 = R.lower_bound({b - 1, 0}); while(it2 != R.end() && it2->first == b - 1 && it2->second <= a + 1) { dfs2(it2->second, it2->first); it2 = R.lower_bound({b - 1, 0}); } for(int i = 0; i < 8; i++) { int ua = a + da[i], ub = b + db[i]; if(S.find({ua, ub}) != S.end()) dfs2(ua, ub); } } int main() { _upgrade cin >> n >> m >> k; for(int i = 0; i < 2 * MAX; i++) rt[i] = m + 1; update_left(n + 1, m); update_right(0, 1); int last = 0; for(int t = 1; t <= k; t++) { //for(int i = 1; i <= n; i++) // DBG(query_left(i), query_right(i)); int a, b, c; cin >> a >> b >> c; a = (a ^ last) % n; b = (b ^ last) % m; a++, b++; int l1 = query_left(a), l2 = query_left(a + 1); int r0 = query_right(a - 1), r1 = query_right(a); if(b <= l1 || r1 <= b) { cout << "NIE\n"; continue; } bool n1 = (l2 + 1 >= b); bool n2 = (r0 - 1 <= b); //DBG(a, b, n1, n2); if(n1 && n2) { cout << "TAK\n"; last ^= c; continue; } S.insert({a, b}); R.insert({b, a}); if(n1) dfs1(a, b); else if(n2) dfs2(a, b); cout << "NIE\n"; } return 0; } |