POJ 1785 Binary Search Heap Construction (线段树)
题目大意:
给出的东西要求建立一个堆,使得后面的数字满足堆的性质。并且字符串满足搜索序
思路分析:
用线段树的最大询问建树。在建树之前先排序,然后用中序遍历递归输出。
注意输入的时候的技巧。
。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e
#define maxn 55555 using namespace std; struct node
{
int data;
char str[50];
bool operator < (const node &cmp)const
{
//return data<cmp.data;
return strcmp(str,cmp.str)<0;
}
}save[maxn];
int res[maxn<<2];
int pos[maxn<<2];
int n; void pushup(int num)
{
res[num]=max(res[num<<1],res[num<<1|1]);
if(res[num<<1]>res[num<<1|1])pos[num]=pos[num<<1];
else pos[num]=pos[num<<1|1];
}
void build(int num,int s,int e)
{
if(s==e)
{
res[num]=save[s].data;
pos[num]=s;
return;
}
int mid=(s+e)>>1;
build(lson);
build(rson);
pushup(num);
}
int query(int num,int s,int e,int l,int r)
{
if(l<=s && r>=e)
{
return pos[num];
}
int mid=(s+e)>>1;
if(r<=mid)return query(lson,l,r);
else if(l>mid)return query(rson,l,r);
else
{
int lmost=query(lson,l,mid);
int rmost=query(rson,mid+1,r);
if(save[lmost].data<=save[rmost].data)return rmost;
else return lmost;
}
}
void print(int l,int r)
{
if(l>r)return;
if(l==r)
{
printf("(%s/%d)",save[l].str,save[l].data);
return;
}
int mid=query(1,1,n,l,r);
printf("(");
print(l,mid-1);
printf("%s/%d",save[mid].str,save[mid].data);
print(mid+1,r);
printf(")");
}
int main()
{
while(scanf("%d",&n)!=EOF && n)
{
for(int i=1;i<=n;i++)
{
scanf(" %[a-z]/%d",save[i].str,&save[i].data);
// printf("%s%d",save[i].str,save[i].data);
}
sort(save+1,save+1+n);
build(1,1,n);
print(1,n);
puts("");
}
return 0;
}
POJ 1785 Binary Search Heap Construction (线段树)的更多相关文章
- 笛卡尔树 POJ ——1785 Binary Search Heap Construction
相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS Memory Limit: 30000K Total Subm ...
- POJ 1785 Binary Search Heap Construction(裸笛卡尔树的构造)
笛卡尔树: 每个节点有2个关键字key.value.从key的角度看,这是一颗二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:从value的角度看,这是一个堆. 题意:以字符串为关键字k ...
- ZOJ - 2243 - Binary Search Heap Construction
先上题目: Binary Search Heap Construction Time Limit: 5 Seconds Memory Limit: 32768 KB Read the sta ...
- [POJ1785]Binary Search Heap Construction(笛卡尔树)
Code #include <cstdio> #include <algorithm> #include <cstring> #define N 500010 us ...
- poj1785 Binary Search Heap Construction
此题可以先排序再用rmq递归解决. 当然可以用treap. http://poj.org/problem?id=1785 #include <cstdio> #include <cs ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
随机推荐
- java中使用二进制进行权限控制
基本概念 package test; publicclass Rights { publicstaticvoid main(String[] args) { int a=1; // 001 状态a i ...
- MFC录制音频和播放音频
一.录制音频 在windows中提供了相应的API函数(waveIn这个族的函数)实现录音功能:在使用这些函数时,一定要引入相应的头文件 #include <windows.h> #inc ...
- nyoj 题目6 喷水装置
喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...
- 深入理解 Flutter 的编译原理与优化
阿里妹导读:对于开发者而言,Flutter工程和我们的Android/iOS工程有何差别?Flutter的渲染和事件传递机制如何工作?构建缓慢或出错又如何去定位,修改和生效呢?凡此种种,都需要对Flu ...
- web标准,可用性和可访问性
web标准,简单的说,是指html,css,JavaScript三者的分离. 网页由三部分组成:结构,表现和行为.对应的标准分为三方面: 1.结构化标准语言XHTML和XML2.表现标准语言主要包括c ...
- Codeforces Round #323 (Div. 2) C 无敌gcd 数学/贪心
C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- VBCodeProvider .net compiler service interface or something like that
https://msdn.microsoft.com/zh-cn/library/microsoft.visualbasic.vbcodeprovider%28v=vs.110%29.aspx ref ...
- android中提示&对话框----Notification
Notification(状态栏通知) 一.Notification用于状态栏显示通知的控件,在不同的设备上面Notification是不一样的 二.Notification的基本布局 元素组成: I ...
- android中Adapter适配器的讲解
Adapter(适配器的讲解) 适配器就我自己来看,我觉得这是一个非常重要的知识点,Adapter是用来帮助填出数据的中间桥梁,简单点说吧:将各种数据以合适的形式显示在View中给用户看.Adapte ...
- 解决mariadb数据库服务无法开启
我的系统Manjaro linux,安装的数据库为mariadb 10.1 1.安装数据库 pacman -S mariadb 2.配置数据库启动环境: mysql_install_db --user ...