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
#include<cstdio>
#include<algorithm>
#include<set>
using namespace std;
const int N=100010,M=262150;
int n,m,_,x,y,z,last,pos[N],mi[M],ma[M];
int q[5555555][2],h,t;
set<int>row[N],col[N];
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline void umin(int&a,int b){a>b?(a=b):0;}
inline void umax(int&a,int b){a<b?(a=b):0;}
void build(int x,int a,int b){
  if(a==b){
    pos[a]=x;
    mi[x]=n+1;
    ma[x]=0;
    if(a==0)mi[x]=0;
    if(a==m+1)ma[x]=n+1;
    return;
  }
  int mid=(a+b)>>1;
  build(x<<1,a,mid),build(x<<1|1,mid+1,b);
  mi[x]=min(mi[x<<1],mi[x<<1|1]);
  ma[x]=max(ma[x<<1],ma[x<<1|1]);
}
int askmin(int x,int a,int b,int c,int d){
  if(c<=a&&b<=d)return mi[x];
  int mid=(a+b)>>1,t=N;
  if(c<=mid)t=askmin(x<<1,a,mid,c,d);
  if(d>mid)umin(t,askmin(x<<1|1,mid+1,b,c,d));
  return t;
}
int askmax(int x,int a,int b,int c,int d){
  if(c<=a&&b<=d)return ma[x];
  int mid=(a+b)>>1,t=0;
  if(c<=mid)t=askmax(x<<1,a,mid,c,d);
  if(d>mid)umax(t,askmax(x<<1|1,mid+1,b,c,d));
  return t;
}
inline void changemin(int x,int p){for(x=pos[x];x;x>>=1)umin(mi[x],p);}
inline void changemax(int x,int p){for(x=pos[x];x;x>>=1)umax(ma[x],p);}
inline int checkS(int y,int x){//-1 inside 0 border 1 not
  int t=askmin(1,0,m+1,x,m+1);
  //printf("  t=%d\n",t);
  if(t<=y)return -1;
  if(t==y+1||askmin(1,0,m+1,x-1,m+1)<=y+1)return 0;
  return 1;
}
inline int checkT(int y,int x){//-1 inside 0 border 1 not
  int t=askmax(1,0,m+1,0,x);
  if(t>=y)return -1;
  if(t==y-1||askmax(1,0,m+1,0,x+1)>=y-1)return 0;
  return 1;
}
inline void extS(int x,int y){
  q[h=t=1][0]=x;
  q[1][1]=y;
  while(h<=t){
    x=q[h][0];
    y=q[h++][1];
    changemin(y,x);
    //printf("extS %d %d\n",x,y);
    while(row[x-1].size()){
      set<int>::iterator it=row[x-1].begin();
      if((*it)<=y+1){
        q[++t][0]=x-1;
        q[t][1]=*it;
        row[x-1].erase(*it);
      }else break;
    }
    while(col[y+1].size()){
      set<int>::reverse_iterator it=col[y+1].rbegin();
      if((*it)>=x-1){
        q[++t][0]=*it;
        q[t][1]=y+1;
        col[y+1].erase(*it);
      }else break;
    }
  }
}
inline void extT(int x,int y){
  q[h=t=1][0]=x;
  q[1][1]=y;
  while(h<=t){
    x=q[h][0];
    y=q[h++][1];
    //printf("extT %d %d\n",x,y);
    changemax(y,x);
    while(row[x+1].size()){
      set<int>::reverse_iterator it=row[x+1].rbegin();
      if((*it)>=y-1){
        q[++t][0]=x+1;
        q[t][1]=*it;
        row[x+1].erase(*it);
      }else break;
    }
    while(col[y-1].size()){
      set<int>::iterator it=col[y-1].begin();
      if((*it)<=x+1){
        q[++t][0]=*it;
        q[t][1]=y-1;
        col[y-1].erase(*it);
      }else break;
    }
  }
}
inline bool merge(int x,int y){
  x++,y++;
  //printf("-> %d %d\n",x,y);
  if(x==1&&y==1)return 1;
  if(x==n&&y==m)return 1;
  int A=checkS(x,y);
  //printf("A=%d\n",A);
  if(A<0)return 0;
  int B=checkT(x,y);
  //printf("B=%d\n",B);
  if(B<0)return 0;
  if(A==0&&B==0)return 1;
  if(A==1&&B==1){
    row[x].insert(y);
    col[y].insert(x);
  }
  if(A==0)extS(x,y);
  if(B==0)extT(x,y);
  return 0;
}
int main(){
  read(n),read(m),read(_);
  build(1,0,m+1);
  while(_--){
    read(x),read(y),read(z);
    x=(x^last)%n;
    y=(y^last)%m;
    if(merge(x,y))puts("TAK"),last^=z;else puts("NIE");
  }
}