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

struct Dirs {
    mutable bool bottom{}, top{}, left{}, right{};
};

struct Fortress {
    int x{}, y{};
    mutable Dirs *dirs;
    struct xyCompare {
        bool operator()(const Fortress &f1, const Fortress &f2) const {
            if (f1.x < f2.x) return true;
            if (f1.x > f2.x) return false;
            return f1.y < f2.y;
        }
    };
    struct yxCompare {
        bool operator()(const Fortress &f1, const Fortress &f2) const {
            if (f1.y < f2.y) return true;
            if (f1.y > f2.y) return false;
            return f1.x < f2.x;
        }
    };
};

std::set<Fortress, Fortress::xyCompare> xyFortresses;
std::set<Fortress, Fortress::yxCompare> yxFortresses;

bool handleFortress(int m, int n, int x, int y);
bool hasBottomConnection(int m, int n, int x, int y);
bool hasTopConnection(int m, int n, int x, int y);
bool hasLeftConnection(int m, int n, int x, int y);
bool hasRightConnection(int m, int n, int x, int y);
void addFortress(const Fortress &);
void updateBottomConnection(std::set<Fortress, Fortress::yxCompare>::iterator);
void updateTopConnection(std::set<Fortress, Fortress::yxCompare>::iterator);
void updateLeftConnection(std::set<Fortress, Fortress::xyCompare>::iterator);
void updateRightConnection(std::set<Fortress, Fortress::xyCompare>::iterator);

int main() {
    int n, m, k, r, c, z, x{};
    std::cin >> n >> m >> k;
    for (int i = 0; i < k; ++i) {
        std::cin >> r >> c >> z;
        r = (r xor x) % n;
        c = (c xor x) % m;
        bool destroyed = handleFortress(m, n, c, r);
        if (destroyed) {
            x = z;
        }
        std::cout << (destroyed ? "TAK" : "NIE") << '\n';
    }
    return 0;
}


bool handleFortress(int m, int n, int x, int y) {
    bool bottom = hasBottomConnection(m, n, x, y);
    bool top    = hasTopConnection(m, n, x, y);
    bool left   = hasLeftConnection(m, n, x, y);
    bool right  = hasRightConnection(m, n, x, y);
    if ((bottom or right) and (top or left)) {
        return true;
    }
    addFortress({x, y, new Dirs{bottom, top, left, right}});
    return false;
}

void addFortress(const Fortress &fortress) {
    auto xyIt = xyFortresses.insert(fortress);
    auto yxIt = yxFortresses.insert(fortress);

    updateBottomConnection(yxIt.first);
    updateTopConnection(yxIt.first);
    updateLeftConnection(xyIt.first);
    updateRightConnection(xyIt.first);
}

void updateBottomConnection(std::set<Fortress, Fortress::yxCompare>::iterator startPos) {
    if (not startPos->dirs->bottom) {
        return;
    }

    for (decltype(startPos) next;; startPos = next) {
        next = yxFortresses.lower_bound({startPos->x - 1, startPos->y + 1});
        if (next == yxFortresses.end()) break;
        if (next->y != startPos->y + 1 or next->dirs->bottom) break;
        for (auto it = next; it != yxFortresses.end() and it->y == next->y and not it->dirs->bottom; ++it) {
            it->dirs->bottom = true;
        }
    }
}

void updateTopConnection(std::set<Fortress, Fortress::yxCompare>::iterator startPos) {
    if (not startPos->dirs->top) {
        return;
    }

    for (decltype(startPos) next;; startPos = next) {
        next = yxFortresses.upper_bound({startPos->x + 1, startPos->y - 1});
        if (next == yxFortresses.begin()) break;
        --next;
        if (next->y != startPos->y - 1 or next->dirs->top) break;
        for (auto it = next; it->y == next->y and not it->dirs->top; --it) {
            it->dirs->top = true;
            if (it == yxFortresses.begin()) break;
        }
    }
}

void updateLeftConnection(std::set<Fortress, Fortress::xyCompare>::iterator startPos) {
    if (not startPos->dirs->left) {
        return;
    }

    for (decltype(startPos) next;; startPos = next) {
        next = xyFortresses.lower_bound({startPos->x + 1, startPos->y - 1});
        if (next == xyFortresses.end()) break;
        if (next->x != startPos->x + 1 or next->dirs->left) break;
        for (auto it = next; it != xyFortresses.end() and it->x == next->x and not it->dirs->left; ++it) {
            it->dirs->left = true;
        }
    }
}

void updateRightConnection(std::set<Fortress, Fortress::xyCompare>::iterator startPos) {
    if (not startPos->dirs->right) {
        return;
    }

    for (decltype(startPos) next;; startPos = next) {
        next = xyFortresses.upper_bound({startPos->x - 1, startPos->y + 1});
        if (next == xyFortresses.begin()) break;
        --next;
        if (next->x != startPos->x - 1 or next->dirs->right) break;
        for (auto it = next; it->x == next->x and not it->dirs->right; --it) {
            it->dirs->right = true;
            if (it == xyFortresses.begin()) break;
        }
    }
}

bool hasBottomConnection(int m, int n, int x, int y) {
    if (y == 0 or x == n- 1) return true;
    auto it = yxFortresses.upper_bound({x + 1, y - 1});
    if (it == yxFortresses.begin()) return false;
    --it;
    return it->y == y - 1 and it->dirs->bottom;
}

bool hasTopConnection(int m, int n, int x, int y) {
    if (y == n - 1 or x == 0) return true;
    auto it = yxFortresses.lower_bound({x - 1, y + 1});
    if (it == yxFortresses.end()) return false;
    return it->y == y + 1 and it->dirs->top;
}

bool hasLeftConnection(int m, int n, int x, int y) {
    if (x == 0 or y == n - 1) return true;
    auto it = xyFortresses.upper_bound({x - 1, y + 1});
    if (it == xyFortresses.begin()) return false;
    --it;
    return it->x == x - 1 and it->dirs->left;
}

bool hasRightConnection(int m, int n, int x, int y) {
    if (x == m - 1 or y == 0) return true;
    auto it = xyFortresses.lower_bound({x + 1, y - 1});
    if (it == xyFortresses.end()) return false;
    return it->x == x + 1 and it->dirs->right;
}