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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>

using namespace std;

void addToList(int, int, int);
void showList();
void mirrors();
void releaseMemory();

struct Firm
{
	// Rozpietosc szerokosci lustra
	int widthRangeMirror;
	// Rozpietosc wysokosci lustra
	int heightRangeMirror;
	// Aktualny numer firmy
	int firmNumber;
	// Wskaznik na nastepnika w liscie
	Firm *next;
	// Wskaznik na poprzednika w liscie
	Firm *prev;
};

// Wskaznik na poczatek i koniec listy
Firm *head = NULL, *tile = NULL;

int main() {
	// Przypadki testowe, Liczba zakladow produkcji luster
	short int t, n; 
	
	// Wysokosci (w1, w2) oraz Szerokosci luster (h1, h2)
	int wOne, wTwo, hOne, hTwo;
	
	int wMinus, hMinus, nr = 0; 
	
	// Wczytanie ilosc testow
	cin >> t;
	while(t--)
	{
		// Wczytanie ilosci zakladow produkcji luster ktore zlozyly oferte w przetargu
		cin >> n;
		while(n--)
		{
			nr += 1;
			cin >> wOne;
			cin >> wTwo;
			cin >> hOne;
			cin >> hTwo;
			
			wMinus = (wTwo-wOne)+1;
			hMinus = (hTwo-hOne)+1;
			
			addToList(wMinus, hMinus, nr);
		}
		
		mirrors();
		releaseMemory();
		//showList();
	}
	
	return 0;
}

void addToList(int w, int h, int nr)
{
	Firm *tmp;
	Firm *newElement = new Firm;
	newElement->widthRangeMirror = w;
	newElement->heightRangeMirror = h;
	newElement->firmNumber = nr;
	
	// Dodaje pierwszy element
	if(head == NULL)
	{
		newElement->next = NULL;
		newElement->prev = NULL;
		head = newElement;
		tile = newElement;
	}
	// Dodaje kolejny element jesli glowa nie jest pusta
	else
	{
		// Dodanie na koncu
		if(tile->widthRangeMirror >= w && tile->next == NULL)
		{
			tile->next = newElement;
			newElement->prev = tile;
			tile = newElement;
		}
		// Dodanie na poczatku
		else if(w > head->widthRangeMirror && head->prev == NULL)
		{
			head->prev = newElement;
			newElement->next = head;
			head = newElement;	
		}
		// Dodanie po srodku
		else
		{
			Firm *tmp = head;
			while(tmp->widthRangeMirror > w && tmp->next->widthRangeMirror > w)
			{
				tmp = tmp->next;
			}	
			// Wstaw element na swoje miejsce
			tmp->next->prev = newElement;
			newElement->next = tmp->next;
			newElement->prev = tmp;
			tmp->next = newElement;	
		}
	}
}

void showList()
{
	Firm *t = head;
	do
	{
		t = t->next;
	}while(t->next != NULL);	
}

void releaseMemory()
{
	Firm *tmp = head;
	while(head->next != NULL)
	{	
		tmp = tmp->next;
		delete head;
		head = tmp;
	}
	
	delete head;
	head = NULL;
	
	delete tile;
	tile = NULL;
	
	delete tmp;
}

void mirrors()
{
	Firm *tOne = head;
	Firm *tTwo = tOne;
	int tmpWidth, tmpHeight;
	bool check = false;
	
	tmpWidth = tOne->widthRangeMirror;
	tmpHeight = tOne->heightRangeMirror;
	
	while(tOne->next != NULL)
	{
		while(tTwo->next != NULL)
		{
			if(tmpWidth >= tTwo->next->widthRangeMirror && tmpHeight >= tTwo->next->heightRangeMirror)
			{
				tTwo = tTwo->next;
				check = true;
			}
			else
			{
				tOne = tOne->next;
				tmpWidth = tOne->widthRangeMirror;
				tmpHeight = tOne->heightRangeMirror;
				tTwo = tOne;
				check = false;
				break;
			}
		}
		
		if(tTwo->next == NULL)
		{
			break;
		}
	}
	
	if(check == true)
	{
		cout << "TAK" << endl;
	}
	else
	{
		cout << "NIE" << endl;
	}
	
}