codeforces 675D D. Tree Construction(线段树+BTS)
题目链接:
2 seconds
256 megabytes
standard input
standard output
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.
- First element a1 becomes the root of the tree.
- 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:
- The pointer to the current node is set to the root.
- 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.
- 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.
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 a itself.
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.
3
1 2 3
1 2
5
4 2 3 1 6
4 2 2 4 题意: 给一个序列,构造一个二叉搜索树,然后输出每个节点的父节点; 思路: 在构造二叉搜索树的时候,每插入一个节点时它的插入位置是一定的,要么插在最大的比它小的数的右边,要么插在最小的比它
大的数左边,用线段树维护最大最小值就可以了;也可以用set+map模拟建树的过程; AC代码:
#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n,l[N],r[N],f[N];
struct Tree
{
int l,r,mmin,mmax;
}tree[*N];
void pushup(int node)
{
tree[node].mmin=min(tree[*node].mmin,tree[*node+].mmin);
tree[node].mmax=max(tree[*node].mmax,tree[*node+].mmax);
}
void build(int node,int L,int R)
{
tree[node].l=L;
tree[node].r=R;
tree[node].mmax=;
tree[node].mmin=inf;
if(L==R)return ;
int mid=(L+R)>>;
build(*node,L,mid);
build(*node+,mid+,R);
}
void update(int node,int pos)
{
// cout<<tree[node].l<<" "<<tree[node].r<<" "<<pos<<"@"<<"\n";
if(tree[node].l==tree[node].r&&tree[node].l==pos)
{
tree[node].mmax=tree[node].mmin=pos;
return ;
}
int mid=(tree[node].l+tree[node].r)>>;
if(pos<=mid)update(*node,pos);
else update(*node+,pos);
pushup(node);
}
int query(int node,int L,int R,int flag)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
if(flag)return tree[node].mmax;
else return tree[node].mmin;
}
int mid=(tree[node].l+tree[node].r)>>;
if(R<=mid)return query(*node,L,R,flag);
else if(L>mid)return query(*node+,L,R,flag);
else
{
if(flag)return max(query(*node,L,mid,flag),query(*node+,mid+,R,flag));
else return min(query(*node,L,mid,flag),query(*node+,mid+,R,flag));
}
}
struct Po
{
int a,pos,num;
}po[N];
int cmp1(Po x,Po y)
{
return x.a<y.a;
}
int cmp2(Po x,Po y)
{
return x.pos<y.pos;
}
int main()
{
scanf("%d",&n);
build(,,n);
Riep(n)
{
scanf("%d",&po[i].a);
po[i].pos=i;
}
sort(po+,po+n+,cmp1);
Riep(n)po[i].num=i,f[i]=po[i].a;
sort(po+,po+n+,cmp2);
update(,po[].num);
for(int i=;i<=n;i++)
{
int s=query(,,po[i].num,);
if(s==||(s!=&&r[s]))
{
s=query(,po[i].num,n,);
l[s]=po[i].num;
}
else
{
r[s]=po[i].num;
}
update(,po[i].num);
printf("%d ",f[s]);
} return ;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9+10;
const int N=1e5+10;
const int maxn=1e3+20;
const double eps=1e-12; set<int>s;
map<int,int>le,ri;
set<int>::iterator it;
int main()
{
int n,x;
read(n);
read(x);s.insert(x);
For(i,2,n)
{
read(x);
it=s.lower_bound(x);
int pos=*it;
if(le[pos]==0&&it!=s.end())le[pos]=x;
else
{
it--;
pos=*it;
ri[pos]=x;
}
s.insert(x);
printf("%d ",pos);
}
return 0;
}
codeforces 675D D. Tree Construction(线段树+BTS)的更多相关文章
- 【Codeforces 675D】Tree Construction
[链接] 我是链接,点我呀:) [题意] 依次序将数字插入到排序二叉树当中 问你每个数字它的父亲节点上的数字是啥 [题解] 按次序处理每一个数字 对于数字x 找到最小的大于x的数字所在的位置i 显然, ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
BZOJ_2212_[Poi2011]Tree Rotations_线段树合并 Description Byteasar the gardener is growing a rare tree cal ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- Codeforces 620E New Year Tree(线段树+位运算)
题目链接 New Year Tree 考虑到$ck <= 60$,那么用位运算统计颜色种数 对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询. 用线段树来维护. #includ ...
- Codeforces 620E New Year Tree【线段树傻逼题】
LINK 题目大意 给你一棵树 让你支持子树染色,子树查询颜色个数,颜色数<=60, 节点数<=4e5 思路 因为颜色数很少,考虑状态压缩变成二进制 然后直接在dfs序上用线段树维护就可以 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
随机推荐
- [Netbeans]为面板设置背景图片
与AndroidStudio等类似IDE不同,在Netbeans开发桌面程序时,不可以直接通过src=@drawable 等方法为窗口设置背景图片.这里介绍一种简便的方法: 1:首先,拖动一个面板到f ...
- Windows下使用NIF扩展Erlang方法
在Erlang中,NIF(Native Implemented Function)被用来扩展erlang的某些功能,一般用来实现一些erlang很难实现的,或者一些erlang实现效率不高的功能. N ...
- stl map高效遍历删除的方法 [转]
for(:iter!=mapStudent.end():) { if((iter->second)>=aa) { //满足删除条件,删除当前结点,并指 ...
- VS2010开发环境最佳字体及配色[转]
从地址:http://www.dev-club.net/xiangxixinxi/42010072906150537/201103010518006.html 获取的,整理如下: 环境:VS2010字 ...
- Eclipse配置PyDev插件
安装python解释器 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官 ...
- LFS7.4编译笔记(3)
在第一部分,我们编译了一个工具链及临时系统,然后在第二部分我们chroot到/mnt/lfs下面,利用临时系统的工具编译了我们最终的LFS系统.不过此时,我们的LFS系统还是不完整的,因为我们还没有安 ...
- Android ListView标题置顶效果实现
一. 有图有真相 二.实现: 1. 基于ListView分类效果 2. TitleView即标题的处理(创建) 3. 处理TitleView的三种状态 三.源码: 例子下载 实现可以看代码,具 ...
- 模块化JavaScript设计模式(一)
在可扩展JavaScript的世界里,假设我们说一个应用程序是模块化(modular)的,那么通常意味着它是由一系列存储于模块中的高度解耦,不同的功能片段组成. 在可能的情况下.通过一处依赖性.松耦合 ...
- js json与对象的相互转换
var str = '{ "name": "cxh", "sex": "man" }'; //JSON字符串:var o ...
- gamework的使用方法
翻译来源地址:https://github.com/Kadoba/gamework gamework是控制LOVE2D游戏进程流的一个项目. ↑ 这个是按原文译的, 当初乍看完全不懂, 接下来我来用图 ...