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
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)
#define sim template<class n
sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
sim> auto operator << (ostream &p, n y) ->
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " =", (x), " "
#define A(a, i) #a "[" #i " =", i, "] =", a[i], " "

// ******************************************************************************

const int MXN = 1e5+5;
int n, m;
int mi_col[MXN];
int mx_col[MXN];
set<int> S[MXN];
set<int> S2[MXN];

const int BASE = 1 << 17;
int mx[BASE*2];
void insert_mx(int i, int v)
  {
  i += BASE;
  while(i)
    {
    maxi(mx[i], v);
    i /= 2;
    }
  }
int query_mx(int a, int b)
  {
  a += BASE-1;
  b += BASE+1;
  int res = -1e9;
  while(a / 2 != b / 2)
    {
    if(a % 2 == 0)maxi(res, mx[a+1]);
    if(b % 2 == 1)maxi(res, mx[b-1]);
    a /= 2;
    b /= 2;
    }
  return res;
  }
int mi[BASE*2];
void insert_mi(int i, int v)
  {
  i += BASE;
  while(i)
    {
    mini(mi[i], v);
    i /= 2;
    }
  }
int query_mi(int a, int b)
  {
  a += BASE-1;
  b += BASE+1;
  int res = 1e9;
  while(a / 2 != b / 2)
    {
    if(a % 2 == 0)mini(res, mi[a+1]);
    if(b % 2 == 1)mini(res, mi[b-1]);
    a /= 2;
    b /= 2;
    }
  return res;
  }

int wieksze_rowne(set<int> &S, int x)
  {
  auto it = S.lower_bound(x);
  if(it == S.end())return 1e9;
  return *it;
  }

int mniejsze_rowne(set<int> &S, int x)
  {
  auto it = S.upper_bound(x);
  if(it == S.begin())return -1e9;
  it--;
  return *it;
  }

int adds_dol;
void aktualizuj_dol(int x, int y)
  {
  if(x > n || y < 1)return;

  int r = query_mx(0, x+1);
  if(r >= y-1 || x == n || y == 1)
    {
    if(query_mx(0, x) >= y)return;
//    debug("dod dol", x, y);
    adds_dol++;
    insert_mx(x, y);

    int x2 = wieksze_rowne(S[y+1], x-1);
    aktualizuj_dol(x2, y+1);

    int y2 = mniejsze_rowne(S2[x-1], y+1);
    aktualizuj_dol(x-1, y2);
    }
  }

int adds_gora;
void aktualizuj_gora(int x, int y)
  {
  if(x <= 0 || y > m)return;

  int r = query_mi(x-1, n+1);
  if(r <= y+1 || x == 1 || y == m)
    {
    if(query_mi(x, n+1) <= y)return;
//    debug("dod gor", x, y);
    adds_gora++;
    insert_mi(x, y);

    int x2 = mniejsze_rowne(S[y-1], x+1);
    aktualizuj_gora(x2, y-1);

    int y2 = wieksze_rowne(S2[x+1], y-1);
    aktualizuj_gora(x+1, y2);
    }
  }

bool add(int x, int y)
  {
  x++; y++;

  if(query_mx(1, x+1) >= y-1 && query_mi(x-1, n+1) <= y+1)return 1;
  S[y].insert(x);
  S2[x].insert(y);
  aktualizuj_dol(x, y);
  aktualizuj_gora(x, y);

  return 0;
  }

int main()
  {
  int k;
  scanf("%d%d%d", &n, &m, &k);

  REP(i, BASE*2)mi[i] = m+1;
  insert_mx(n+1, m+1);
  insert_mi(0, 0);

  int x = 0;
  FOR(_, 1, k)
    {
    int a, b, z;
    scanf("%d%d%d", &a, &b, &z);

    a = (a ^ x) % n;
    b = (b ^ x) % m;
    debug(a, b);
    bool ans = add(a, b);
    puts(ans ? "TAK" : "NIE");
    if(ans)x = x ^ z;
//    assert(adds_gora + adds_dol <= _);
    }
  
  }