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
#include <iostream>
#include <vector>
using namespace std;

namespace {

struct Solver {
  int n, m;
  vector<vector<bool>> reachable;
  Solver(int n_, int m_)
    : n{n_}, m{m_}, reachable(n, vector<bool>(m, true))
  {
  }

  bool operator()(int x, int y)
  {
    vector<pair<int, int>> queue{{x, y}};
    reachable[x][y] = false;
    int q = 0;
    auto check = [&](int xx, int yy) {
      if (xx < 0 || xx >= n) return;
      if (yy < 0 || yy >= m) return;
      if (!reachable[xx][yy]) return;
      if (xx > 0 && reachable[xx - 1][yy]) return;
      if (yy > 0 && reachable[xx][yy - 1]) return;
      reachable[xx][yy] = false;
      queue.emplace_back(xx, yy);
    };
    while (q < int(queue.size())) {
      auto [x2, y2] = queue[q];
      ++q;
      check(x2 + 1, y2);
      check(x2, y2 + 1);
    }
    if (!reachable[n-1][m-1]) {
      for (auto const& [x2, y2]: queue) reachable[x2][y2] = true;
      return true;
    }
    return false;
  }
};

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  Solver solver(n, m);

  int k;
  cin >> k;
  int x = 0;
  for (int i = 0; i < k; ++i) {
    int r, c, z;
    cin >> r >> c >> z;
    auto res = solver((r ^ x) % n, (c ^ x) % m);
    if (res) x ^= z;
    cout << (res ? "TAK" : "NIE") << '\n';
  }

  return 0;
}