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 ...
随机推荐
- [漏洞复现] [Vulhub靶机] OpenSSL Heartbleed Vulnerability (CVE-2014-0160)
免责声明:本文仅供学习研究,严禁从事非法活动,任何后果由使用者本人负责. 0x00 背景知识 传输层安全协议SSL 安全套接字协议SSL(Secure Sockets Layer),及其继任者传输层安 ...
- Spark: 单词计数(Word Count)的MapReduce实现(Java/Python)
1 导引 我们在博客<Hadoop: 单词计数(Word Count)的MapReduce实现 >中学习了如何用Hadoop-MapReduce实现单词计数,现在我们来看如何用Spark来 ...
- mybatis 转义符号
< <= > >= & ' " < <= > >= & ' "
- React history.push()无法跳转 url改变页面不渲染
一.history.push()无法跳转参考了很多文章 研究一下生命周期 render是要有state变化才会执行 BrowserHistory只有props变化 无法触发render 如下改造环境 ...
- Sec资产管理——SwebUI开源应用解决方案
产品简介 Sweb Sec是一款资产管理类的开源解决方案,通过SwebUI平台开发,包含资产管理.耗材管理两种管理系统,由部门管理.区域管理.盘点.标签打印.出入库等核心功能组成. 免费获取方案 开源 ...
- 第6章 字符串(下)——C++字符串
6.5 C++ strings(C++字符串) C风格字符串常见错误:试图去访问数组范围以外的元素:没有使用函数strcpy( )来实现字符串之间的复制:没有使用函数strcmp( )来比较两个字符串 ...
- Mysql中文存储、显示及不区分大小写控制
刚开始使用mysql,以为安装了完了就可以使用了,结果是我太天真了.mysql5.7版本,默认严格区分大小写,并且不支持中文存储. 严格区分大小写,即A表和a表示两个不同的表 实例 修改 在/etc/ ...
- python:selenium测试登录在chrome中闪退
问题描述:使用selenium.webdriver时测试网页,进行自动登录测试总是在登录成功时闪退.使用指定驱动器位置的方式chrome也会闪退 1.正常使用chrome驱动打开一个网页,正常访问 f ...
- 【Redis】字典
Redis 字典 基本语法 字典是Redis中的一种数据结构,底层使用哈希表实现,一个哈希表中可以存储多个键值对,它的语法如下,其中KEY为键,field和value为值(也是一个键值对): HSET ...
- 用Typescript 的方式封装Vue3的表单绑定,支持防抖等功能。
Vue3 的父子组件传值.绑定表单数据.UI库的二次封装.防抖等,想来大家都很熟悉了,本篇介绍一种使用 Typescript 的方式进行统一的封装的方法. 基础使用方法 Vue3对于表单的绑定提供了一 ...