import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class par { private static Parser input = new Parser(System.in); private static BufferedOutputStream output = new BufferedOutputStream( System.out); public static void main(String[] args) throws Exception { int t = input.nextInt(); while (t-- > 0) { int n = input.nextInt(); int h = input.nextInt(); Kox kox = new Kox(n); MagicTree tree = new MagicTree(n); List<Item> now = new ArrayList<Item>(); for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int d = input.nextInt(); now.add(new Item(i, Math.abs(d - b), Math.min(a, c))); kox.addHeight(Math.abs(d - b)); } List<Item> then = new ArrayList<Item>(); for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int d = input.nextInt(); then.add(new Item(i, Math.abs(d - b), Math.min(a, c))); } Collections.sort(now); Collections.sort(then); kox.organize(h); int[] nowVal = new int[n]; for (Item item : now) { int pos = kox.getPosition(item.getHeight()); int maxOK = kox.getPosOfLastOK(pos); nowVal[item.getId()] = tree.ask(maxOK + 1); tree.insert(pos); } boolean result = true; tree = new MagicTree(n); for (Item item : then) { int pos = kox.getPosition(item.getHeight()); int maxOK = kox.getPosOfLastOK(pos); if (nowVal[item.getId()] != tree.ask(maxOK + 1)) { result = false; break; } tree.insert(pos); } if (result) { output.write(("TAK\n").getBytes()); } else { output.write(("NIE\n").getBytes()); } } output.flush(); } } class Item implements Comparable<Item> { int id; int height; int x; public Item(int id, int height, int x) { super(); this.id = id; this.height = height; this.x = x; } public int getId() { return id; } public int getHeight() { return height; } public int getX() { return x; } @Override public int compareTo(Item o) { return Integer.compare(x, o.getX()); } } class Kox { private int[] heights; private int heightsCount; private int[] lastOK; private int stackj; public Kox(int maxCapacity) { heights = new int[maxCapacity]; lastOK = new int[maxCapacity]; stackj = 0; } public void addHeight(int height) { heights[stackj++] = height; } public void organize(int maxHeight) { Arrays.sort(heights); int last = heights[0]; int firstFree = 1; for (int i = 1; i < heights.length; i++) { if (heights[i] == 0) { break; } if (heights[i] == last) { continue; } heights[firstFree++] = last = heights[i]; } heightsCount = firstFree; for (int i = firstFree; i < heights.length; i++) { heights[i] = 0; } int curLast = heightsCount - 1; int curFirst = 0; while (curLast >= 0) { while (curFirst < heightsCount && heights[curFirst] + heights[curLast] <= maxHeight) { lastOK[curFirst] = curLast; curFirst++; } curLast--; } while (curFirst < heightsCount) { lastOK[curFirst] = -1; curFirst++; } } public int getPosition(int height) { int imin = 0; int imax = heightsCount - 1; while (imin < imax) { int imid = (imin + imax) / 2; if (heights[imid] < height) { imin = imid + 1; } else { imax = imid; } } if ((imax == imin) && (heights[imin] == height)) { return imin; } return -1; } public int getHeight(int position) { return heights[position]; } public int getPosOfLastOK(int pos) { return lastOK[pos]; } } class MagicTree { private int size; private int[] tree; public MagicTree(int capacity) { size = Integer.highestOneBit(capacity) << 1; tree = new int[size * 2]; } public void insert(int val) { int cur = size + val; while (cur > 0) { tree[cur]++; cur >>= 1; } } public int ask(int from) { int ca = size + from; int cb = size * 2 - 1; if (ca == cb) { return tree[ca]; } int result = tree[ca] + tree[cb]; while (ca >> 1 != cb >> 1) { if ((ca & 1) == 0) { result += tree[ca + 1]; } if ((cb & 1) == 1) { result += tree[cb - 1]; } ca >>= 1; cb >>= 1; } return result; } } class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws Exception { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } }
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 | import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class par { private static Parser input = new Parser(System.in); private static BufferedOutputStream output = new BufferedOutputStream( System.out); public static void main(String[] args) throws Exception { int t = input.nextInt(); while (t-- > 0) { int n = input.nextInt(); int h = input.nextInt(); Kox kox = new Kox(n); MagicTree tree = new MagicTree(n); List<Item> now = new ArrayList<Item>(); for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int d = input.nextInt(); now.add(new Item(i, Math.abs(d - b), Math.min(a, c))); kox.addHeight(Math.abs(d - b)); } List<Item> then = new ArrayList<Item>(); for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int d = input.nextInt(); then.add(new Item(i, Math.abs(d - b), Math.min(a, c))); } Collections.sort(now); Collections.sort(then); kox.organize(h); int[] nowVal = new int[n]; for (Item item : now) { int pos = kox.getPosition(item.getHeight()); int maxOK = kox.getPosOfLastOK(pos); nowVal[item.getId()] = tree.ask(maxOK + 1); tree.insert(pos); } boolean result = true; tree = new MagicTree(n); for (Item item : then) { int pos = kox.getPosition(item.getHeight()); int maxOK = kox.getPosOfLastOK(pos); if (nowVal[item.getId()] != tree.ask(maxOK + 1)) { result = false; break; } tree.insert(pos); } if (result) { output.write(("TAK\n").getBytes()); } else { output.write(("NIE\n").getBytes()); } } output.flush(); } } class Item implements Comparable<Item> { int id; int height; int x; public Item(int id, int height, int x) { super(); this.id = id; this.height = height; this.x = x; } public int getId() { return id; } public int getHeight() { return height; } public int getX() { return x; } @Override public int compareTo(Item o) { return Integer.compare(x, o.getX()); } } class Kox { private int[] heights; private int heightsCount; private int[] lastOK; private int stackj; public Kox(int maxCapacity) { heights = new int[maxCapacity]; lastOK = new int[maxCapacity]; stackj = 0; } public void addHeight(int height) { heights[stackj++] = height; } public void organize(int maxHeight) { Arrays.sort(heights); int last = heights[0]; int firstFree = 1; for (int i = 1; i < heights.length; i++) { if (heights[i] == 0) { break; } if (heights[i] == last) { continue; } heights[firstFree++] = last = heights[i]; } heightsCount = firstFree; for (int i = firstFree; i < heights.length; i++) { heights[i] = 0; } int curLast = heightsCount - 1; int curFirst = 0; while (curLast >= 0) { while (curFirst < heightsCount && heights[curFirst] + heights[curLast] <= maxHeight) { lastOK[curFirst] = curLast; curFirst++; } curLast--; } while (curFirst < heightsCount) { lastOK[curFirst] = -1; curFirst++; } } public int getPosition(int height) { int imin = 0; int imax = heightsCount - 1; while (imin < imax) { int imid = (imin + imax) / 2; if (heights[imid] < height) { imin = imid + 1; } else { imax = imid; } } if ((imax == imin) && (heights[imin] == height)) { return imin; } return -1; } public int getHeight(int position) { return heights[position]; } public int getPosOfLastOK(int pos) { return lastOK[pos]; } } class MagicTree { private int size; private int[] tree; public MagicTree(int capacity) { size = Integer.highestOneBit(capacity) << 1; tree = new int[size * 2]; } public void insert(int val) { int cur = size + val; while (cur > 0) { tree[cur]++; cur >>= 1; } } public int ask(int from) { int ca = size + from; int cb = size * 2 - 1; if (ca == cb) { return tree[ca]; } int result = tree[ca] + tree[cb]; while (ca >> 1 != cb >> 1) { if ((ca & 1) == 0) { result += tree[ca + 1]; } if ((cb & 1) == 1) { result += tree[cb - 1]; } ca >>= 1; cb >>= 1; } return result; } } class Parser { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Parser(InputStream in) { din = new DataInputStream(in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public int nextInt() throws Exception { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = c == '-'; if (neg) c = read(); do { ret = ret * 10 + c - '0'; c = read(); } while (c > ' '); if (neg) return -ret; return ret; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws Exception { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } |