|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Point{ |
| 7 | + int x, y, id; |
| 8 | + Point(int x, int y, int id){ |
| 9 | + this.x = x; |
| 10 | + this.y = y; |
| 11 | + this.id = id; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class Query{ |
| 16 | + int a, b, v, id; |
| 17 | + Query(int a, int b, int v, int id){ |
| 18 | + this.a = a; |
| 19 | + this.b = b; |
| 20 | + this.v = v; |
| 21 | + this.id = id; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class Edge{ |
| 26 | + int a, b, c; |
| 27 | + Edge(int a, int b, int c){ |
| 28 | + this.a = a; |
| 29 | + this.b = b; |
| 30 | + this.c = c; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +class Main { |
| 35 | + |
| 36 | + // IO field |
| 37 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 38 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 39 | + static StringTokenizer st; |
| 40 | + |
| 41 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 42 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 43 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 44 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 45 | + |
| 46 | + // Additional field |
| 47 | + static Point[] arr; |
| 48 | + static Query[] que; |
| 49 | + static int[] root; |
| 50 | + static int N, Q; |
| 51 | + static List<Edge> edges; |
| 52 | + |
| 53 | + public static void main(String[] args) throws Exception { |
| 54 | + |
| 55 | + ready(); |
| 56 | + solve(); |
| 57 | + |
| 58 | + bwEnd(); |
| 59 | + } |
| 60 | + |
| 61 | + static void ready() throws Exception{ |
| 62 | + |
| 63 | + nextLine(); |
| 64 | + N = nextInt(); |
| 65 | + Q = nextInt(); |
| 66 | + root = new int[N]; |
| 67 | + arr = new Point[N]; |
| 68 | + for(int i=0;i<N;i++) { |
| 69 | + root[i] = i; |
| 70 | + nextLine(); |
| 71 | + int x = nextInt(), y = nextInt(); |
| 72 | + arr[i] = new Point(x,y,i); |
| 73 | + } |
| 74 | + |
| 75 | + que = new Query[Q]; |
| 76 | + for(int i=0;i<Q;i++) { |
| 77 | + nextLine(); |
| 78 | + int a = nextInt(), b = nextInt(), v = nextInt(); |
| 79 | + que[i] = new Query(a-1,b-1,v,i); |
| 80 | + } |
| 81 | + edges = new ArrayList<>(); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + static void solve() throws Exception{ |
| 86 | + |
| 87 | + Arrays.sort(arr, (a,b) -> a.x-b.x); |
| 88 | + for(int i=1;i<N;i++) { |
| 89 | + edges.add(new Edge(arr[i-1].id, arr[i].id, arr[i].x - arr[i-1].x)); |
| 90 | + } |
| 91 | + Arrays.sort(arr, (a,b) -> a.y-b.y); |
| 92 | + for(int i=1;i<N;i++) { |
| 93 | + edges.add(new Edge(arr[i-1].id, arr[i].id, arr[i].y - arr[i-1].y)); |
| 94 | + } |
| 95 | + Collections.sort(edges, (a,b) -> a.c-b.c); |
| 96 | + |
| 97 | + Arrays.sort(que, (a,b) -> a.v-b.v); |
| 98 | + boolean[] ans = new boolean[Q]; |
| 99 | + int idx = 0; |
| 100 | + for(int i=0;i<Q;i++) { |
| 101 | + while(idx<edges.size() && edges.get(idx).c <= que[i].v) { |
| 102 | + int x = f(edges.get(idx).a), y = f(edges.get(idx).b); |
| 103 | + if(x != y) root[x] = y; |
| 104 | + idx++; |
| 105 | + } |
| 106 | + int x = f(que[i].a), y = f(que[i].b); |
| 107 | + ans[que[i].id] = x == y; |
| 108 | + } |
| 109 | + |
| 110 | + for(int i=0;i<Q;i++) bw.write(ans[i] ? "YES\n" : "NO\n"); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + static int f(int x) { return x==root[x] ? x : (root[x]=f(root[x]));} |
| 115 | + |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +``` |
0 commit comments