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
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_map>



int next_pow2(int v)
{
  int pow = 1;
  while (pow < v) {
    pow *= 2;
  }
  return pow;
}

constexpr int inf = 999999999;


struct MaxMaxTree
{
  MaxMaxTree(int _size)
    : load(next_pow2(_size) * 2)
    , sub(next_pow2(_size) * 2)
    , size(_size)
  {
  }

  void insert(int a, int b, int v)
  {
    // [a, b] - range includes both ends
    int l = size + a, r = size + b;

    load[l] = std::max(load[l], v);
    sub[l] = std::max(sub[l], v);

    if (r != l) {
      load[r] = std::max(load[r], v);
      sub[r] = std::max(sub[r], v);
    }

    while (l >= 1) {
      if (l < r - 1) {
        if ((l & 1) == 0) {
          load[l + 1] = std::max(load[l + 1], v);
          sub[l + 1] = std::max(sub[l + 1], v);
        }
        if ((r & 1) == 1) {
          load[r - 1] = std::max(load[r - 1], v);
          sub[r - 1] = std::max(sub[r - 1], v);
        }
      }

      if (r < size) {
        sub[l] = std::max({ sub[2 * l], sub[2 * l + 1], load[l] });
        sub[r] = std::max({ sub[2 * r], sub[2 * r + 1], load[r] });
      }

      l /= 2; r /= 2;
    }
  }

  int query(int a)
  {
    if (a < 0 || a >= size) {
      return inf;
    }
    return query(a, a);
  }

  int query(int a, int b)
  {
    // [a, b] - range includes both ends
    int l = size + a, r = size + b;
    int res = -inf;

    while (l >= 1) {
      res = std::max({ res, load[l], load[r] });

      if (l < r - 1) {
        if ((l & 1) == 0) {
          res = std::max(res, sub[l + 1]);
        }
        if ((r & 1) == 1) {
          res = std::max(res, sub[r - 1]);
        }
      }

      l /= 2; r /= 2;
    }
    return res;
  }

  int size;
  std::vector<int> load;
  std::vector<int> sub;
};


int main()
{
  int n, m, k;
  std::cin >> n >> m >> k;

  auto right  = MaxMaxTree(n);
  auto bottom = MaxMaxTree(m);

  int x = 0;

  std::unordered_map<int, std::set<int>> r_ord_todo; // ordered by n-axis (rows)
  std::unordered_map<int, std::set<int>> c_ord_todo; // ordered by m-axis (cols)

  while (k--) {
    int r, c, z;
    std::cin >> r >> c >> z;

    r = (r ^ x) % n;
    c = (c ^ x) % m;

    // check if (r, c) would "block" trade route
    int bborder = bottom.query(c - 1);
    int rborder = right.query(r - 1);

    bool blocked_bottom = n - bborder <= r + 1;  // CHECK IF CORRECT!
    bool blocked_right = m - rborder <= c + 1;

    if (blocked_bottom && blocked_right) {
      // we would end up blocked,
      // this one needs to be destroyed
      x ^= z;
      std::cout << "TAK" << std::endl;
      continue;
    }

    // check if (r, c) borders with any "blocked" regons
    // and if so, trigger an update

    // if we are blocked from bottom or right we can
    // try to merge as many nodes from todo as possible
    if (blocked_bottom) {
      if (n - bottom.query(c) > r) {
        // (r, c) blocks from bottom

        // search for closest nodes to expand
        std::set<std::pair<int, int>> todo;
        todo.emplace(r, c);
        while (!todo.empty()) {
          auto [y, x] = *todo.begin();
          todo.erase(todo.begin());

          bottom.insert(0, x, n - y); // CHECK!
          r_ord_todo[y].erase(x);
          c_ord_todo[x].erase(y);

          auto &r_todo = r_ord_todo[y - 1];
          for (auto it = r_todo.begin(); it != r_todo.end() && *it <= x; ++it) {
            todo.emplace(y - 1, *it);
          }
          auto &c_todo = c_ord_todo[x + 1];
          for (auto it = c_todo.rbegin(); it != c_todo.rend() && *it >= y - 1; ++it) {
            todo.emplace(*it, x + 1);
          }
        }
      }
    } else if (blocked_right) {
      if (m - right.query(r) > c) {
        // (r, c) blocks from right

        // search for closest nodes to expand
        std::set<std::pair<int, int>> todo;
        todo.emplace(r, c);
        while (!todo.empty()) {
          auto [y, x] = *todo.begin();
          todo.erase(todo.begin());

          right.insert(0, y, m - x); // CHECK!
          r_ord_todo[y].erase(x);
          c_ord_todo[x].erase(y);

          auto &r_todo = r_ord_todo[y + 1];
          for (auto it = r_todo.rbegin(); it != r_todo.rend() && *it >= x; ++it) {
            todo.emplace(y + 1, *it);
          }
          auto &c_todo = c_ord_todo[x - 1];
          for (auto it = c_todo.begin(); it != c_todo.end() && *it <= y + 1; ++it) {
            todo.emplace(*it, x - 1);
          }
        }
      }
    } else {
      // add this node on "todo" list
      r_ord_todo[r].insert(c);
      c_ord_todo[c].insert(r);
    }

    std::cout << "NIE" << std::endl;
  }
  
  return 0;
}