跳到正文

题解

P4198 楼房重建 (线段树)

木xx木大约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年1月18日
更新
2026年1月18日
篇幅
约 2 分钟

版本

  1. v1当前

P4198 楼房重建

  • pushup递归化(log化)。

  • 考虑左子树对右子树的贡献。

C++
#include<bits/stdc++.h>
using namespace std;
struct tree{
	int l,r,siz;
	double mx;
}t[400005];
int n,m;
void build(int ro,int l,int r)
{
	t[ro].l=l,t[ro].r=r;
	if(l==r)return;
	int mid=(l+r)>>1;
	build(ro<<1,l,mid);
	build(ro<<1|1,mid+1,r);
}
int pushup(int ro,double k)
{
	if(t[ro].mx<k)return 0;
	if(t[ro].l==t[ro].r)return t[ro].mx>k;
	if(t[ro<<1].mx<=k) return pushup(ro<<1|1,k);
	return pushup(ro<<1,k)+t[ro].siz-t[ro<<1].siz;
}
void updat(int ro,int l,double val)
{
	if(t[ro].l==l&&t[ro].r==l)
	{
		t[ro].siz=1;
		t[ro].mx=val;
		return;
	}
	int mid=(t[ro].l+t[ro].r)>>1;
	if(l<=mid)updat(ro<<1,l,val);
	if(l>mid)updat(ro<<1|1,l,val);
	t[ro].mx=max(t[ro<<1].mx,t[ro<<1|1].mx);
	t[ro].siz=t[ro<<1].siz+pushup(ro<<1|1,t[ro<<1].mx);
}
int main()
{
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		double k=1.0*y/x;
		updat(1,x,k);
		printf("%d\n",t[1].siz);
	}
	return 0;
}
原文