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>


using namespace std;

struct car
{
    long x1, y1;
    long x2, y2;
    long h;
    long id;
};

long g_w;

bool compare(const car& a, const car& b)
{
    if(a.x1>b.x1)
        return true;
    return false;
}

bool compare_h(const car& a, const car& b)
{
    if(a.h > b.h && a.h + b.h<=g_w)
        return true;
    return false;
}

bool compare_id(const car& a, const car& b)
{
    if(a.id > b.id && a.h + b.h<=g_w && a.h == b.h)
        return true;
    return false;
}

template <class T>
T* merge_sort(T arr[], int n, bool (*cmp)(const T&, const T&))
{
    if(n < 2)
    {
        T* narr = new T[n];
        for(int i=0; i<n; i++)
            narr[i] = arr[i];
        return narr;
    }
    int mid = n/2;
    T *arr1 = merge_sort<T>(arr,mid,cmp);
    T *arr2 = merge_sort<T>(arr+mid,n-mid,cmp);
    return merge(arr1, mid, arr2, n-mid, cmp);
}

template <class T>
T* merge(T* arr1, int size1, T* arr2, int size2, bool (*cmp)(const T&, const T&))
{
    int i = 0,j = 0;

    T* out_array = new T[size1+size2];

    while((i < size1) && (j < size2))
    {
        if(cmp(arr1[i],arr2[j]))
        {
            out_array[i+j] = arr2[j];
            ++j;
        }
        else
        {
            out_array[i+j] = arr1[i];
            ++i;
        }
    }
    while(i < size1)
    {
        //copy the reminder
        out_array[i+j] = arr1[i];
        i++;
    }
    while( j < size2)
    {
        out_array[i+j] = arr2[j];
        j++;
    }
    delete [] arr1;
    delete [] arr2;

    return out_array;
}

int main()
{
    long n, w, z, i, k, t;
    cin.sync_with_stdio(false);

    cin>>z;
    car* p_cars = new car[50000];
    car* p_new_cars = new car[50000];
    car* cars = p_cars;
    car* new_cars = p_new_cars;

    while(z--)
    {
        cars = p_cars;
        new_cars = p_new_cars;

        cin>>n>>w;
        g_w =w;

        for(i=0; i<n; i++)
        {
            cin>>cars[i].x1>>cars[i].y1>>cars[i].x2>>cars[i].y2;
            cars[i].id = i;
            cars[i].h = cars[i].y1-cars[i].y2;
            if(cars[i].h<0)
                cars[i].h*=-1;
            if(cars[i].x1>cars[i].x2)
            {
               t =  cars[i].x1;
               cars[i].x1 = cars[i].x2;
               cars[i].x2 = t;

               t = cars[i].y1;
               cars[i].y1 = cars[i].y2;
               cars[i].y2 = t;
            }
        }

        for(i=0; i<n; i++)
        {
            cin>>new_cars[i].x1>>new_cars[i].y1>>new_cars[i].x2>>new_cars[i].y2;
            new_cars[i].id = i;
            new_cars[i].h = new_cars[i].y1-new_cars[i].y2;
            if(new_cars[i].h<0)
                new_cars[i].h*=-1;

            if(new_cars[i].x1>new_cars[i].x2)
            {
               t =  new_cars[i].x1;
               new_cars[i].x1 = new_cars[i].x2;
               new_cars[i].x2 = t;

               t = new_cars[i].y1;
               new_cars[i].y1 = new_cars[i].y2;
               new_cars[i].y2 = t;
            }
        }

        car t_car;
        car* tt_cars = merge_sort<car>(cars, n, compare);
        car* tt_ncars = merge_sort<car>(new_cars, n, compare);
        cars = tt_cars;
        new_cars = tt_ncars;

        tt_cars = merge_sort<car>(cars, n, compare_h);
        tt_ncars = merge_sort<car>(new_cars, n, compare_h);

        delete [] cars;
        delete [] new_cars;

        cars = tt_cars;
        new_cars = tt_ncars;

        tt_cars = merge_sort<car>(cars, n, compare_id);
        tt_ncars = merge_sort<car>(new_cars, n, compare_id);

        delete [] cars;
        delete [] new_cars;

        cars = tt_cars;
        new_cars = tt_ncars;



        for(i=0;i<n; i++)
            if(new_cars[i].id!=cars[i].id)
            {
                cout<<"NIE"<<endl;
                break;
            }

        if(i == n)
            cout<<"TAK"<<endl;

        delete [] cars;
        delete [] new_cars;

    }
    return 0;
}