Tree Construction

CodeForces - 675D

During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help.

You are given a sequence a, consisting of n distinct integers, that is used to construct the binary search tree. Below is the formal description of the construction process.

  1. First element a1 becomes the root of the tree.
  2. Elements a2, a3, ..., an are added one by one. To add element ai one needs to traverse the tree starting from the root and using the following rules:
    1. The pointer to the current node is set to the root.
    2. If ai is greater than the value in the current node, then its right child becomes the current node. Otherwise, the left child of the current node becomes the new current node.
    3. If at some point there is no required child, the new node is created, it is assigned value ai and becomes the corresponding child of the current node.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the length of the sequence a.

The second line contains n distinct integers ai (1 ≤ ai ≤ 109) — the sequence aitself.

Output

Output n - 1 integers. For all i > 1 print the value written in the node that is the parent of the node with value ai in it.

Examples

Input
3
1 2 3
Output
1 2
Input
5
4 2 3 1 6
Output
4 2 2 4

Note

Picture below represents the tree obtained in the first sample.

Picture below represents the tree obtained in the second sample.

sol:一个很显然的性质就是当前这个点父亲的权值一定是他的前驱后者后继,但是怎么判断是前驱的右儿子或者后继的左儿子嫩?貌似有点蛋碎,挂了两次之后知道了是出现时间最晚的那个就是了,前驱后继写(copy)一个splay就可以了

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,inf=0x3f3f3f3f;
int n,a[N],b[N];
namespace Pht
{
int Points,Root;
int Child[N][];
int Parent[N];
int Quanzhi[N];
int Cnt[N];
int Size[N]; inline void Init();
inline int Check(int x);
inline void PushUp(int x);
inline void Rotate(int x);
inline void Splay(int At,int To);
inline void Insert(int Val);
inline void Remove(int Val);
inline void Find(int Val);
inline int Ask_Lower(int Val);
inline int Ask_Upper(int Val);
inline int Ask_Kth(int Id);
inline void Solve(); inline void Init()
{
Points=Root=;
Insert(-inf);
Insert(inf);
}
inline int Check(int x)
{
return (Child[Parent[x]][]==x)?:;
}
inline void PushUp(int x)
{
Size[x]=Size[Child[x][]]+Size[Child[x][]]+Cnt[x];
}
inline void Rotate(int x)
{
int y,z,oo;
y=Parent[x];
z=Parent[y];
oo=Check(x);
Child[y][oo]=Child[x][oo^]; Parent[Child[x][oo^]]=y;
Child[z][Check(y)]=x; Parent[x]=z;
Child[x][oo^]=y; Parent[y]=x;
PushUp(x); PushUp(y);
}
inline void Splay(int At,int To)
{
while(Parent[At]!=To)
{
int Father=Parent[At];
if(Parent[Father]==To)
{
Rotate(At);
}
else if(Check(At)==Check(Father))
{
Rotate(Father); Rotate(At);
}
else
{
Rotate(At); Rotate(At);
}
}
if(To==) Root=At;
}
inline void Insert(int Val)
{
int Now=Root,Par=;
while(Now&&(Quanzhi[Now]!=Val))
{
Par=Now;
Now=Child[Now][(Val>Quanzhi[Now])?:];
}
if(Now)
{
Cnt[Now]++; Size[Now]++;
}
else
{
Now=++Points;
if(Par)
{
Child[Par][(Val>Quanzhi[Par])?:]=Now;
}
Parent[Now]=Par;
Child[Now][]=Child[Now][]=;
Cnt[Now]=Size[Now]=;
Quanzhi[Now]=Val;
}
Splay(Now,);
}
inline void Remove(int Val)
{
int Lower=Ask_Lower(Val),Upper=Ask_Upper(Val);
Splay(Lower,);
Splay(Upper,Lower);
if(Cnt[Child[Upper][]]>)
{
Cnt[Child[Upper][]]--; Size[Child[Upper][]]--; Splay(Child[Upper][],);
}
else
{
Child[Upper][]=;
}
}
inline void Find(int Val)
{
int Now=Root;
if(!Now) return;
while(Child[Now][(Val>Quanzhi[Now])?:]&&(Quanzhi[Now]!=Val))
{
Now=Child[Now][(Val>Quanzhi[Now])?:];
}
Splay(Now,);
}
inline int Ask_Lower(int Val)
{
Find(Val);
int Now=Root;
if(Quanzhi[Now]<Val) return Now;
Now=Child[Now][];
while(Child[Now][]) Now=Child[Now][];
return Now;
}
inline int Ask_Upper(int Val)
{
Find(Val);
int Now=Root;
if(Quanzhi[Now]>Val) return Now;
Now=Child[Now][];
while(Child[Now][]) Now=Child[Now][];
return Now;
}
inline int Ask_Kth(int Id)
{
int Now=Root;
if(Size[Root]-<Id) return ;
for(;;)
{
if(Size[Child[Now][]]>=Id)
{
Now=Child[Now][];
}
else if(Size[Child[Now][]]+Cnt[Now]<Id)
{
Id-=Size[Child[Now][]]+Cnt[Now];
Now=Child[Now][];
}
else return Now;
}
}
int Time[N];
inline void Solve()
{
Init();
int i,Min=a[],Max=a[],tt=;
Insert(a[]); Time[Root]=++tt;
for(i=;i<=n;i++)
{
int Lower=Ask_Lower(a[i]),Upper=Ask_Upper(a[i]);
// printf("T[%d]=%d T[%d]=%d ",Lower,Time[Lower],Upper,Time[Upper]);
if(Time[Lower]>Time[Upper])
{
W(Quanzhi[Lower]);
}
else
{
W(Quanzhi[Upper]);
}
Insert(a[i]);
Time[Root]=++tt;
// puts("");
}
}
}
int main()
{
int i;
R(n);
for(i=;i<=n;i++) R(a[i]);
Pht::Solve();
return ;
}
/*
Input
3
1 2 3
Output
1 2 Input
5
4 2 3 1 6
Output
4 2 2 4 Input
10
991309218 517452607 870021923 978357992 136426010 10601767 302627526 883615372 163475700 600546765
Output
991309218 517452607 870021923 517452607 136426010 136426010 978357992 302627526 870021923
*/

