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 思路:裸的笛卡尔树,学习新知识,这题输入唯一,一定有解,参考博客:https://blog.csdn.net/qq_36056315/article/details/79845193
https://blog.csdn.net/code92007/article/details/94591571
注意不要在退栈的时候改变fa指针就行,要根据退栈完毕后left和right指针进行fa的更改,不然已经定好的顺序会乱(
const int maxm = 5e4+;

int fa[maxm], Left[maxm], Right[maxm], N;

struct Node {
int key, value, id;
bool operator<(const Node &node) const {
return key < node.key;
}
} Nodes[maxm], s[maxm]; int main() {
scanf("%d", &N);
for(int i = ; i <= N; ++i) {
scanf("%d%d", &Nodes[i].key, &Nodes[i].value);
Nodes[i].id = i;
}
sort(Nodes+, Nodes+N+);
int top = ;
bool flag = false;
for(int i = ; i <= N; ++i) {
while(top && s[top].value > Nodes[i].value) {
Left[Nodes[i].id] = s[top].id;
top--;
}
fa[Nodes[i].id] = s[top].id;
fa[Left[Nodes[i].id]] = Nodes[i].id;
if(top)
Right[s[top].id] = Nodes[i].id;
s[++top] = Nodes[i]; }
printf("YES\n");
for(int i = ; i <= N; ++i)
printf("%d %d %d\n", fa[i], Left[i], Right[i]);
return ;
}

最新文章

  1. iOS使用Zbar扫描二维码
  2. 安装vmall5:从ebak恢复数据,需要配置php.ini
  3. HDU 4310 贪心
  4. 请求转发(Forward)和重定向(Redirect)的区别
  5. 如何区别PeekMessage&amp;GetMessage SendMessage&amp;PostMessage
  6. CNZZ每天百亿条日志写入,SLS+ODPS轻松拆招
  7. JMS概述
  8. API网关
  9. 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection
  10. jssdk微信图片上传功能
  11. python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)
  12. gulp learning note
  13. C++中全排列算法函数next_permutation的使用方法
  14. 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境
  15. 如何在cygwin中运行crontab定时脚本[利刃篇]
  16. BZOJ1070[SCOI2007]修车——最小费用最大流
  17. APP端上传图片 - php接口
  18. 软件项目的开发之svn的使用
  19. linux下安装jenkins
  20. [linux] shell脚本编程-xunsearch安装脚本学习

热门文章

  1. idea 添加 阿里代码规范
  2. c++拷贝构造函数(翁恺c++公开课[26-27]学习笔记)
  3. MAC97A6检测
  4. 监听EditView中的文本改变事件详解--转
  5. iOS 开发之 开发一款自己的美颜相机
  6. web组件化开发第一天
  7. Navicat相关注册码
  8. css 图形样式
  9. stringstream常见用法介绍
  10. Linux命令:history命令历史的管理及用法