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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
program Para;

{$mode objfpc}{$H+}

uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes
   { you can add units after this };

Type
     TPoint=Record
        Case Boolean of
          False : (x:Int64;y:Int64);
          True  : (A:Array[0..1] of Int64);
        End;


     PLink=^TLink;
     TLink=Record
        Prev,Next:PLink;
        Sqare  : Array[0..3] of TPoint;
        Length : Longint;
        Heigth : Longint;
        Minimal: Longint;
        InPos  : Longint;
        Number : Longint;
        OutPos : Longint;
     End;

     TDat=Array[0..50001] of PLink;

     TLinkedList=Class
     Private
        Head,Last:PLink;
     Public
        Constructor Create;
        Procedure Add(var ALink:PLink);
        Procedure Clear;
        Procedure Delete(var ALink:PLink);
        Procedure Print;
     End;

     TParking=Class
        Private
           eCar   : Longint;
           eEnd   : TPoint;
           eHeigth:Longint;
        Public
        Org:TDat;
        InSort:TLinkedList;
        Constructor Create;
        Procedure   Clear;
        Procedure   MoveEnd(ALoop:Longint);
        Procedure   MoveAllEnd;
        Procedure   MoveAllTop;
        Procedure   NewCar(Const ANumber:Longint);
        Procedure   Put(ALink:PLink;Count:Longint;P1,P2:TPoint);Overload;
        Procedure   Put(Const ANumber,Count:Longint;P1,P2:TPoint);Overload;
        Procedure   SetCar(AValue:Longint);
        Procedure   SortInput;
        Procedure   SortOutput;
        Property  Car:Longint Read eCar Write SetCar;
     End;

Var Parking:TParking;
    D:TDat;

Function Min(A,B:Int64):Int64;
Begin
   If  A<B Then Result:=A Else Result:=B;
End;

Function Max(A,B:Int64):Int64;
Begin
   If  A>B Then Result:=A Else Result:=B;
End;

procedure MergeSort(var Dat:TDat;Const APos, ACount, Start, Stop : integer);
var
  Med,Rock1,Rock2,Loop : integer;
begin
  Med := (Start + Stop + 1) div 2;
  If Med-Start>1 Then MergeSort( Dat, APos, ACount, Start, Med - 1);
  If Stop-Med>0  Then MergeSort( Dat, APos, ACount, Med, Stop);
  Rock1:=Start; Rock2:=Med;
  For Loop:=Start to Stop do
    if (Rock1=Med) or ((Rock2<=Stop) and (Dat[Rock1]^.Sqare[APos].A[ACount]>Dat[Rock2]^.Sqare[APos].A[ACount])) then
    Begin
      D[Loop]:= Dat[Rock2]; inc(Rock2);
    End Else Begin
      D[Loop]:= Dat[Rock1]; inc(Rock1);
    End;
  For Loop:=Start to Stop do Dat[Loop]:=D[Loop];
End;

operator -(A,B:TPoint):TPoint;
Begin
   Result.x:=A.x-B.x;
   Result.y:=A.y-B.y;
End;

operator +(A,B:TPoint):TPoint;
Begin
   Result.x:=A.x+B.x;
   Result.y:=A.y+B.y;
End;

Function Vector(A,B:TPoint):TPoint;
Begin
   Result.x:=B.x-A.x;
   Result.y:=B.y-A.y;
End;

//----------------------------------------//
Constructor TLinkedList.Create;
Begin
   Head:=Nil;
   Last:=Nil;
End;

Procedure TLinkedList.Add(Var ALink:PLink);
Begin
   ALink^.Prev:=Last;
   ALink^.Next:=Nil;
   If Last<>Nil Then Last^.Next:=ALink;
   Last:=ALink;
   If Head =Nil Then Head:=ALink;
End;

Procedure TLinkedList.Clear;
Begin
   Head:=Nil;
   Last:=Nil;
End;

Procedure TLinkedList.Delete(var ALink:PLink);
var Prev,Next:PLink;
Begin
   Prev:=ALink^.Prev;
   Next:=ALink^.Next;
   If Prev<>Nil Then Prev^.Next:=Next;
   If Next<>Nil Then Next^.Prev:=Prev;
   If ALink=Last Then Last:=Prev;
   If ALink=Head Then Head:=Next;
End;

Procedure TlinkedList.Print;
Var T:Plink;
Begin
   T:=Head;
   While T<>Nil Do Begin
      WriteLn('X=',T^.Sqare[0].x,' Y=',T^.Sqare[0].y,' X=',T^.Sqare[1].x,' Y=',T^.Sqare[1].y,' X=',T^.Sqare[2].x,' Y=',T^.Sqare[2].y,' X=',T^.Sqare[3].x,' Y=',T^.Sqare[3].y);
      T:=T^.Next;
   End;
End;

//----------------------------------------//
Constructor TParking.Create;
var Loop:Longint;
Begin
   InSort :=TLinkedList.Create;
   eCar:=0;
   For Loop:=1 to 50000 do NewCar(Loop);
End;

