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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

#define MAXN 50000

vector<pair<int,int> > order(int n, int w) {
    pair<int,int> carorder[MAXN];
    int carsizes[MAXN];
    int t1,t2,t3,t4;

    for(int i = 0; i < n; i++) {
        cin >> t1 >> t2 >> t3 >> t4;
        if(t3 > t1)
            t1 = t3;
        carsizes[i] = abs(t2 - t4);
        carorder[i] = pair<int,int>(t1,i);
    }

    sort(carorder, carorder + n);


    vector<pair<int,int> > smallcars;
    smallcars.resize(n);

    multimap<int,int, greater<int> > cars;

    for(int i = 0; i < n; i++) {
        if(carsizes[carorder[i].second] > w/2)
            while(cars.begin() != cars.end() && (*(cars.begin())).first + carsizes[carorder[i].second] > w) {
                smallcars[ (*(cars.begin())).second ].second = carorder[i].second;
                cars.erase(cars.begin());
            }

        cars.insert(pair<int,int>(carsizes[carorder[i].second], carorder[i].second));
    }
    while(!cars.empty()) {
        smallcars[ (*(cars.begin())).second ].second = -1;
        cars.erase(cars.begin());
    }

    for(int i = n-1; i >= 0; i--) {
        if(carsizes[carorder[i].second] > w/2)
            while(cars.begin() != cars.end() && (*(cars.begin())).first + carsizes[carorder[i].second] > w) {
                smallcars[ (*(cars.begin())).second ].first = carorder[i].second;
                cars.erase(cars.begin());
            }

        cars.insert(pair<int,int>(carsizes[carorder[i].second], carorder[i].second));
    }

    while(!cars.empty()) {
        smallcars[ (*(cars.begin())).second ].first = -1;
        cars.erase(cars.begin());
    }

    return smallcars;
}



void testcase() {
     int n, w;
    cin >> n >> w;
    if(order(n,w) == order(n,w))
        cout << "TAK\n";
    else
        cout << "NIE\n";
}





int main()
{
    int t;
    cin >> t;
    for(int i = 0; i < t; i++)
        testcase();
    return 0;
}