时间限制: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. 使用Maven构建Web项目的目录结构

    1.Web项目的目录结构     基于Java的Web项目,标准的打包方式是WAR.与JAR比较,包含更多的内容,比如JSP文件.Servlet.Java类.web.xml配置文件.依赖JAR包.静态 ...

  2. ☀【组件 - 工具】Parallax 视差

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Delphi 编写的Web Service

      一编写服务程序 第一步:File----->New----->Other------>WebServices----->Soap Server Application选择I ...

  4. std::numeric_limits<int>::max() error C2589: '(' : illegal token on right side of '::' 解决办法

    int max =std::numeric_limits<int>::max();     根据错误提示: f:\code\cpp\webspider\main.cpp(47) : war ...

  5. ubuntu下使用ngrok外网映射

    好久之前想搞明白这个事情,可是就是不知道这个词叫外网映射,所以也一直不知怎么做,在慕课网看用java开发微信公众号的时候教程里提到了外网映射,查了一些资料终于把本地给映射到外网了,直接变成了80端口, ...

  6. (经常看看)jdk 设计模式

    在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改JDK源码的前提下开发出自己的应用软件,本文列出了部分JDK中的模式应用实例,有兴趣 ...

  7. Using QEMU for Embedded Systems Development

    http://www.opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/ http://www.opens ...

  8. UVa11925 Generating Premutations

    留坑(p.254) #include<cstdio> #include<cstring> #include<cstdlib> #include<algorit ...

  9. Hibernate配置文件详解

    Hibernate配置方式 Hibernate给人的感受是灵活的,要达到同一个目的,我们可以使用几种不同的办法.就拿Hibernate配置来说,常用的有如下三种方式,任选其一. 在 hibernate ...

  10. DM8168 GPIO驱动与測试程序

    本次測试针对GPIO1进行,挑选了GP1[31],引脚的复用默认的就是GPIO 还是老规矩,贴上driver.c,Makefile,test.c: dm8168_gpio.c: #include &l ...