「洛谷 4198」楼房重建

传送门


problem

在一个二维平面上有 n n n 栋楼房,第 i i i 栋楼房可以用以 ( i , 0 ) (i,0) (i,0) ( i , h i ) (i,h_i) (i,hi) 为端点的线段表示,一开始所有的 h i = 0 h_i=0 hi=0

m m m 次操作,每次可以修改一个 h i h_i hi,并且在每次修改之后查询在 ( 0 , 0 ) (0,0) (0,0) 能看到的楼房个数(如果以 ( i , h i ) (i,h_i) (i,hi) ( 0 , 0 ) (0,0) (0,0) 为端点的线段与其它楼房没有交点,那么 i i i 就可见)。

数据范围: n , m ≤ 1 0 5 n,m\le10^5 n,m105


solution

可以先算一下 ( i , h i ) (i,h_i) (i,hi) ( 0 , 0 ) (0,0) (0,0) 的直线的斜率,能看到楼房的斜率一定是递增的。

考虑用线段树来维护这个东西,那么关键就在于 pushup 操作了。

首先,左儿子的答案是可以直接加进来的,而右儿子会受到左儿子中最大值的限制。

具体来说,我们可以找到右儿子中第一个比左儿子的 m x mx mx 大的位置,它的左边都不能选,右边按照原来的计算即可。

一边找一边算答案即可。

由于 pushup O ( log ⁡ n ) O(\log n) O(logn) 的,所以总时间复杂度 O ( n log ⁡ 2 n ) O(n\log^2n) O(nlog2n)


code

#include<bits/stdc++.h>
using namespace std;
namespace IO{
	const int Rlen=1<<22|1;
	char buf[Rlen],*p1,*p2;
	inline char gc(){
		return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
	}
	template<typename T>
	inline T Read(){
		char c=gc();T x=0,f=1;
		while(!isdigit(c))  f^=(c=='-'),c=gc();
		while( isdigit(c))  x=(x+(x<<2)<<1)+(c^48),c=gc();
		return f?x:-x;
	}
	inline int in()  {return Read<int>();}
}
using IO::in;
const int N=1e5+5;
int n,m,sum[N<<2];
double mx[N<<2];
#define mid ((l+r)>>1)
int find(double num,int root,int l,int r){
	if(l==r)  return mx[root]>num;
	if(mx[root<<1]>num)  return find(num,root<<1,l,mid)+sum[root]-sum[root<<1];  //注意这里不是sum[root<<1|1]。
	return find(num,root<<1|1,mid+1,r);
}
void pushup(int root,int l,int r){
	sum[root]=sum[root<<1];
	mx[root]=max(mx[root<<1],mx[root<<1|1]);
	if(mx[root<<1]>mx[root<<1|1])  return;
	sum[root]+=find(mx[root<<1],root<<1|1,mid+1,r);
}
void Modify(int root,int l,int r,int pos,double k){
	if(l==r){
		mx[root]=k,sum[root]=1;
		return;
	}
	if(pos<=mid)  Modify(root<<1,l,mid,pos,k);
	else  Modify(root<<1|1,mid+1,r,pos,k);
	pushup(root,l,r);
}
#undef mid
int main(){
	n=in(),m=in();
	while(m--){
		int x=in(),y=in();
		Modify(1,1,n,x,1.0*y/x),printf("%d\n",sum[1]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的楼房销售系统的 Python 代码: ```python class House: def __init__(self, address, price, area, rooms): self.address = address self.price = price self.area = area self.rooms = rooms class Salesman: def __init__(self, name, commission): self.name = name self.commission = commission def sell_house(self, house, buyer): if buyer.budget >= house.price: buyer.budget -= house.price self.commission += house.price * 0.1 print("Congratulations, " + buyer.name + "! You have successfully purchased " + house.address + " for " + str(house.price) + " dollars.") else: print("Sorry, " + buyer.name + "! You cannot afford " + house.address + ".") class Buyer: def __init__(self, name, budget): self.name = name self.budget = budget salesman = Salesman("John", 0) house1 = House("123 Main St", 500000, 2000, 4) house2 = House("456 Elm St", 300000, 1500, 3) house3 = House("789 Oak St", 700000, 2500, 5) buyer1 = Buyer("Alice", 600000) buyer2 = Buyer("Bob", 400000) salesman.sell_house(house1, buyer1) # Congratulations, Alice! You have successfully purchased 123 Main St for 500000 dollars. salesman.sell_house(house2, buyer2) # Sorry, Bob! You cannot afford 456 Elm St. salesman.sell_house(house3, buyer2) # Congratulations, Bob! You have successfully purchased 789 Oak St for 700000 dollars. ``` 这个系统包含三个类:House、Salesman 和 Buyer。House 类表示一栋房子,包含地址、价格、面积和房间数等属性。Salesman 类表示销售员,包含名字和佣金等属性,以及一个 sell_house 方法,用于销售房子。Buyer 类表示买家,包含名字和预算等属性。在主程序中,创建了三个 House 对象和两个 Buyer 对象,然后调用 Salesman 的 sell_house 方法进行销售。如果买家的预算足够支付房子的价格,销售员会计算佣金并打印一条祝贺消息;否则会打印一条失败消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值