递归,$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 ;
}

CodeForces 675D Tree Construction的更多相关文章

  1. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  2. codeforces 675D Tree Construction set

    转自:http://blog.csdn.net/qwb492859377/article/details/51447350 #include <stdio.h> #include < ...

  3. CF 675D——Tree Construction——————【二叉搜索树、STL】

    D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. 【CF 675D Tree Construction】BST

    题目链接:http://codeforces.com/problemset/problem/675/D 题意:给一个由n个互异整数组成的序列a[],模拟BST的插入过程,依次输出每插入一个元素a[i] ...

  5. codeforces 675D D. Tree Construction(线段树+BTS)

    题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...

  6. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  7. 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

  8. HDOJ 3516 Tree Construction

    四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. 【Codeforces 675D】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...

随机推荐

  1. Web.Config Transformation配置灵活的配置文件

    使用Web.Config Transformation配置灵活的配置文件 发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常 ...

  2. MongoDb的“not master and slaveok=false”错误及解决方法,读写分离

    首先这是正常的,因为SECONDARY是不允许读写的, 在写多读少的应用中,使用Replica Sets来实现读写分离.通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力 ...

  3. [转]A Faster UIWebView Communication Mechanism

    ref:http://blog.persistent.info/2013/10/a-faster-uiwebview-communication.html Use location.hash or t ...

  4. ios开发实践之UIDatePicker(已对之前无法解决的问题做了解答)

    需求:要做一个生日选择的控件,但除了选择之外还需要自定义几个控件,跟生日选择控件组合一起. 做法:自定义了一个UIImageView,并且作为背景.在这个背景view上再添加其他button和时间选择 ...

  5. CVPR 2013

    CVPR2013大部分文章都已经公开了,大家可以通过下面的网址访问: http://www.cv-foundation.org/openaccess/CVPR2013.py# 还有一篇根据CVPR20 ...

  6. Matlab下多径衰落信道的仿真

    衰落信道参数包括多径扩展和多普勒扩展.时不变的多径扩展相当于一个延时抽头滤波器,而多普勒扩展要注意多普勒功率谱密度,通常使用Jakes功率谱.高斯.均匀功率谱. 多径衰落信道由单径信道叠加而成,而单径 ...

  7. 通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页

    通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页 YbSoftwareFactory 的 YbRapidSolution for WinForm 插件使用CSLA.N ...

  8. Web缓存解决方案

    缓存是构建于HTTP统一接口之上的最有用功能之一.可以利用缓存减少终端用户感知到的延时,增加可靠性,减少带宽使用和成本,降低服务器负载.缓存无处不在,可以在服务器网络里,内容分发网络(Content ...

  9. [置顶] “河软CSDN2011级表彰暨实习动员大会”顺利召开!

    9点30分 伴随着激昂的开场曲,主持人走到台前!“河软CSDN2011级表彰暨 实习动员大会即将开始,请各位嘉宾入场!”他们分别是“CSDN教育事业部总经 理李天山先生”“河北软件职业技术学院 软件工 ...

  10. 函数sql黑马程序员——SQL常用函数

    最近使用开辟的过程中出现了一个小问题,顺便记录一下原因和方法--函数sql ---------------------- ASP.Net+Android+IO开辟S..Net培训.等待与您交流! -- ...