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

using namespace std;

set<int> s[100000];
int n, m, k, r, c, z, x;

bool empty(int x, int y) {
    return s[x].find(y) == s[x].end();
}

bool trail(int x1, int y1, int x2, int y2) {
    int d = x2 + y2 - x1 - y1;
    if (d == 1)
        return true;
    d /= 2;
    if (x2 - x1 >= y2 - y1) {
        for (int x=x1+d, y=y1; y<=y2; x--, y++)
            if (empty(x, y) && trail(x1, y1, x, y) && trail(x, y, x2, y2))
                return true;
    } else {
        for (int y=y1+d, x=x1; x<=x2; y--, x++)
            if (empty(x, y) && trail(x1, y1, x, y) && trail(x, y, x2, y2))
                return true;
    }
    return false;
}

main() {
    x = 0;
    cin >> n >> m >> k;
    for (int i=0; i<k; i++) {
        cin >> r >> c >> z;
        r = (r ^ x) % n;
        c = (c ^ x) % m;
        s[r].insert(c);
        if (trail(0, 0, n-1, m-1)) {
            cout << "NIE" << endl;
        } else {
            cout << "TAK" << endl;
            s[r].erase(c);
            x ^= z;
        }
    }

    return 0;
}