bzoj 2957: 楼房重建 线段树

2957: 楼房重建

Time Limit: 10 Sec  Memory Limit: 256 MB
[Submit][Status][Discuss]

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 第一天

看hzwer的

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=4e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
struct is
{
int l,r;
double maxx;
int ans;
}tree[N];
void build(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].ans=;
tree[pos].maxx=0.0;
if(l==r)return;
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
}
int getans(double v,int pos)
{
if(tree[pos].l==tree[pos].r)
return (tree[pos].maxx>v);
if(tree[pos<<].maxx<=v)
return getans(v,pos<<|);
return tree[pos].ans-tree[pos<<].ans+getans(v,pos<<);
}
void pushup(int pos)
{
tree[pos].maxx=max(tree[pos<<].maxx,tree[pos<<|].maxx);
tree[pos].ans=tree[pos<<].ans+getans(tree[pos<<].maxx,pos<<|);
}
void update(int p,double c,int pos)
{
if(tree[pos].l==tree[pos].r)
{
tree[pos].maxx=c;
tree[pos].ans=;
return;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(p<=mid)
update(p,c,pos<<);
else
update(p,c,pos<<|);
pushup(pos);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(,n,);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
update(x,(double)(y/x),);
printf("%d\n",tree[].ans);
}
}
return ;
}
上一篇:TLD目标跟踪算法


下一篇:Random类短时间大量随机重复的问题