hdu1828 矩形周长并

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2435    Accepted Submission(s): 1294


Problem Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.



The corresponding boundary is the whole set of line segments drawn in Figure 2.



The vertices of all rectangles have integer coordinates.
 

Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.
 

Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
 

Sample Input
7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16
 

Sample Output
228

  周长并和面积并一样是扫描线法,从下到上扫过去,用len表示当前区间线段长度,那么每次扫描的时候要加上当前的len[1]减去上次的len[1]的绝对值(因为上次的len已经加过了),这是水平方向的。还有竖直方向,用segnum数组维护当前区间的线段共能在竖直方向构成多少条线段(其实就是有当前区间的线段有多少个端点),维护segnum数组还需要两个bool数组lb,rb分别表示当前区间的左端点和右端点是否被线段覆盖,那么segnum[o]=segnum[o<<1]+segnum[o<<1|1],若rb[o<<1]&&lb[o<<1|1],说明两条线段重合了,segnum[o]要减2。



  别忘了最后一条边,因为扫描完倒数第二条边后len[1]的长度就是最后一条边的长度,所以最后还要加上len[1]。

  因为这道题坐标范围不大,所以可以用线段树表示坐标,也可以像面积并那样先把所有x记下来从小到大排序,每次更新的时候先二分找到坐标的编号,线段树表示的不是真实坐标,而是x的相对坐标,相当于离散化一下。

  两种方法的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define MAXN 10010
#define MAXM 20010
#define MAXNODE 4*MAXN
using namespace std;
int N,M,covered[MAXM<<2],segnum[MAXM<<2],len[MAXM<<2];
bool lb[MAXM<<2],rb[MAXM<<2];
struct Line{
    int x1,x2,y,cover;
    bool operator < (const Line& x) const{
        return y<x.y;
    }
}line[MAXN];
void add_line(int x1,int y1,int x2,int y2){
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y1;
    line[M++].cover=1;
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y2;
    line[M++].cover=-1;
}
void maintain(int o,int L,int R){
    if(covered[o]){
        lb[o]=rb[o]=1;
        len[o]=R-L;
        segnum[o]=2;
    }
    else if(L+1>=R) lb[o]=rb[o]=len[o]=segnum[o]=0;
    else{
        lb[o]=lb[o<<1];
        rb[o]=rb[o<<1|1];
        len[o]=len[o<<1]+len[o<<1|1];
        segnum[o]=segnum[o<<1]+segnum[o<<1|1];
        if(rb[o<<1]&&lb[o<<1|1]) segnum[o]-=2;
    }
}
void update(int o,int L,int R,int ql,int qr,int cover){
    if(ql<=L&&qr>=R){
        covered[o]+=cover;
        maintain(o,L,R);
        return;
    }
    int mid=(L+R)>>1;
    if(ql<mid) update(o<<1,L,mid,ql,qr,cover);
    if(qr>mid) update(o<<1|1,mid,R,ql,qr,cover);
    maintain(o,L,R);
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N)!=EOF){
        M=0;
        int LB=INF,RB=-INF;
        while(N--){
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            LB=min(LB,x1);
            RB=max(RB,x2);
            add_line(x1,y1,x2,y2);
        }
        sort(line,line+M);
        for(int i=0;i<(MAXM<<2);i++) lb[i]=rb[i]=covered[i]=segnum[i]=len[i]=0;
        int last=0,ans=0;
        for(int i=0;i<M-1;i++){
            update(1,LB,RB,line[i].x1,line[i].x2,line[i].cover);
            ans+=segnum[1]*(line[i+1].y-line[i].y);
            ans+=abs(len[1]-last);
            last=len[1];
        }
        ans+=len[1];
        printf("%d\n",ans);
    }
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define MAXN 10010
#define MAXM 10010
#define MAXNODE 4*MAXN
using namespace std;
int N,M,covered[MAXM<<2],segnum[MAXM<<2],len[MAXM<<2],x[MAXN<<1];
bool lb[MAXM<<2],rb[MAXM<<2];
struct Line{
    int x1,x2,y,cover;
    bool operator < (const Line& x) const{
        return y<x.y;
    }
}line[MAXN];
void add_line(int x1,int y1,int x2,int y2){
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y1;
    line[M++].cover=1;
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y2;
    line[M++].cover=-1;
}
void maintain(int o,int L,int R){
    if(covered[o]){
        lb[o]=rb[o]=1;
        len[o]=x[R]-x[L];
        segnum[o]=2;
    }
    else if(L+1>=R) lb[o]=rb[o]=len[o]=segnum[o]=0;
    else{
        lb[o]=lb[o<<1];
        rb[o]=rb[o<<1|1];
        len[o]=len[o<<1]+len[o<<1|1];
        segnum[o]=segnum[o<<1]+segnum[o<<1|1];
        if(rb[o<<1]&&lb[o<<1|1]) segnum[o]-=2;
    }
}
void update(int o,int L,int R,int ql,int qr,int cover){
    if(ql<=L&&qr>=R){
        covered[o]+=cover;
        maintain(o,L,R);
        return;
    }
    int mid=(L+R)>>1;
    if(ql<mid) update(o<<1,L,mid,ql,qr,cover);
    if(qr>mid) update(o<<1|1,mid,R,ql,qr,cover);
    maintain(o,L,R);
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N)!=EOF){
        M=0;
        int NX=1;
        while(N--){
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x[NX++]=x1;
            x[NX++]=x2;
            add_line(x1,y1,x2,y2);
        }
        sort(x+1,x+NX);
        sort(line,line+M);
        NX=unique(x+1,x+NX)-x-1;
        for(int i=0;i<(MAXM<<2);i++) lb[i]=rb[i]=covered[i]=segnum[i]=len[i]=0;
        int last=0,ans=0;
        for(int i=0;i<M-1;i++){
            int l=lower_bound(x+1,x+NX,line[i].x1)-x,r=lower_bound(x+1,x+NX,line[i].x2)-x;
            update(1,1,NX,l,r,line[i].cover);
            ans+=segnum[1]*(line[i+1].y-line[i].y);
            ans+=abs(len[1]-last);
            last=len[1];
        }
        ans+=len[1];
        printf("%d\n",ans);
    }
    return 0;
}


