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
// 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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));


using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (2e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

const int pot = 1<<20;

int drze[2 * pot + 7][3];
set<int> X[maxn][3];
set<int> Y[maxn][3];

int maxi(int l, int p, int f)
{
  l += pot;
  p += pot;
  int res = 0;
  while(l <= p)
  {
    if(l&1)
      res = max(res, drze[l][f]);
    if(!(p&1))
      res = max(res, drze[p][f]);
    l = (l + 1) / 2;
    p = (p - 1) / 2;
  }
  return res;
}

void ustaw(int x, int val, int f)
{
  x += pot;
  while(x)
  {
    drze[x][f] = max(drze[x][f], val);
    x /= 2;
  }
}

bool blokuje(int r, int c, int f)
{
  return maxi(1, r, f) < c and c - 1 <= maxi(1, r + 1, f);
}

void dodaj(int r, int c, int f)
{
  if(!blokuje(r, c, f))
    return;
  ustaw(r, c, f);
  auto it = X[r - 1][f].begin();
  while(SZ(X[r - 1][f]) > 0 and (*it) < c + 2)
  {
    int val = *it;
    X[r - 1][f].erase(it);
    Y[val][f].erase(r - 1);
    dodaj(r - 1, val, f);
    if(SZ(X[r - 1][f]))
      it = X[r - 1][f].begin();
  }
  auto it2 = Y[c + 1][f].rbegin();
  while(SZ(Y[c + 1][f]) > 0 and (*it2) > r - 2)
  {
    int val = *it2;
    Y[c + 1][f].erase(val);
    X[val][f].erase(c + 1);
    dodaj(val, c + 1, f);
    if(SZ(Y[c + 1][f]))
      it2 = Y[c + 1][f].rbegin();
  }
}


bool pytaj(int r, int c)
{
  return maxi(1, r + 1, 0) > c - 2 and maxi(1, c + 1, 1) > r - 2;
}

int main()
{
	_upgrade
  int n, m, k;
  cin>>n>>m>>k;
  int x = 0;
  ustaw(n + 1, inf, 0);
  ustaw(m + 1, inf, 1);
  while(k--)
  {
    int r, c, z;
    cin>>r>>c>>z;
    r = (r ^ x) % n + 1;
    c = (c ^ x) % m + 1;
    if(pytaj(r, c))
    {
      cout<<"TAK\n";
      x ^= z;
    }
    else
    {
      X[r][0].insert(c);
      Y[c][0].insert(r);
      X[c][1].insert(r);
      Y[r][1].insert(c);
      dodaj(r, c, 0);
      dodaj(c, r, 1);
      cout<<"NIE\n";
    }
  }

	return 0;
}