扫描线,矩形面积求并hdu 1542

该程序需要处理多个输入的地图数据,每个数据包含四个坐标,表示地图覆盖的矩形区域。目标是计算所有矩形覆盖的总面积,并精确到小数点后两位。输出包括测试用例编号和总探索面积。

 

                                                      Atlantis

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity. 

Input

The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area. 

The input file is terminated by a line containing a single 0. Don’t process it.

Output

For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 

Output a blank line after each test case. 

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<cmath>
using namespace std;
const int INF=0x7f7f7f7f;
const int maxn=2e6+5;
const int MOD=10007;
struct node
{
    double l,r,h;//线段的左右端点 以及高度
    int flag;
}a[205];
double x1,y1,x2,y2;
vector<double> vx;
bool cmp(node a,node b)
{
    return a.h<b.h;
}
struct tree
{
    int cnt;
    double sum;
}b[205<<2];
void up(int l,int r,int rt)
{
    if(b[rt].cnt)
    {
        b[rt].sum=vx[r]-vx[l-1];
        //cout<<r+1<<' '<<l<<endl;
    }
    else if(l==r)
    {
        b[rt].sum=0;
    }
    else b[rt].sum=b[rt<<1].sum+b[rt<<1|1].sum;

}
void update(int l,int r,int rt,int ll,int rr,int c)
{
    if(ll<=l&&r<=rr)
    {
        b[rt].cnt+=c;
        up(l,r,rt);
        return ;
    }
    int mid=(l+r)>>1;
    if(ll<=mid) update(l,mid,rt<<1,ll,rr,c);
    if(rr>mid) update(mid+1,r,rt<<1|1,ll,rr,c);
    up(l,r,rt);
}
int n;
int main()
{
    int cas=1;
    double ans;
    while(scanf("%d",&n)!=EOF&&n)
    {
        vx.clear();
        ans=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            a[i].l=x1;
            a[i].r=x2;
            a[i].h=y1;
            a[i].flag=1;

            a[i+n].l=x1;
            a[i+n].r=x2;
            a[i+n].h=y2;
            a[i+n].flag=-1;
            vx.push_back(x1);
            vx.push_back(x2);
        }
        printf("Test case #%d\n",cas++);
        sort(a+1,a+1+2*n,cmp);
        sort(vx.begin(),vx.end());
        for(int i=1;i<vx.size();i++)
            if(vx[i]==vx[i-1]) vx.erase(i+vx.begin());//去重
        memset(b,0,sizeof b);
        for(int i=1;i<=n*2;++i)
        {
            int l=lower_bound(vx.begin(),vx.end(),a[i].l)-vx.begin()+1;
            int r=lower_bound(vx.begin(),vx.end(),a[i].r)-vx.begin();//区间左开右闭;
            update(1,vx.size(),1,l,r,a[i].flag);
            ans+=b[1].sum*(a[i+1].h-a[i].h);
        }
        printf("Total explored area: %.2f\n\n",ans);
    }
}
### 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、付费专栏及课程。

余额充值