155. Cartesian Tree

time limit per test: 0.25 sec. 
memory limit per test: 65536 KB
input: standard input 
output: standard output
Let us consider a special type of binary search trees, called cartesian trees. Recall that a binary searchtree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left subtree has the key less than the key of x, and each node in its right subtree has the key greater than the key of x. 
That is, if we denote the left subtree of the node x by L(x), its right subtree by R(x) and its key by kx, for each node x we will have 
  * if y in L(x) then ky < kx 
  * if z in R(x) then kz > kx 
The binary search tree is called cartesian if its every node x in addition to the main key kx also has an auxiliary key that we will denote by ax, and for these keys the heap condition is satisfied, that is 
  * if y is the parent of x then ay < ax 
Thus a cartesian tree is a binary rooted ordered tree, such that each of its nodes has a pair of two keys (k, a) and three conditions described are satisfied. 
Given a set of pairs, construct a cartesian tree out of them, or detect that it is not possible.
Input
The first line of the input file contains an integer number N - the number of pairs you should build cartesian tree out of (1 <= N <= 50000). The following N lines contain two integer numbers each - given pairs (ki, ai). For each pair |ki|, |ai| <= 30000. All main keys and all auxiliary keys are different, i.e. ki <> kj and ai <> aj for each i <> j.
Output
On the first line of the output file print YES if it is possible to build a cartesian tree out of given pairs or NO if it is not. If the answer is positive, output the tree itself in the following N lines. Let the nodes be numbered from 1 to N corresponding to pairs they contain as these pairs are given in the input file. For each node output three numbers - its parent, its left child and its right child. If the node has no parent or no corresponding child, output 0 instead. 
If there are several possible trees, output any one.
Sample test(s)
Input
 

5 4 
2 2 
3 9 
0 5 
1 3 
6 6 
4 11
 
Output
 
YES 
2 3 6 
0 5 1 
1 0 7 
5 0 0 
2 4 0 
1 0 0 
3 0 0
 
 

cartesian tree,俗称“笛卡尔树”。

笛卡尔树是什么呢?就是一颗类treap树。

即每个节点有两个域值,一个阈值满足二叉排序树性质,另一个域值满足堆性质。

首先说一下,笛卡尔树和treap结构上相似,但实际上,treap的第二个阈值只是用于维护平衡,而笛卡尔树的阈值是真正的值。

这道题目主要是笛卡尔树的建树。

它是O(n)的(核心部分)。

我们先把所有节点按照二叉树值从大到小排序,并假设现在已经建成了一个1~i-1的节点的树。

那么当前节点i按照二叉树值插入一点会插入在最右链的末端。

不过这样,可能会使其父节点的堆值大于他。怎么办?我们相当于做一个左旋操作。

找到第一个堆值比i小的i的祖先u,使i成为u的右儿子,u原来的右子树变为u的左子树,这样就可以满足要求了,并且这样的树一定是存在的。

那么,现在的问题是,怎样找到u?显然,可以二分查找O(nlogn)的复杂度。但这样不是最优的。

最优的就是就是用一个单调栈,记录最右链的节点。由于每一个节点最多访问一次,所以复杂度是O(n)的。

code:

 #include<bits/stdc++.h>
 using namespace std;
 ,inf=0x1a1a1a1a;
 int n,s[N],top;
 struct node {int k,x,i,f,l,r;}a[N];
 bool cmp_k(const node &u,const node &v) {return u.k<v.k;}
 bool cmp_i(const node &u,const node &v) {return u.i<v.i;}
 int main() {
     cin>>n,top=;
     ; i<=n; i++)
         scanf("%d%d",&a[i].k,&a[i].x),a[i].i=i;
     a[].l=a[].r=a[].i=,a[].k=a[].x=-inf;
     sort(a+,a++n,cmp_k);
     s[++top]=;
     ,las=-; i<=n; i++,las=-) {
         while (top&&a[s[top]].x>a[i].x) las=s[top],top--;
         a[i].f=a[s[top]].i,a[s[top]].r=a[i].i;
         if (~las) a[i].l=a[las].i,a[las].f=a[i].i;
         s[++top]=i;
     }
     sort(a+,a++n,cmp_i);
     puts("YES");
     ; i<=n; i++)
         printf("%d %d %d\n",a[i].f,a[i].l,a[i].r);
     ;
 }

[sgu P155] Cartesian Tree的更多相关文章

  1. SGU 155.Cartesian Tree

    时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...

  2. Algorithm: cartesian tree

    http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...

  3. 笛卡尔树Cartesian Tree

    前言 最近做题目,已经不止一次用到笛卡尔树了.这种数据结构极为优秀,但是构造的细节很容易出错.因此写一篇文章做一个总结. 笛卡尔树 Cartesian Tree 引入问题 有N条的长条状的矩形,宽度都 ...

  4. PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)

    7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct ...

  5. Day6 - J - Cartesian Tree POJ - 2201

    Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binar ...

  6. PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆

    Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...

  7. POJ 2201 Cartesian Tree ——笛卡尔树

    [题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...

  8. OpenJudge Cartesian Tree

    [代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...

  9. CF1290E Cartesian Tree

    考虑笛卡尔树的意义: 一个点在笛卡尔树中的子树,代表以他为最小/最大值的区间. 所以一个点的子树大小,一定是类似到达序列边界或者被一个比他更大的数隔离. 考虑记录 \(l_i,r_i\) 为第 \(i ...

随机推荐

  1. 012-docker-安装-fabric:1.4

    一.准备工作 linux 版本.docker.docker-compose go安装且版本较新 uname -a docker --version docker-compose --version g ...

  2. 初识GitHub之创建文件

    在新建了一个项目(repository)后,会跳转到项目主页,如下图 Create new file(创建新文件)就是新建一个代码文件,Upload file(上传文件)即从内存中将代码文件上传至本项 ...

  3. Spark实时案例

    1.概述 最近有同学问道,除了使用 Storm 充当实时计算的模型外,还有木有其他的方式来实现实时计算的业务.了解到,在使用 Storm 时,需要编写基于编程语言的代码.比如,要实现一个流水指标的统计 ...

  4. Redis单机配置以及集群(sentinel、jediscluster)配置

    最近一直在使用Redis作为缓存数据库,在使用当中,刚开始没有注意配置问题. 1.纯粹的注入单机模式 <bean id="jedisPoolConfig" class=&qu ...

  5. Sitecore系统教程即时查阅编辑内容

    实时模式中的Sitecore 作为开发人员,我们经常需要在本地环境中使用代码和内容.在本地,能够立即看到任何内容更改,以节省时间和提高效率是有意义的.这是在实时模式下运行Sitecore.默认情况下, ...

  6. 聊聊return false

    最近在做一些关于视频切换的时候.由于是用a标签做的会有默认的跳转.这时候我就想到了,return flase.阻止默认行为,也达到了预期的效果.后来就详细查了查.让我们来看看 “return fals ...

  7. VMware vSphere 5.x 与 vSphere 6.0各版本功能特性对比

    各版本中的新特性及功能对比:   VMware vSphere 5.0 VMware vSphere 5.1 VMware vSphere 5.5 VMware vSphere 6.0 ESXi 5. ...

  8. 第九节 JS运动应用

    多物体运动框架 多个物体同时运动 例子:多个Div,鼠标移入变宽 单定时器,存在问题 每个Div一个定时器 <!DOCTYPE html> <html lang="en&q ...

  9. Docker Kubernetes 查询字段说明

    Docker Kubernetes  查询字段说明 # 打印受支持的API版本 kubectl api-versions # 扩展 apiextensions.k8s.io/v1beta1 # 注册 ...

  10. 剑指offer(7)斐波那契数列

    题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 题目分析 我们都知道斐波那契可以用递归,但是递归重复计算的部分太多了(虽然可以通过),但是这 ...