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
//created by Albert Mosialek
#include<bits/stdc++.h>
using namespace std;
set<int> warownieVertical[100001];
set<int> warownieHorizontal[100001];
int sciezkaHorizontal[100001];
int sciezkaVertical[100001];
int currentSpecialValue=0;
bool teardown=0;

void updateSciezka(int* sciezka, set<int>* warownie, int y1,int y)
{
  //cerr<<"update_sciezka("<<y1<<", "<<y<<")"<<endl;
    for(int i=y1;i<y;i++)
    {
      int oldValue = sciezka[i];
      if(sciezka[i-1]==0)
      {
        sciezka[i]=0;
      }
      else
        {
          //cerr<<"i="<<i<<endl;
            set<int>::iterator temp = warownie[i].upper_bound(sciezka[i-1]-1-1);
          //cerr<<" upper_bound(sciezka[i-1]-1-1)="<<*temp<<" sciezka[i-1]="<<sciezka[i-1]<<endl;
            set<int>::iterator first = warownie[i].begin();
            int val2=*temp;
            if(temp!=first)
                temp--;
          //cerr<<"temp==begin"<<(temp==warownie[i].begin())<<" y= "<<i<<" *temp="<<*temp<<" val2="<<val2<<endl;
            while(temp!=first and (*temp)+1==val2)
            {
                val2--;
                temp--;
            //cerr<<"temp "<<*temp<<" val2 "<<val2<<endl;
            }
            if(temp==warownie[i].begin() and (*temp)+1==val2)
            {
                val2=*temp;
              //cerr<<"first value for "<<i<<endl;
            }
            sciezka[i]=val2;
        }
        if(sciezka[i]==oldValue)
            return;
    }
}

void cout_sciezka(int* sciezka, int y)
{
    // for(int i=0;i<y;i++)
    // {
    // cerr<<"sciezka["<<i<<"] = "<<sciezka[i]<<endl;
    // }

}

int main()
{
    ios_base::sync_with_stdio(0);
    int  k, x,y,x1,y1,a,b,c;
    cin>>y>>x>>k;
    for(int i=0;i<y;i++)
    {
        warownieHorizontal[i].insert(x);
        sciezkaHorizontal[i]=x;
    }
    for(int i=0;i<x;i++)
    {
        warownieVertical[i].insert(y);
        sciezkaVertical[i]=y;
    }
    for(int i = 0;i < k;i++)
    {
      teardown=0;
        cin>>a>>b>>c;

        y1 = (a ^ currentSpecialValue) % y;
        x1 = (b ^ currentSpecialValue) % x;
     //cerr<<"stawiam wartownie w "<<x1<<", "<<y1<<endl;;
       cout_sciezka(sciezkaHorizontal,y);
       cout_sciezka(sciezkaVertical,x);
        warownieHorizontal[y1].insert(x1);
        warownieVertical[x1].insert(y1);
       //cerr<<"sciezka[y1] = "<<sciezka[y1]<<"; x1 = "<<x1<<"; sciezka[y1-1]-1 = "<<sciezka[y1-1]-1<<"; y1 = "<<y1<<endl;;
       if(y1>0)
          updateSciezka(sciezkaHorizontal, warownieHorizontal,y1,y);
        if(x1>0)
          updateSciezka(sciezkaVertical, warownieVertical,x1,x);  
        if(((y1>0 and sciezkaHorizontal[y1-1]-1 <= x1) or y1==0))
        {
            if((y1==0 and sciezkaHorizontal[0]>x1) or y1>0)
              sciezkaHorizontal[y1] = x1;
            if(y1>0)
                updateSciezka(sciezkaHorizontal, warownieHorizontal,y1,y);
            updateSciezka(sciezkaHorizontal,warownieHorizontal,y1+1,y);
          //cerr<<"tu"<<endl;
            if(sciezkaHorizontal[y-1] < x or x1==0)
            {
              teardown=1;
              
            }
        }
        if(((x1>0 and sciezkaVertical[x1-1]-1 <= y1) or x1==0))
        {
            if((x1==0 and sciezkaVertical[0]>y1) or x1>0)
                sciezkaVertical[x1] = y1;
            if(x1>0)
                updateSciezka(sciezkaVertical, warownieVertical,x1,x);
            updateSciezka(sciezkaVertical,warownieVertical,x1+1,x);
            if(sciezkaVertical[x-1] < y or y1==0)
            {
              teardown=1;
               //cerr<<"sciezka[y] = "<<sciezka[y-1]<<endl;   
            }            
        }
        if(sciezkaVertical[x-1]<y or sciezkaHorizontal[y-1]<x)
          teardown=1;
        if(teardown)
        {
                //cout_sciezka(sciezkaVertical,x);
                cout<<"TAK\n";
                currentSpecialValue = currentSpecialValue ^ c;
                warownieVertical[x1].erase(y1);
                sciezkaVertical[x1]=*(warownieVertical[x1].upper_bound(x1-1));//sprawdzic czy upperbound
                if(x1>0)
                  updateSciezka(sciezkaVertical,warownieVertical,x1,x);
                updateSciezka(sciezkaVertical,warownieVertical,x1 + 1,x);

                //cout_sciezka(sciezkaVertical,x);
                warownieHorizontal[y1].erase(x1);
                sciezkaHorizontal[y1]=*(warownieHorizontal[y1].upper_bound(x1-1));//sprawdzic czy upperbound
                if(y1>0)
                  updateSciezka(sciezkaHorizontal,warownieHorizontal,y1,y);
                updateSciezka(sciezkaHorizontal,warownieHorizontal,y1 + 1,y);
        }
        else
        {
            cout<<"NIE\n";
        }
    }
    return 0;
}