import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; public class ple { public static class Area implements Comparable<Area> { int x1, x2, y1, y2; public Area(int x1, int x2, int y1, int y2) { super(); this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } public String toStringDebug() { return "(" + x1 + "," + x2 + ";" + y1 + "," + y2 + ")"; } public String toString() { return x1 + " " + x2 + " " + y1 + " " + y2; } public boolean isInside(int x, int y) { return x1 < x && x < x2 && y1 < y && y < y2; } public boolean isOverlaping(Area o) { // return (isInside(o.x1,o.y1) || isInside(o.x1,o.y2) || // isInside(o.x2,o.y1) || isInside(o.x2,o.y2)); return (x1 < o.x2 && x2 > o.x1 && y1 < o.y2 && y2 > o.y1); // return true; } public static Area minContainingArea(Area a, Area b) { return new Area(Math.min(a.x1, b.x1), Math.max(a.x2, b.x2), Math .min(a.y1, b.y1), Math.max(a.y2, b.y2)); } @Override public int compareTo(Area o) { int res = 0; res = (res == 0 ? x1 - o.x1 : res); res = (res == 0 ? x2 - o.x2 : res); res = (res == 0 ? y1 - o.y1 : res); res = (res == 0 ? y2 - o.y2 : res); return res; } } public static class AreaSet { Set<Area> areas = new HashSet<Area>(); public int size() { return areas.size(); } public void add(Area area) { Area adding = area; while(adding != null) { Area overlaping = null; for (Area a : areas) { if (adding.isOverlaping(a)) { overlaping = a; break; } } if (overlaping == null) { areas.add(adding); adding = null; } else { areas.remove(overlaping); adding = Area.minContainingArea(overlaping, adding); } } } public void fill(Area[] states) { int num = 0; for (Area a : areas) { states[num++] = a; } } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); StringTokenizer tokenizer; tokenizer = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); // Area[] tribes = new Area[n]; AreaSet areaSet = new AreaSet(); for (int i = 0; i < n; i++) { tokenizer = new StringTokenizer(reader.readLine()); int x1 = Integer.parseInt(tokenizer.nextToken()); int x2 = Integer.parseInt(tokenizer.nextToken()); int y1 = Integer.parseInt(tokenizer.nextToken()); int y2 = Integer.parseInt(tokenizer.nextToken()); // tribes[i] = new Area(x1, y1, x2, y2); areaSet.add(new Area(x1, x2, y1, y2)); //if(i % 1000 == 0) { // System.err.println(i / 1000); //} } Area[] states = new Area[areaSet.size()]; areaSet.fill(states); Arrays.sort(states); System.out.println(states.length); for (int i = 0; i < states.length; i++) { System.out.println(states[i]); } /* * Arrays.sort(tribes); * * for(int i = 0; i < n; i++) { System.out.println(tribes[i]); } */ /* * int minX = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE; Map<Integer, * List<Integer>> begins = new HashMap<Integer, List<Integer>>(); Map<Integer, * List<Integer>> ends = new HashMap<Integer, List<Integer>>(); * for(int i = 0; i < n; i++) { minX = Math.min(minX, tribes[i].x1); * maxX = Math.max(maxX, tribes[i].x2); * * if(begins.get(tribes[i].x1) == null) { begins.put(tribes[i].x1, new * ArrayList<Integer>()); } begins.get(tribes[i].x1).add(i); * * if(ends.get(tribes[i].x2) == null) { ends.put(tribes[i].x2, new * ArrayList<Integer>()); } ends.get(tribes[i].x2).add(i); } * * * IntervalTree<Integer> tree = new IntervalTree<Integer>(); for(int * movingX = minX; movingX <= maxX; movingX++) { List<Integer> begin = * begins.get(movingX); List<Integer> end = ends.get(movingX); * * if(end != null) { for(Integer ) } } */ /* * Interval over = new Interval<Integer>(5+0.001, 9-0.001); * System.out.println(over); IntervalTree<Integer> tree = new * IntervalTree<Integer>(); tree.insert(over); List res = * tree.search(9.01, 15.99); System.out.println(res.size()); */ // tree. // SegmentTree.DynamicSegmentTree<SegmentTree.Data.QuadrantData> tree = // new // SegmentTree.DynamicSegmentTree<SegmentTree.Data.QuadrantData>(null);// // = new DynamicSegmentTree // tree. // IntervalTree<Integer> tree = new IntervalTree<Integer>(null); // tree. } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; public class ple { public static class Area implements Comparable<Area> { int x1, x2, y1, y2; public Area(int x1, int x2, int y1, int y2) { super(); this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } public String toStringDebug() { return "(" + x1 + "," + x2 + ";" + y1 + "," + y2 + ")"; } public String toString() { return x1 + " " + x2 + " " + y1 + " " + y2; } public boolean isInside(int x, int y) { return x1 < x && x < x2 && y1 < y && y < y2; } public boolean isOverlaping(Area o) { // return (isInside(o.x1,o.y1) || isInside(o.x1,o.y2) || // isInside(o.x2,o.y1) || isInside(o.x2,o.y2)); return (x1 < o.x2 && x2 > o.x1 && y1 < o.y2 && y2 > o.y1); // return true; } public static Area minContainingArea(Area a, Area b) { return new Area(Math.min(a.x1, b.x1), Math.max(a.x2, b.x2), Math .min(a.y1, b.y1), Math.max(a.y2, b.y2)); } @Override public int compareTo(Area o) { int res = 0; res = (res == 0 ? x1 - o.x1 : res); res = (res == 0 ? x2 - o.x2 : res); res = (res == 0 ? y1 - o.y1 : res); res = (res == 0 ? y2 - o.y2 : res); return res; } } public static class AreaSet { Set<Area> areas = new HashSet<Area>(); public int size() { return areas.size(); } public void add(Area area) { Area adding = area; while(adding != null) { Area overlaping = null; for (Area a : areas) { if (adding.isOverlaping(a)) { overlaping = a; break; } } if (overlaping == null) { areas.add(adding); adding = null; } else { areas.remove(overlaping); adding = Area.minContainingArea(overlaping, adding); } } } public void fill(Area[] states) { int num = 0; for (Area a : areas) { states[num++] = a; } } } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); StringTokenizer tokenizer; tokenizer = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); // Area[] tribes = new Area[n]; AreaSet areaSet = new AreaSet(); for (int i = 0; i < n; i++) { tokenizer = new StringTokenizer(reader.readLine()); int x1 = Integer.parseInt(tokenizer.nextToken()); int x2 = Integer.parseInt(tokenizer.nextToken()); int y1 = Integer.parseInt(tokenizer.nextToken()); int y2 = Integer.parseInt(tokenizer.nextToken()); // tribes[i] = new Area(x1, y1, x2, y2); areaSet.add(new Area(x1, x2, y1, y2)); //if(i % 1000 == 0) { // System.err.println(i / 1000); //} } Area[] states = new Area[areaSet.size()]; areaSet.fill(states); Arrays.sort(states); System.out.println(states.length); for (int i = 0; i < states.length; i++) { System.out.println(states[i]); } /* * Arrays.sort(tribes); * * for(int i = 0; i < n; i++) { System.out.println(tribes[i]); } */ /* * int minX = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE; Map<Integer, * List<Integer>> begins = new HashMap<Integer, List<Integer>>(); Map<Integer, * List<Integer>> ends = new HashMap<Integer, List<Integer>>(); * for(int i = 0; i < n; i++) { minX = Math.min(minX, tribes[i].x1); * maxX = Math.max(maxX, tribes[i].x2); * * if(begins.get(tribes[i].x1) == null) { begins.put(tribes[i].x1, new * ArrayList<Integer>()); } begins.get(tribes[i].x1).add(i); * * if(ends.get(tribes[i].x2) == null) { ends.put(tribes[i].x2, new * ArrayList<Integer>()); } ends.get(tribes[i].x2).add(i); } * * * IntervalTree<Integer> tree = new IntervalTree<Integer>(); for(int * movingX = minX; movingX <= maxX; movingX++) { List<Integer> begin = * begins.get(movingX); List<Integer> end = ends.get(movingX); * * if(end != null) { for(Integer ) } } */ /* * Interval over = new Interval<Integer>(5+0.001, 9-0.001); * System.out.println(over); IntervalTree<Integer> tree = new * IntervalTree<Integer>(); tree.insert(over); List res = * tree.search(9.01, 15.99); System.out.println(res.size()); */ // tree. // SegmentTree.DynamicSegmentTree<SegmentTree.Data.QuadrantData> tree = // new // SegmentTree.DynamicSegmentTree<SegmentTree.Data.QuadrantData>(null);// // = new DynamicSegmentTree // tree. // IntervalTree<Integer> tree = new IntervalTree<Integer>(null); // tree. } } |