Procedure TParking.Clear;
var Loop:Longint;
Begin
   InSort .Clear;
   For Loop:=1 to eCar do Begin
      With Org[Loop]^ do Begin
         Prev:=Nil;
         Next:=Nil;
      End;
   End;
End;

Procedure TParking.MoveEnd(ALoop:Longint);
var V,T:TPoint;
Begin
   T.x:=Org[ALoop]^.Sqare[0].x;
   T.y:=Org[ALoop]^.Sqare[1].y;
   V:=eEnd-T;
   With Org[ALoop]^ do Begin
       Sqare[0]:=Sqare[0]+V;
       Sqare[1]:=Sqare[1]+V;
       Heigth:=Sqare[1].y-Sqare[0].y;
       Minimal:=eHeigth-Heigth;
       Number:=ALoop;
   End;
   eEnd.x:=Org[ALoop]^.Sqare[1].x;
End;

Procedure TParking.MoveAllEnd;
Var Loop:Longint;
Begin
   eEnd.x:=2000000001;
   eEnd.y:=eHeigth;
   For Loop:=1 to Car do MoveEnd(Loop);
End;

Procedure TParking.MoveAllTop;
Var Loop:Longint;
    T,Prev:PLink;
    Test:Boolean;
Begin
   Test:=True;
   For Loop:=1 to eCar do Begin
      T:=Org[Loop];
      Prev:=T^.Prev;
      While Prev<>Nil do Begin
         If Prev^.Minimal<T^.Heigth Then Begin
            Test:=False;
            Break;
         End;
         Prev:=Prev^.Prev;
      End;
      InSort.Delete(T);
      If Not Test Then Break;
   End;
   If Test Then WriteLn('TAK') Else WriteLn('NIE');
End;

Procedure TParking.NewCar(const ANumber:Longint);
var T:PLink;
Begin
   T:=New(PLink);
   T^.Next:=Nil;
   T^.Prev:=Nil;
   Org[ANumber]:=T;
End;

Procedure TParking.Put(ALink:PLink;Count:Longint;P1,P2:TPoint);
Begin
   With ALink^ do Begin
      Sqare[Count+0].x:=Min(P1.x,P2.x);
      Sqare[Count+0].y:=Min(P1.y,p2.y);
      Sqare[Count+1].x:=Max(P1.x,P2.x);
      Sqare[Count+1].y:=Max(P1.y,P2.y);
      Length:=Sqare[Count+1].y-Sqare[Count+0].y;
      Heigth:=Sqare[Count+1].x-Sqare[Count+0].x;
      Minimal:=eHeigth-Heigth;
   End;
End;

Procedure TParking.Put(Const ANumber,Count:Longint;P1,P2:TPoint);
Begin
   With Org[ANumber]^ do Begin
//      Sqare[Count+0]:=P1;
//      Sqare[Count+1]:=P2;
      Sqare[Count+0].x:=Min(P1.x,P2.x);
      Sqare[Count+0].y:=Min(P1.y,p2.y);
      Sqare[Count+1].x:=Max(P1.x,P2.x);
      Sqare[Count+1].y:=Max(P1.y,P2.y);
      Length:=Sqare[Count+1].y-Sqare[Count+0].y;
      Heigth:=Sqare[Count+1].x-Sqare[Count+0].x;
      Minimal:=eHeigth-Heigth;
   End;
End;

Procedure TParking.SortInput;
var Loop:Longint;
Begin
   MergeSort(Org,0,1,1,Car);
   MergeSort(Org,0,0,1,Car);
   For Loop:=1 to Car do Begin
      InSort.Add(Org[Loop]);
   End;
End;

Procedure TParking.SortOutput;
Begin
   MergeSort(Org,2,1,1,Car);
   MergeSort(Org,2,0,1,Car);
End;


Procedure TParking.SetCar(AValue:Longint);
Begin
   eCar:=AValue;
End;

Procedure ReadCar(const Count,Loop:Longint);
var P1,P2:TPoint;
Begin
   ReadLn(P1.x,P1.y,P2.x,P2.y);
   Parking.Put(Loop,Count,P1,P2);
End;

Procedure ReadTest;
var Loop:Longint;
Begin
   Parking.Clear;
   ReadLn(Loop,Parking.eHeigth);
   Parking.Car:=Loop;
   For Loop:=1 to Parking.Car do ReadCar(0,Loop);
   For Loop:=1 to Parking.Car do ReadCar(2,Loop);
   Parking.SortInput;
   Parking.MoveAllEnd;
   Parking.SortOutput;
   Parking.MoveAllTop;
End;

Var Count,Loop:Longint;
begin
   For Loop:=0 to 50001 do D[Loop]:=New(PLink);
   Parking:=TParking.Create;
   ReadLn( Count );
   For Loop:=1 To Count do ReadTest;
 //  ReadLn;
end.

{
2
3 3
0 0 2 2
2 1 4 3
4 0 6 1
0 0 2 2
2 1 4 3
0 2 2 3
3 3
0 0 2 2
2 1 4 3
4 0 6 1
2 1 4 3
0 0 2 2
4 0 6 1
}