首页 技术 正文
技术 2022年11月9日
0 收藏 384 点赞 3,671 浏览 1570 个字

递归,$RMQ$。

因为$n$较大,可以采用递归建树的策略。

对每一个点标一个$id$。然后按照$v$从小到大排序,每一段$[L,R]$的根节点就是$id$最小的那个。

因为二叉搜索树可能是一条链,所以不能暴力找$id$最小的,需要用线段树或者$RMQ$预处理快速寻找。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
}const int maxn=;
struct X{int x,id;}s[maxn];
int n,sz,ans[maxn],a[maxn];
struct Node { int id,L,R; }node[maxn];bool cmp(X a,X b){ return a.x<b.x;}
bool cmp1(X a,X b){ return a.id<b.id;}int dp[maxn][];
void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=i;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++){
if(a[dp[i][j-]]<a[dp[i+(<<(j-))][j-]]) dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
}int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
if(a[dp[L][k]]<a[dp[R-(<<k)+][k]]) return dp[L][k];
return dp[R-(<<k)+][k];
}void build(int L,int R,int fa,int f)
{
int pos=RMQ(L-,R-); pos++;
if(fa!=-)
{
if(f==) node[fa].L=s[pos].id;
else node[fa].R=s[pos].id;
} if(pos--L>=) build(L,pos-,s[pos].id,);
if(R-(pos+)>=) build(pos+,R,s[pos].id,);
}int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&s[i].x),s[i].id=i;
sort(s+,s++n,cmp);
for(int i=;i<=n;i++) a[i-]=s[i].id;
RMQ_init(); build(,n,-,);
sort(s+,s++n,cmp1);
for(int i=;i<=n;i++) ans[node[i].L]=s[i].x, ans[node[i].R]=s[i].x;
for(int i=;i<=n;i++) printf("%d ",ans[i]);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297