Description

在一个平面上放置一些矩形,所有的边都为垂直或水平。每个矩形可以被其它矩形部分或完全遮盖,所有矩形合并成区域的边界周长称为轮廓周长。

要求:计算轮廓周长。

数据规模:

0≤矩形数目<5000;

坐标数值为整数,范围是[-10000,10000]。

Input

第一横列是墙上所贴的长方形总数。之后每一横列是一个长方形的左下角与右上角的整数坐标。个各坐标的x值在前,y值在后。

Output

应有一非负整数,即为长方形覆盖图形的总周长

Sample Input

7

-15 0 5 10

-5 8 20 25

15 -4 24 14

0 -6 16 4

2 15 10 22

30 10 36 20

34 0 40 16

Sample Output

228


线段树+扫描线,参考[Baltic 2001]Mars Maps

不过还是要讲一个东西,关于排列方式的问题。

排序的时候,在同一位置的起始边需要在终止边之前枚举

为什么?

首先本题统计答案的时候,应该是计算每次操作后,添加或删除的区间大小。

然后扫描线从两个方向扫描一下即可。

那么如果我们将终止边排在起始边之前的话,两条线之间必然会有因为时间差而导致的空隙,也就是程序实现的时候,先加了终止边,又加了起始边,等于是把这条边算了两次,但是实际是不能算的!!!

所以说我们在排序的时候,要将起始边排在终止边的前面,这样就不会出现多加的问题了。

(ps:这个代码是我很久之前写的,所以和上一题的码风可能有所区别)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e4;
int tree[N*10+100],cnt[N*10+100];
int ans;
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*10+ch-'0';
return x*f;
}
struct AC{
int val,first,last,cnt;
};
bool cmp(AC x,AC y){
if (x.val<y.val) return 1;
if (x.val==y.val) if (x.cnt>y.cnt) return 1;
return 0;
}
void updata(int p){
if (cnt[p]){
if (tree[p]!=-1) tree[p]+=cnt[p];
cnt[p*2]+=cnt[p];
cnt[p*2+1]+=cnt[p];
cnt[p]=0;
}
}
void change(int p,int l,int r,int x,int y,int t){
if (x<=l&&r<=y&&tree[p]!=-1){
if ((tree[p]==1&&t==-1)||(tree[p]==0&&t==1)) ans+=r-l;
tree[p]+=t; cnt[p*2]+=t; cnt[p*2+1]+=t;
return;
}
int mid=(l+r)>>1;
updata(p*2);updata(p*2+1);
if (x<mid) change(p*2,l,mid,x,y,t);
if (y>mid) change(p*2+1,mid,r,x,y,t);
if (tree[p*2]==tree[p*2+1]) tree[p]=tree[p*2];
else tree[p]=-1;
}
AC h[N+100],s[N+100];
int main(){
int n=read();
for (int i=1;i<=n;i++){
int a=read(),b=read(),c=read(),d=read();
h[i].cnt=-1; h[n+i].cnt=1;
s[i].cnt=1; s[n+i].cnt=-1;
h[i].val=d; h[i].first=a; h[i].last=c;
h[i+n].val=b; h[i+n].first=a; h[i+n].last=c;
s[i].val=a; s[i].first=b; s[i].last=d;
s[i+n].val=c; s[i+n].first=b; s[i+n].last=d;
}
sort(h+1,h+2*n+1,cmp);
sort(s+1,s+2*n+1,cmp);
ans=0;
for (int i=1;i<=n*2;i++)
change(1,-N,N,h[i].first,h[i].last,h[i].cnt);
for (int i=1;i<=n*2;i++)
change(1,-N,N,s[i].first,s[i].last,s[i].cnt);
printf("%d\n",ans);
return 0;
}

[IOI1998]Picture的更多相关文章

  1. [BZOJ1382]Mars Maps

    Description In the year 2051, several Mars expeditions have explored different areas of the red plan ...

  2. 【IOI1998】Picture(扫描线+线段树)

    问题来源:IOI1998 D2T1 题意:就是在一个平面内给出n个矩形,叫你计算将这些矩形合并以后,新图形的周长. 例如: 上图是原本的矩形们 ---------->合并后的图形 解题思路:拿一 ...

  3. IOI1998 hdu1828 poj1177 Picture

    写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分..)都过了. 然后看到谋篇blog里有评论,把数据拿下来发现WA了. 数据是 20 0 1 11 0 ...

  4. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...

  5. MFC Picture控件加载图片

    CStatic *pPic = (CStatic*)GetDlgItem(IDC_PICTURE); CBitmap bitmap; bitmap.LoadBitmapW(IDB_BITMAP2); ...

  6. [POJ1177]Picture

    [POJ1177]Picture 试题描述 A number of rectangular posters, photographs and other pictures of the same sh ...

  7. USACO 5.5 Picture(周长并)

    POJ最近做过的原题. /* ID: cuizhe LANG: C++ TASK: picture */ #include <cstdio> #include <cstring> ...

  8. 彩色照片转换为黑白照片(Color image converted to black and white picture)

    This blog will be talking about the color image converted to black and white picture. The project st ...

  9. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

随机推荐

  1. centos Crontab

    minute   hour   day   month   week   command     顺序:分 时 日 月 周 命令 第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4 ...

  2. Rust 1.7.0 匹配器 match 的简介和使用

    使用过正則表達式的人应该都知道 matcher ,通过 matcher 匹配器运算正則表達式,完毕一系列的匹配规则. 在Rust 中 没有 switch 语句.matcher 就是 switch 的一 ...

  3. setenv LD_LIBRARY_PATH

    For most Linux binaries, NCL was built using gcc and gfortran. This may cause a dependency on a file ...

  4. Android之弹出多级菜单

    使用布局文件创建菜单:(多级菜单) 在res下创建目录menu(假设已经有啦就不用再创建了) 在该menu目录下创建XML文件这里我把文件名称命名为menu 在创建的menu.XML文件里 写入: & ...

  5. js 二维码

    https://larsjung.de/jquery-qrcode/ 源码 <!DOCTYPE html> <html> <head> <title>j ...

  6. Lnixu Bash

    一.简单命令 1.创建文件(vi) vi hellowold.txt2.创建目录(mkdir) mkdir linux_bash3.删除文件(rm) rm helloworld.txt4.复制文件(c ...

  7. ios开发--NSDate与NSDateFormatter的相关用法【转】

    原文地址:http://blog.sina.com.cn/s/blog_91ff71c0010188u9.html 1.NSDateFormatter配合NSDate与NSString之间的转化  N ...

  8. How to use filters in a GridPanel

    You can just link statically required files in your index.html <link rel="stylesheet" t ...

  9. Rod Johnson

    Spring Framework创始人,著名作者. Rod在悉尼大学不仅获得了计算机学位,同时还获得了音乐学位.更令人吃惊的是在回到软件开发领域之前,他还获得了音乐学的博士学位. 有着相当丰富的C/C ...

  10. ibatis和mybatis的区别

    区别1:全局配置文件(sqlMapConfig.xml)的差异 主要是元素标签命名的差异,比如mybatis的根元素标签为<configuration>,ibatis的 根元素标签为< ...