Atlantis HDU - 1542(扫描线板子题)

本文深入探讨了段树和扫描线算法在解决复杂几何问题中的应用,通过实例讲解了如何利用这两种数据结构和算法来高效计算特定区域的属性,如面积等。文章详细介绍了算法的实现过程,包括关键的数据结构定义、更新操作以及查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Atlantis

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define MT(a,b) memset(a,b,sizeof(a))
const int maxn=2E3+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
double lsh[600];
struct node1{
    double sum;
    int cnt;
}qwe[maxn<<2];

struct node2{
    double stx,edx,y;
    int val;
}edge[maxn<<2];

bool cmp(node2 a,node2 b){
    return a.y<b.y;
}

void push_up(int l,int r,int root){
    if (qwe[root].cnt) qwe[root].sum=lsh[r+1]-lsh[l];
    else if (l==r) qwe[root].sum=0;
    else qwe[root].sum=qwe[root<<1].sum+qwe[root<<1|1].sum;
}

void update(int l,int r,int ql,int qr,int root,int val){
    if (l==ql&&r==qr){
        qwe[root].cnt+=val;
        push_up(l,r,root);
        return;
    }
    int mid=(l+r)>>1;
    if (qr<=mid) update(l,mid,ql,qr,root<<1,val);
    else if (ql>mid) update(mid+1,r,ql,qr,root<<1|1,val);
    else{
        update(l,mid,ql,mid,root<<1,val);
        update(mid+1,r,mid+1,qr,root<<1|1,val);
    }
    push_up(l,r,root);
}

int main(){
    int n;
    int turn=0;
    double x1,y1,x2,y2;
    while (~scanf ("%d",&n)){
        if (n==0) break;
        turn++;
        int cnt=1;
        for (int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            edge[cnt]={x1,x2,y1,1},lsh[cnt++]=x1;
            edge[cnt]={x1,x2,y2,-1},lsh[cnt++]=x2;
        }
        sort(edge+1,edge+cnt,cmp);
        sort(lsh+1,lsh+cnt);
        int len =unique(lsh+1,lsh+cnt)-lsh;
        double ans=0;
        for (int i=1;i<cnt;i++){
            int l=lower_bound(lsh+1,lsh+len,edge[i].stx)-lsh;
            int r=lower_bound(lsh+1,lsh+len,edge[i].edx)-lsh-1;
            update(1,len,l,r,1,edge[i].val);
            ans+=(edge[i+1].y-edge[i].y)*qwe[1].sum;
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",turn,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值