poj2201 Cartesian Tree

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.

笛卡尔树模板题。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int a,k,num;
    bool operator < (const node & nn) const
    {
        return k<nn.k;
    }
}a[50010];
int lson[50010],rson[50010],sta[50010],
ansl[50010],ansr[50010],ansfa[50010],
n,top,root;
void init()
{
    int i;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
      scanf("%d%d",&a[i].k,&a[i].a),a[i].num=i;
    sort(a+1,a+n+1);
}
void build()
{
    int i,p;
    for (i=1;i<=n;i++)
    {
        p=0;
        while (top&&a[sta[top]].a>a[i].a) p=sta[top--];
        lson[i]=p;
        if (top) rson[sta[top]]=i;
        else root=i;
        sta[++top]=i;
    }
}
void out()
{
    int i,u,v;
    for (i=1;i<=n;i++)
    {
        u=a[i].num;
        if (lson[i])
        {
            v=a[lson[i]].num;
            ansl[u]=v;
            ansfa[v]=u;
        }
        if (rson[i])
        {
            v=a[rson[i]].num;
            ansr[u]=v;
            ansfa[v]=u;
        }
    }
    printf("YES\n");
    for (i=1;i<=n;i++)
      printf("%d %d %d\n",ansfa[i],ansl[i],ansr[i]);
}
int main()
{
    init();
    build();
    out();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值