面积并

本文介绍了两道算法题目,一道涉及计算多个圆形区域之间的重叠数量,另一道则关注平面上多个矩形区域覆盖至少两次的总面积。通过使用数据结构如线段树等来优化解决方案。

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

3835: Crop Circles 分享至QQ空间

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 101            Accepted:70

Description

 

Bessie and her fellow herd-mates have become extremely territorial. The N (1 <= N <= 400) cows conveniently numbered 1..N have all staked out a grazing spot in the pasture. Each cow i has a spot on an integer grid (0 <= X_i <= 10,000; 0 <= Y_i <= 10,000) and an integer radius R_i that indicates the circle she is staking out (1 <= R_i <= 500).

The cows are a bit greedy and sometimes stake out territory of their herd-mates. For each cow, calculate the number of other cows whose territory overlaps her territory.

By way of example, consider these six cows with indicated locations and radii (don't confuse radius with diameter!):



By visual inspection, we can see and count the overlaps, as shown.

NOTE: the test data will avoid pathological situations like tangents where the circles just barely touch.

 

 

Input

 

* Line 1: A single integer: N

* Lines 2..N+1: Three space-separated integers: X_i, Y_i, and R_i

 

 

Output

 

* Lines 1..N: Line i contains a single integer that is the number of other fields that overlap with cow i's field.

 

 

Sample Input

6
7 7 7
16 14 7
11 13 2
10 17 3
29 8 5
15 7 4

Sample Output

3
4
3
2
0
2

Source

USACO Elite 2011 January

Toj的一个题,问题只是n个圆和自己交的有多少个

 

#include <stdio.h>
int x[410],y[410],r[410];
int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=0; i<n; i++)
        scanf("%d%d%d",&x[i],&y[i],&r[i]);
    for(i=0; i<n; i++)
    {
        int f=0;
        for(j=0; j<n; j++)
            if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])<(r[i]+r[j])*(r[i]+r[j]))
                f++;
        printf("%d\n",f-1);
    }
    return 0;
}

 

 

 

矩形面积并,线段树经典

2719: 覆盖的面积 分享至QQ空间

Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte
Total Submit: 13            Accepted:10

Description

给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

 

Input

输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.

Output

对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.

Sample Input

2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1

Sample Output

7.63
0.00

用先sort+二分离散化,再用线段树维护左右端点

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N=2005;
struct seg
{
    double l,r,h;
    int v;
    bool friend operator <(seg u,seg v)
    {
        return u.h<v.h;
    }
} s[N];
struct Node
{
    int l,r,cnt;
    double s,len;
} q[5000];
double has[N];
void build(int l,int r,int i)
{
    q[i].l=l,q[i].r=r;
    q[i].len=q[i].s=q[i].cnt=0;
    if(l==r)return;
    int mid=(l+r)/2;
    build(l,mid,i*2);
    build(mid+1,r,i*2+1);
}
void pushup(int i)
{
    if(q[i].cnt)q[i].s=has[q[i].r+1]-has[q[i].l];
    else if(q[i].l==q[i].r)q[i].s=0;
    else q[i].s=q[i*2].s+q[i*2+1].s;
    if(q[i].cnt>1)q[i].len=has[q[i].r+1]-has[q[i].l];
    else if(q[i].l==q[i].r)q[i].len=0;
    else if(q[i].cnt==1)q[i].len=q[i*2].s+q[i*2+1].s;
    else q[i].len=q[i*2].len+q[i*2+1].len;
}
void update(int l,int r,int f,int i)
{
    if(q[i].l==l&&q[i].r==r)
    {
        q[i].cnt+=f;
        pushup(i);
        return;
    }
    int mid=(q[i].l+q[i].r)/2;
    if(r<=mid)update(l,r,f,i*2);
    else if(l>mid)update(l,r,f,i*2+1);
    else
    {
        update(l,mid,f,i*2);
        update(mid+1,r,f,i*2+1);
    }
    pushup(i);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        int k=0;
        for(int i=0; i<n; i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            s[k].l=x1;s[k].r=x2;
            s[k].h=y1,s[k].v=1;
            has[k++]=x1;
            s[k].l=x1;s[k].r=x2;
            s[k].h=y2;s[k].v=-1;
            has[k++]=x2;
        }
        sort(s,s+k);
        sort(has,has+k);
        int kk=unique(has,has+k)-has;
        build(0,kk-1,1);
        double sum=1e-6;
        for(int i=0;i<k-1;i++)
        {
            int l=lower_bound(has,has+kk,s[i].l)-has;
            int r=lower_bound(has,has+kk,s[i].r)-has-1;
            update(l,r,s[i].v,1);
            sum+=q[1].len*(s[i+1].h-s[i].h);
        }
        printf("%.2f\n",sum);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/BobHuang/p/7389301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值