### HDU 3342 并查集 解题思路与实现 #### 题目背景介绍 HDU 3342 是一道涉及并查集的数据结构题目。该类问题通常用于处理动态连通性查询,即判断若干元素是否属于同一集合,并支持高效的合并操作。 #### 数据描述 给定一系列的人际关系网络中的朋友关系对 (A, B),表示 A 和 B 是直接的朋友。目标是通过这些已知的关系推断出所有人之间的间接友谊连接情况。具体来说,如果存在一条路径使得两个人可以通过中间人的链条相连,则认为他们是间接朋友。 #### 思路分析 为了高效解决此类问题,可以采用带按秩压缩启发式的加权快速联合-查找算法(Weighted Quick Union with Path Compression)。这种方法不仅能够有效地管理大规模数据集下的分组信息,而且可以在几乎常数时间内完成每次查找和联合操作[^1]。 当遇到一个新的友链 `(a,b)` 时: - 如果 a 和 b 已经在同一棵树下,则无需任何动作; - 否则,执行一次 `union` 操作来把它们所在的两棵不同的树合并成一棵更大的树; 最终目的是统计有多少个独立的“朋友圈”,也就是森林里的树木数量减一即是所需新建桥梁的数量[^4]。 #### 实现细节 以下是 Python 版本的具体实现方式: ```python class DisjointSet: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, p): if self.parent[p] != p: self.parent[p] = self.find(self.parent[p]) # 路径压缩 return self.parent[p] def union(self, p, q): rootP = self.find(p) rootQ = self.find(q) if rootP == rootQ: return # 按秩合并 if self.rank[rootP] > self.rank[rootQ]: self.parent[rootQ] = rootP elif self.rank[rootP] < self.rank[rootQ]: self.parent[rootP] = rootQ else: self.parent[rootQ] = rootP self.rank[rootP] += 1 def solve(): N, M = map(int, input().split()) dsu = DisjointSet(N+1) # 初始化不相交集 for _ in range(M): u, v = map(int, input().split()) dsu.union(u,v) groups = set() for i in range(1,N+1): groups.add(dsu.find(i)) bridges_needed = len(groups)-1 print(f"Bridges needed to connect all components: {bridges_needed}") solve() ``` 这段代码定义了一个名为 `DisjointSet` 的类来进行并查集的操作,包括初始化、寻找根节点以及联合两个子集的功能。最后,在主函数 `solve()` 中读取输入参数并对每一对好友调用 `dsu.union()` 方法直到遍历完所有的边为止。之后计算不同组件的数量从而得出所需的桥接次数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值