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
#include <iostream>

struct Car {
  int lp;
  int x1;
  Car* next;
  Car* before;
} ;


int partition(Car* input, int p, int r)
{
    int pivot = input[r].x1;
    Car temp;
    while ( p < r )
    {
        while ( input[p].x1 < pivot )
            p++;

        while ( input[r].x1 > pivot )
            r--;

        if ( input[p].x1 == input[r].x1 )
            p++;
        else if ( p < r )
        {
        	temp.x1 = input[p].x1;
        	temp.lp = input[p].lp;

        	input[p].x1 = input[r].x1;
        	input[p].lp = input[r].lp;

        	input[r].x1 = temp.x1;
        	input[r].lp = temp.lp;
        }
    }

    return r;
}

void quicksort(Car* input, int p, int r)
{
    if ( p < r )
    {
        int j = partition(input, p, r);
        quicksort(input, p, j-1);
        quicksort(input, j+1, r);
    }
}

int main(int argc, char** argv) {

	int testNumber = 2;
    scanf ("%d",&testNumber);
	int carNumber;
	int height;
	Car* currentPosition = new Car[50001];
	Car* finalPosition = new Car[50001];
	int carHeight[50001];
	int freeSpace[50001];
	int mapTable[50001];

	int y1;
	int y2;
	int x2;
	for(int j = 0; j < testNumber; ++j){

		//read data
		scanf("%d %d",&carNumber, &height);
		for(int i = 0; i< carNumber; ++i){
			scanf("%d %d %d %d", &currentPosition[i].x1,&y1, &x2, &y2);
			carHeight[i] = y2-y1;
			freeSpace[i] = height - carHeight[i];
			currentPosition[i].lp = i;
		}
		for(int i = 0; i< carNumber; ++i){
			scanf("%d %d %d %d", &finalPosition[i].x1,&y1, &x2, &y2);
			finalPosition[i].lp = i;
		}
		////////

		//sort currentPosition table
		quicksort(currentPosition, 0, carNumber-1);
		quicksort(finalPosition, 0, carNumber-1);
		//


		//create list from currentPosition
		Car* listHead = &currentPosition[0];
		listHead->before = NULL;
		Car* beforeItem = &currentPosition[0];
		for(int i = 1; i < carNumber; ++i){
			beforeItem->next = &currentPosition[i];
			currentPosition[i].before = beforeItem;
			beforeItem = &currentPosition[i];
		}
		currentPosition[carNumber-1].next = NULL;
		///

		//map car position from currentPosition by car lp;
		for(int i = 0; i< carNumber; ++i){
			mapTable[currentPosition[i].lp] = i;
		}
		///

		//for each car check if possible move to final place
		int flag = 0;
		for(int i = 0; i< carNumber; ++i){
			flag = 0;
			int carLp = finalPosition[i].lp;
			Car* car = &currentPosition[mapTable[carLp]];
			Car* carBefor = car->before;
			int carH = carHeight[carLp];
			while(carBefor != NULL){
				if(freeSpace[carBefor->lp] < carH){
					flag = 1;
					break;
				}else{
					carBefor = carBefor->before;
				}
			}
			if(flag != 0){
				printf("NIE\n");
				break;
			}else{
				//remove node
				if(car->before != NULL && car->next != NULL){
					car->before->next = car->next;
					car->next->before = car->before;
				}else{
					if(car->before == NULL && car->next == NULL){
						//do nothing
					}else{
						if(car->before != NULL){
							car->before->next = NULL;
						}else{
							car->next->before = NULL;
							//listHead = car->next;
						}
					}
				}

				//
			}

		}
		if(flag == 0){
			printf("TAK\n");
		}
		//
	}

    return 0;
}