[codevs3044]矩形面积求并


题目描述 Description

输入n个矩形,求他们总共占地面积(也就是求一下面积的并)


输入描述 Input Description

可能有多组数据,读到n=0为止(不超过15组)

每组数据第一行一个数n,表示矩形个数(n<=100)

接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的左下角坐标和右上角坐标


输出描述 Output Description

每组数据输出一行表示答案

样例输入 Sample Input

2
10 10 20 20
15 15 25 25.5
0

样例输出 Sample Output


180.00

数据范围及提示 Data Size & Hint


 无

矩形面积并,扫描线+线段树

本题的线段树比较奇怪
其中的坑点在pushup,线段树维护的区间[l,r]如果l,r相等并不是一段空集,而是线段pos[l]-pos[r+1] 这一线段

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define mem(a,b) memset(a,b,sizeof(a))
inline int read()
{
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c==‘-‘)f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
const int maxn=210;
int n,add[maxn<<2];
double a,b,c,d,pos[maxn],sum[maxn<<2],ans;
struct segment
{
    double X,x,Y;int tag;
    segment(){}
    segment(double _1,double _2,double _3,int _4):X(_1),x(_2),Y(_3),tag(_4) {}
    bool operator < (const segment &s)const {return Y<s.Y;}
}seg[maxn];
void pushup(int l,int r,int o)
{
    if(add[o])sum[o]=pos[r+1]-pos[l];
    else if(l==r)sum[o]=0;
    else sum[o]=sum[o<<1]+sum[o<<1|1];
}
void update(int l,int r,int o,int L,int R,int c)
{
    if(L==l && r==R)
    {
        add[o]+=c;
        pushup(l,r,o);
        return;
    }
    int mid=(l+r)>>1,lo=o<<1,ro=lo|1;
    if(R<=mid)update(l,mid,lo,L,R,c);
    else if(L>mid)update(mid+1,r,ro,L,R,c);
    else update(l,mid,lo,L,mid,c),update(mid+1,r,ro,mid+1,R,c);
    pushup(l,r,o);
}
int main()
{
    while(scanf("%d",&n)!=EOF && n)
    {
        mem(pos,0);mem(sum,0);mem(add,0);ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            seg[i*2]=segment(a,c,b,1);pos[i*2]=a;
            seg[i*2-1]=segment(a,c,d,-1);pos[i*2-1]=c;
        }
        sort(seg+1,seg+n*2+1);sort(pos+1,pos+n*2+1);
        for(int i=1;i<=2*n;i++)
        {
            int l=lower_bound(pos+1,pos+n*2+1,seg[i].X)-pos;
            int r=lower_bound(pos+1,pos+n*2+1,seg[i].x)-pos-1;
            update(1,2*n,1,l,r,seg[i].tag);
            ans+=sum[1]*(seg[i+1].Y-seg[i].Y);
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}

时间: 2024-08-07 04:33:08

[codevs3044]矩形面积求并的相关文章

codves 3044 矩形面积求并

codves  3044 矩形面积求并  题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n,表示矩形个数(n<=100) 接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的

codevs 3044 矩形面积求并

3044 矩形面积求并 时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n,表示矩形个数(n<=100) 接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2

已知矩形面积求最小周长

Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper. Input In the first line, there is an integer T indicates the number of te

矩形面积求并(codevs 3044)

题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行一个数n,表示矩形个数(n<=100) 接下来n行每行4个实数x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的左下角坐标和右上角坐标 输出描述 Output Description 每组数

矩形面积求并

[题目描述] 输入n个矩形,求它们总共占地面积(也就是求一下面积的并). [输入描述] 可能有多组数据,读到n=0为止(不超过15组). 每组数据第一行包含1个数n,表示矩形个数(n <= 100): 接下来n行每行4个实数x1.y1.x2.y1(0 <= x1 < x2 <= 100000,0 <= y1 < y2 <= 100000),表示矩形的左下角坐标和右上角坐标. [输出描述] 对于每组数据输出一行表示答案. [样例输入] 2 10 10 20 20 1

codevs 3044 矩形面积求并 (扫描线)

/* 之前一直偷懒离散化+暴力做着题 今天搞一下扫描线 自己按照线段树的一般写法写的有些问题 因为不用于以前的区间sum so 题解搬运者23333 Orz~ 去掉了打标记的过程 同时更新区间的时候先判断是不是已经需要赋值 还有一些细节的处理 线段树是离散化之后的x轴建的 每个线段的权值转移到点上 每个点代表他右侧一小段的长度 所以修改[l,r]变为[l,r-1] 另外维护lazy 表示这个区间压了几次 只要lazy[k]>0 s[k]就存着值 碰到顶边就lazy-- 每次计算面积用的是s[1]

25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有

package zhongqiuzuoye; public class Rect { public double width; public double height; Rect(double width,double height) //带有两个参数的构造方法,用于将width和height属性初化; { this.width=width; this.height=height; } Rect() //不带参数的构造方法,将矩形初始化为宽和高都为10. { width=10; height=

HDU 1542 Atlantis (求矩形面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of

HDU 1255 覆盖的面积 (求矩形面积的交)

覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个