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
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ST first
#define ND second
#define PB push_back
#define SIZE(a) (int(a.size()))

const int maxn = 100100;
set<int> points[2][maxn];

struct Path {
    set<pii> s;
    int t;

    Path(int t) : t(t) {}

    void addv(vector<pii> v) {
        for(pii a : v) {
            s.insert(a);
        }
    }

    bool has_point(pii a) {
        auto it = s.upper_bound(a);
        pii c = (*it);
        it--;
        pii b = (*it);
        if(b.ST == c.ST && b.ST == a.ST && b.ND <= a.ND && a.ND <= c.ND) {
            return true;
        }
        if(b.ND == c.ND && b.ND == a.ND && b.ST <= a.ST && a.ST <= c.ST) {
            return true;
        }
        return false;
    }

    void upd(pii a) {
        auto it = s.upper_bound(a);
        a.ST--;
        a.ND++;
        vector<pii> td;
        vector<pii> ta;
        ta.PB(a);
        it--;
        while(true) {
            if((*it).ST < a.ST) {
                break;
            }
            td.PB((*it));
            if(it == s.begin()) {
                break;
            }
            it--;
        }
        if(td.empty()) {
            ta.PB({a.ST, a.ND-1});
        } else if(td.back().ST > a.ST) {
            ta.PB({a.ST, td.back().ND});
        } else {
            td.pop_back();
        }

        it = s.upper_bound(a);
        while(true) {
            if((*it).ND > a.ND) {
                break;
            }
            td.PB((*it));
            it++;
            if(it == s.end()) {
                break;
            }
        }
        if(td.empty()) {
            ta.PB({a.ST+1, a.ND});
        } else if(td.back().ND < a.ND) {
            ta.PB({td.back().ST, a.ND});
        } else {
            td.pop_back();
        }

        for(pii b : td) {
            s.erase(b);
        }
        for(pii b : ta) {
            s.insert(b);
        }

        auto itr = points[t][a.ST].upper_bound(a.ND);
        if(itr != points[t][a.ST].begin()) {
            itr--;
            pii e = {a.ST, (*itr)};
            if(has_point(e)) {
                upd(e);
            }
        }

        auto itc = points[1-t][a.ND].upper_bound(a.ST-1);
        if(itc != points[1-t][a.ND].end()) {
            pii e = {(*itc), a.ND};
            if(has_point(e)) {
                upd(e);
            }
        }
    }
    
    void show() {
        for(pii x : s) {
            cerr << x.ST << "," << x.ND << " ";
        }
        cerr << "\n";
    }
};

Path p(0), q(1);

bool check(pii a, int id) {
    bool x, y;
    x = p.has_point(a);
    y = q.has_point({a.ND, a.ST});
    // cerr << "x: " << x << " y: " << y << "\n";
    if(x && y) {
        return true;
    }
    if(x) {
        p.upd(a);
    }
    if(y) {
        q.upd({a.ND, a.ST});
    }
    points[0][a.ST].insert(a.ND);
    points[1][a.ND].insert(a.ST);
    return false;
}

int main() {
    ios_base::sync_with_stdio(0);
    int n, m, k;
    cin >> n >> m >> k;
    p.addv({{0, 0}, {n-1, 0}, {n-1, m-1}});
    q.addv({{0, 0}, {m-1, 0}, {m-1, n-1}});
    int x = 0;
    for(int i=2; i < k+2; i++) {
        int r, c, z;
        // cin >> r >> c;
        cin >> r >> c >> z;
        r = ((r^x)%n+n)%n;
        c = ((c^x)%m+m)%m;
        // cerr << r << " " << c << "\n";
        bool a = check({r, c}, i);
        if(a) {
            cout << "TAK\n";
            x = x^z;
        } else {
            cout << "NIE\n";
        }
        // p.show();
        // q.show();
    }
}