codeforces675D的更多相关文章

  1. Codeforces675D(SummerTrainingDay06-J)

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

  2. codeforces675D Tree Construction

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. 如何用cmake编译

    本文由云+社区发表 作者:工程师小熊 CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake ...

  2. 使用LR编写windows sockets协议xml报文格式脚本实战

    以下是测试脚本Demo: #include "lrs.h" Action() { char * resultCode;//结果代码 char * time; //系统时间 char ...

  3. Cayley图数据库的简介及使用

    图数据库   在如今数据库群雄逐鹿的时代中,非关系型数据库(NoSQL)已经占据了半壁江山,而图数据库(Graph Database)更是攻城略地,成为其中的佼佼者.   所谓图数据库,它应用图理论( ...

  4. Office组件无法正常使用的解决方法

    问题与现象     开发时调用Office组件,代码编译是通过的,但在运行时当ApplicationClass对象初始化后程序出现异常.     异常信息如下:     无法将类型为“Microsof ...

  5. [TCP/IP] 数据链路层-ethereal 抓包分析数据帧

    1.下载 http://dx1.pc0359.cn/soft/e/ethereal.rar 2.打开软件,指定抓取的网卡,下面是我抓取自己的主要网卡数据 3.开启个ping命令 , 不停的ping一台 ...

  6. Java笔记(day7-day8)

    this关键字: (1)this(当局部变量和成员变量重名时,可以用关键字this区分)    this代表对象,当前对象       this就是所在函数所属对象的引用      简单来说,哪个对象 ...

  7. Spring中关于AOP的实践之AspectJ方式实现通知

    (本文中如有不当之处,恳请批评指正) AspectJ方式的简化了通知的出现复杂度.但是对配置文件的操作复杂度有了一定的提升 一. 配置通知 package com.xkx.adviceDemo; im ...

  8. 设计模式 - 单例模式(Singleton Pattern)

    单例模式 介绍 模式:创建型 意图:保证一个类只有一个实例,并提供一个访问它的全局访问点 解决:一个全局使用的类频繁地创建与销毁 场景: 唯一序列号 web中的计数器 I/O与数据库的连接 …… 实现 ...

  9. css文字超出一行用点表示

    1,css超出一行用点表示 white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 2,css超出二行用点表示 overflow:hi ...

  10. Win10系统下装Ubuntu虚拟机的遇到的问题总结

    环境和工具 win10操作系统 VMware Workstation 12 Ubuntu 14.0 64位 教程可参考:VMware Ubuntu安装详细过程(非常靠谱) [因为我的安装过程不是十分顺 ...