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
#define NDEBUG

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <climits>
#include <map>
#include <set>
#include <vector>
#include <cassert>

const long long LLMAX = (std::numeric_limits<long long>()).max();
const long long LLMIN = (std::numeric_limits<long long>()).min();

struct Car {
    int x1, y1, x2, y2;

    int getW() { return y2 - y1; }
};
const int MaxCarsLen = 50000;
Car beforeBuf[MaxCarsLen];
Car afterBuf[MaxCarsLen];

struct DirectedSegment {
    void init(int x1, int x2, int w) {
        if (x1 <= x2) {
            this->x1 = x1;
            this->x2 = x2;
            this->isForward = true;
        } else {
            this->x1 = x2;
            this->x2 = x1;
            this->isForward = false;
        }

        this->w = w;
    }

    int x1, x2;
    bool isForward;

    int w;

    static bool cmpByW(DirectedSegment* a, DirectedSegment* b)
    {
        return a->w > b->w;
    }
};
DirectedSegment segmentsBuf[MaxCarsLen];

struct Endpoint {
    void init(DirectedSegment* segment, bool isX1) {
        this->segment = segment;
        this->isX1 = isX1;
    }

    DirectedSegment* segment;
    bool isX1;

    bool isX2() { return !isX1; }
    int getX() { return (isX1) ? segment->x1 : segment->x2; }

    static bool cmp(Endpoint* a, Endpoint* b)
    {
        if (a->segment != b->segment) { return a->getX() < b->getX(); }
        else                          { return a->isX1; }
    }
};
Endpoint endpointsBuf[2 * MaxCarsLen];
Endpoint* endpoints[2 * MaxCarsLen];
int endpointsLen;

std::vector<DirectedSegment*> forwards;
std::vector<DirectedSegment*> backs;

void initBuf(Car buf[], int n)
{
    for (int i=0; i < n; ++i) {
        Car& car = buf[i];

        int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

        if (x1 <= x2) {
            car.x1 = x1;
            car.x2 = x2;
        } else {
            car.x1 = x2;
            car.x2 = x1;
        }
        if (y1 <= y2) {
            car.y1 = y1;
            car.y2 = y2;
        } else {
            car.y1 = y2;
            car.y2 = y1;
        }
    }
}

int main()
{
    assert(sizeof(Car) == 16 && sizeof(DirectedSegment) == 16 && sizeof(Endpoint) == 8);
    //printf("assert(sizeof(Car) == %d && sizeof(DirectedSegment) == %d && sizeof(Endpoint) == %d);\n", sizeof(Car), sizeof(DirectedSegment), sizeof(Endpoint));

    std::ios_base::sync_with_stdio(false);

    int t; scanf("%d", &t);
    for (int it=0; it < t; ++it) {
        int n, w; scanf("%d %d", &n, &w);
        initBuf(beforeBuf, n);
        initBuf(afterBuf, n);

        for (int i=0; i < n; ++i) {
            DirectedSegment& segment = segmentsBuf[i];
            Endpoint& endpointX1 = endpointsBuf[2 * i];
            Endpoint& endpointX2 = endpointsBuf[2 * i + 1];

            Car& before = beforeBuf[i];
            Car& after = afterBuf[i];
            segment.init(before.x1, after.x1, before.getW());

            endpointX1.init(&segment, true);
            endpointX2.init(&segment, false);

            endpoints[2 * i] = &endpointsBuf[2 * i];
            endpoints[2 * i + 1] = &endpointsBuf[2 * i + 1];
        }
        endpointsLen = 2 * n;

        std::sort(endpoints, endpoints + endpointsLen, Endpoint::cmp);

        bool no = false;
        forwards.clear();
        backs.clear();
        for (int i=0; i < endpointsLen && !no; ++i) {
            Endpoint* endpoint = endpoints[i];
            if (endpoint->isX1) {
                if (endpoint->segment->isForward) {
                    std::vector<DirectedSegment*>::iterator pos = std::lower_bound(forwards.begin(), forwards.end(), endpoint->segment, DirectedSegment::cmpByW);
                    forwards.insert(pos, endpoint->segment);

                    if (!backs.empty()) {
                        DirectedSegment* back = backs[0];
                        if (endpoint->segment->w + back->w > w) {
                            no = true;
                            break;
                        }
                    }
                } else {
                    std::vector<DirectedSegment*>::iterator pos = std::lower_bound(backs.begin(), backs.end(), endpoint->segment, DirectedSegment::cmpByW);
                    backs.insert(pos, endpoint->segment);

                    if (!forwards.empty()) {
                        DirectedSegment* forward = forwards[0];
                        if (endpoint->segment->w + forward->w > w) {
                            no = true;
                            break;
                        }
                    }
                }
            } else {
                if (endpoint->segment->isForward) {
                    std::vector<DirectedSegment*>::iterator foundIt = std::find(forwards.begin(), forwards.end(), endpoint->segment);
                    assert(foundIt != forwards.end());
                    forwards.erase(foundIt);

                    for (int j=0; j < (int) forwards.size() && !no && endpoint->segment->w + forwards[j]->w > w; ++j) {
                        DirectedSegment* forward = forwards[j];
                        if (endpoint->segment->x1 >= forward->x1) {
                            no = true;
                            break;
                        }
                    }
                } else {
                    std::vector<DirectedSegment*>::iterator foundIt = std::find(backs.begin(), backs.end(), endpoint->segment);
                    assert(foundIt != backs.end());
                    backs.erase(foundIt);

                    for (int j=0; j < (int) backs.size() && !no && endpoint->segment->w + backs[j]->w > w; ++j) {
                        DirectedSegment* back = backs[j];
                        if (endpoint->segment->x1 >= back->x1) {
                            no = true;
                            break;
                        }
                    }
                }
            }
        }
        printf("%s\n", (no) ? "NIE" : "TAK");
    }
}