|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | +
|
| 6 | +public class Main { |
| 7 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 8 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 9 | + private static int[][] trees, queries; |
| 10 | + private static int[] uf, size; |
| 11 | + private static int N, Q; |
| 12 | +
|
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | + unionFind(); |
| 16 | +
|
| 17 | + for (int[] query : queries) { |
| 18 | + if (uf[query[0]] == uf[query[1]]) bw.write("1" + "\n"); |
| 19 | + else bw.write("0" + "\n"); |
| 20 | + } |
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + N = Integer.parseInt(st.nextToken()); |
| 29 | + Q = Integer.parseInt(st.nextToken()); |
| 30 | +
|
| 31 | + trees = new int[N+1][3]; |
| 32 | + queries = new int[Q][2]; |
| 33 | + uf = new int[N+1]; |
| 34 | + size = new int[N+1]; |
| 35 | +
|
| 36 | +
|
| 37 | + for (int i = 1; i <= N; i++) { |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + int x1 = Integer.parseInt(st.nextToken()); |
| 40 | + int x2 = Integer.parseInt(st.nextToken()); |
| 41 | + int y = Integer.parseInt(st.nextToken()); |
| 42 | +
|
| 43 | + trees[i][0] = x1; |
| 44 | + trees[i][1] = x2; |
| 45 | + trees[i][2] = i; |
| 46 | +
|
| 47 | + uf[i] = i; |
| 48 | + size[i] = 1; |
| 49 | + } |
| 50 | +
|
| 51 | + for (int i = 0; i < Q; i++) { |
| 52 | + st = new StringTokenizer(br.readLine()); |
| 53 | + int start = Integer.parseInt(st.nextToken()); |
| 54 | + int end = Integer.parseInt(st.nextToken()); |
| 55 | +
|
| 56 | + queries[i][0] = start; |
| 57 | + queries[i][1] = end; |
| 58 | + } |
| 59 | +
|
| 60 | + Arrays.sort(trees, (o1, o2) -> { |
| 61 | + if (o1[0] == o2[0]) return Integer.compare(o1[1], o2[1]); |
| 62 | + return Integer.compare(o1[0], o2[0]); |
| 63 | + }); |
| 64 | + } |
| 65 | +
|
| 66 | + private static void unionFind() { |
| 67 | + int maxEnd = trees[1][1]; |
| 68 | + int start = 1; |
| 69 | +
|
| 70 | + for (int i = 2; i <= N; i++) { |
| 71 | + if (valid(maxEnd, trees[i][0])) { |
| 72 | + union(trees[start][2], trees[i][2]); |
| 73 | + } else { |
| 74 | + start = i; |
| 75 | + } |
| 76 | + maxEnd = Math.max(maxEnd, trees[i][1]); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + private static boolean valid(int x2, int x3) { |
| 81 | + return x2 >= x3; |
| 82 | + } |
| 83 | +
|
| 84 | + private static void union(int x, int y) { |
| 85 | + int X = find(x); |
| 86 | + int Y = find(y); |
| 87 | +
|
| 88 | + if (X == Y) return; |
| 89 | +
|
| 90 | + if (size[X] < size[Y]) { |
| 91 | + uf[X] = Y; |
| 92 | + size[Y] += size[X]; |
| 93 | + } else { |
| 94 | + uf[Y] = X; |
| 95 | + size[X] += size[Y]; |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + private static int find(int x) { |
| 100 | + if (uf[x] == x) return x; |
| 101 | +
|
| 102 | + return uf[x] = find(uf[x]); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments