155. Cartesian Tree
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard input
output: standard output
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.
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.
If there are several possible trees, output any one.
Sample test(s)
Input
7
5 4
2 2
3 9
0 5
1 3
6 6
4 11
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
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); ; }