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
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vi>
#define vpii vector<pair<int, int>>
#define str string
#define vs vector<str>
#define lli long long int
#define vc vector<char>
#define vvc vector<vc>

bool go_NE(vvi &M, lli ax, lli ay)
{
    if (ax - 1 == -1 || ay + 1 == M[0].size())
        return false;
    if (M[ax-1][ay+1] == 2)
        return false;
    else 
        return true;
}

bool go_SW(vvi &M, lli ax, lli ay)
{
    if (ax + 1 == M.size() || ay - 1 == -1)
        return false;
    if (M[ax+1][ay-1] == 1)
        return false;
    else 
        return true;
}

void occupy_NE(vvi &M, int ax, int ay)
{
    for (int bx = ax; bx >= 0; bx--)
            M[bx][ay] = 2;
        for (int by = ay; by < M[0].size(); by++)
            M[ax][by] = 2;
    
    for (int i = ax-1; i <= ax+1; i++)
    {
        for (int j = ay-1; j <= ay+1; j++)
        {
            if (i >= 0 && j >= 0 && i < M.size() && j < M[0].size() && (i != ax || j != ay) &&M[i][j] == 5)
            {
                occupy_NE(M, i, j);
            }
        }
    }
}

void occupy_SW(vvi &M, int ax, int ay)
{
    for (int bx = ax; bx < M.size(); bx++)
            M[bx][ay] = 1;
        for (int by = ay; by >= 0; by--)
            M[ax][by] = 1;
    
    for (int i = ax-1; i <= ax+1; i++)
    {
        for (int j = ay-1; j <= ay+1; j++)
        {
            if (i >= 0 && j >= 0 && i < M.size() && j < M[0].size() && (i != ax || j != ay) &&M[i][j] == 5)
            {
                occupy_SW(M, i, j);
            }
        }
    }
}

void build_warownia(vvi &M, int ax, int ay, bool NE, bool SW)
{
    if (SW && NE) 
    {
        M[ax][ay] = 5; // symbol warowni
    }
    else if (!SW)
    {
        occupy_SW(M, ax, ay);
    }
    else if (!NE)
    {
        occupy_NE(M, ax, ay);
    }
}

int main() 
{
    lli n, m, k;
    cin >> n >> m >> k;
    vvi M(n);
    for (int i = 0; i < n; i++)
        M[i] = vi(m, 0);

    bool destr = false;
    lli x = 0;
    for (int i = 0; i < k; i++) 
    {
        lli r, c, z;
        cin >> r >> c >> z;
        lli ax = (r^x)%n;
        lli ay = (c^x)%m;

        bool NE = go_NE(M, ax, ay);
        bool SW = go_SW(M, ax, ay);
        if (!NE && !SW)
        {
            cout << "TAK" << endl;
            destr = true;
        }
        else 
        {
            cout << "NIE" << endl;
            build_warownia(M, ax, ay, NE, SW);
            destr = false;
        }
        if (destr)
            x = x ^ z;
    }
    return 0;
}
    
    
        //cout << "T" << NE << SW << " " << ax << ":" << ay << endl;
    
    // for (int i = 0; i < n; i++)
    // {
    // for (int j = 0; j < m; j++)
    //     cout << M[i][j];
    // cout << endl;
    // }