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
#include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
bool operatorMode=true; //true=x  false=y

struct Point{
	int x,y;
};

struct Segment{
	int x1,y1,x2,y2;
	bool operator<(const Segment& seg)const{
		if(operatorMode){
			if(x1!=seg.x1)
				return x1<seg.x1;
			return y1<seg.y1;
		}
		else{
			if(y1!=seg.y1)
				return y1<seg.y1;
			return x1<seg.x1;
		}
	}
	bool operator!=(const Segment& seg)const{
		return x1!=seg.x1 || x2!=seg.x2 || y1!=seg.y1 || y2!=seg.y2;
	}
	bool containsPoint(int x, int y)const{
		return x1<=x && x<=x2 && y1<=y && y<=y2;
	}
	bool vertical()const{
		return x1==x2;
	}
};

struct Field{
	set<int> *VDotContainer;
	set<int> *HDotContainer;
	set<Segment> Path;
	int n,m;
	
	void init(int nn, int mm){
		n=nn;
		m=mm;
		VDotContainer = new set<int>[m];
		HDotContainer = new set<int>[n];
		
		Path.insert({0,-1,0,0});
		Path.insert({0,0,m-1,0,});
		Path.insert({m-1,0,m-1,n-1});
		Path.insert({m-1,n-1,m,n-1});
	}
	void deinit(){
		delete []VDotContainer;
		delete []HDotContainer;
	}
	
	void addNeutralDot(int x, int y){
		VDotContainer[x].insert(y);
		HDotContainer[y].insert(x);
	}
	
	vector<Segment> addL(int x, int y){
		vector<Segment>newSegs;
		
		//find first contact
		operatorMode=true;
		auto it1 = Path.lower_bound({x,-10,x,-10}); //zawsze znajdzie pionowy odcinek
		if(it1->x1 == x)
			newSegs.push_back({x, it1->y1, x, y});
		else{
			--it1;
			newSegs.push_back({it1->x1, it1->y1, x, it1->y1});
			newSegs.push_back({x, it1->y1, x, y});
		}
		
		//find second contact
		operatorMode=false;
		auto it2 = Path.lower_bound({-10,y,-10,y});
		if(it2->y1 == y)
			newSegs.push_back({x, y, it2->x2, y});
		else{
			--it2;
			newSegs.push_back({x, y, it2->x1, y});
			newSegs.push_back({it2->x2, y, it2->x2, it2->y2});
		}
		
		//remove old segments and insert new ones
		Path.erase(it1, it2);
		Path.erase(it2);
		for(Segment seg : newSegs)
			Path.insert(seg);
		
		return move(newSegs);
	}
	
	Point firstDotOnSegment(Segment seg){
		if(seg.vertical()){
			auto it = VDotContainer[seg.x1].lower_bound(seg.y1);
			if(it==VDotContainer[seg.x1].end() || *it>seg.y2)
				return {-42,-42};
			return {seg.x1, *it};
		}
		else{
			auto it = HDotContainer[seg.y1].lower_bound(seg.x1);
			if(it==HDotContainer[seg.y1].end() || *it>seg.x2)
				return {-42, -42};
			return {*it, seg.y1};
		}
	}
	
	void addDotOnPath(int x, int y){
		addNeutralDot(x,y);
		queue<Segment>Q;
		Q.push(*findPath(x,y));
		
		while(!Q.empty()){
			Segment seg = Q.front();
			Q.pop();
			auto it = Path.find(seg);
			if(it==Path.end() || *it!=seg)
				continue;
			
			Point p = firstDotOnSegment(seg);
			if(p.x==-42)
				continue;
			vector<Segment>newSegs = addL(p.x-1, p.y+1);
			for(Segment s : newSegs)
				Q.push(s);
		}
	}
	
	bool isOnPath(int x, int y)const{
		auto it = prev(Path.lower_bound({x,y,x,y}));
		return it->containsPoint(x,y);
	}
	set<Segment>::iterator findPath(int x, int y)const{
		return prev(Path.lower_bound({x,y,x,y}));
	}
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n,m,k,x=0;
	cin >> n >> m >> k;
	
	Field f1, f2;
	f1.init(n, m);
	f2.init(m, n);
	
	while(k--){
		int r,c,z;
		cin >> r >> c >> z;
		
		int posX = (c^x)%m;
		int posY = (r^x)%n;
		
		bool onPath1 = f1.isOnPath(posX, posY);
		bool onPath2 = f2.isOnPath(posY, posX);
		
		if(onPath1 && onPath2){
			cout << "TAK\n";
			x^=z;
		}
		else{
			if(onPath1)
				f1.addDotOnPath(posX, posY);
			else
				f1.addNeutralDot(posX, posY);
			
			if(onPath2)
				f2.addDotOnPath(posY, posX);
			else
				f2.addNeutralDot(posY, posX);
			
			cout << "NIE\n";
		}
		
		
	}
	
	f1.deinit();
	f2.deinit();
	return 0;
}