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
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

long examples = 0L;
long parkH = 0L;
long cars = 0L;

class Car {
public:
   long nr;
   long minX;
   long height;
   long posBefore;

   Car(long n, long x, long h) : nr(n), minX(x), height(h), posBefore(0L) {}
};

struct carHeightComparator {
   bool operator() (const Car& lhc, const Car& rhc) const
   {
      if(lhc.height > rhc.height)
         return true;
      else if(lhc.height == rhc.height)
      {
         if(lhc.nr < rhc.nr)
            return true;
      }

      return false;
   }
};

struct carComparator {
   bool operator() (const Car& lhc, const Car& rhc) const
   {
      if(lhc.minX < rhc.minX)
         return true;
      else if(lhc.minX == rhc.minX)
      {
         if(lhc.nr < rhc.nr)
            return true;
      }

      return false;
   }
};

struct carPosComparator {
   bool operator() (const Car& lhc, const Car& rhc) const
   {
      if(lhc.posBefore < rhc.posBefore)
         return true;

      return false;
   }
};


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

   scanf("%ld\n", &examples);
   long x1 = 0L, x2 = 0L, y1 = 0L, y2 = 0L, carH = 0L;

   for (long ex = 1; ex <= examples; ++ex)
   {
      vector<Car> vecC1;
      vector<Car> vecC2;
      scanf("%ld %ld\n", &cars, &parkH);

      vecC1.reserve(cars);
      for (long cc = 1L; cc <= cars; ++cc)
      {
         scanf("%ld %ld %ld %ld\n", &x1, &y1, &x2, &y2);
         vecC1.push_back(Car(cc, x1 < x2 ? x1 : x2, y1 < y2 ? y2 - y1 : y1 - y2));
      }

      vecC2.reserve(cars);
      for (long cc = 1L; cc <= cars; ++cc)
      {
         scanf("%ld %ld %ld %ld\n", &x1, &y1, &x2, &y2);
         vecC2.push_back(Car(cc, x1 < x2 ? x1 : x2, y1 < y2 ? y2 - y1 : y1 - y2));
      }

      if(cars == 1L)
      {
         printf("TAK\n");
         continue;
      }

      vector<Car> vecFinal(vecC1);
      sort(vecC1.begin(), vecC1.end(), carComparator());
      sort(vecC2.begin(), vecC2.end(), carComparator());

      size_t vecCSize = vecC1.size();
      for (size_t vidx = 0; vidx < vecCSize; ++vidx)
         vecFinal[vecC1[vidx].nr - 1].posBefore = vidx + 1;
      
      sort(vecFinal.begin(), vecFinal.end(), carHeightComparator());
      if(vecFinal[0].height + vecFinal[1].height <= parkH)
      {
         printf("TAK\n");
         continue;
      }

      bool bBlocked = false;
      set<Car, carPosComparator> setPoss;
      setPoss.insert(vecFinal.begin(), vecFinal.end());
      set<Car, carPosComparator>::iterator sit;

      for(size_t idxF1 = 0, sen1 = vecC2.size() - 1; idxF1 < sen1 && !bBlocked; ++idxF1)
      {
         sit = setPoss.begin();
         set<Car, carPosComparator>::iterator sentinel = setPoss.end();
         Car cC = vecC2[idxF1];
         long maxH = parkH - cC.height;

         while(sit != sentinel)
         {
            if(cC.nr == sit->nr)
            {
               setPoss.erase(sit);
               break;
            }

            if(sit->height > maxH)
            {
               bBlocked = true;
               break;
            }

            ++sit;
         }
      }      

      if(bBlocked)
         printf("NIE\n");
      else
         printf("TAK\n");
   }

   return 0;
}