Lock Her Up
题意描述
同样没有链接...。
Problem 2: Lock Her Up [Jan Kuipers, 2003]
Bessie has been bad again and Farmer John must punish her by locking her up for a while. On his pastures he has N (1 <= N <= 250,000) rectangular fences in which he can pen her. His fences don't overlap or touch, however any given fenced area might wholly contain one or more other areas.
He knows that Bessie is a very smart cow and good at escaping from her prison. Therefore he wants to put her within a pen that is surrounded by as many other pens as possible. Furthermore he wants to know how many of those 'good prisons' there exist on his pastures. Help him learn these numbers.
PROBLEM NAME: lock
INPUT FORMAT:
Line 1: A single line with the single integer, N
Lines 2..N+1: Each line contains four space-separated integers (respectively X1, Y1, X2 and Y2) that are the coordinates of the lower left and upper right corner of the fences. All coordinates are in the range 1..1,000,000,000 and both X1<X2 and Y1<Y2.
SAMPLE INPUT (file lock.in):
4
1 1 16 16
6 6 11 13
7 7 9 12
3 3 10 5OUTPUT FORMAT:
- Line 1: Two space-separated integers: the maximal number of fences that can surround Bessie and how many such places have this property.
SAMPLE OUTPUT (file lock.out):
3 1
OUTPUT DETAILS:
The 3rd fence is surrounded by fence 1 and 2, so Bessie can be locked within 3 fences. There is only 1 such place.
给定平面内 \(n\) 个矩形的左下角和右上角坐标,每个矩形边界没有重叠或覆盖。
求被最多矩形圈住的矩形的被圈层数,以及这种矩形的数量。
算法分析
像极了扫描线,于是就过了...。
将每个矩形变为上下两条边,离散化后直接线段树维护即可。
线段树维护的信息是这个点所在的矩形数量,具体流程如下:
- 遇到一个矩形的上边时将 \([x_1,x_2]\) 区间加 \(1\)。
- 遇到一个矩形的下边时查询点 \(x_1\) 的值并更新答案。
- 再将 \([x_1,x_2]\) 区间减 \(1\)。
具体看代码吧。
代码实现
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#define N 500010
using namespace std;
int n,ans=-1,num,cnt=0;
int tree[N<<2];
int X[N],Y[N];
struct node{
int x1,x2,y,flag;
}p[N];
int read(){
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0' && c<='9') x=x*10+c-48,c=getchar();
return x*f;
}
bool cmp(node a,node b){
return a.y<b.y;
}
int find(int p,int l,int r,int x){
if(l==r || tree[p]) return tree[p];
int mid=(l+r)>>1;
if(x<=mid) return find(p<<1,l,mid,x);
else return find(p<<1|1,mid+1,r,x);
}
void change(int p,int L,int R,int l,int r,int x){
if(l<=L && R<=r){tree[p]+=x;return;}
int mid=(L+R)>>1;
if(tree[p]) tree[p<<1]=tree[p<<1|1]=tree[p],tree[p]=0;
if(l<=mid) change(p<<1,L,mid,l,r,x);
if(r>mid) change(p<<1|1,mid+1,R,l,r,x);
return;
}
int main(){
//freopen("lock.in","r",stdin);
//freopen("lock.out","w",stdout);
n=read();
int x1,y1,x2,y2;
for(int i=1;i<=n;i++){
x1=read(),y1=read(),x2=read(),y2=read();
p[++cnt].x1=x1;p[cnt].x2=x2;p[cnt].y=y1;p[cnt].flag=1;X[cnt]=x1;Y[cnt]=y1;
p[++cnt].x1=x1;p[cnt].x2=x2;p[cnt].y=y2;p[cnt].flag=-1;X[cnt]=x2;Y[cnt]=y2;
}
sort(X+1,X+cnt+1);
sort(Y+1,Y+cnt+1);
int mx=unique(X+1,X+cnt+1)-(X+1);
int my=unique(Y+1,Y+cnt+1)-(Y+1);
for(int i=1;i<=cnt;i++){
int px1=lower_bound(X+1,X+mx+1,p[i].x1)-X;
int px2=lower_bound(X+1,X+mx+1,p[i].x2)-X;
int py=lower_bound(Y+1,Y+my+1,p[i].y)-Y;
p[i].x1=px1;p[i].x2=px2;p[i].y=py;
}
sort(p+1,p+cnt+1,cmp);
//上面是离散化。
for(int i=1;i<=cnt;i++){
if(p[i].flag==-1){
int now=find(1,1,cnt,p[i].x1);//查询点 x1 的值。
if(now>ans)
ans=now,num=1;
else if(now==ans)
num++;
}
change(1,1,cnt,p[i].x1,p[i].x2,p[i].flag);//区间加/减 1。
}
printf("%d %d\n",ans,num);
//fclose(stdin);fclose(stdout);
return 0;
}
完结撒花。
Lock Her Up的更多相关文章
- C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent
看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...
- 多线程同步工具——Lock
本文原创,转载请注明出处. 参考文章: <"JUC锁"03之 公平锁(一)> <"JUC锁"03之 公平锁(二)> 锁分独占锁与共享锁, ...
- java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)
一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...
- InnoDB:Lock & Transaction
InnoDB 是一个支持事务的Engine,要保证事务ACID,必然会用到Lock.就像在Java编程一下,要保证数据的线程安全性,必然会用到Lock.了解Lock,Transaction可以帮助sq ...
- 使用四元数解决万向节锁(Gimbal Lock)问题
问题 使用四元数可以解决万向节锁的问题,但是我在实际使用中出现问题:我设计了一个程序,显示一个三维物体,用户可以输入绕zyx三个轴进行旋转的指令,物体进行相应的转动. 由于用户输入的是绕三个轴旋转的角 ...
- 万向节锁(Gimbal Lock)的理解
[TOC] 结论 我直接抛出结论: Gimbal Lock 产生的原因不是欧拉角也不是旋转顺序,而是我們的思维方式和程序的执行逻辑没有对应,也就是说是我们的观念导致这个情况的发生. 他人解释 首先我们 ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
- 谈谈 Lock
上来先看MSDN关于lock的叙述: lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁. 下面的示例包含一个 lock 语句. lock 关键字可确保当一 ...
- LOCK TABLES和UNLOCK TABLES与Transactions的交互
LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...
- SQL 性能调优中可参考的几类Lock Wait
在我们的系统出现性能问题时,往往避不开调查各种类型 Lock Wait,如Row Lock Wait.Page Lock Wait.Page IO Latch Wait等.从中找出可能的异常等待,为性 ...
随机推荐
- 常见加密算法的 JS 实现
前言 为破解前端加密做准备,先了解一些常见的加密算法 分类 对称加密 采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密. DES DES ...
- 00 what is C Programming?C编程是什么?
C语言简介 C is a programming language that lets us give a computer very specifio commands. C语言是一种编程语言,它让 ...
- Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'
查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss 服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日 ...
- Java 使用UDP传输一个小文本文件
工具1:Eclipse 工具2:IntelliJ IDEA Java工程的目录结构(基于IntelliJ IDEA) 例1.1:接收方,因为接收到的数据是字节流,为了方便,这里是基于Apache co ...
- 手写一个类SpringBoot的HTTP框架:几十行代码基于Netty搭建一个 HTTP Server
本文已经收录进 : https://github.com/Snailclimb/netty-practical-tutorial (Netty 从入门到实战:手写 HTTP Server+RPC 框架 ...
- Vim最强调试插件:vimspector
最近看到了韦大在知乎的回答后,想去试用一下vimspector,却发现vimspector诞生两年了却没有介绍它的中文资料.我查阅官方文档遇到不少困难,在这里记录折腾出来的结果,与大家分享. vims ...
- arcgis-java-100.8.0.jar下载
链接: https://pan.baidu.com/s/1HoW2IhPvHRw9LBZphxC5Rw 提取码: pexn
- MySQL5.7版本sql_mode=only_full_group_by问题解决办法
原因分析:MySQL5.7版本默认设置了 mysql sql_mode = only_full_group_by 属性,导致报错. 1.查看sql_mode SELECT @@sql_mode; 2. ...
- MeteoInfoLab脚本示例:中尺度气旋散点图
全球长时间序列中尺度气旋数据(http://cioss.coas.oregonstate.edu/eddies/)有netCDF格式,散点数据类型,只有一个很大的维Nobs = 2590938.尝试读 ...
- 发布MeteoInfo 1.2.6
增加了对AWX卫星数据格式的支持(和C#版本的功能相当).在MeteoInfo中打开AWX数据: 在MeteoInfoLab中打开AWX数据: