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
// Artur Kraska, II UWr

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <list>
#include <set>
#include <map>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define deb(X)                      ;

#define M 1000000007
#define INF 1000000007

//#define S 65536

using namespace std;

int n, w;
int a, b, c, e;
int d[200007];
int S;

struct samochod
{
    int wys;
    int p_pocz, p_kon;
    int nr;
};
samochod tab[100007];

static inline bool comp_pocz(const samochod &s1, const samochod &s2)
{
    return s1.p_pocz < s2.p_pocz;
}

static inline bool comp_kon(const samochod &s1, const samochod &s2)
{
    return s1.p_kon < s2.p_kon;
}

static inline void wkladaj(int ile, int nr)
{
    nr += S;
    d[nr] = ile;

    nr >>= 1;
    while(nr > 0)
    {
        d[nr] = max(d[nr*2], d[nr*2+1]);
        nr >>= 1;
    }
}

static inline int maks(int nr)
{
    nr += S;
    int m = 0;

    while(nr > 0)
    {
        if(nr&1)
            m = max(m, d[nr-1]);
        nr >>= 1;
    }

    return m;
}

int main()
{
    testy()
    {
        scanf("%d %d", &n, &w);

        S = 1;
        while(S < n)
            S *= 2;

        forr(i, n)
        {
            scanf("%d %d %d %d", &a, &b, &c, &e);
            tab[i].wys = abs(e-b);
            tab[i].p_pocz = min(a, c);
            tab[i].nr = i;
        }
        forr(i, n)
        {
            scanf("%d %d %d %d", &a, &b, &c, &e);
            //if(tab[i].wys != abs(e-b))
            //    printf("ALERT! (chyba niepoprawne testy)\n");
            tab[i].p_kon = min(a, c);
        }

        sort(tab, tab+n, comp_pocz);

        forr(i, n)
        {
            tab[i].p_pocz = i;
            d[i+S] = tab[i].wys;
        }
        FOR(i, S+n, 2*S)
            d[i] = 0;
        FORD(i, S-1, 1)
            d[i] = max(d[i*2], d[i*2+1]);

        sort(tab, tab+n, comp_kon);

        bool b = 1;
        forr(i, n)
        {
            //cout << "tu" << endl;
            int m = maks(tab[i].p_pocz);
            deb(cout << "samochod " << tab[i].p_pocz << " - maks: " << m << ", wys: " << tab[i].wys << " (liczy maksa z " << tab[i].p_pocz << ")" << endl);
            if(m + tab[i].wys > w)
            {
                b = 0;
                break;
            }
            wkladaj(0, tab[i].p_pocz);
        }

        printf(b ? "TAK\n" : "NIE\n");
    }

	return 0;
}