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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ISNUMERIC(x) (x >= '0' && x <= '9')

#define COMPFUNC

int getInt()
{
	static char num[12];
	int n = 0;
	int c;
	for (c = getchar(); !ISNUMERIC(c); c = getchar());
	num[n++] = c;
	for (c = getchar(); ISNUMERIC(c); c = getchar()) {
		num[n++] = (char)c;
	}
	num[n] = 0;
	return atoi(num);
}

typedef struct{
	int x;
	int y;
}Punkt;

typedef struct{
	Punkt p1;
	Punkt p2;
}Prostokat;

typedef struct{
	Prostokat poz;
	Prostokat pozDoc;
}Samochod;

typedef struct{
	int h;
	int nSam;
	Samochod *samochody;
}Parking;

#define GetProst(pros) pros.p1.x = getInt(); pros.p1.y = getInt(); pros.p2.x = getInt(); pros.p2.y = getInt()
#define H(sam) (sam.poz.p2.y - sam.poz.p1.y)

int compSam(const void *a, const void *b)
{
#define SA (((const Samochod *)a)->poz.p1)
#define SB (((const Samochod *)b)->poz.p1)

	if (SA.x < SB.x) return -1;
	if (SA.x > SB.x) return  1;

	return 0;

#undef SA
#undef SB
}


int main(int argc, char **argv)
{
	Parking parking;
	int n;
	bool sukces;


	n = getInt();

	for (int ind = 0; ind<n; ++ind) {
		parking.nSam = getInt();
		parking.h = getInt();

		parking.samochody = new Samochod[parking.nSam];

		for (int i = 0; i<parking.nSam; ++i) {
			GetProst(parking.samochody[i].poz);
		}
		for (int i = 0; i<parking.nSam; ++i) {
			GetProst(parking.samochody[i].pozDoc);
		}

		qsort(parking.samochody, parking.nSam, sizeof(Samochod), compSam);

		sukces = true;

		for (int i = 1; i<parking.nSam && sukces; ++i) {

			int x = parking.samochody[i].poz.p1.x;
			int dH = parking.h - H(parking.samochody[i]);
			int dX = parking.samochody[i].pozDoc.p1.x;

			for (int j = i - 1; j >= 0 && parking.samochody[j].poz.p1.x >= dX; --j) {
				if (H(parking.samochody[j]) > dH) {
					sukces = false;
					break;
				}
			}
		}

		puts((sukces ? "TAK" : "NIE"));

		delete parking.samochody;
	}

}