CodeForces 675D Tree Construction
递归,$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的更多相关文章
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- codeforces 675D Tree Construction set
转自:http://blog.csdn.net/qwb492859377/article/details/51447350 #include <stdio.h> #include < ...
- CF 675D——Tree Construction——————【二叉搜索树、STL】
D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 【CF 675D Tree Construction】BST
题目链接:http://codeforces.com/problemset/problem/675/D 题意:给一个由n个互异整数组成的序列a[],模拟BST的插入过程,依次输出每插入一个元素a[i] ...
- codeforces 675D D. Tree Construction(线段树+BTS)
题目链接: D. Tree Construction D. Tree Construction time limit per test 2 seconds memory limit per test ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
- HDOJ 3516 Tree Construction
四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 【Codeforces 675D】Tree Construction
[链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...
随机推荐
- C++库研究笔记——函数名的宏定义
6.47 Function Names as Strings:http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html GCC provides th ...
- WPF专业编程指南 - DispatcherUnhandledException
WPF的Application类中有一个事件:DispatcherUnhandledException,在应用程序未对其中的异常加以处理的情况下发生,从而为应用程序把好最后的大门 namespace ...
- 公众平台Bee.WeiXin
微信公众平台Bee.WeiXin开发介绍 阅读目录 开始 预览 配置项说明 调用链方式的应答 理解调用链上下文 自定义MVC响应 总结 我们来看一下如何通过Bee.WeiXin开发微信公众平台.关于微 ...
- 将JDBC ResultSet结果集转成List
private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>() ...
- pipe----管道
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h&g ...
- OpenCV环境搭建
前言 我在上本科时候曾经用过opencv,那时候还是1.x版本,还必须在linux下自己编译. 时过境迁,最近突然想起来写个小程序来分析图片,就又想起了opencv.现在已然是2.4的版本. 环境搭建 ...
- JavaScript 中 if 条件判断
在JS中,If 除了能够判断bool的真假外,还能够判断一个变量是否有值. 下面的例子说明了JS中If的判断逻辑: 变量值 true '1' 1 '0' 'null' 2 '2' false 0 n ...
- Html 模态框操作
<style> #modal-overlay { visibility: hidden; position: absolute; /* 使用绝对定位或固定定位 */ left: 0px; ...
- mabatis传入参数
1.传入一个list,遍历时collection默认是list,如果在参数前面使用@Param,则list里面应该换成相应的value. 2.传入一个array,遍历时collection默认是arr ...
- .NET基础——方法
这一篇,我们来学习C#中的方法——函数 1. 方法的功能 方法的功能:用来复用代码的,当我们在一个程序中反复的写了同样的代码,我们就可以把需要重复写的代码定义在一个方法中,用到的时候只需要调用就可 ...