【线段树】Snow Boots

本文介绍了一种通过枚举高度来解决寻找最长连续相同元素串的问题。具体方法是从小到大枚举高度,将不能通过的块标记为1,能通过的块标记为0。通过动态更新树状数组,每次枚举高度后,都能快速求得当前最大连续1的长度,即最长连续段。此方法适用于处理动态变化的序列,特别在靴子高度变化时,能高效计算出每个高度对应的最长连续可通过路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

 

从小到大枚举高度。不能过的块标为1,能过的块标为0。

也就是当前枚举的高度如果大于当前块的高度,就把这个块标成0,

如果小于等于当前块的高度,就标成1。

枚举高度的过程,就相当于是一个一个修改。最初假设高度无穷小,所有都是1。然后开始慢慢增加高度,把这些块按照高度从小到大一个一个地变成1。对于每种深度求一个最长1串就是答案。每种高度有一个下限,即max(连续1串的长度)

注意对于高度相邻的两个靴子a,b,在【h[a],h[b])的范围内它们对应出来的01串是相同的。。前闭后开。。r

举个例子;两个坑高度为10,12。

靴高10对应:0,1;

靴高11对应:0,1;

靴高12对应:0,0;

所以询问的时候要找到第一个比它大的块  的前面的那个块的下限,再和当前的d比较。

然后就是注意一下pushup函数的写法。。。r

#include<bits/stdc++.h>
#define mid ((T[root].l+T[root].r)>>1)
#define len(root) (T[root].r-T[root].l+1)
#define lc (root<<1)
#define rc (root<<1|1)
using namespace std;
const int maxn=1e5+10;
struct node{int l,r,sum,lans,rans,maxsub;}T[maxn<<2];
struct block{
	int dep,id;
	friend inline bool operator<(const block &x,const block &y){return x.dep<y.dep;}
}a[maxn];
int n,B,h[maxn],M[maxn];
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
inline void print(int x){
	if(x>9) print(x/10);
	putchar(x%10+'0');
}
inline void pushup(int root){
	T[root].maxsub=max(max(T[lc].maxsub,T[rc].maxsub),T[lc].rans+T[rc].lans);
	T[root].lans=T[lc].lans,T[root].rans=T[rc].rans;
	if(T[lc].lans==len(lc)) T[root].lans+=T[rc].lans;
	if(T[rc].rans==len(rc)) T[root].rans+=T[lc].rans;
}
void build(int root,int l,int r){
	T[root].l=l,T[root].r=r;
	if(l==r){
		T[root].sum=T[root].maxsub=T[root].lans=T[root].rans=1;
		return;
	}
	build(lc,l,mid),build(rc,mid+1,r);
	pushup(root);
}
void change(int root,int pos){
	if(T[root].l==T[root].r){
		T[root].sum=T[root].maxsub=T[root].lans=T[root].rans=0;
		return;
	}
	if(pos<=mid) change(lc,pos);
	else change(rc,pos);
	pushup(root);
}
int main(){
	n=read(),B=read();
	for(int i=1;i<=n;++i) h[i]=a[i].dep=read(),a[i].id=i;
	build(1,1,n),sort(h+1,h+n+1),sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)
	change(1,a[i].id),M[i]=max(max(T[1].lans,T[1].rans),T[1].maxsub);
	while(B--){
		int s=read(),d=read();
		s=upper_bound(h+1,h+n+1,s)-h-1;
		print(M[s]<d),putchar(10);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值