LG P4198 楼房重建(线段树)

LG P4198 楼房重建

Solution

基础的线段树题,虽然我还不熟练就是了。

大概就是单点修改,求全局的极大子序列。
我们需要维护一个区间最大值aaa和极大子序列长度sss

合并xxx的左右儿子ls,rsls,rsls,rs时,axa_xax直接取max{als,ars}max\{a_{ls},a_{rs}\}max{als,ars}即可。

对于sxs_xsxslss_{ls}sls的答案可以继承,对于右儿子,若其最大值小于alsa_{ls}als,显然贡献为000,否则我们考虑rsrsrs的左右儿子L,RL,RL,R,若aL≤alsa_{L}\leq a_{ls}aLals,则LLL子树没有贡献,可以递归到右子树统计,若aL>alsa_{L}>a_{ls}aL>als,则RRR中本来可以选的还是可以选,递归到左子树统计即可。

时间复杂度O(nlgn)O(nlgn)O(nlgn)

实现时建议用实数,不要用分数比大小,容易出锅。

Code

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=100005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
lod a[MAXN<<2];
int s[MAXN<<2];
PR max(PR x,PR y) { return x<y?y:x; }
int up(int x,int l,int r,lod y)
{
	if (a[x]<y) return 0;
	if (l==r) return y<a[x];
	int mid=(l+r)>>1;
	if (a[x<<1]<y) return up(x<<1|1,mid+1,r,y);
	else return up(x<<1,l,mid,y)+s[x]-s[x<<1];
}
void update(int x,int l,int r,int y,int z)
{
	if (l==r) { a[x]=(lod)z/y,s[x]=1; return;  }
	int mid=(l+r)>>1;
	if (y<=mid) update(x<<1,l,mid,y,z);
	else update(x<<1|1,mid+1,r,y,z);
	a[x]=max(a[x<<1],a[x<<1|1]);
	s[x]=s[x<<1]+up(x<<1|1,mid+1,r,a[x<<1]);
}
signed main()
{
	int n=read(),m=read();
	for (int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		update(1,1,n,x,y);
		printf("%d\n",s[1]);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值