#include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " " << x << endl; // #define int long long // uważaj const int N = 1e5 + 7; set <int> kol_bez[N], wie_bez[N]; set <int> kol_gura[N], wie_gura[N]; set <int> kol_dul[N], wie_dul[N]; int n, m, k; void dfs_gura(int a, int b) { if(kol_bez[b].find(a) != kol_bez[b].end()) { kol_bez[b].erase(a); } if(wie_bez[a].find(b) != wie_bez[a].end()) { wie_bez[a].erase(b); } kol_gura[b].insert(a); wie_gura[a].insert(b); while(b > 0 and kol_bez[b - 1].size() > 0 and (*kol_bez[b - 1].begin()) <= a + 1) { int tmp = (*kol_bez[b - 1].begin()); kol_bez[b - 1].erase(tmp); dfs_gura(tmp, b - 1); } while(wie_bez[a + 1].size() > 0 and (*prev(wie_bez[a + 1].end())) >= b - 1) { int tmp = (*prev(wie_bez[a + 1].end())); wie_bez[a + 1].erase(tmp); dfs_gura(a + 1, tmp); } } void dfs_dul(int a, int b) { if(kol_bez[b].find(a) != kol_bez[b].end()) { kol_bez[b].erase(a); } if(wie_bez[a].find(b) != wie_bez[a].end()) { wie_bez[a].erase(b); } kol_dul[b].insert(a); wie_dul[a].insert(b); while(kol_bez[b + 1].size() > 0 and (*prev(kol_bez[b + 1].end())) >= a - 1) { int tmp = (*prev(kol_bez[b + 1].end())); kol_bez[b + 1].erase(tmp); dfs_dul(tmp, b + 1); } while(a > 0 and wie_bez[a - 1].size() > 0 and (*wie_bez[a - 1].begin()) <= b + 1) { int tmp = (*wie_bez[a - 1].begin()); wie_bez[a - 1].erase(tmp); dfs_dul(a - 1, tmp); } } pair<bool, bool> czy_moge(int a, int b) { bool gura = false; bool dul = false; if(a == 0 or b == m - 1) gura = true; if(a == n - 1 or b == 0) dul = true; if(b > 0 and kol_dul[b - 1].size() > 0 and (*kol_dul[b - 1].begin()) <= a + 1) dul = true; if(kol_gura[b + 1].size() > 0 and (*prev(kol_gura[b + 1].end())) >= a - 1) gura = true; if(wie_dul[a + 1].size() > 0 and (*prev(wie_dul[a + 1].end())) >= b - 1) dul = true; if(a > 0 and wie_gura[a - 1].size() > 0 and (*wie_gura[a - 1].begin()) <= b + 1) gura = true; return {gura, dul}; } void dodaj(int a, int b) { pair<bool, bool> gdzie_dfs = czy_moge(a, b); if(gdzie_dfs.first) { dfs_gura(a, b); } else if(gdzie_dfs.second) { dfs_dul(a, b); } else { kol_bez[b].insert(a); wie_bez[a].insert(b); } } void solve() { cin >> n >> m >> k; int x = 0; for(int i = 0; i < k; i++) { int a, b, c; cin >> a >> b >> c; a = (a ^ x) % n; b = (b ^ x) % m; if(czy_moge(a, b) != make_pair(true, true)) { dodaj(a, b); cout << "NIE\n"; } else { cout << "TAK\n"; x ^= c; } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; while(test--){ solve(); } 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 | #include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " " << x << endl; // #define int long long // uważaj const int N = 1e5 + 7; set <int> kol_bez[N], wie_bez[N]; set <int> kol_gura[N], wie_gura[N]; set <int> kol_dul[N], wie_dul[N]; int n, m, k; void dfs_gura(int a, int b) { if(kol_bez[b].find(a) != kol_bez[b].end()) { kol_bez[b].erase(a); } if(wie_bez[a].find(b) != wie_bez[a].end()) { wie_bez[a].erase(b); } kol_gura[b].insert(a); wie_gura[a].insert(b); while(b > 0 and kol_bez[b - 1].size() > 0 and (*kol_bez[b - 1].begin()) <= a + 1) { int tmp = (*kol_bez[b - 1].begin()); kol_bez[b - 1].erase(tmp); dfs_gura(tmp, b - 1); } while(wie_bez[a + 1].size() > 0 and (*prev(wie_bez[a + 1].end())) >= b - 1) { int tmp = (*prev(wie_bez[a + 1].end())); wie_bez[a + 1].erase(tmp); dfs_gura(a + 1, tmp); } } void dfs_dul(int a, int b) { if(kol_bez[b].find(a) != kol_bez[b].end()) { kol_bez[b].erase(a); } if(wie_bez[a].find(b) != wie_bez[a].end()) { wie_bez[a].erase(b); } kol_dul[b].insert(a); wie_dul[a].insert(b); while(kol_bez[b + 1].size() > 0 and (*prev(kol_bez[b + 1].end())) >= a - 1) { int tmp = (*prev(kol_bez[b + 1].end())); kol_bez[b + 1].erase(tmp); dfs_dul(tmp, b + 1); } while(a > 0 and wie_bez[a - 1].size() > 0 and (*wie_bez[a - 1].begin()) <= b + 1) { int tmp = (*wie_bez[a - 1].begin()); wie_bez[a - 1].erase(tmp); dfs_dul(a - 1, tmp); } } pair<bool, bool> czy_moge(int a, int b) { bool gura = false; bool dul = false; if(a == 0 or b == m - 1) gura = true; if(a == n - 1 or b == 0) dul = true; if(b > 0 and kol_dul[b - 1].size() > 0 and (*kol_dul[b - 1].begin()) <= a + 1) dul = true; if(kol_gura[b + 1].size() > 0 and (*prev(kol_gura[b + 1].end())) >= a - 1) gura = true; if(wie_dul[a + 1].size() > 0 and (*prev(wie_dul[a + 1].end())) >= b - 1) dul = true; if(a > 0 and wie_gura[a - 1].size() > 0 and (*wie_gura[a - 1].begin()) <= b + 1) gura = true; return {gura, dul}; } void dodaj(int a, int b) { pair<bool, bool> gdzie_dfs = czy_moge(a, b); if(gdzie_dfs.first) { dfs_gura(a, b); } else if(gdzie_dfs.second) { dfs_dul(a, b); } else { kol_bez[b].insert(a); wie_bez[a].insert(b); } } void solve() { cin >> n >> m >> k; int x = 0; for(int i = 0; i < k; i++) { int a, b, c; cin >> a >> b >> c; a = (a ^ x) % n; b = (b ^ x) % m; if(czy_moge(a, b) != make_pair(true, true)) { dodaj(a, b); cout << "NIE\n"; } else { cout << "TAK\n"; x ^= c; } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; while(test--){ solve(); } return 0; } |