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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for(int i=0,_=(n);i<_;++i)
#define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1e9 + 9;

///// BEGIN FIND_UNION

vector<int> fu_parent;

void fu_init(int n) {
    fu_parent.resize(0);
    fu_parent.resize(n, -1);
}

int fu_find(int x) {
    if (fu_parent[x] < 0)
        return x;
    return fu_parent[x] = fu_find(fu_parent[x]);
}

void fu_union(int x, int y) {
    x = fu_find(x), y = fu_find(y);
    if (fu_parent[x] < fu_parent[y]) {
        fu_parent[y] = x;
    } else if (fu_parent[x] > fu_parent[y]) {
        fu_parent[x] = y;
    } else if (x != y) {
        fu_parent[y] = x;
        --fu_parent[x];
    }
}

///// END FIND_UNION

template<typename T>
T my_abs(const T &x) {
    if (x < 0) {
        return -x;
    }
    return x;
}

struct Fort {
    int coordinate;
    int fort_id;

    bool operator<(const Fort &o) const {
        return coordinate < o.coordinate;
    }
};

ostream &operator<<(ostream &os, const Fort &o) {
    return os << "Fort(coord=" << o.coordinate << ", id=" << o.fort_id << ")";
}

struct Reach {
    int bits; //left, right, bottom, top

    Reach(int _bits = 0) :
            bits(_bits) {
    }

    void set_left() {
        bits |= (1 << 0);
    }

    void set_right() {
        bits |= (1 << 1);
    }

    void set_bottom() {
        bits |= (1 << 2);
    }

    void set_top() {
        bits |= (1 << 3);
    }

    bool is_left() const {
        return bool(bits & (1 << 0));
    }

    bool is_right() const {
        return bool(bits & (1 << 1));
    }

    bool is_bottom() const {
        return bool(bits & (1 << 2));
    }

    bool is_top() const {
        return bool(bits & (1 << 3));
    }

    bool is_blocking() const {
        return (is_left() and is_right()) or (is_left() and is_bottom()) or
               (is_bottom() and is_top()) or (is_right() and is_top());
    }

    void update(const Reach &reach) {
        bits |= reach.bits;
    }
};

ostream &operator<<(ostream &os, const Reach &o) {
    return os << "Reach("
              << "left=" << o.is_left()
              << ", right=" << o.is_right()
              << ", bottom=" << o.is_bottom()
              << ", top=" << o.is_top()
              << ")";
}

int max_x, max_y, forts_count;
vector <set<Fort>> by_x, by_y;
vector <Reach> reaches;

Reach &get_reach_by_fort_id(int fort_id) {
    fort_id = fu_find(fort_id);
    return reaches[fort_id];
}

void merge_forts(int fort_id_1, int fort_id_2) {
    Reach new_reach;
    new_reach.update(get_reach_by_fort_id(fort_id_1));
    new_reach.update(get_reach_by_fort_id(fort_id_2));
    fu_union(fort_id_1, fort_id_2);
    get_reach_by_fort_id(fort_id_1).update(new_reach);
    get_reach_by_fort_id(fort_id_2).update(new_reach);
}

void push_right(int x, int y) {
    if (x == max_x - 1) {
        return;
    }
    auto &s = by_x[x + 1];
    auto first = s.lower_bound(Fort{y, -1});
    if (first == s.end()) {
        return;
    }
    int first_coordinate = first->coordinate;
    const auto &first_reach = get_reach_by_fort_id(first->fort_id);
    if (first_reach.is_left()) {
        return;
    }
    for (auto it = first; it != s.end(); ++it) {
        auto &reach = get_reach_by_fort_id(it->fort_id);
        if (reach.is_left()) {
            break;
        }
        reach.set_left();
    }
    push_right(x + 1, first_coordinate);
}

void push_left(int x, int y) {
    if (x == 0) {
        return;
    }
    auto &s = by_x[x - 1];
    auto first = s.upper_bound(Fort{y, -1});
    if (first == s.begin()) {
        return;
    }
    --first;
    int first_coordinate = first->coordinate;
    const auto &first_reach = get_reach_by_fort_id(first->fort_id);
    if (first_reach.is_right()) {
        return;
    }
    for (auto it = first;; --it) {
        auto &reach = get_reach_by_fort_id(it->fort_id);
        if (reach.is_right()) {
            break;
        }
        reach.set_right();
        if (it == s.begin()) {
            break;
        }
    }
    push_left(x - 1, first_coordinate);
}

