• 【清华集训】楼房重建 BZOJ 2957


    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 <iostream>
      2 #include <cstring>
      3 #include <string>
      4 #include <cstdio>
      5 #include <cstdlib>
      6 #include <cmath>
      7 #include <algorithm>
      8 #include <queue>
      9 #include <stack>
     10 #include <map>
     11 #include <set>
     12 #include <list>
     13 #include <vector>
     14 #include <ctime>
     15 #include <functional>
     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
    ",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<Building[l]) return 0;
     88    if (height<Building[Seg_Tree[lc].ed]) return Query(lc,l,m,height);
     89    return Seg_Tree[lc].length+Query(rc,m+1,r,height)-(Seg_Tree[lc].length+Seg_Tree[rc].length-Seg_Tree[o].length);
     90 }
     91 void Modify(int o,int l,int r,int k,double h){
     92    int m=(l+r)>>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
  • 相关阅读:
    js 安全
    js压缩 uglify(2)
    js压缩 uglify
    抢红包算法 java
    手机调试
    Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
    字符集编码 定长与变长
    db2 sqlcode
    2015.7.14(大盘结束红色,中色连坐4T)
    2015.7.10(全部涨停!想逢高出货,但是担心周一创新高)
  • 原文地址:https://www.cnblogs.com/Houjikan/p/4353081.html
Copyright © 2020-2023  润新知