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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int drzewo[ 200005 ], pos[ 50005 ], n, w;
vector < pair < pair <int,int>, int> > k[2];
inline int bez( int a ){
    if (a < 0)
        return -a;
    return a;
}
void add_tree(int pos, int s, int cs, int ce, int v){
    if ((s == cs) && (s == ce)){
        drzewo[ pos ] = v;
    }
    else if (s <= (cs+ce)/2){
        add_tree(pos*2, s, cs, (cs+ce)/2, v);
        drzewo[ pos ] = max( drzewo[pos*2], drzewo[pos*2+1] );
    }
    else {
        add_tree(pos*2+1, s, (cs+ce)/2+1, ce, v);
        drzewo[ pos ] = max( drzewo[pos*2], drzewo[pos*2+1]);
    }
}
int get_tree(int pos, int s, int e, int cs, int ce){
    if ((s == cs) && (e == ce)){
        return drzewo[pos];
    }
    else if ((s <= (cs+ce)/2) && (e <= (cs+ce)/2)){
        return get_tree(pos*2, s, e, cs, (cs+ce)/2);
    }
    else if ((s > (cs+ce)/2) && (e > (cs+ce)/2)){
        return get_tree(pos*2+1, s, e, (cs+ce)/2+1, ce);
    }
    else {
        return max( get_tree(pos*2, s, (cs+ce)/2, cs, (cs+ce)/2), get_tree(pos*2+1, (cs+ce)/2+1, e, (cs+ce)/2+1, ce));
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        scanf("%d%d",&n,&w);
        for (int i = 0; i < 2; i++){
            for (int j = 0; j < n; j++){
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                k[i].push_back( make_pair( make_pair( min(x1,x2), bez(y1-y2) ), j ) );
            }
            sort( k[i].begin(), k[i].end() );
        }
        for (int i = 1; i <= n; i++){
            add_tree( 1, i, 1, n, k[0][i-1].first.second );
            pos[ k[0][i-1].second ] = i;
        }
        bool good = true;
        for (int i = 1; i <= n; i++){
            if (pos[ k[1][i-1].second ] - 1 >= 1){
                if ( get_tree( 1, 1, pos[ k[1][i-1].second ] - 1, 1, n ) + k[1][i-1].first.second > w ){
                    good = false;
                    break;
                }
            }
            add_tree( 1, pos[ k[1][i-1].second ], 1, n, 0);
        }
        if (good){
            printf("TAK\n");
        }
        else {
            printf("NIE\n");
        }
        k[0].clear();
        k[1].clear();
    }
}