POJ 2201: Cartesian Tree 笛卡尔树

Cartesian Tree
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 3893 Accepted: 1551
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

Source

Northeastern Europe 2002, Northern Subregion

这是一道实实在在的笛卡尔树裸题

题面就是对笛卡尔树的介绍

之后建树的话 感觉代码超好理解 看code吧


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=10*x+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=50100;

struct node
{
	int key,auxiliary_key,pos;
	friend bool operator <(const node &x,const node &y)
	{return x.auxiliary_key<y.auxiliary_key;}
}p[N];

int n;
int fa[N],ch[N][2],st[N];

void build()
{
	register int i,top(0),tmp,rs;
	for(i=1;i<=n;++i)
	{
		while(top && p[st[top]].key>=p[i].key) top--;
		tmp=p[st[top]].pos;rs=ch[tmp][1];
		fa[p[i].pos]=tmp;
		ch[tmp][1]=p[i].pos;
		fa[rs]=p[i].pos;
		ch[p[i].pos][0]=rs;
		st[++top]=i;
	}
}

int main()
{
	n=read();
	register int i;
	for(i=1;i<=n;++i)
		p[i].pos=i,p[i].auxiliary_key=read(),p[i].key=read();
	sort(p+1,p+1+n);
	build();
	puts("YES");
	for(i=1;i<=n;++i)
		print(fa[i]),putchar(' '),
		print(ch[i][0]),putchar(' '),
		print(ch[i][1]),putchar('\n');
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值