时间限制:0.25s

空间限制:6M

题意:

给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆.


Solution :

先按k值从小到大排序.

再从序列中找到最小的val值,将其作为根.再对它的左边和右边做同样的操作.左边最大的数做左儿子,右边做右儿子。递归即可.

这里要快速找到一个序列区间的最大值,用ST方法求RMQ就行了.

时间复杂度O(nlogN),空间复杂度O(n)

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
using namespace std; struct node {
int key, val, ID;
} p; struct answer {
int fa, lson, rson;
} ans[51000]; typedef pair<int , int > P;
vector<node> f;
P st[51000][20];
int n, x, y; bool cmp (node a, node b) {
return a.key < b.key;
}
//ST RMQ
void ST() {
for (int i = n - 1; i >= 0 ; i--)
for (int j = 1; i + (1 << j) <= n; j++)
{
if (st[i][j - 1].first < st[i + (1 << j - 1)][j - 1].first)
st[i][j] = st[i][j - 1];
else
st[i][j] = st[i + (1 << j - 1)][j - 1];
}
}
//得到区间最小值的位置
int getmin (int l, int r)
{
P tem;
tem=st[l][0];
for (int k = 0; l + (1 << k) <= r; k++)
{
if (st[l][k].first < tem.first)
tem = st[l][k];
if (st[r - (1 << k) + 1][k].first < tem.first)
tem = st[r - (1 << k) + 1][k];
}
return tem.second;
}
//递归建树
int make (int l, int r, int fa)
{
int k = getmin (l, r);
int pos = f[k].ID;
ans[pos].fa = fa;
if (l >= r) return pos;
if (l < k) ans[pos].lson = make (l, k - 1, pos);
if (k < r) ans[pos].rson = make (k + 1, r, pos);
return pos;
} int main()
{
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d %d",&x,&y);
p.key = x, p.val = y, p.ID = i + 1;
f.push_back (p);
}
//按key值从小到大排序
sort (f.begin(), f.end(), cmp);
for (int i = 0; i < (int) f.size(); i++)
st[i][0] = make_pair (f[i].val, i);
ST();
make (0, n - 1, 0);
//一定有解直接输出 "YES"
puts("YES\n");
for (int i = 1; i <= n; i++)
printf("%d %d %d\n",ans[i].fa,ans[i].lson,ans[i].rson);
return 0;
}

  

  

SGU 155.Cartesian Tree的更多相关文章

  1. [sgu P155] Cartesian Tree

    155. Cartesian Tree time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard i ...

  2. Algorithm: cartesian tree

    http://baike.baidu.com/link?url=XUt5fXQ-jtFBM0UdKiGA41_NWFvdFSYwVsy4SVvCRRuEBvNkLfT9TgOtzsXvaOT9nuq_ ...

  3. 笛卡尔树Cartesian Tree

    前言 最近做题目,已经不止一次用到笛卡尔树了.这种数据结构极为优秀,但是构造的细节很容易出错.因此写一篇文章做一个总结. 笛卡尔树 Cartesian Tree 引入问题 有N条的长条状的矩形,宽度都 ...

  4. PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)

    7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct ...

  5. Day6 - J - Cartesian Tree POJ - 2201

    Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binar ...

  6. PAT-1167(Cartesian Tree)根据中序遍历序列重建最小堆

    Cartesian Tree PAT-1167 一开始我使用数组进行存储,但是这样可能会导致无法开足够大的数组,因为树如果是链表状的则无法开这么大的数组(虽然结点很少). 正确的解法还是需要建树,使用 ...

  7. POJ 2201 Cartesian Tree ——笛卡尔树

    [题目分析] 构造一颗笛卡尔树,然后输出这棵树即可. 首先进行排序,然后用一个栈维护最右的树的节点信息,插入的时候按照第二关键字去找,找到之后插入,下面的树成为它的左子树即可. 然后插入分三种情况讨论 ...

  8. OpenJudge Cartesian Tree

    [代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...

  9. CF1290E Cartesian Tree

    考虑笛卡尔树的意义: 一个点在笛卡尔树中的子树,代表以他为最小/最大值的区间. 所以一个点的子树大小,一定是类似到达序列边界或者被一个比他更大的数隔离. 考虑记录 \(l_i,r_i\) 为第 \(i ...

随机推荐

  1. Eclipse C/C++环境配置

    一.C/C++环境配置: Window - Preferences - C/C++ 1. Editor - Content Assist - Auto-Activation - Delay(ms),原 ...

  2. Go语言的学习

    1.配置环境变量 2.本地阅读报的说明和文档 不用FQ window+R  出现黑窗口   执行    godoc -http :8080 在本地浏览器 localhost:8080 回车 3多行注释 ...

  3. app.config应该放哪?

    一:做了一个简单的三层构架的小例子,在主项目里调用工具类的方法实现在数据库里添加一条信息.先看下错误的提示信息是什么样的,如下图一,图二是调用工具类.直接在工具类里写上连接字符串就没问题,如果写到ap ...

  4. C++中new和malloc

    1.malloc的工作原理: malloc使用一个数据结构(链表)来维护分配空间链表的构成:分配的空间/上一个空间的数据/下一个空间/空间大小等信息.    对malloc分配的空间不要越界访问,因为 ...

  5. linux驱动面试题目汇总

    http://blog.csdn.net/blueice8601/article/details/7666427 1.linux驱动分类 2.信号量与自旋锁 3.platform总线设备及总线设备如何 ...

  6. LianLianKan - HDU 4272(状态压缩)

    题目大意:有一列数据,可以从最上面的开始连接下面相同的元素,然后消除,不过距离不能超过6,询问最后能不能消除完整个数列. 分析:首先讨论一点最远能消除的地方,比如点的位置是x,如若想要消除x+1位置处 ...

  7. LogMiner详细讲解

    原文地址:LogMiner 一.LogMiner的用途 日志文件中存放着所有进行数据库恢复的数据,记录了针对数据库结构的每一个变化,也就是对数据库操作的所有DML语句. 在Oracle 8i之前,Or ...

  8. memcached与redis 对比

    一. 综述 读一个软件的源码,首先要弄懂软件是用作干什么的,那memcached和redis是干啥的?众所周知,数据一般会放在数据库中,但是查询数据会相对比较慢,特别是用户很多时,频繁的查询,需要耗费 ...

  9. redis中各种数据类型对应的jedis操作命令

    redis中各种数据类型对应的jedis操作命令 一.常用数据类型简介: redis常用五种数据类型:string,hash,list,set,zset(sorted set). 1.String类型 ...

  10. 获取文件路径 分类: WinForm 2014-07-25 14:27 103人阅读 评论(0) 收藏

    //可获得当前执行的exe的文件名. string str1 =Process.GetCurrentProcess().MainModule.FileName; //获取和设置当前目录(即该进程从中启 ...