当前位置: 首页 > news >正文

解题报告-P3081 USACO13MAR Hill Walk G

P3081 USACO13MAR Hill Walk G

题目描述

There are N hills (1 <= N <= 100,000). Each hill takes the form of a line segment from (x1, y1) to (x2, y2) where x1 < x2 and y1 < y2. None of these segments intersect or touch, even at their endpoints, and furthermore, the first hill satisfies (x1, y1) = (0,0).

Bessie the cow starts at (0,0) on the first hill. Whenever Bessie is on a hill, she climbs up until she reaches the end. Then she jumps off the edge. If she lands on another hill, she continues walking on that hill; otherwise, she falls very far until she lands safely on a cushion of pillows at y = -infinity. Each hill (x1, y1) -> (x2, y2) should be regarded as containing the point (x1, y1) but not containing the point (x2, y2), so that Bessie will land on the hill if she falls on it from above at a position with x = x1, but she will not land on the hill if she falls on it from above at x = x2.

Please count the total number of hills that Bessie touches at some point during her walk.

\(N(1 \le N \le 10 ^ 5)\)座小山,每座山所占的区域用直线 \((x1, y1)\)\((x2, y2)\) 来表示(\(x1 < x2\) 并且 \(y1 < y2\))。也就是说这些山用笛卡尔坐标系里的线段来表示,这些用于表示小山的线段都没有任何交点,第一座山的一端位于 \((x1, y1) = (0,0)\)

贝西从 \((0,0)\) 开始在第一座山上漫步,一旦贝西到了一座山,她会一直走到该山的终点,这时,她会从边缘处起跳,如果她降落到另一座山上,她会继续在新的山上漫步。贝西起跳后沿 \(y\) 轴方向下落,如果贝西不能降落到一座山上,她会一直下落,直到到达 \(y\) 轴的负无穷大位置(\(y = -\infin\))。

每座用线段表示的山 \((x1, y1) \to (x2, y2)\) 包含 \((x1, y1)\) 这个点,但不包含 \((x2, y2)\),请计算出贝西总共在多少座山上漫步了。

输入格式

* Line 1: The number of hills, N.

* Lines 2..1+N: Line i+1 contains four integers (x1,y1,x2,y2)

describing hill i. Each integer is in the range

0..1,000,000,000.

输出格式

* Line 1: The number of hills Bessie touches on her journey.

输入输出样例 #1

输入 #1

4 
0 0 5 6 
1 0 2 1 
7 2 8 5 
3 0 7 7

输出 #1

3

说明/提示

There are four hills. The first hill runs from (0,0) to (5,6), and so on.

Bessie walks on hills #1, #4, and finally #3.


解题报告

扫描线+平衡树。

具体的,用扫描线维护线段的横坐标,用平衡树维护线段之间的高度。

首先将每个线段拆成起点和终点,按横坐标升序排序后扫描。

扫到了某个线段的起点就插进平衡树中。

扫到了某个线段的终点,如果这个线段是当前线段,那么用平衡树找到该线段下方的第一条线段作为新的当前线段,并把之前的当前线段从平衡树中删除;如果不是当前线段,就直接从删除平衡树中删除。

那么就剩下一个问题:怎么比较两个线段的上下关系?

其实我自己没推明白,这里引一个图解,是Soh_paramEEMS 的 题解:P3081 USACO13MAR Hill Walk G。

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1001100;#define ckmax(x,y) ( x=max(x,y) )
#define ckmin(x,y) ( x=min(x,y) )inline int read()
{int f=1,x=0; char ch=getchar();while(!isdigit(ch)) { if(ch=='-') f=-1; ch=getchar(); }while(isdigit(ch))  { x=x*10+ch-'0';    ch=getchar(); }return f*x;
}int n;struct seg
{int xl,yl,xr,yr,pos;inline void putin(int p){xl=read(),yl=read();xr=read(),yr=read();pos=p;}bool operator < (const seg &t)const{if(xr<t.xr)return 1LL*(t.xr-xr)*(t.yr-t.yl)<1LL*(t.xr-t.xl)*(t.yr-yr);elsereturn 1LL*(xr-t.xr)*(yr-yl)>1LL*(xr-xl)*(yr-t.yr);}}s[N];struct dot
{int x,y,pos;bool operator < (const dot &t)const{  return x<t.x || ( x==t.x && y<t.y );  }
}p[N];
int nP;set<seg> T;
set<seg>::iterator it1;
set<seg>::iterator it2;signed main()
{n=read();for(int i=1;i<=n;i++){s[i].putin(i);p[++nP]=(dot){ s[i].xl,s[i].yl,i };p[++nP]=(dot){ s[i].xr,s[i].yr,i };}sort(p+1,p+nP+1);T.insert(s[1]);int pos=1,tot=1;for(int i=2;i<=nP;i++){dot u=p[i];seg tmp=s[u.pos];if(u.x==tmp.xl) T.insert(tmp);else if(u.pos==pos){it1=T.find(tmp);if(it1==T.begin()) break;it2=it1;--it2;pos=it2->pos;T.erase(it1);tot++;}else T.erase(tmp);}printf("%d\n",tot);return 0;
}
http://www.jsqmd.com/news/139423/

相关文章:

  • [Windows] 时间和倒计时关机小工具V2.0
  • [吾爱大神原创工具] Net Tools-网络运维工具箱
  • [吾爱大神原创工具] Net Tools-网络运维工具箱
  • 实用指南:量子计算入门:Python量子编程基础
  • HTTP请求方法
  • Pinia状态管理实战教程
  • hot100 141.环形链表
  • 实现自定义指令 v-scrollBar,用于动态显示/隐藏滚动条,提升用户体验
  • 巨量AD广告专业服务商:诚信之选带来的行业变革
  • doris中的分区上卷
  • 工商注册服务推荐:选对公司,开启企业省心之旅
  • doris中的Broadcast Join
  • 工商注册服务哪家好?靠谱之选看这里
  • 某机构趁低买入以太坊,持仓超300万枚
  • 2025年好吃的重庆香肠品牌排行,满足不同场合和个人喜好需求 - 讯息观点
  • 启用Qoder编写ztdaq的C#跨专业的平台示例总结
  • ProfiNet转CAN网关优质生产商推荐
  • 2025最新!继续教育必备9个AI论文平台深度测评
  • doris的Bucket Shuffle Join
  • 8个AI论文软件推荐,继续教育学生轻松搞定毕业论文!
  • 2026设计师私藏,正版高清图片素材网站,商用无风险,购买超省心 - 品牌2026
  • 2025年推荐电池厂排行榜,新测评精选电池正规厂商与电池生产企业推荐
  • XZ Utils库后门漏洞深度剖析:CVE-2024-3094的RCE风险与缓解方案
  • 微信小程序vue_uniapp二手书交易平台
  • 全网热议!2025年热门空调安装品牌推荐,助您选择优质的合作伙伴 - 讯息观点
  • 会议精灵:用ModelEngine构建智能办公助手实战记录
  • Doris的Colocation[托管] Join
  • 2026全网精选,商用高清正版图片素材网站合集,无版权风险放心用 - 品牌2026
  • Spring Boot 与 Apache POI 实现复杂嵌套结构 Excel 导出
  • 3453453