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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <set>


using namespace std;

typedef pair<int,int> ipair;
typedef pair<ipair,int> itriple;
typedef pair<ipair,ipair> ifour;


bool solve_quick(int n, int height) {

    int ix2h[n];
    vector<ipair> order1, order2;
    for(int ix=0; ix<n; ++ix) {
        int y1, y2, tmp1, tmp2;
        scanf("%i%i%i%i", &tmp1, &y1, &tmp2, &y2); //x1,y1,x2,y2
        int x1 = min(tmp1, tmp2); 
        int x2 = max(tmp1, tmp2);

        ix2h[ix] = abs(y2-y1);        
        order1.push_back(ipair(x1, ix));
    }
    for(int ix=0; ix<n; ++ix) {
        int y1, y2, tmp1, tmp2;
        scanf("%i%i%i%i", &tmp1, &y1, &tmp2, &y2); //x1,y1,x2,y2
        int x1 = min(tmp1, tmp2);
        int x2 = max(tmp1, tmp2);

        order2.push_back(ipair(x1, ix));
    }

    sort(order1.begin(), order1.end());
    sort(order2.begin(), order2.end());

    int ix2orderi[n], ix2orderf[n];
    for (int i=0; i<n; ++i) {
        ix2orderi[ order1[i].second ] = i;
        ix2orderf[ order2[i].second ] = i;
    }


    bool ix2direction[n];
    for (int ix=0; ix<n; ++ix) {
        int posi = ix2orderi[ix];
        int posf = ix2orderf[ix];
        ix2direction[ix] = posf>=posi; //forward or no move
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////

           
    vector<itriple> borders; //wektor zawierajacy poczatki i konce przesuniec: (pozycja, poczatek/koniec, indeks)
    for (int ix=0; ix<n; ++ix) {
        int posi = ix2orderi[ix];
        int posf = ix2orderf[ix];
        borders.push_back( itriple(ipair(posi, 1), ix) );
        borders.push_back( itriple(ipair(posf, 0), ix) );
    }
    sort(borders.begin(), borders.end());

    //uwaga: sa zawsze dwa elementy o tej samej wartosci
    for (int i=1; i<borders.size(); i+=2) {
        int pos1     = borders[i].first.first;
        bool begin1  = (borders[i].first.second==1);
        int ix1      = borders[i].second;
        int h1       = ix2h[ix1];

        int pos2     = borders[i-1].first.first;
        bool begin2  = (borders[i-1].first.second==1);
        int ix2      = borders[i-1].second;
        int h2       = ix2h[ix2];

        //jesli to ten sam to zignoruj
        if (ix1==ix2) continue;         

        //if (pos1!=pos2) printf("ERRRRORR!!!!");
        //jesli przeciwne kierunki i wieksze od 
        if (ix2direction[ix1]!=ix2direction[ix2] && h1+h2>height) return false; 
    }
    

    set<int> seen;          //indeksy tych ktore sa juz w lewo albo prawo
    set<ipair> forward;     //najpierw najdluzsze z tych co ida w lewo
    set<ipair> backward;    //najpierw najdluzsze z tych co ida w prawo
    for (int i=0; i<borders.size(); ++i) {
        bool begin  = (borders[i].first.second==1);
        int ix      = borders[i].second;
        int h       = ix2h[ix];

        //aktualizacja zbiorow    
        if (seen.count(ix)==0) {    //not seen before: add to sets of elements
            if (begin) {
                forward.insert( ipair(-h, ix) );                        
            } else {
                backward.insert( ipair(-h, ix) );
            }
            seen.insert(ix);        //mark as seen
        } else {                    //seen before: remove from sets of elements
            if (!begin) {
                forward.erase( ipair(-h, ix) );                        
            } else {
                backward.erase( ipair(-h, ix) );
            }                
            seen.erase(ix);         //remove from seen
        }
        
        //jesli to ten sam to zignoruj
        if (forward.begin()->second == backward.begin()->second) continue;

        //sprawdz czy sie mijaja
        int forward_maxh    = -forward.begin()->first;
        int backward_maxh   = -backward.begin()->first;
        if (forward_maxh+backward_maxh > height) {
            return false;
        }
    }

    return true;
}

bool solve_slow(int n, int wysokosc);

int main() {
    int t;
    scanf("%i", &t);
    for( ; t>0; --t) {
        int n, height;                
        scanf("%i%i", &n, &height);

        bool ok;
        if (n>1100) {
            ok = solve_quick(n, height);        
        } else {
            ok = solve_slow(n, height);
        }
        printf(ok? "TAK\n": "NIE\n");
    }
    
    return 0;
}


bool solve_slow(int n, int wysokosc) {
        int ix2h[n];
        vector<ipair> order1, order2;
        for(int ix=0; ix<n; ++ix) {
            int x1, y1, x2, y2, h, tmp1, tmp2;
            scanf("%i%i%i%i", &tmp1, &y1, &tmp2, &y2);
            x1 = min(tmp1, tmp2); 
            x2 = max(tmp1, tmp2);

            ix2h[ix] = abs(y2-y1);        
            order1.push_back( ipair(x1, ix) );
        }
        for(int ix=0; ix<n; ++ix) {
            int x1, y1, x2, y2, tmp1, tmp2;
            scanf("%i%i%i%i", &tmp1, &y1, &tmp2, &y2);
            x1 = min(tmp1, tmp2);
            x2 = max(tmp1, tmp2);

            order2.push_back( ipair(x1, ix) );
        }

        sort(order1.begin(), order1.end());
        sort(order2.begin(), order2.end());

        int ix2orderi[n], ix2orderf[n];
        for (int i=0; i<n; ++i) {
            ix2orderi[ order1[i].second ] = i;
            ix2orderf[ order2[i].second ] = i;
        }


        for (int ix1=0; ix1<n; ++ix1) {
            int posi1 = ix2orderi[ix1];
            int posf1 = ix2orderf[ix1];

            for (int ix2=ix1+1; ix2<n; ++ix2) {
                int posi2 = ix2orderi[ix2];
                int posf2 = ix2orderf[ix2];

                if ( (posi1<=posi2 && posf1>=posf2) ||
                     (posi1>=posi2 && posf1<=posf2)) {
                    if (ix2h[ix1]+ix2h[ix2]>wysokosc) {
                        return false;                   
                    }                   
                } 
 
           }
        }

        return true;
}



                //printf("cmp %i (%i->%i) %i (%i->%i)\n", ix1, posi1, posf1, ix2, posi2, posf2);


        //for (int i=0; i<order1.size(); ++i) printf("order: ix=%i x1=%i\n", order1[i].second, order1[i].first);
        //for (int i=0; i<n; ++i) printf("ix=%i posi=%i posf=%i\n", i, ix2orderi[i], ix2orderf[i]);
        

/*
        bool ix2direction[n];
        for (int ix=0; ix<n; ++ix) {
            int posi = ix2orderi[ix];
            int posf = ix2orderf[ix];
            ix2direction[ix] = posf>=posi; //forward or no move
        }
*/