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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <vector>
#include <cstddef>
#include <cstdio>
#include <algorithm>


/* moje drzewko - https://github.com/prazek/structures/blob/master/interval_tree.h */
template <
    typename Object,
    typename Query,
    class Alloc = std::allocator<Object>
>
class interval_tree 
{
    size_t size_;
    std::vector<Object, Alloc> vec_;
    Query query_;
    class interElement;
    friend class interElement;
public:

    typedef Object value_type;
    typedef Alloc allocator_type;

    explicit interval_tree(size_t size = 0, const Object &val = Object()) :
        size_(calc(size)),
        vec_(size_ * 2, val)
    {
        build();
    }

    interElement & operator[] (size_t index)
    {
        element_.index_ = index + size_;
        element_.ptr_ = this;
        return element_;
    }
    const Object & operator[] (size_t index) const
    {
        return vec_.at(index+size_);
    }

    Object query(size_t lhs, size_t rhs) const
    {
        lhs += size_;
        rhs += size_;
        Object result = vec_.at(lhs);
    
        if (lhs != rhs)
            result = query_(result, vec_.at(rhs));
            
        while ((lhs >> 1) != (rhs >> 1))
        {
            if (!(lhs & 1))
                result = query_(result, vec_[lhs + 1]);
            if (rhs & 1) 
                result = query_(result, vec_[rhs - 1]);
            lhs >>= 1;
            rhs >>= 1;
        }
        return result;
    }

private:
    /*
     * Function sets valid values to the rest of the tree, based on
     * values in the leafs 
     */
    void build()
    {
        for (int i = size_ - 1; i > 0 ; --i)
        {
            vec_[i] = query_(vec_[i*2], vec_[i*2 + 1]);
        }
    }
    /*
     * Function to calculate size of tree used to make full binary tree.
     * Returns first bigger or equal power of 2 than p.
     */
    int calc(unsigned int p)
    { 

        if (p == 0) return 0;
        int res = 1;
        while (res <= p){
            res <<= 1;
        }
        return res;
    }
    void insert(size_t index)
    {
        index /= 2;
        while (index != 1)
        {
            vec_[index] = query_(vec_[index*2], vec_[index*2+1]);
            index /= 2;
        }
    }

    class interElement
    {
        friend class interval_tree;
        size_t index_;
        interval_tree<Object, Query, Alloc> *ptr_;
        interElement(interval_tree *ref) : ptr_(ref)
        {
        }
    public:
        interElement() {}
        #define OPERATOR(OP)\
        const Object & operator OP (const Object &val){\
            ptr_->vec_.at(index_) OP val;\
            ptr_->insert(index_);\
            return ptr_->vec_[index_];\
        }

        OPERATOR(=)
        OPERATOR(+=)
        OPERATOR(-=)
        OPERATOR(*=)
        OPERATOR(%=)
        OPERATOR(/=)
        OPERATOR(|=)
        OPERATOR(&=)
        OPERATOR(^=)
        OPERATOR(>>=)
        OPERATOR(<<=)
        #undef OPERATOR
        operator const Object& () const
        {
            return ptr_->vec_.at(index_);
        }
    };
    static interElement element_;
};

template <typename Object,typename Query,class Alloc>
typename interval_tree<Object, Query, Alloc>::interElement 
interval_tree<Object, Query, Alloc>::element_;



bool oblicz();

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        printf("%s\n", oblicz() ? "TAK" : "NIE");
    }
}

struct Auto
{
    Auto(int x = 0, int pos = 0, int h = 0)
    : x_(x), pos_(pos), h_(h)
    {
    }
    
    bool operator < (const Auto &samochod) const
    {
        return x_ < samochod.x_;
    }
    
    int getHigh() const //Lets all get high
    {
        return h_;
    } //scores on potyczki 2014
    
    int getPosition() const
    {
        return pos_;
    }
    
private:
    int x_, pos_, h_;
};

void getAuta(std::vector<Auto> &auta, int n)
{
    for (int i = 1 ; i <= n ; i++)
    {
        int x1, y1, x2, y2;
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        auta[i] = Auto(std::min(x1, x2), i, std::abs(y2-y1));
    }
    std::sort(auta.begin() + 1, auta.end());
}

struct Max
{
    int operator()(int a, int b) const
    {
        return std::max(a, b);
    }
};

bool oblicz()
{
    int n, h;
    scanf("%d %d", &n, &h);
    interval_tree<int, Max> drzewo(n);
    
    std::vector<Auto> auta(n + 1);
    getAuta(auta, n);
    
    std::vector<int> indexys(n + 1);
    for (int i = 1 ; i <= n ; i++)
    {
        drzewo[i] = auta[i].getHigh();
        indexys[auta[i].getPosition()] = i;
    }
    
    
    std::vector<Auto> auta2(n + 1);
    
    getAuta(auta2, n);
    for (int i = 1 ; i <= n ; i++)
    {
        int index = indexys[auta2[i].getPosition()];
        
        if (drzewo.query(0, index - 1) + auta2[i].getHigh() > h)
        {
            return false;
        }
        drzewo[index] = 0;
    }
    return true;
}