题目链接:https://vjudge.net/contest/301219#problem/F
解题思路:
枚举每个矩形的时候,看它是否需要和其他人合并只需要查看它的外形边框是否又被标记,这个可以直接用离散化,然后set存一下每个矩形四个格子,就可以用log(n)找到合并的矩形,然后后并查集并一下就好了。
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair <int,int> pa;
const int mx = 1e5 + 10;
int n,m,num[mx],a[mx],b[mx];
map<int,set<pa>> r,c;
int fa[mx];
ll sum[mx];
struct node
{
int x1,y1;
int x2,y2;
}s[mx];
int find(int x){
return x==fa[x]? x:fa[x]=find(fa[x]);
}
void merge(int x,set<pa>& st,int l,int r)
{
auto it = st.lower_bound(pa(l,0));
x = find(x);
while(it!=st.end()&&(it->fi)<=r)
{
l = find(it->se);
if(l!=x) sum[x] += sum[l],fa[l] = x;
it++;
}
}
int main()
{
while(~scanf("%d",&n)){
int cx = 0,cy = 0;
r.clear(),c.clear();
for(int i=1;i<=n;i++){
scanf("%d%d%d%d",&s[i].x1,&s[i].y1,&s[i].x2,&s[i].y2);
sum[i] = s[i].x2*s[i].y2;
s[i].x2 += s[i].x1 - 1;
s[i].y2 += s[i].y1 - 1;
fa[i] = i;
}
for(int i=1;i<=n;i++){
int x1 = s[i].x1;
int x2 = s[i].x2;
int y1 = s[i].y1;
int y2 = s[i].y2;
r[x1].insert(pa(y1,i));r[x1].insert(pa(y2,i));
r[x2].insert(pa(y1,i));r[x2].insert(pa(y2,i));
c[y1].insert(pa(x1,i));c[y1].insert(pa(x2,i));
c[y2].insert(pa(x1,i));c[y2].insert(pa(x2,i));
}
for(int i=1;i<=n;i++){
if(r.count(s[i].x1-1))
merge(i,r[s[i].x1-1],s[i].y1-1,s[i].y2+1);
if(r.count(s[i].x2+1))
merge(i,r[s[i].x2+1],s[i].y1-1,s[i].y2+1);
if(c.count(s[i].y1-1))
merge(i,c[s[i].y1-1],s[i].x1-1,s[i].x2+1);
if(c.count(s[i].y2+1))
merge(i,c[s[i].y2+1],s[i].x1-1,s[i].x2+1);
}
ll ans = 0;
for(int i=1;i<=n;i++) if(find(i)==i)
ans = max(ans,sum[i]);
printf("%d\n",ans);
}
return 0;
}
标签:fa,STL,sum,查集,find,int,UVALive,st,mx From: https://blog.51cto.com/u_12468613/6384509