155. Cartesian Tree

time limit per test: 0.25 sec. 
memory limit per test: 65536 KB
input: standard input 
output: standard output
Let us consider a special type of binary search trees, called cartesian trees. Recall that a binary searchtree 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 than the key of x, and each node in its right subtree has the key greater than the key of x. 
That is, if we denote the left subtree of the node x by L(x), its right subtree by R(x) and its key by kx, for each node x we will have 
  * if y in L(x) then ky < kx 
  * if z in 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 <= 50000). The following N lines contain two integer numbers each - given pairs (ki, ai). For each pair |ki|, |ai| <= 30000. 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, output the tree itself in the following N lines. Let the nodes be numbered from 1 to N corresponding to pairs they contain as these pairs 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. 
If there are several possible trees, output any one.
Sample test(s)
Input
 

5 4 
2 2 
3 9 
0 5 
1 3 
6 6 
4 11
 
Output
 
YES 
2 3 6 
0 5 1 
1 0 7 
5 0 0 
2 4 0 
1 0 0 
3 0 0
 
 

cartesian tree,俗称“笛卡尔树”。

笛卡尔树是什么呢?就是一颗类treap树。

即每个节点有两个域值,一个阈值满足二叉排序树性质,另一个域值满足堆性质。

首先说一下,笛卡尔树和treap结构上相似,但实际上,treap的第二个阈值只是用于维护平衡,而笛卡尔树的阈值是真正的值。

这道题目主要是笛卡尔树的建树。

它是O(n)的(核心部分)。

我们先把所有节点按照二叉树值从大到小排序,并假设现在已经建成了一个1~i-1的节点的树。

那么当前节点i按照二叉树值插入一点会插入在最右链的末端。

不过这样,可能会使其父节点的堆值大于他。怎么办?我们相当于做一个左旋操作。

找到第一个堆值比i小的i的祖先u,使i成为u的右儿子,u原来的右子树变为u的左子树,这样就可以满足要求了,并且这样的树一定是存在的。

那么,现在的问题是,怎样找到u?显然,可以二分查找O(nlogn)的复杂度。但这样不是最优的。

最优的就是就是用一个单调栈,记录最右链的节点。由于每一个节点最多访问一次,所以复杂度是O(n)的。

code:

 #include<bits/stdc++.h>
 using namespace std;
 ,inf=0x1a1a1a1a;
 int n,s[N],top;
 struct node {int k,x,i,f,l,r;}a[N];
 bool cmp_k(const node &u,const node &v) {return u.k<v.k;}
 bool cmp_i(const node &u,const node &v) {return u.i<v.i;}
 int main() {
     cin>>n,top=;
     ; i<=n; i++)
         scanf("%d%d",&a[i].k,&a[i].x),a[i].i=i;
     a[].l=a[].r=a[].i=,a[].k=a[].x=-inf;
     sort(a+,a++n,cmp_k);
     s[++top]=;
     ,las=-; i<=n; i++,las=-) {
         while (top&&a[s[top]].x>a[i].x) las=s[top],top--;
         a[i].f=a[s[top]].i,a[s[top]].r=a[i].i;
         if (~las) a[i].l=a[las].i,a[las].f=a[i].i;
         s[++top]=i;
     }
     sort(a+,a++n,cmp_i);
     puts("YES");
     ; i<=n; i++)
         printf("%d %d %d\n",a[i].f,a[i].l,a[i].r);
     ;
 }

最新文章

  1. 精益化设计:把敏捷方法和Lean UX相结合
  2. 安装AdventureWorks2008R2
  3. Redis学习笔记二
  4. ios蓝牙开发(四)app作为外设被连接的实现-转发
  5. python2.7之MySQLdb模块 for linux安装
  6. FastDFS在centos上的安装配置与使用
  7. Scrum Meeting---Five(2015-10-29)
  8. jQuery 工具类库集锦
  9. laravel项目return back()-&gt;withErrors($validator)或return back()-&gt;with(&#39;errors&#39;,&#39;原密码错误!&#39;)在前台原密码错误的情况下不能正确显示错误信息,变成报错!
  10. windows系统下Python环境的搭建及Selenium的安装
  11. java JNI 的实现(2)-java和C/C++的相互调用.
  12. Windows核心编程&amp;内存管理
  13. Servlet.service() for Servlet jsp threw exception javax.servlet.ServletException:File &amp;quot;/pageFoo
  14. 轻松测试 logstash 的配置文件
  15. (webpack系列二)webpack打包优化探索
  16. 《Linux.Shell编程从入门到精通》读书笔记
  17. Java web错误总结~
  18. Wi-Fi 协议和数率?
  19. cocos2d-x中使用CCOrbitCamera做水平翻转
  20. Java 基础【15】 压缩与解压缩

热门文章

  1. 安装和使用ZFS
  2. NOIP2017感想
  3. Zookeeper应用之——栅栏(barrier)
  4. Java web开发环境搭配
  5. flask_mail发送163邮件,报553错误的原因
  6. java并发之线程间通信
  7. Springboot 上传excel并解析文件内容
  8. 剑指offer(32)把数组排成最小的数
  9. day04流程控制,if分支结构,while,for循环
  10. js前端文件收集(一)