例①、POJ 2201:
题目:
笛卡尔树:笛卡尔树中的每个节点有两个数据域key,val,对于数据域key,满足二叉搜索树性质,对于数据域val,满足最小堆性质。给出N个节点,1<=N<=50000,每个节点由一对key,val构成,判断能否根据这些节点构建一颗笛卡尔树,如果可以构建则输出构造出的笛卡尔树,否则输出“NO”。
解题思路:首先,根据N个节点,肯定可以构造出一颗笛卡尔树。
//本来是按照val排序,然后建立搜索二叉树,T了
//学了笛卡尔之后才知道,它是按照key排序,然后O(n)建树
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=50100;
const int inf=0x3f3f3f3f;
struct node
{
int l,r,fa;
int val,key;
int id;
bool operator <(const node &b) const
{
return key<b.key;
}
}t[maxn];
int l[maxn],r[maxn],fa[maxn];
void init(void)
{
t[0].l=t[0].r=0;
t[0].val=-inf;
t[0].fa=0;
t[0].id=0;
}
void _insert(int x)
{
int now=x-1;