import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.LinkedList; import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.Map.Entry; import java.util.TreeMap; public class par { private static class Vector { int direction; int x1, x2; Car c; @Override public String toString() { return "[v] " + "[" + x1 + "," + x2 + "]" + " " + direction; } } private static class Car { int startX1, startY1, startX2, startY2; int endX1, /*endY1,*/ endX2 /*, endY2*/; final Vector v; int height; Car() { v = new Vector(); } private final void createVector() { v.c = this; if (startX1 > endX1) { v.direction = -1; v.x1 = endX1; v.x2 = startX2; } else if (startX1 < endX1) { v.direction = 1; v.x1 = startX1; v.x2 = endX2; } else { v.direction = 0; v.x1 = startX1; v.x2 = startX2; } height = startY2 - startY1; // System.out.println(v); } } private static class Point { final int x; final LinkedList<Vector> vectors; Point(int x) { this.x = x; vectors = new LinkedList<>(); } @Override public String toString() { return "([p] " + x + "<" + vectors.size() + ">)"; } } public static void main(String[] args) { try { final BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // final BufferedReader bf = new BufferedReader(new FileReader( // new File("C:\\Users\\Szafa\\prog\\Potyczki2014\\par\\dane"))); int t = Integer.parseInt(bf.readLine()); // System.out.println("t=" + t); for (int tt = 0; tt < t; tt++) { boolean success = true; String line[] = bf.readLine().split(" "); int n = Integer.parseInt(line[0]); int H = Integer.parseInt(line[1]); // System.out.println("t=" + t); Car[] cars = new Car[n]; TreeMap<Integer, Point> points = new TreeMap<>(); for (int i = 0; i < n; i++) { Car c = new Car(); line = bf.readLine().split(" "); c.startX1 = Integer.parseInt(line[0]); c.startY1 = Integer.parseInt(line[1]); c.startX2 = Integer.parseInt(line[2]); c.startY2 = Integer.parseInt(line[3]); cars[i] = c; } for (int i = 0; i < n; i++) { Car c = cars[i]; line = bf.readLine().split(" "); c.endX1 = Integer.parseInt(line[0]); // c.endY1 = Integer.parseInt(line[1]); c.endX2 = Integer.parseInt(line[2]); // c.endY2 = Integer.parseInt(line[3]); c.createVector(); Integer p = new Integer(c.v.x1); Point mapPoint = points.get(p); if(mapPoint == null) { mapPoint = new Point(c.v.x1); points.put(p, mapPoint); } mapPoint.vectors.add(c.v); p = new Integer(c.v.x2); mapPoint = points.get(p); if(mapPoint == null) { mapPoint = new Point(c.v.x2); points.put(p, mapPoint); } mapPoint.vectors.add(c.v); } // System.out.println("\n\n-------\n"); HashSet<Vector> vectors = new HashSet<>(); Entry<Integer, Point> entry = points.pollFirstEntry(); while (entry != null) { Point p = entry.getValue(); // System.out.println("processing point" + p+"\n"); ListIterator<Vector> iterator = p.vectors.listIterator(); Vector v = iterator.next(); while(v != null) { if (v.x2 == p.x) { // System.out.println("processing vector" + v); vectors.remove(v); for (Vector realatedVector : vectors) { if ((realatedVector.x1 <= v.x1 && realatedVector.x2 >= v.x2)) { // System.out.println("zawiera sie w " + realatedVector); if (realatedVector.c.height + v.c.height > H) { success = false; break; } } else { // System.out.println("nachodzi na " + realatedVector); if (realatedVector.direction != v.direction) { if (realatedVector.c.height + v.c.height > H) { success = false; break; } } } } iterator.remove(); } if (!success) { break; } try { v = iterator.next(); } catch (NoSuchElementException e) { v = null; } } // put all remaining point to vectors // System.out.println("adding" + p.vectors); vectors.addAll(p.vectors); if (!success) { break; } entry = points.pollFirstEntry(); } if (success) { System.out.println("TAK"); } else { System.out.println("NIE"); } } bf.close(); } catch (Throwable t) { t.printStackTrace(); } } }
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 | import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.LinkedList; import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.Map.Entry; import java.util.TreeMap; public class par { private static class Vector { int direction; int x1, x2; Car c; @Override public String toString() { return "[v] " + "[" + x1 + "," + x2 + "]" + " " + direction; } } private static class Car { int startX1, startY1, startX2, startY2; int endX1, /*endY1,*/ endX2 /*, endY2*/; final Vector v; int height; Car() { v = new Vector(); } private final void createVector() { v.c = this; if (startX1 > endX1) { v.direction = -1; v.x1 = endX1; v.x2 = startX2; } else if (startX1 < endX1) { v.direction = 1; v.x1 = startX1; v.x2 = endX2; } else { v.direction = 0; v.x1 = startX1; v.x2 = startX2; } height = startY2 - startY1; // System.out.println(v); } } private static class Point { final int x; final LinkedList<Vector> vectors; Point(int x) { this.x = x; vectors = new LinkedList<>(); } @Override public String toString() { return "([p] " + x + "<" + vectors.size() + ">)"; } } public static void main(String[] args) { try { final BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // final BufferedReader bf = new BufferedReader(new FileReader( // new File("C:\\Users\\Szafa\\prog\\Potyczki2014\\par\\dane"))); int t = Integer.parseInt(bf.readLine()); // System.out.println("t=" + t); for (int tt = 0; tt < t; tt++) { boolean success = true; String line[] = bf.readLine().split(" "); int n = Integer.parseInt(line[0]); int H = Integer.parseInt(line[1]); // System.out.println("t=" + t); Car[] cars = new Car[n]; TreeMap<Integer, Point> points = new TreeMap<>(); for (int i = 0; i < n; i++) { Car c = new Car(); line = bf.readLine().split(" "); c.startX1 = Integer.parseInt(line[0]); c.startY1 = Integer.parseInt(line[1]); c.startX2 = Integer.parseInt(line[2]); c.startY2 = Integer.parseInt(line[3]); cars[i] = c; } for (int i = 0; i < n; i++) { Car c = cars[i]; line = bf.readLine().split(" "); c.endX1 = Integer.parseInt(line[0]); // c.endY1 = Integer.parseInt(line[1]); c.endX2 = Integer.parseInt(line[2]); // c.endY2 = Integer.parseInt(line[3]); c.createVector(); Integer p = new Integer(c.v.x1); Point mapPoint = points.get(p); if(mapPoint == null) { mapPoint = new Point(c.v.x1); points.put(p, mapPoint); } mapPoint.vectors.add(c.v); p = new Integer(c.v.x2); mapPoint = points.get(p); if(mapPoint == null) { mapPoint = new Point(c.v.x2); points.put(p, mapPoint); } mapPoint.vectors.add(c.v); } // System.out.println("\n\n-------\n"); HashSet<Vector> vectors = new HashSet<>(); Entry<Integer, Point> entry = points.pollFirstEntry(); while (entry != null) { Point p = entry.getValue(); // System.out.println("processing point" + p+"\n"); ListIterator<Vector> iterator = p.vectors.listIterator(); Vector v = iterator.next(); while(v != null) { if (v.x2 == p.x) { // System.out.println("processing vector" + v); vectors.remove(v); for (Vector realatedVector : vectors) { if ((realatedVector.x1 <= v.x1 && realatedVector.x2 >= v.x2)) { // System.out.println("zawiera sie w " + realatedVector); if (realatedVector.c.height + v.c.height > H) { success = false; break; } } else { // System.out.println("nachodzi na " + realatedVector); if (realatedVector.direction != v.direction) { if (realatedVector.c.height + v.c.height > H) { success = false; break; } } } } iterator.remove(); } if (!success) { break; } try { v = iterator.next(); } catch (NoSuchElementException e) { v = null; } } // put all remaining point to vectors // System.out.println("adding" + p.vectors); vectors.addAll(p.vectors); if (!success) { break; } entry = points.pollFirstEntry(); } if (success) { System.out.println("TAK"); } else { System.out.println("NIE"); } } bf.close(); } catch (Throwable t) { t.printStackTrace(); } } } |