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
#include "bits/stdc++.h"

using namespace std;

struct Dimension {
  Dimension(int n, int m) {
    tab.resize(n+2);
    update(n+1, INT_MAX);
  };

  int get(int ix) {
    int ret = 0;
    for (int i=ix; i>0; i-=(i&(-i))) ret = max(ret, tab[i]);
    return ret;
  }

  void update(int ix, int value) {
    for (int i=ix; i<(int)tab.size(); i+=(i&(-i))) tab[i] = max(tab[i], value);
  }

  void insert(int p, int q) {
    inc[p].insert(q);
    dec[q].insert(p);
  }

  void put(int p, int q) {
    // cerr << "PUT " << p << " " << q << "\n";
    if (get(p) >= q or get(p+1) < q-1) return;
    update(p, q);
    for (auto it = inc[p-1].begin(); it != inc[p-1].end() and (*it) <= q+1; ) {
      int val = (*it++);
      inc[p-1].erase(val);
      put(p-1, val);
    }
    for (auto it = dec[q+1].begin(); it != dec[q+1].end() and (*it) >= p-1; ) {
      int val = (*it++);
      dec[q+1].erase(val);
      put(val, q+1);
    }
  }

  vector<int> tab;
  unordered_map<int, set<int>> inc;
  unordered_map<int, set<int, greater<int>>> dec;
};

int main() {
  ios_base::sync_with_stdio(0);
  int n, m, k;
  cin >> n >> m >> k;
  auto X = Dimension(n, m), Y = Dimension(m, n);
  int x = 0;
  auto query = [&](int r, int c) {
    if (X.get(r) > c-1 or Y.get(c) > r-1) return false;
    if (X.get(r+1) >= c-1 and Y.get(c+1) >= r-1) return true;
    X.put(r, c);
    Y.put(c, r);
    return false;
  };
  while (k--) {
    int r, c, z;
    cin >> r >> c >> z;
    r = (r^x)%n;
    c = (c^x)%m;
    if (query(r+1, c+1)) {
      puts("TAK");
      x ^= z;
    } else {
      puts("NIE");
      X.insert(r+1, c+1);
      Y.insert(c+1, r+1);
    }
  }
}