Cartesian Tree
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 3998 | Accepted: 1605 | |
Case Time Limit: 2000MS |
Description
Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binary search tree 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 then the key of x, and each node in its right subtree has the key greater then the key of x.
That is, if we denote left subtree of the node x by L(x), its right subtree by R(x) and its key by kx then for each node x we have
- if y ∈ L(x) then ky < kx
- if z ∈ 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 <= 50 000). The following N lines contain two numbers each -- given pairs (ki, ai). For each pair |ki|, |ai| <= 30 000. 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, on the following N lines output the tree. Let nodes be numbered from 1 to N corresponding to pairs they contain as they 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.
The input ensure these is only one possible tree.
Sample Input
7 5 4 2 2 3 9 0 5 1 3 6 6 4 11
Sample Output
YES 2 3 6 0 5 1 1 0 7 5 0 0 2 4 0 1 0 0 3 0 0
题意: 给你一个一些节点,每个节点都有key 和val 要求建立一颗树,树的中序遍历是key的从小到大排列,同时这棵树也是一颗以val为标准的最小堆,最后输出每个节点和其父亲,儿子节点的关系。
思路: 笛卡尔树解决问题。先按key 值排序,逐个插入二叉排序树。(单调栈实现)
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N =50005;
struct node
{
int l,r,p;
int key;
int val;
int id;
}a[N];
int stk[N];
int n;
void solve()
{
int top=0;
stk[0]=1;
for(int i=2;i<=n;i++)
{
while(top>=0&&a[i].val<a[stk[top]].val)// 最小堆
{
top--;
}
if(top>-1){ // 找到了小于等于自己的节点
//cout<<"zhaodaohaizi"<<" stk[top] "<<stk[top]<<endl;
//cout<<"***** "<<a[stk[top]].r<<endl;
a[i].p=stk[top]; //将自己的父亲 设为找到的节点
a[a[stk[top]].r].p=i; // 将找到的节点的原来的右孩子的父亲设为自己
a[i].l=a[stk[top]].r; //将自己的左孩子设为找到的节点的右孩子
a[stk[top]].r=i; // 将找到的节点的右孩子设为自己
// 改变顺序可能会造成指针乱指
}
else{ // 没有找到小于等于自己的节点
//cout<<"meiyouzhaodao"<<endl;
a[i].l=stk[0]; // 自己的左孩子设为原来的根节点
a[stk[0]].p=i; // 自己设为原来根节点的父亲
}
stk[++top]=i;
}
/*
for(int i=1;i<=n;i++){
cout<<a[i].p<<" "<<a[i].l<<" "<<a[i].r<<endl;
}
*/
return ;
}
bool cmp(node a,node b)
{
return a.key<b.key;
}
int p[N];
int l[N];
int r[N];
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++){
scanf("%d %d",&a[i].key,&a[i].val);
a[i].p=a[i].l=a[i].r=0;
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
solve();
memset(p,0,sizeof(p));
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
for(int i=1;i<=n;i++){
p[a[i].id]=a[a[i].p ].id;
l[a[i].id]=a[a[i].l ].id;
r[a[i].id]=a[a[i].r].id;
}
printf("YES\n");
for(int i=1;i<=n;i++){
printf("%d %d %d\n",p[i],l[i],r[i]);
}
}
return 0;
}