void push_top(int x, int y) {
    DEB("push_top(" << x << ", " << y << ")\n");
    if (y == max_y - 1) {
        return;
    }
    auto &s = by_y[y + 1];
    auto fort = Fort{x, -1};
    DEB("before lower\n");
    auto first = s.lower_bound(fort);
    DEB("after lower\n");
    if (first == s.end()) {
        return;
    }
    int first_coordinate = first->coordinate;
    const auto &first_reach = get_reach_by_fort_id(first->fort_id);
    if (first_reach.is_bottom()) {
        return;
    }
    for (auto it = first; it != s.end(); ++it) {
        auto &reach = get_reach_by_fort_id(it->fort_id);
        if (reach.is_bottom()) {
            break;
        }
        reach.set_bottom();
    }
    push_top(first_coordinate, y + 1);
}

void push_bottom(int x, int y) {
    if (y == 0) {
        return;
    }
    auto &s = by_y[y - 1];
    auto first = s.upper_bound(Fort{x, -1});
    if (first == s.begin()) {
        return;
    }
    --first;
    int first_coordinate = first->coordinate;
    const auto &first_reach = get_reach_by_fort_id(first->fort_id);
    if (first_reach.is_top()) {
        return;
    }
    for (auto it = first;; --it) {
        auto &reach = get_reach_by_fort_id(it->fort_id);
        if (reach.is_top()) {
            break;
        }
        reach.set_top();
        if (it == s.begin()) {
            break;
        }
    }
    push_bottom(first_coordinate, y - 1);
}

void try_push(int source_fort_id, int target_fort_id, int x, int y) {
    const Reach &source_reach = get_reach_by_fort_id(source_fort_id);
    const Reach &target_reach = get_reach_by_fort_id(target_fort_id);
    bool should_push_right = source_reach.is_left() and not target_reach.is_left();
    bool should_push_left = source_reach.is_right() and not target_reach.is_right();
    bool should_push_top = source_reach.is_bottom() and not target_reach.is_bottom();
    bool should_push_bottom = source_reach.is_top() and not target_reach.is_top();
    if (should_push_right) {
        push_right(x, y);
    }
    if (should_push_left) {
        push_left(x, y);
    }
    if (should_push_top) {
        push_top(x, y);
    }
    if (should_push_bottom) {
        push_bottom(x, y);
    }
}

vector <Fort> get_neighbors(const vector <set<Fort>> &by_coord, int coord1, int coord2) {
    vector <Fort> result;
    auto it = by_coord[coord1].lower_bound(Fort{coord2, -1});
    if (it != by_coord[coord1].begin()) {
        auto prev = it;
        --prev;
        DEB("    prev=" << *prev << "\n");
        result.PB(*prev);
    }
    if (it != by_coord[coord1].end()) {
        DEB("    it=" << *it << "\n");
        result.PB(*it);
        auto next = it;
        ++next;
        if (next != by_coord[coord1].end()) {
            DEB("    next=" << *next << "\n");
            result.PB(*next);
        }
    }
    return result;
}

bool is_close(int coord1, int coord2) {
    return my_abs(coord1 - coord2) <= 1;
}

Reach get_reach_if_placed_fort(int x, int y) {
    DEB("  get_reach_if_placed_fort(x=" << x << ", y=" << y << ")\n");
    Reach current_reach;
    if (x == 0) {
        current_reach.set_left();
    }
    if (x == max_x - 1) {
        current_reach.set_right();
    }
    if (y == 0) {
        current_reach.set_bottom();
    }
    if (y == max_y - 1) {
        current_reach.set_top();
    }

    FOR(nx, x - 1, x + 1) {
        DEB("    nx=" << nx << "\n");
        if (nx < 0 || nx >= max_x) {
            continue;
        }
        auto neighbors = get_neighbors(by_x, nx, y);

        for (const Fort &fort: neighbors) {
            const Reach &other_reach = get_reach_by_fort_id(fort.fort_id);
            if (fort.coordinate < y) {
                if (is_close(fort.coordinate, y) or current_reach.is_right() or other_reach.is_left()) {
                    current_reach.update(other_reach);
                }
            } else {
                if (is_close(fort.coordinate, y) or current_reach.is_left() or other_reach.is_right()) {
                    current_reach.update(other_reach);
                }
            }
        }
    }

    FOR(ny, y - 1, y + 1) {
        DEB("    ny=" << ny << "\n");
        if (ny < 0 || ny >= max_y) {
            continue;
        }
        auto neighbors = get_neighbors(by_y, ny, x);

        for (const Fort &fort: neighbors) {
            const Reach &other_reach = get_reach_by_fort_id(fort.fort_id);
            if (fort.coordinate < x) {
                if (is_close(fort.coordinate, x) or current_reach.is_top() or other_reach.is_bottom()) {
                    current_reach.update(other_reach);
                }
            } else {
                if (is_close(fort.coordinate, x) or current_reach.is_bottom() or other_reach.is_top()) {
                    current_reach.update(other_reach);
                }
            }
        }
    }
    return current_reach;
}

