//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; const int L = 1000009; const int maleL=100009; unordered_map<long long, int > plansz; set<int> rows[maleL]; set<int> cols[maleL]; int numer=1; int blokpraw[L]; int blokdol[L]; int zyw[L]; int x; int n, m, k; long long wyw=0; queue<pair<int, int> > tocheck; //queue<int> numbrs; long long pr(pii qwerty){ long long ret = qwerty.fi; ret = ret << 30LL; ret+=qwerty.se; return ret; } void updt(pair<int, int> pos, int prop){//++wyw;cerr<<wyw<<endl; int mynum = plansz[pr(pos)]; // uptd me // uptd blok praw if(blokpraw[mynum] == 0){ for(int j=-1;j<=1;++j){ if(pos.se+j < 0 || pos.se+j>m-1)continue; auto it = cols[pos.se+j].lower_bound(pos.fi-1); if(it != cols[pos.se+j].end()){ int num = plansz[pr({*(it), pos.se+j})]; if(blokpraw[num]==1){ blokpraw[mynum]=1; } } } for(int i=-1;i<=1;++i){ if(pos.fi+i < 0 || pos.fi+i > n-1)continue; if(sz(rows[pos.fi+i]) == 0)continue; auto it = rows[pos.fi+i].upper_bound(pos.se+1); if(it != rows[pos.fi+i].begin()){ --it; int num = plansz[pr({pos.fi+i, *(it)})]; if(blokpraw[num]==1){ blokpraw[mynum]=1; } } } } if(blokdol[mynum] == 0){ for(int j=-1;j<=1;++j){ if(pos.se+j < 0 || pos.se+j>m-1)continue; if(sz(cols[pos.se+j]) == 0)continue; auto it = cols[pos.se+j].upper_bound(pos.fi+1); if(it != cols[pos.se+j].begin()){ --it; int num = plansz[pr({*(it), pos.se+j})]; if(blokdol[num]==1){ blokdol[mynum]=1; } } } for(int i=-1;i<=1;++i){ if(pos.fi+i < 0 || pos.fi+i > n-1)continue; auto it = rows[pos.fi+i].lower_bound(pos.se-1); if(it != rows[pos.fi+i].end()){ int num = plansz[pr({pos.fi+i, *(it)})]; if(blokdol[num]==1){ blokdol[mynum]=1; } } } } // uptd others if(prop){ tocheck.push(pos); while(!tocheck.empty()){ pair<int, int> pos2 = tocheck.front(); //cerr<<pos2.fi<<" "<<pos2.se<<endl; tocheck.pop(); mynum = plansz[pr(pos2)]; if(blokdol[mynum] == 1){ for(int j=-1;j<=1;++j){ if(pos2.se+j < 0 || pos2.se+j>m-1)continue; auto it = cols[pos2.se+j].lower_bound(pos2.fi-1); int qwe=1; if(j==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != cols[pos2.se+j].end()){ int num = plansz[pr({*(it), pos2.se+j})]; if(blokdol[num]==0){ blokdol[num]=1; tocheck.push({*(it), pos2.se+j}); } ++it; } } } for(int i=-1;i<=1;++i){ if(pos2.fi+i < 0 || pos2.fi+i > n-1)continue; if(sz(rows[pos2.fi+i]) == 0)continue; auto it = rows[pos2.fi+i].upper_bound(pos2.se+1); int qwe=1; if(i==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != rows[pos2.fi+i].begin()){ --it; int num = plansz[pr({pos2.fi+i, *(it)})]; if(blokdol[num]==0){ blokdol[num]=1; tocheck.push({pos2.fi+i, *(it)}); } } } } } } tocheck.push(pos); while(!tocheck.empty()){ pair<int, int> pos2 = tocheck.front(); //cerr<<pos2.fi<<" "<<pos2.se<<endl; tocheck.pop(); mynum = plansz[pr(pos2)]; if(blokpraw[mynum] == 1){ for(int j=-1;j<=1;++j){ if(pos2.se+j < 0 || pos2.se+j>m-1)continue; if(sz(cols[pos2.se+j]) == 0)continue; auto it = cols[pos2.se+j].upper_bound(pos2.fi+1); int qwe=1; if(j==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != cols[pos2.se+j].begin()){ --it; int num = plansz[pr({*(it), pos2.se+j})]; if(blokpraw[num]==0){ blokpraw[num]=1; tocheck.push({*(it), pos2.se+j}); } } } } for(int i=-1;i<=1;++i){ if(pos2.fi+i < 0 || pos2.fi+i > n-1)continue; auto it = rows[pos2.fi+i].lower_bound(pos2.se-1); int qwe=1; if(i==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != rows[pos2.fi+i].end()){ int num = plansz[pr({pos2.fi+i, *(it)})]; if(blokpraw[num]==0){ blokpraw[num]=1; tocheck.push({pos2.fi+i, *(it)}); } ++it; } } } } } } } int main(){ _upgrade cout<<fixed<<setprecision(15); cin>>n>>m>>k; for(int i=0;i<k;++i){ int r, c, z;cin>>r>>c>>z; r = r^x; r%=n; c = c^x; c%=m; plansz[pr({r, c})] = numer; ++numer; if(r == 0 || c == m-1)blokpraw[numer-1]=1; if(r == n-1 || c == 0)blokdol[numer-1]=1; updt({r, c}, 0); if(blokpraw[numer-1] && blokdol[numer-1]){ cout<<"TAK\n"; x^=z; } else{ zyw[numer-1]=1; cout<<"NIE\n"; rows[r].insert(c); cols[c].insert(r); updt({r, c}, 1); } /*if(1){ for(int q=0;q<m;++q){ for(int e=0;e<n;++e){ if(zyw[plansz[{e, q}]]) cerr<<blokdol[plansz[{e, q}]]*4+ blokpraw[plansz[{e, q}]]*2 +1<<" "; else if(q == c && e == r)cerr<<"! "; else cerr<<"0 "; }cerr<<endl; }cerr<<endl; }*/ } return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). */
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; const int L = 1000009; const int maleL=100009; unordered_map<long long, int > plansz; set<int> rows[maleL]; set<int> cols[maleL]; int numer=1; int blokpraw[L]; int blokdol[L]; int zyw[L]; int x; int n, m, k; long long wyw=0; queue<pair<int, int> > tocheck; //queue<int> numbrs; long long pr(pii qwerty){ long long ret = qwerty.fi; ret = ret << 30LL; ret+=qwerty.se; return ret; } void updt(pair<int, int> pos, int prop){//++wyw;cerr<<wyw<<endl; int mynum = plansz[pr(pos)]; // uptd me // uptd blok praw if(blokpraw[mynum] == 0){ for(int j=-1;j<=1;++j){ if(pos.se+j < 0 || pos.se+j>m-1)continue; auto it = cols[pos.se+j].lower_bound(pos.fi-1); if(it != cols[pos.se+j].end()){ int num = plansz[pr({*(it), pos.se+j})]; if(blokpraw[num]==1){ blokpraw[mynum]=1; } } } for(int i=-1;i<=1;++i){ if(pos.fi+i < 0 || pos.fi+i > n-1)continue; if(sz(rows[pos.fi+i]) == 0)continue; auto it = rows[pos.fi+i].upper_bound(pos.se+1); if(it != rows[pos.fi+i].begin()){ --it; int num = plansz[pr({pos.fi+i, *(it)})]; if(blokpraw[num]==1){ blokpraw[mynum]=1; } } } } if(blokdol[mynum] == 0){ for(int j=-1;j<=1;++j){ if(pos.se+j < 0 || pos.se+j>m-1)continue; if(sz(cols[pos.se+j]) == 0)continue; auto it = cols[pos.se+j].upper_bound(pos.fi+1); if(it != cols[pos.se+j].begin()){ --it; int num = plansz[pr({*(it), pos.se+j})]; if(blokdol[num]==1){ blokdol[mynum]=1; } } } for(int i=-1;i<=1;++i){ if(pos.fi+i < 0 || pos.fi+i > n-1)continue; auto it = rows[pos.fi+i].lower_bound(pos.se-1); if(it != rows[pos.fi+i].end()){ int num = plansz[pr({pos.fi+i, *(it)})]; if(blokdol[num]==1){ blokdol[mynum]=1; } } } } // uptd others if(prop){ tocheck.push(pos); while(!tocheck.empty()){ pair<int, int> pos2 = tocheck.front(); //cerr<<pos2.fi<<" "<<pos2.se<<endl; tocheck.pop(); mynum = plansz[pr(pos2)]; if(blokdol[mynum] == 1){ for(int j=-1;j<=1;++j){ if(pos2.se+j < 0 || pos2.se+j>m-1)continue; auto it = cols[pos2.se+j].lower_bound(pos2.fi-1); int qwe=1; if(j==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != cols[pos2.se+j].end()){ int num = plansz[pr({*(it), pos2.se+j})]; if(blokdol[num]==0){ blokdol[num]=1; tocheck.push({*(it), pos2.se+j}); } ++it; } } } for(int i=-1;i<=1;++i){ if(pos2.fi+i < 0 || pos2.fi+i > n-1)continue; if(sz(rows[pos2.fi+i]) == 0)continue; auto it = rows[pos2.fi+i].upper_bound(pos2.se+1); int qwe=1; if(i==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != rows[pos2.fi+i].begin()){ --it; int num = plansz[pr({pos2.fi+i, *(it)})]; if(blokdol[num]==0){ blokdol[num]=1; tocheck.push({pos2.fi+i, *(it)}); } } } } } } tocheck.push(pos); while(!tocheck.empty()){ pair<int, int> pos2 = tocheck.front(); //cerr<<pos2.fi<<" "<<pos2.se<<endl; tocheck.pop(); mynum = plansz[pr(pos2)]; if(blokpraw[mynum] == 1){ for(int j=-1;j<=1;++j){ if(pos2.se+j < 0 || pos2.se+j>m-1)continue; if(sz(cols[pos2.se+j]) == 0)continue; auto it = cols[pos2.se+j].upper_bound(pos2.fi+1); int qwe=1; if(j==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != cols[pos2.se+j].begin()){ --it; int num = plansz[pr({*(it), pos2.se+j})]; if(blokpraw[num]==0){ blokpraw[num]=1; tocheck.push({*(it), pos2.se+j}); } } } } for(int i=-1;i<=1;++i){ if(pos2.fi+i < 0 || pos2.fi+i > n-1)continue; auto it = rows[pos2.fi+i].lower_bound(pos2.se-1); int qwe=1; if(i==0)qwe=3; for(int u=0;u<qwe;++u){ if(it != rows[pos2.fi+i].end()){ int num = plansz[pr({pos2.fi+i, *(it)})]; if(blokpraw[num]==0){ blokpraw[num]=1; tocheck.push({pos2.fi+i, *(it)}); } ++it; } } } } } } } int main(){ _upgrade cout<<fixed<<setprecision(15); cin>>n>>m>>k; for(int i=0;i<k;++i){ int r, c, z;cin>>r>>c>>z; r = r^x; r%=n; c = c^x; c%=m; plansz[pr({r, c})] = numer; ++numer; if(r == 0 || c == m-1)blokpraw[numer-1]=1; if(r == n-1 || c == 0)blokdol[numer-1]=1; updt({r, c}, 0); if(blokpraw[numer-1] && blokdol[numer-1]){ cout<<"TAK\n"; x^=z; } else{ zyw[numer-1]=1; cout<<"NIE\n"; rows[r].insert(c); cols[c].insert(r); updt({r, c}, 1); } /*if(1){ for(int q=0;q<m;++q){ for(int e=0;e<n;++e){ if(zyw[plansz[{e, q}]]) cerr<<blokdol[plansz[{e, q}]]*4+ blokpraw[plansz[{e, q}]]*2 +1<<" "; else if(q == c && e == r)cerr<<"! "; else cerr<<"0 "; }cerr<<endl; }cerr<<endl; }*/ } return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). */ |