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

using namespace std;

struct Field {
    int r;
    int c;

    bool operator==(const Field& other) const {
        return r == other.r && c == other.c;
    }

    bool operator!=(const Field& other) const {
        return !(*this == other);
    }

    bool operator<(const Field& other) const {
        if (c == other.c) {
            return r < other.r;
        } else {
            return c < other.c;
        }
    }
};


struct Frame {
    Field f;
    vector<Field> next;
    int level;

    Frame(int lvl, Field _f)
        : f(_f)
        , level(lvl)
    {
        int row_first = rand() % 2;
        next.push_back(Field{ f.r + row_first, f.c + 1 - row_first });
        next.push_back(Field{ f.r + 1 - row_first, f.c + row_first });
    }
};

struct Board {
    int n, m;
    set<Field> niezbedne;
    vector<vector<bool>> zakazane;
    vector<Field> path;

    Board(int _n, int _m)
        : n(_n)
        , m(_m)
        , zakazane(_n, vector<bool>(_m, false))
    {
        Field first = {0, 0};
        Field last = {n-1, m-1};
        niezbedne.insert(first);
        niezbedne.insert(last);
        vector<Field> somePath = findPath(first, last);
        path.swap(somePath);
    }

    bool removeField(int r, int c) {
        Field f = {r, c};
        if (contains(niezbedne, f)) {
            return false;
        }
        if (zakazane[r][c]) {
            return true;
        }
        if (!contains(path, f)) {
            zakazane[f.r][f.c] = true;
            return true;
        }
        // pole jest sciezce
        pair<Field, Field> surr = surrundingNiezbedne(f);
        zakazane[f.r][f.c] = true;
        vector<Field> pathFragment = findPath(surr.first, surr.second);
        if (pathFragment.empty()) { // nie ma sciezki po usunieciu tego pola
            zakazane[f.r][f.c] = false;
            niezbedne.insert(f);
            return false;
        } else { // jest inna sciezka
            changePathFragment(pathFragment);
            return true;
        }
    }

    pair<Field, Field> surrundingNiezbedne(const Field& f) {
        auto prev = niezbedne.lower_bound(f);
        prev--;
        auto next = niezbedne.upper_bound(f);
        return pair<Field, Field>(*prev, *next);
    }

    void changePathFragment(const vector<Field>& pathFragment) {
        Field first = pathFragment[0];
        auto it = lower_bound(path.begin(), path.end(), first);
        if (*it != first) {
            throw "internal error: changePathFragment";
        }
        for (unsigned int i = 0; i < pathFragment.size(); ++i) {
            *it = pathFragment[i];
            it++;
        }
    }

    vector<Field> findPath(Field b, Field e) {
        vector<Field> swiezoZakazane;
        vector<Field> p = _findPath(b, e, swiezoZakazane);
        if (p.empty()) {
            for (Field f: swiezoZakazane) {
                zakazane[f.r][f.c] = false;
            }
        }
        reverse(p.begin(), p.end());
        return p;
    }

    vector<Field> _findPath(Field orig_b, Field e, vector<Field>& swiezoZakazane) {
        stack<Frame> frames;
        frames.push(Frame(0, orig_b));

        while (!frames.empty()) {
            Frame currFr = frames.top();
            frames.pop();
            Field b = currFr.f;

            if (b.c > e.c) {
                continue;
            }
            if (b.r > e.r) {
                continue;
            }
            if (b == e) {
                vector<Field> res;
                while (!frames.empty()) {
                    res.push_back(frames.top().f);
                    frames.pop();
                }
                return res;
            }
            if (zakazane[b.r][b.c]) {
                continue;
            }

            if (currFr.next.empty()) {
                zakazane[b.r][b.c] = true;
                swiezoZakazane.push_back(b);
                // z tego pola nie da sie dojsc do celu
            } else {
                Field f = currFr.next.back();
                currFr.next.pop_back();
                Frame newFr(currFr.level + 1, f);
                frames.push(currFr);
                frames.push(newFr);
            }
        }

        return vector<Field>();
    }

    bool contains(const set<Field>&s, const Field& f) {
        return s.find(f) != s.end();
    }

    bool contains(const vector<Field>& v, const Field& f) {
        return binary_search(v.begin(), v.end(), f);
    }
};


int main()
{
    //srand(time(0));
    srand(112358);
    ios_base::sync_with_stdio(false);
    int n, m, k;
    cin >> n >> m >> k;
    Board b(n, m);
    int x = 0;
    while (k--) {
        int r, c, z;
        cin >> r >> c >> z;
        bool removed = b.removeField((r ^ x) % n, (c ^ x) % m);
        if (removed) {
            cout << "NIE" << endl;
        } else {
            cout << "TAK" << endl;
            x = x ^ z;
        }
    }
    return 0;
}