Description

In the year 2051, several Mars expeditions have explored different areas of the red planet and produced maps of these areas. Now, the BaSA (Baltic Space Agency) has an ambitious plan: they would like to produce a map of the whole planet. In order to calculate the necessary effort, they need to know the total size of the area for which maps already exist. It is your task to write a program that calculates this area.

Task Write a program that:

  • reads the description of map shapes from the input file mar.in,
  • computes the total area covered by the maps,
  • writes the result to the output file mar.out.

题目大意:给你N个矩形,给出矩形对角坐标,要你求出这些矩形所覆盖的面积

Input

The input file mar.in starts with a line containing a single integer N (1<=N<=10 000) the number of available maps. Each of the following N lines describes a map. Each of these lines contains four integers x1, y1, x2 and y2 (0<=x1the coordinates of, respectively, the bottom-left and the top-right corner of the mapped area. Each map has rectangular shape, and its sides are parallel to the x- and y-axis of the coordinate system.

Output

The output should contain one integer A, the total explored area

(i.e. the area of the union of all rectangles).

Sample Input

2

10 10 20 20

15 15 25 30

Sample Output

225


这道题目是一个比较经典的题目,它用到了线段树扫描线这个方法。

扫描线是什么东西?

首先我们给出一张图,图中共有3个矩形,覆盖的总面积为24。



图中绿色的线即为扫描线。线段树上维护的信息为扫描线上的区间是否被覆盖。

扫描线从左方向右方移动,图中红色的边叫做起始边,黄色的边叫做终止边。在扫描线不断扫描的过程中,碰到起始边,我们就将起始边覆盖上去;碰到终止边时,就从扫描线上将其抹去。

这样做有什么用?



我们把关建边(起始边和终止边的统称)所在的位置标明出来。每两个关建边之间都有一段面积(图中相同颜色的为同一段),这些面积之和即为总覆盖面积。这些面积怎么算?

其实相信大家心中已经有了答案。

每一小段的面积,就是上一个关建边操作完之后,线段树上覆盖区间大小乘上两个关建边之间的距离。

所以接下来就是傻逼的处理和线段树的维护了。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=5e4;
struct AC{
int l,r,val,ID;
bool operator <(const AC &x)const{return val!=x.val?val<x.val:ID>x.ID;}//按横坐标排序,起始边要摆在终止边之前。至于为什么,见文章底
}A[N];
struct Segment{
#define ls (p<<1)
#define rs (p<<1|1)
int tree[N*16+10],Lazy[N*16+10];//线段树记录的就是一个个区间,而不是一个个坐标,因此主程序中使用r-1
void updata(int p,int l,int r){//区间覆盖大小的维护
if (Lazy[p]) tree[p]=r-l+1;
else tree[p]=(l==r)?0:(tree[ls]+tree[rs]);
}
void change(int p,int l,int r,int x,int y,int t){//简单打标记
if (x<=l&&r<=y){
Lazy[p]+=t;
updata(p,l,r);
return;
}
int mid=(l+r)>>1;
if (x<=mid) change(ls,l,mid,x,y,t);
if (y>mid) change(rs,mid+1,r,x,y,t);
updata(p,l,r);
}
}T;
int main(){
int n=read(),ans=0;
for (int i=1;i<=n;i++){
int a=read(),b=read(),c=read(),d=read();
A[i].ID=1,A[i+n].ID=-1;//ID记录是起始边还是结束边
A[i].val=a, A[i].l=b, A[i].r=d-1;//按关建边的x坐标排序,因此要记录,l,r即为区间左右边界
A[i+n].val=c,A[i+n].l=b,A[i+n].r=d-1;
}
sort(A+1,A+1+2*n);
for (int i=1;i<=2*n;i++){
if (i!=1) ans+=(A[i].val-A[i-1].val)*T.tree[1];//i=1的时候没有什么东西可以加
T.change(1,-N,N,A[i].l,A[i].r,A[i].ID);
}
printf("%d\n",ans);
return 0;
}

为什么在排序的时候,起始边要在终止边前面呢?

其实对于本题而言,并没有什么影响。

不过起始边和终止边的摆放位置对下一题有影响。

请参考[IOI1998]Picture

[BZOJ1382]Mars Maps的更多相关文章

  1. bzoj1382: [Baltic2001]Mars Maps

    Description 给出N个矩形,N<=10000.其坐标不超过10^9.求其面积并 Input 先给出一个数字N,代表有N个矩形. 接下来N行,每行四个数,代表矩形的坐标. Output ...

  2. BZOJ1382:[Baltic2001]Mars Maps

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. [IOI1998]Picture

    Description 在一个平面上放置一些矩形,所有的边都为垂直或水平.每个矩形可以被其它矩形部分或完全遮盖,所有矩形合并成区域的边界周长称为轮廓周长. 要求:计算轮廓周长. 数据规模: 0≤矩形数 ...

  5. How to Tell Science Stories with Maps

    Reported Features How to Tell Science Stories with Maps August 25, 2015   Greg Miller This map, part ...

  6. 【腾讯Bugly干货分享】微信终端跨平台组件 Mars 系列 - 我们如约而至

    导语 昨天上午,微信在广州举办了微信公开课Pro.于是,精神哥这两天的朋友圈被小龙的"八不做"刷屏了.小伙伴们可能不知道,下午,微信公开课专门开设了技术分论坛.在分论坛中,微信开源 ...

  7. 【腾讯Bugly干货分享】微信终端跨平台组件 mars 系列(二) - 信令传输超时设计

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/9DJxipJaaBC8yC-buHgnTQ 作者简介: ...

  8. 如约而至:微信自用的移动端IM网络层跨平台组件库Mars已正式开源

    1.前言 关于微信内部正在使用的网络层封装库Mars开源的消息,1个多月前就已满天飞(参见<微信Mars:微信内部正在使用的网络层封装库,即将开源>),不过微信团队没有失约,微信Mars ...

  9. 检索Google Maps地图位置(小训练)

    名称:检索地图位置 内容:地图初期显示和检索显示 功能:根据条件检索地图的经度与纬度 1.在这之前我们需要创建一个表(Accoun__c),添加一个重要的字段地理位置情報,它会默认的给你两个字段经度和 ...

随机推荐

  1. jsoup 提取 html 中的所有链接、图片和媒体

    原文:http://www.open-open.com/code/view/1420729333515 package org.jsoup.examples; import org.jsoup.Jso ...

  2. 【APUE】信号

    一.信号概念 信号都被定义为正整数,不存在编号为0的信号. 信号是异步事件的经典实例.产生信号的事件对进程而言是随机出现的,进程不能简单地测试一个变量来判别是否出现了一个信号,而是必须告诉内核在此信号 ...

  3. 用Lazarus编写第一个程序Pascal版的hello world

    安装 Lazarus的过程不用多说,都是傻瓜式的. 打开Lazarus.Lazarus会自己主动新建一个窗体形式的应用程序. 你会看到五个窗体. 主窗体 这个窗体显示有标题栏.菜单条和工具栏. 对象视 ...

  4. AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)

    首先引申下AIDL.什么是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情况下,我们在同一进程内会使用Binder.Broad ...

  5. Make 编译脚本上手

    考察下面的示例代码: main.c #include <stdio.h> int main(){ printf("hello world!"); return 0; } ...

  6. Centos 6.4 实际工作环境搭建(LNMP)

    基本配置 服务器IP设置.编辑网卡配置文件,命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 注:ifcfg-eth0参数  TYPE=Ethernet ...

  7. “var arr = []; ”和 “var arr = {};” 的差别

    1.面试题    var arr = [];    var arr = {};    比較上述代码有什么差别? 2.解析    var arr = [];是一个数组对象    var arr = {} ...

  8. HLJU 1220: AC100天 (枚举)

    1220: AC100天 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 89  Solved: 12 [id=1220">Submit ...

  9. Oracle学习(18)【DBA向】:分布式数据库

    分布式数据库 什么是分布数据库? l数据物理上被存放在网络的多个节点上,逻辑上是一个总体. 分布式数据库的独立性 l分布数据的独立性指用户不必关心数据怎样切割和存储,仅仅需关心他须要什么数据. Ora ...

  10. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM(转载)

    http://www.cnblogs.com/indream/p/3602348.html 刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code ...