【BZOJ2957】楼房重建

来源:互联网 发布:淘宝店铺的装修视频 编辑:程序博客网 时间:2024/06/08 05:53

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
HINT

Source

中国国家队清华集训 2012-2013 第一天

要找的是严格递增的斜率的最长长度,因此我们使用线段树来维护一个长度一个区间最大斜率,然后对每个节点的这个长度,我们可以用左右子树来更新.

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define lchild rt<<1,l,mid#define rchild rt<<1|1,mid+1,r#define ln rt<<1#define rn rt<<1|1#define MAXN 100010#define GET (ch>='0'&&ch<='9')using namespace std;int n,m;struct seg{    int l,r,ans;    double maxn;}tree[MAXN<<2];void in(int &x){    char ch=getchar();x=0;    while (!GET)    ch=getchar();    while (GET) x=x*10+ch-'0',ch=getchar();}void build(int rt=1,int l=1,int r=n){    tree[rt].l=l;tree[rt].r=r;    if (l==r)   return;    int mid=(l+r)>>1;build(lchild);build(rchild);}int update(int rt,double k){    int L=tree[rt].l,R=tree[rt].r;    if (L==R)   return tree[rt].maxn>k;    if (tree[ln].maxn<=k)   return update(rn,k);    return tree[rt].ans-tree[ln].ans+update(ln,k);}void modify(int rt,int x,double k){    int L=tree[rt].l,R=tree[rt].r,mid=(L+R)>>1;    if (L==R)   {tree[rt].ans=1;tree[rt].maxn=k;return;}    if (x<=mid) modify(ln,x,k);    else    modify(rn,x,k);    tree[rt].maxn=max(tree[ln].maxn,tree[rn].maxn);tree[rt].ans=tree[ln].ans+update(rn,tree[ln].maxn);}int main(){    in(n);in(m);int x,y;build();    for (int i=1;i<=m;i++)    {        in(x);in(y);modify(1,x,(double)(y)/x);        printf("%d\n",tree[1].ans);    }}
0 0
原创粉丝点击