void place_fort(int fort_id, int x, int y, Reach new_reach) {
    Reach &current_reach = get_reach_by_fort_id(fort_id);
    current_reach.update(new_reach);
    vi fort_ids_to_merge;

    FOR(nx, x - 1, x + 1) {
        if (nx < 0 || nx >= max_x) {
            continue;
        }
        auto neighbors = get_neighbors(by_x, nx, y);

        for (const Fort &fort: neighbors) {
            const Reach &other_reach = get_reach_by_fort_id(fort.fort_id);
            if (fort.coordinate < y) {
                if (is_close(fort.coordinate, y) or current_reach.is_right() or other_reach.is_left()) {
                    try_push(fort_id, fort.fort_id, x, y);
                    fort_ids_to_merge.PB(fort.fort_id);
                }
            } else {
                if (is_close(fort.coordinate, y) or current_reach.is_left() or other_reach.is_right()) {
                    try_push(fort_id, fort.fort_id, x, y);
                    fort_ids_to_merge.PB(fort.fort_id);
                }
            }
        }
    }

    FOR(ny, y - 1, y + 1) {
        if (ny < 0 || ny >= max_y) {
            continue;
        }
        auto neighbors = get_neighbors(by_y, ny, x);

        for (const Fort &fort: neighbors) {
            const Reach &other_reach = get_reach_by_fort_id(fort.fort_id);
            if (fort.coordinate < x) {
                if (is_close(fort.coordinate, x) or current_reach.is_top() or other_reach.is_bottom()) {
                    try_push(fort_id, fort.fort_id, x, y);
                    fort_ids_to_merge.PB(fort.fort_id);
                }
            } else {
                if (is_close(fort.coordinate, x) or current_reach.is_bottom() or other_reach.is_top()) {
                    try_push(fort_id, fort.fort_id, x, y);
                    fort_ids_to_merge.PB(fort.fort_id);
                }
            }
        }
    }

    for (int merged_fort_id :fort_ids_to_merge) {
        merge_forts(fort_id, merged_fort_id);
    }

    by_x[x].insert(Fort{y, fort_id});
    by_y[y].insert(Fort{x, fort_id});
}

void inline one() {
    cin >> max_x >> max_y >> forts_count;
    by_x.resize(max_x);
    by_y.resize(max_y);
    DEB("INIT: " << max_x << " " << max_y << " " << forts_count << "\n");
    int current_hash = 0;
    fu_init(forts_count + 1);
    reaches.resize(forts_count + 1);
    DEB("after resize\n");
    FOR(fort_id, 1, forts_count) {
        int x_xor, y_xor, blocker_xor;
        cin >> x_xor >> y_xor >> blocker_xor;
        DEB("PROCESSING FORT: fort_id=" << fort_id << " xors:" << x_xor << " " << y_xor << " " << blocker_xor << "\n");
        int x = (x_xor ^ current_hash) % max_x;
        int y = (y_xor ^ current_hash) % max_y;
        DEB("x,y = " << x << ", " << y << "\n");
        Reach new_reach = get_reach_if_placed_fort(x, y);
        DEB("new_reach=" << new_reach << "\n");
        if (new_reach.is_blocking()) {
            current_hash ^= blocker_xor;
            cout << "TAK\n";
        } else {
            place_fort(fort_id, x, y, new_reach);
            cout << "NIE\n";
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    //int z; cin >> z; while(z--)
    one();
}