POJ2201 Cartesian Tree (cartesian tree)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#else
#define D_e_Line ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 50007;
struct node{
int l,r,fa,id;
int val,pri;
bool operator < (const node &com)const{
return val < com.val;
}
}t[N],ans[N];
inline void Insert(int rt){
int tmp;
for(tmp = rt - 1; t[tmp].pri > t[rt].pri; tmp = t[tmp].fa);
t[rt].l = t[tmp].r;
t[t[tmp].r].fa = rt;
t[rt].fa = tmp;
t[tmp].r = rt;
}
int vis[30007];
int main(){
int n;
while(scanf("%d", &n) != EOF){
Fill(vis, false);
int flag = true;
R(i,0,n){
t[i].l = t[i].r = t[i].fa = 0;
t[i].w = -0x7fffffff;
}
R(i,1,n){
scanf("%d%d", &t[i].val, &t[i].w);
t[i].id = i;
if(vis[t[i].val])
flag = false;
else
vis[t[i].val] = true;
}
if(!flag){
printf("NO\n");
continue;
}
printf("YES\n");
sort(t + 1, t + n + 1);
R(i,1,n) Insert(i);
R(i,1,n){
if(!t[i].l)
ans[t[i].id].l = 0;
else
ans[t[i].id].l = t[t[i].l].id;
if(!t[i].r)
ans[t[i].id].r = 0;
else
ans[t[i].id].r = t[t[i].r].id;
if(!t[i].fa)
ans[t[i].id].fa = 0;
else
ans[t[i].id].fa = t[t[i].fa].id;
}
R(i,1,n)
printf("%d %d %d\n", ans[i].fa, ans[i].l, ans[i].r);
}
return 0;
}

POJ2201 Cartesian Tree (cartesian tree)的更多相关文章
- ZOJ 3201 Tree of Tree
树形DP.... Tree of Tree Time Limit: 1 Second Memory Limit: 32768 KB You're given a tree with weig ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- B-tree/B+tree/B*tree [转]
(原文出处:http://blog.csdn.net/hbhhww/article/details/8206846) B~树 1.前言: 动态查找树主要有:二叉查找树(Binary Search Tr ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树
[BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...
- easyui tree扩展tree方法获取目标节点的一级子节点
Easyui tree扩展tree方法获取目标节点的一级子节点 /* 只返回目标节点的第一级子节点,具体的用法和getChildren方法是一样的 */ $.extend($.fn.tree.meth ...
- Tree - Decision Tree with sklearn source code
After talking about Information theory, now let's come to one of its application - Decision Tree! No ...
- Binary Indexed Tree (Fenwick Tree)
Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- B-tree & B+tree & B*Tree 结构浅析——转
转自http://www.cnblogs.com/coder2012/p/3330311.html http://blog.sina.com.cn/s/blog_6776884e0100ohvr.ht ...
随机推荐
- django框架4
内容概要 编辑删除功能编写 虚拟环境 django路由层版本区别 视图函数的返回值 JsonResponse对象 form表单上传文件 request其他方法 FBV与CBV(基于函数的视图.基于类的 ...
- Crontab在服务端进行设置定时执行任务
Crontab简crontab是一个可以根据时间.日期.月份.星期的组合调度对重复任务的执行的守护进程.也可以讲Linux crontab是用来定期执行程序的命令. 当安装完成操作系统之后,默认便会启 ...
- C#/VB.NET 在Word转PDF时生成目录书签
当我们在转换Word文档到PDF格式时,想保留Word文档的标题作为PDF书签,那么应该如何操作呢?那么本文将以C#及VB.NET代码为例,介绍如何在Word转PDF时生成目录书签.下面是具体方法和步 ...
- (持续更新)虚树,KD-Tree,长链剖分,后缀数组,后缀自动机
真的就是讲课两天,吸收一个月呢! \(1.\)虚树 \(2.\)KD-Tree \(3.\)长链剖分 \(4.\)后缀数组 后缀数组 \(5.\)后缀自动机 后缀自动机
- 【Java面试】请说一下ReentrantLock的实现原理?
一个工作了3年的粉丝私信我,在面试的时候遇到了这样一个问题. "请说一下ReentrantLock的实现原理",他当时根据自己的理解零零散散的说了一些. 但是似乎没有说到关键点上, ...
- kubernetes code-generator使用
目录 Overview Prerequisites CRD code-generator 编写代码模板 code-generator Tag说明 开始填写文件内容 type.go doc.go reg ...
- 线程池:ThreadPoolExcutor源码阅读
ThreadPoolExcutor源码流程图:(图片较大,下载再看比较方便) 线程池里的二进制奥秘 前言: 线程池的五种状态state(RUNNING.SHUTDOWN.STOP.TIDYING.TE ...
- C++ 练气期之一文看懂字符串
C++ 练气期之细聊字符串 1. 概念 程序不仅仅用于数字计算,现代企业级项目中更多流转着充满了烟火气的人间话语.这些话语,在计算机语言称为字符串. 从字面上理解字符串,类似于用一根竹签串起了很多字符 ...
- 配置Apollo阿波罗.net core 3.1 c#
直接上代码: public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(a ...
- Elasticsearch面试题
Elasticsearch面试题 1.Elasticsearch是如何实现master选举的? 1.对所有可以成为master的节点根据nodeId排序,每次选举每个节点都把自己所知道节点排一次序,然 ...