Skip to content

Commit b872e5d

Browse files
authored
Merge pull request #601 from AlgorithmWithGod/khj20006
[20250803] BOJ / P5 / 우주 정거장 / 권혁준
2 parents 05c5322 + 2a9d01a commit b872e5d

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
class Line {
51+
int s, e, num;
52+
Line(int s, int e, int num) {
53+
this.s = s;
54+
this.e = e;
55+
this.num = num;
56+
}
57+
}
58+
59+
public class Main {
60+
61+
static IOController io;
62+
63+
//
64+
65+
static int N, Q;
66+
static int[] r;
67+
static Line[] x, y;
68+
69+
static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}
70+
71+
public static void main(String[] args) throws Exception {
72+
73+
io = new IOController();
74+
75+
init();
76+
solve();
77+
78+
io.close();
79+
80+
}
81+
82+
static void init() throws Exception {
83+
84+
N = io.nextInt();
85+
Q = io.nextInt();
86+
x = new Line[N];
87+
y = new Line[N];
88+
r = new int[N];
89+
for(int i=0;i<N;i++) {
90+
r[i] = i;
91+
int x1 = io.nextInt();
92+
int y1 = io.nextInt();
93+
int x2 = io.nextInt();
94+
int y2 = io.nextInt();
95+
if(x1 > x2) {
96+
int tmp = x1;
97+
x1 = x2;
98+
x2 = tmp;
99+
}
100+
if(y1 > y2) {
101+
int tmp = y1;
102+
y1 = y2;
103+
y2 = tmp;
104+
}
105+
106+
x[i] = new Line(x1, x2, i);
107+
y[i] = new Line(y1, y2, i);
108+
}
109+
110+
}
111+
112+
static void solve() throws Exception {
113+
114+
connect(x);
115+
connect(y);
116+
while(Q-->0) {
117+
int a = f(io.nextInt()-1), b = f(io.nextInt()-1);
118+
io.write((a==b ? "1\n" : "0\n"));
119+
}
120+
121+
}
122+
123+
static void connect(Line[] arr) {
124+
Arrays.sort(arr, (a,b) -> a.s==b.s ? a.e-b.e : a.s-b.s);
125+
int max = arr[0].e, idx = arr[0].num;
126+
for(int i=1;i<arr.length;i++) {
127+
if(arr[i].s <= max) {
128+
int a = f(arr[i].num), b = f(idx);
129+
if(a != b) r[a] = b;
130+
}
131+
if(arr[i].e > max) {
132+
max = arr[i].e;
133+
idx = arr[i].num;
134+
}
135+
}
136+
}
137+
138+
}
139+
```

0 commit comments

Comments
 (0)