时间限制: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. ServiceStack.Redis之IRedisClient

    事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...

  2. 传智播客C语言视频第二季(第一季基础上增加诸多C语言案例讲解,有效下载期为10.5-10.10关闭)

    卷 backup 的文件夹 PATH 列表卷序列号为 00000025 D4A8:14B0J:.│  1.txt│  c语言经典案例效果图示.doc│  ├─1传智播客_尹成_C语言从菜鸟到高手_第一 ...

  3. Bitmap 与Drawable相互转换

    Drawable 转 Bitmap import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import ...

  4. 可压Navier-Stokes方程组的爆破现象

    在 Z.P. Xin, Blowup of smooth solutions to the compressible Navier-Stokes  equations with compact den ...

  5. spring中context:property-placeholder/元素 转载

    spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...

  6. Java中的包

    包:定义包用package关键字. 1:对类文件进行分类管理. 2:给类文件提供多层名称空间. 如果生成的包不在当前目录下,需要最好执行classpath,将包所在父目录定义到classpath变量中 ...

  7. 2015/11/06 社保查询系统持续 挂机ing

  8. [置顶] SpecDD系列:“完成” 的定义

    在当今充满变数和快节奏的大环境下,一个产品的上市时间已经成为产品成功与否的重要因素.如果对于“完成”没有一个清晰的定义,那么很可能会遭遇产品延期,风险,并且增加公司成本. “完成的定义”也被俗称为Do ...

  9. rails tutorial sample app

    阅读第3章时,按照教程第一次运行rspec失败了: E:\05 Create\Code\temp\railstutorial\sample_app>rspec spec/requests/sta ...

  10. DirectShow VS2013 控制台下捕捉摄像头而且显示

    须要lib库文件 strmiids.lib,下载地址:http://download.csdn.net/detail/dopamy_busymonkey/8872687 放在解决方式中项目的根文件夹中 ...