Skip to content

Commit d3fab85

Browse files
authored
Merge pull request #665 from AlgorithmWithGod/khj20006
[20250814] BOJ / D5 / 플러드 필 (Flood Fill) / 권혁준
2 parents 812b6c7 + 544aefc commit d3fab85

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
using ll = long long;
5+
6+
ll INF = 1e12;
7+
8+
ll N, D;
9+
int r[100000]{}, c[100000]{};
10+
pair<ll,ll> a[100000]{};
11+
set<tuple<ll,ll,int>> s;
12+
13+
int f(int x) {return x==r[x] ? x : r[x]=f(r[x]);}
14+
15+
int main(){
16+
cin.tie(0)->sync_with_stdio(0);
17+
18+
cin>>N>>D;
19+
iota(r, r+N, 0);
20+
fill(c, c+N, 1);
21+
for(int i=0;i<N;i++) {
22+
ll x, y;
23+
cin>>x>>y;
24+
a[i] = {y+x, y-x};
25+
}
26+
27+
sort(a, a+N);
28+
priority_queue<tuple<ll,ll,ll,int>, vector<tuple<ll,ll,ll,int>>, greater<>> pq;
29+
for(int j=0;j<N;j++) {
30+
auto [x,y] = a[j];
31+
while(!pq.empty() && get<0>(pq.top()) <= x) {
32+
auto [_a, _b, _c, _d] = pq.top();
33+
pq.pop();
34+
s.erase({_b,_c,_d});
35+
}
36+
auto it = s.upper_bound({y, -INF, -1});
37+
if(it != s.end() && get<0>(*it) <= y+D) {
38+
int p = f(j), q = f(get<2>(*it));
39+
if(p != q) {
40+
c[p] += c[q];
41+
r[q] = p;
42+
}
43+
44+
}
45+
if(it != s.begin()) {
46+
it--;
47+
if(get<0>(*it) >= y-D) {
48+
int p = f(j), q = f(get<2>(*it));
49+
if(p != q) {
50+
c[p] += c[q];
51+
r[q] = p;
52+
}
53+
}
54+
}
55+
56+
s.emplace(y,x,j);
57+
pq.emplace(x+D+1,y,x,j);
58+
}
59+
60+
int cnt = 0, mx = 0;
61+
bitset<100000> v;
62+
for(int i=0;i<N;i++) {
63+
int x = f(i);
64+
if(v[x]) continue;
65+
v[x] = 1;
66+
mx = max(mx, c[x]);
67+
cnt++;
68+
}
69+
cout<<cnt<<' '<<mx;
70+
71+
}
72+
```

0 commit comments

Comments
 (0)