BZOJ2957&&洛谷P4198 楼房重建

线段树维护上升斜率

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 700010
using namespace std;
int t[maxn],n,q;
double mx[maxn];
inline int read()
{
	int x=0;char ch=getchar();
	while (ch>'9'||ch<'0') ch=getchar();
	while (ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar();
	return x;
}
inline int query(int x,int l,int r,double tmp)
{// > tmp 的个数 
	if(l==r)
		return mx[x]>tmp;
	int mid=l+r>>1;
	if(mx[x<<1]>tmp)
		return query(x<<1,l,mid,tmp)+t[x] - t[x<<1];
	else
		return query(x<<1|1,mid+1,r,tmp);
}
inline void update(int x,int l,int r,int pos,double tmp)
{
	if(l==r) return (void)(t[x]=1,mx[x]=tmp);
	int mid=l+r>>1;
	if(pos<=mid)
		update(x<<1,l,mid,pos,tmp);
	else
		update(x<<1|1,mid+1,r,pos,tmp);
	t[x]=t[x<<1]+query(x<<1|1,mid+1,r,mx[x<<1]);
	mx[x]=max(mx[x<<1],mx[x<<1|1]);
}
int main()
{
	n=read();q=read();
	for(int i=1,x,y;i<=q;i++)
	{
		x=read();y=read();
		update(1,1,n,x,(double)y/x);
		printf("%d\n",t[1]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/acerandaker/article/details/80899888