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

int wysokosc[50001];
int w;


int merge(int arr[], int temp[], int left, int mid, int right)

{
    int i, j, k;
    int inv_count = 0;
    i = left;
    j = mid;
    k = left;

    while ((i <= mid - 1) && (j <= right))
    {
        if (arr[i] <= arr[j])
        {
            temp[k++] = arr[i++];
        }
        else
        {
        	//cout << "Wysokosci na pozycjach " << arr[i] << " oraz " << arr[j] << ":" << wysokosc[arr[i]] << " " <<  wysokosc[arr[j]] << endl;
        	if(wysokosc[arr[i]] + wysokosc[arr[j]] > w) return -1;
            temp[k++] = arr[j++];
            inv_count = inv_count + (mid - i);
        }
    }
    while (i <= mid - 1)
        temp[k++] = arr[i++];
    while (j <= right)
        temp[k++] = arr[j++];
    for (i = left; i <= right; i++)
        arr[i] = temp[i]; 
    return inv_count;

}

int mSort(int arr[], int temp[], int left, int right)
{

    int mid, inv_count = 0;

    if (right > left)

    {

        mid = (right + left)/2;

        int x = mSort(arr, temp, left, mid);
        if(x == -1) return -1;
        inv_count = x;
        int y = mSort(arr, temp, mid+1, right);
        if(y == -1) return -1;
        inv_count += y;
        int z = merge(arr, temp, left, mid+1, right);
        if(z == -1) return -1;
        inv_count += z;

    }

    return inv_count;

}

int mergeSort(int arr[], int array_size)
{

    int *temp = new int [array_size];

    return mSort(arr, temp, 0, array_size - 1);

}


int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n;
		int a, b, c, d;
		cin >> n >> w;
		vector< pair<int, int> > A, B; //x, wysokosc
		

		for(int i = 0; i < n; i++)
		{
			cin >> a >> b >> c >> d;
			A.push_back(make_pair(a, i));
			wysokosc[i] = d - b;
		}

		for(unsigned int i = 0; i < n; i++)
		{
			cin >> a >> b >> c >> d;
			B.push_back(make_pair(a, i));
		}

		auto comp = []( pair<int, int> a, pair<int, int> b) { return a.first < b.first; };
		sort(B.begin(), B.end(), comp);
		/*
		cout << endl << "Sorted B" << endl;
		for(int i = 0; i < B.size(); ++i)
		{
			cout << B[i].first << " " << B[i].second << endl;
		}*/

		vector<int> H(A.size());
		vector<int> T(A.size());

		for(int i = 0; i < A.size(); ++i)
		{
			H[A[i].second] = i;
		}

		for(int i = 0; i < A.size(); ++i)
		{
			T[i] = B[A[i].second].second;
		}

		if(mergeSort(&T[0], T.size()) == -1) cout << "NIE\n";
		else cout << "TAK\n";
	}
	return 0;
}