博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【清华集训】楼房重建 BZOJ 2957
阅读量:4636 次
发布时间:2019-06-09

本文共 3722 字,大约阅读时间需要 12 分钟。

Description

  小A的楼房外有一大片施工工地,工地上有N栋待建的楼房。每天,这片工地上的房子拆了又建、建了又拆。他经常无聊地看着窗外发呆,数自己能够看到多少栋房子。

  为了简化问题,我们考虑这些事件发生在一个二维平面上。小A在平面上(0,0)点的位置,第i栋楼房可以用一条连接(i,0)和(i,Hi)的线段表 示,其中Hi为第i栋楼房的高度。如果这栋楼房上任何一个高度大于0的点与(0,0)的连线没有与之前的线段相交,那么这栋楼房就被认为是可见的。
  施工队的建造总共进行了M天。初始时,所有楼房都还没有开始建造,它们的高度均为0。在第i天,建筑队将会将横坐标为Xi的房屋的高度变为Yi(高度 可以比原来大---修建,也可以比原来小---拆除,甚至可以保持不变---建筑队这天什么事也没做)。请你帮小A数数每天在建筑队完工之后,他能看到多 少栋楼房?

Input

  第一行两个正整数N,M

  接下来M行,每行两个正整数Xi,Yi

Output

        M行,第i行一个整数表示第i天过后小A能看到的楼房有多少栋

Sample Input

3 4
2 4
3 6
1 1000000000
1 1

Sample Output

1
1
1
2
数据约定
  对于所有的数据1<=Xi<=N,1<=Yi<=10^9
N,M<=100000

思路

    恩。。网上用分块做的居多。那我们就用线段树做吧!

    对于[l,r]建立一个节点,记录下从l节点开始的最长上升序列的长度以及在哪里结尾,这样上升子序列的最大值也知道了。

    然后两个区间怎么合并呢?

    如果左子树的最高点比右子树最左边的点还要高,那么就是左子树。

    不然就在右子树中找,小于等于左子树最高点的有多少个,记为Rank。结尾变成右子树的结尾,长度变成左子树+右子树-Rank。

    如何在一颗子树中找小于等于一个数的点有多少个?实际上跟合并差不多。

    如果小于最小点或者大于等于最大点直接返回,不然分情况讨论是不是大于左子树的最大值。

 1.不是:在左子树中递归查找

    2.是:在右子树中查找出小于这个数的个数Rank,返回Rank-(左子树长度+右子树长度-当前节点的长度)+左子树长度

    我特意没有化简表达式。。画个图很明白吧(不要吐槽)

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #define pritnf printf 17 #define scafn scanf 18 #define sacnf scanf 19 #define For(i,j,k) for(int i=(j);i<=(k);(i)++) 20 #define Clear(a) memset(a,0,sizeof(a)) 21 using namespace std; 22 typedef unsigned int Uint; 23 const int INF=0x3fffffff; 24 const double eps=1e-10; 25 ///==============struct declaration============== 26 struct Seg_Node{ 27 int ed,length; 28 }; 29 ///==============var declaration================= 30 const int MAXN=100010; 31 int n,L,R,m; 32 long double Building[MAXN]; 33 Seg_Node Seg_Tree[MAXN*5],Ans; 34 ///==============function declaration============ 35 void BuildTree(int o,int l,int r); 36 void Update(int o,int l,int r); 37 void Query(int o,int l,int r); 38 int Query(int o,int l,int r,double height); 39 void Modify(int o,int l,int r,int k,double h); 40 bool Equal(double a,double b){ return fabs(a-b)<=1e-13;} 41 ///==============main code======================= 42 int main() 43 { 44 #define FILE__ 45 #ifdef FILE__ 46 freopen("input","r",stdin); 47 freopen("output","w",stdout); 48 #endif 49 scanf("%d%d",&n,&m); 50 BuildTree(1,1,n); 51 Building[0]=-10; 52 while (m--){ 53 int X,Y;scanf("%d%d",&X,&Y); 54 Modify(1,1,n,X,double(Y)/X); 55 L=1,R=n; 56 printf("%d\n",Seg_Tree[1].length); 57 } 58 return 0; 59 } 60 ///================fuction code==================== 61 void BuildTree(int o,int l,int r){ 62 int m=(l+r)>>1,lc=o*2,rc=o*2+1; 63 if (l==r){ 64 Seg_Tree[o].ed=l; 65 Seg_Tree[o].length=0; 66 return ; 67 } 68 BuildTree(lc,l,m); 69 BuildTree(rc,m+1,r); 70 Update(o,l,r); 71 } 72 void Update(int o,int l,int r){ 73 int m=(l+r)>>1,lc=o*2,rc=o*2+1; 74 if (Seg_Tree[rc].length==0||Building[Seg_Tree[lc].ed]>=Building[Seg_Tree[rc].ed]) 75 Seg_Tree[o]=Seg_Tree[lc]; 76 else if (Seg_Tree[lc].length==0) 77 Seg_Tree[o]=Seg_Tree[rc]; 78 else{ 79 int Rank=Query(rc,m+1,r,Building[Seg_Tree[lc].ed]); 80 Seg_Tree[o].ed=Seg_Tree[rc].ed; 81 Seg_Tree[o].length=Seg_Tree[lc].length+Seg_Tree[rc].length-Rank; 82 } 83 } 84 int Query(int o,int l,int r,double height){ ///Return how many numbers Less than or equal to height 85 int m=(l+r)>>1,lc=o*2,rc=o*2+1; 86 if (height>=Building[Seg_Tree[o].ed]) return Seg_Tree[o].length; 87 if (height
>1,lc=o*2,rc=o*2+1; 93 if (l==r){ 94 Building[l]=h; 95 if (!Equal(h,0)) 96 Seg_Tree[o].length=1; 97 else 98 Seg_Tree[o].length=0; 99 }100 else{101 if (m>=k) Modify(lc,l,m,k,h);102 else Modify(rc,m+1,r,k,h);103 Update(o,l,r);104 }105 }
BZOJ 2957

 

转载于:https://www.cnblogs.com/Houjikan/p/4353081.html

你可能感兴趣的文章
儿子和女儿——解释器和编译器的区别与联系
查看>>
第一阶段冲刺3
查看>>
2014百度面试题目---“求比指定整数大且最小的不重复数”解答
查看>>
父类引用指向子类对象
查看>>
linux epoll用法
查看>>
viewport使用 html5
查看>>
网页如何实现下载功能
查看>>
IT男专用表白程序
查看>>
【BZOJ】2120: 数颜色
查看>>
spring boot 文件上传工具类(bug 已修改)
查看>>
《机电传动控制》学习笔记03-1
查看>>
读《大道至简》第六章感想
查看>>
ef linq 中判断实体中是否包含某集合
查看>>
金蝶K/3 BOS产品培训教案
查看>>
章三 链表
查看>>
react组件回顶部
查看>>
【LeetCode】Palindrome Partitioning 解题报告
查看>>
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
从壹开始微服务 [ DDD ] 之一 ║ D3模式设计初探 与 我的计划书
查看>>
python 错误之SyntaxError: Missing parentheses in call to 'print'
查看>>