|
| 1 | +import java.util.*; |
| 2 | +import java.io.*; |
| 3 | + |
| 4 | +class Rect{ |
| 5 | + int x, y, w, h; |
| 6 | + Rect(int x, int y, int w, int h){ |
| 7 | + this.x = x; |
| 8 | + this.y = y; |
| 9 | + this.w = w; |
| 10 | + this.h = h; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +class Main { |
| 15 | + |
| 16 | + // IO field |
| 17 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 19 | + static StringTokenizer st; |
| 20 | + |
| 21 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 22 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 23 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 24 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 25 | + |
| 26 | + // Additional field |
| 27 | + |
| 28 | + |
| 29 | + static Rect[] arr; |
| 30 | + static int N; |
| 31 | + static int[] r; |
| 32 | + |
| 33 | + public static void main(String[] args) throws Exception { |
| 34 | + |
| 35 | + ready(); |
| 36 | + solve(); |
| 37 | + |
| 38 | + bwEnd(); |
| 39 | + } |
| 40 | + |
| 41 | + static void ready() throws Exception{ |
| 42 | + |
| 43 | + N = Integer.parseInt(br.readLine()); |
| 44 | + r = new int[N+1]; |
| 45 | + for(int i=0;i<=N;i++) r[i] = i; |
| 46 | + arr = new Rect[N+1]; |
| 47 | + arr[0] = new Rect(0,0,0,0); |
| 48 | + for(int i=1;i<=N;i++) { |
| 49 | + nextLine(); |
| 50 | + int x1 = nextInt(); |
| 51 | + int y1 = nextInt(); |
| 52 | + int x2 = nextInt(); |
| 53 | + int y2 = nextInt(); |
| 54 | + arr[i] = new Rect(x1, y1, x2-x1, y2-y1); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + static void solve() throws Exception{ |
| 60 | + |
| 61 | + for(int i=1;i<=N;i++) { |
| 62 | + for(int j=0;j<i;j++) { |
| 63 | + if(!intersect(arr[i],arr[j])) continue; |
| 64 | + int x = f(i), y = f(j); |
| 65 | + if(x == y) continue; |
| 66 | + r[x] = y; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + int ans = 0; |
| 71 | + boolean vis[] = new boolean[N+1]; |
| 72 | + vis[f(0)] = true; |
| 73 | + for(int i=1;i<=N;i++) { |
| 74 | + int x = f(i); |
| 75 | + if(!vis[x]) { |
| 76 | + vis[x] = true; |
| 77 | + ans++; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + bw.write(ans+"\n"); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + static boolean intersect(Rect a, Rect b) { |
| 86 | + if(a.x + a.w < b.x) return false; |
| 87 | + if(b.x + b.w < a.x) return false; |
| 88 | + if(a.y + a.h < b.y) return false; |
| 89 | + if(b.y + b.h < a.y) return false; |
| 90 | + if((a.x < b.x && b.x + b.w < a.x + a.w) && (a.y < b.y && b.y + b.h < a.y + a.h)) return false; |
| 91 | + if((b.x < a.x && a.x + a.w < b.x + b.w) && (b.y < a.y && a.y + a.h < b.y + b.h)) return false; |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));} |
| 96 | + |
| 97 | +} |
0 commit comments