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
155
156
157
158
159
160
/*
 * par.c

 */
#include <stdio.h>
#include <stdlib.h>
int n, w, start;

struct samochod {
	int nr;
	int x1, x2, y1, y2;
	int wys;
};
struct samochod **psamochody1, **psamochody2;
struct samochod **psamochody1_sorted = 0, **psamochody2_sorted = 0;

void insert(struct samochod **sorted, struct samochod *ps) {
	int i = start + 1;
	start--;
	//fprintf(stderr, "Inserting nr %i\n", ps->nr);
	for (; i <= n - 1; i++) {
		if (ps->wys + sorted[i]->wys <= w || sorted[i]->x2 <= ps->x1)
			sorted[i - 1] = sorted[i];
		else {
			break;
		}
	}
	sorted[i - 1] = ps;
}

void corect(struct samochod * ps) {
	int tmp;
	if (ps->x1 > ps->x2) {
		tmp = ps->x1;
		ps->x1 = ps->x2;
		ps->x2 = tmp;
	}
	if (ps->y1 > ps->y2) {
		tmp = ps->y1;
		ps->y1 = ps->y2;
		ps->y2 = tmp;
	}
	ps->wys = ps->y2 - ps->y1;

}

int main() {
	int t, i, j, k;
	scanf("%i", &t);
	for (i = 0; i < t; i++) {
		scanf("%i %i", &n, &w);

		//---------------
		psamochody1 = (struct samochod **) malloc(
				n * sizeof(struct samochod *));
		psamochody1_sorted = (struct samochod **) malloc(
				n * sizeof(struct samochod *));

		for (j = 0; j < n; j++) {
			psamochody1[j] = (struct samochod *) malloc(
					sizeof(struct samochod));
			psamochody1_sorted[j] = 0;
		}
		for (j = 0; j < n; j++) {

			scanf("%i %i %i %i", &psamochody1[j]->x1, &psamochody1[j]->y1,
					&psamochody1[j]->x2, &psamochody1[j]->y2);
			psamochody1[j]->nr = j;
			corect(psamochody1[j]);
		}
		start = n - 1;
		for (j = 0; j < n; j++) {
			insert(psamochody1_sorted, psamochody1[j]);
			/*fprintf(stderr, "After insert:\n");
			 for (k = 0; k < n; k++) {
			 if (psamochody1_sorted[k]) {
			 fprintf(stderr, "%i, ", psamochody1_sorted[k]->nr);
			 fprintf(stderr, "(%i), ", psamochody1_sorted[k]->wys);

			 } else
			 fprintf(stderr, "NULL, ");
			 }
			 fprintf(stderr, "\n");*/
		}
		//fprintf(stderr, "Bef qsort\n");
		//qsort(psamochody1, n, sizeof(struct samochod *), compare);
		//fprintf(stderr, "Aft qsort\n");
//-----------------------------
		psamochody2 = (struct samochod **) malloc(
				n * sizeof(struct samochod *));
		psamochody2_sorted = (struct samochod **) malloc(
				n * sizeof(struct samochod *));
		for (j = 0; j < n; j++) {
			psamochody2[j] = (struct samochod *) malloc(
					sizeof(struct samochod));
			psamochody2_sorted[j] = 0;
		}

		for (j = 0; j < n; j++) {
			scanf("%i %i %i %i", &(psamochody2[j]->x1), &(psamochody2[j]->y1),
					&(psamochody2[j]->x2), &(psamochody2[j]->y2));
			psamochody2[j]->nr = j;
			corect(psamochody2[j]);
		}

		start = n - 1;
		for (j = 0; j < n; j++) {
			insert(psamochody2_sorted, psamochody2[j]);
			/*fprintf(stderr, "After insert:\n");
			 for (k = 0; k < n; k++) {
			 if (psamochody2_sorted[k]) {
			 fprintf(stderr, "%i, ", psamochody2_sorted[k]->nr);
			 fprintf(stderr, "(%i), ", psamochody2_sorted[k]->wys);
			 } else
			 fprintf(stderr, "NULL, ");
			 }
			 fprintf(stderr, "\n");*/
		}
		//fprintf(stderr, "Bef qsort\n");
		//qsort(psamochody2, n, sizeof(struct samochod *), compare);
		/*for (k = 0; k < n; k++) {
		 if (psamochody1[k]) {
		 fprintf(stderr, "%i, ", psamochody1[k]->nr);
		 fprintf(stderr, "(%i), ", psamochody1[k]->wys);
		 } else
		 fprintf(stderr, "NULL, ");
		 }
		 fprintf(stderr, "\n");

		 for (k = 0; k < n; k++) {
		 if (psamochody2[k]) {
		 fprintf(stderr, "%i, ", psamochody2[k]->nr);
		 fprintf(stderr, "(%i), ", psamochody2[k]->wys);
		 } else
		 fprintf(stderr, "NULL, ");
		 }
		 fprintf(stderr, "\n");*/

		for (k = 0; k < n; k++) {
			if (psamochody1_sorted[k]->nr != psamochody2_sorted[k]->nr) {
				printf("NIE\n");
				break;
			}
		}

		if (k == n)
			printf("TAK\n");

		for (j = 0; j < n; j++) {
			free(psamochody1[j]);
			free(psamochody2[j]);
		}
		free(psamochody1);
		free(psamochody2);
		free(psamochody1_sorted);
		free(psamochody2_sorted);
	}

	return 0;
}