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
#include <bits/stdc++.h>
// #define MULTIPLE_TESTS
// #define ENDLESS_TESTS
#define MEMORY_LIMIT 400
#define TIME_LIMIT 5.0
// #define ADDITIONAL_LOGS // enable blocks used only for cerr

#define VARIANT 1

using namespace std;

struct Point {
    int x, y;
};

struct CompareByY
{ 
    bool operator() (const Point &lhs, const Point &rhs) const
    {
        return lhs.y < rhs.y;
    }
};

#if VARIANT == 1
struct CompareByYX
{ 
    bool operator() (const Point &lhs, const Point &rhs) const
    {
        if (lhs.y == rhs.y)
            return lhs.x < rhs.x;
        return lhs.y < rhs.y;
    }
};

struct CompareByXY
{ 
    bool operator() (const Point &lhs, const Point &rhs) const
    {
        if (lhs.x == rhs.x)
            return lhs.y < rhs.y;
        return lhs.x < rhs.x;
    }
};
#endif

class Structure
{
private:
    set<Point, CompareByY> m_stack;
#if VARIANT == 1
    set<Point, CompareByYX> m_rows;
    set<Point, CompareByXY> m_cols;
#else
    map<int, set<int>> m_rows;
    map<int, set<int>> m_cols;
#endif

public:
    Structure(int width, int height)
    {
        m_stack.insert({-1, -1});
        m_stack.insert({width, 0});
        m_stack.insert({width+1, height});
#if VARIANT == 1
        m_rows.insert({width, height});
        m_cols.insert({width, height});
#endif
    }

    bool is(int x, int y) const
    {
        auto next = m_stack.lower_bound({0, y});
        auto prev = ::prev(next);

        if (x == prev->x-1)
            return true;

        return y == next->y && x >= prev->x && x < next->x;
    }

    void expand(int x, int y)
    {
        // cerr << "expand(" << x << ", " << y << ");" << endl;
        assert(is(x, y));
        const auto end = m_stack.upper_bound({0, y});
        auto begin = end;

        int x2 = prev(end)->x;

        while (begin->x >= x)
            --begin;
        ++begin;

        int y1 = begin->y;

        m_stack.erase(begin, end);

        m_stack.insert({x, y1});
        if (end->y != y+1)
            m_stack.insert({x2, y+1});
    }

    void add(int x, int y)
    {
        // cerr << "add(" << x << ", " << y << ");" << endl;
#if VARIANT == 1
        m_rows.insert({x, y});
        m_cols.insert({x, y});
#else
        m_rows[y].insert(x);
        m_cols[x].insert(y);
#endif
    }

    void search(int x, int y, queue<Point> &points)
    {
        // cerr << "search(" << x << ", " << y << ");" << endl;
        const auto next = m_stack.upper_bound({0, y});
        const auto prev = ::prev(next);

        assert(next->y == y+1);

        // cerr << "Interesting range column = (" << x-1 << ", " << prev->y+1 << ".." << y+1 << ")" << endl;
        // cerr << "Interesting range row = (" << x-1 << ".." << next->x-1 << ", " << y+1 << ")" << endl;

        {
            const int the_x = x-1;
            const int first_y = prev->y+1;
            const int last_y = y+1;

#if VARIANT == 1
            auto it = m_cols.lower_bound({the_x, first_y});

            while(it->x == the_x && it->y <= last_y)
            {
                assert(is(it->x, it->y));
                points.push(*it);

                m_rows.erase(*it);
                it = m_cols.erase(it);
            }
#else
            auto &col = m_cols[the_x];
            auto it = col.lower_bound(first_y);

            while(it != col.end() && *it <= last_y)
            {

                assert(is(the_x, *it));
                points.push({the_x, *it});

                m_rows[*it].erase(the_x);
                it = col.erase(it);
            }
#endif
        }

        {
            const int first_x = x-1;
            const int last_x = next->x-1;
            const int the_y = y+1;
#if VARIANT == 1
            auto it = m_rows.lower_bound({first_x, the_y});

            while(it->x <= last_x && it->y == the_y)
            {
                assert(is(it->x, it->y));
                points.push(*it);
                
                m_cols.erase(*it);
                it = m_rows.erase(it);
            }
#else
            auto &row = m_rows[the_y];
            auto it = row.lower_bound(first_x);

            while(it != row.end() && *it <= last_x)
            {
                assert(is(*it, the_y));
                points.push({*it, the_y});

                m_cols[*it].erase(the_y);
                it = row.erase(it);
            }
#endif
        }
    }
};

#ifdef ADDITIONAL_LOGS
void draw(const Structure &data, int width, int height)
{
    const string border = "+" + string(width, '-') + "+";

    // cerr << border << endl;
    for (int y = 0; y < height; ++y)
    {
        // cerr << "|";
        for (int x = 0; x < width; ++x)
        {
            // cerr << (data.is(x, y) ? '#' : ' ');
        }
        // cerr << "|\n";
    }
    // cerr << border << endl;
}
#endif

void test()
{        
    int height, width, k;
    cin >> height >> width >> k;

    Structure top(width, height);
    Structure left(height, width);

    int x = 0;

    while (k-->0)
    {
        int ri, ci, zi;
        cin >> ri >> ci >> zi;

        const int r = (ri^x)%height;
        const int c = (ci^x)%width;

        const bool is1 = top.is(c, r);
        const bool is2 = left.is(r, c);

        if (is1 && is2)
        {
            cout << "TAK\n";
            x ^= zi;
            continue;
        }

        cout << "NIE\n";

        auto handle = [](Structure &s, bool is, int x, int y)
        {
            if (!is)
            {
                s.add(x, y);
                return;
            }
            
            queue<Point> expand;
            expand.push({x, y});
            while (!expand.empty())
            {
                const auto p = expand.front();
                expand.pop();

                // this can happen during chain reaction
                if (!s.is(p.x, p.y))
                    continue; 
                
                s.expand(p.x, p.y);
                s.search(p.x, p.y, expand);
            }
        };

        handle(top, is1, c, r);
        handle(left, is2, r, c);
    }
}

/******************************************************************************/

int main()
{
#ifdef CONTEST_WORKSPACE
    #ifdef MEMORY_LIMIT
        void limit_ram(float);
        limit_ram(MEMORY_LIMIT);
    #endif
    #ifdef TIME_LIMIT
        void limit_cpu(float);
        limit_cpu(TIME_LIMIT);
    #endif
#endif

#if !defined(CONTEST_WORKSPACE)
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif

#if defined(MULTIPLE_TESTS)
    int T = 0;
    cin >> T;

    while (T --> 0)
        test();

#elif defined(ENDLESS_TESTS)
    while(!(cin >> std::ws).eof())
        test();

#else
    test();
#endif

    return EXIT_SUCCESS;
}