poj 2761 Feed the dogs (treap树)
/*************************************************************
题目: Feed the dogs(poj 2761)
链接: http://poj.org/problem?id=2761
题意: 给一个数列,在给一些区间,求这些区间第k小的值,这些
区间没有包含关系,也就是说如果La>Lb那么Ra>Rb;
算法: treap树
思路: 由于这些区间没有包含关系,把这些区间排序后从左边
开始计算,每计算一个区间把前面多余数删除,这样每
个点只要插入和删除一次就可以了。
**************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; const int mx=;
struct S
{
int l, r;
int di,k;
};
S s[mx];
int a[mx];
int ans[mx];
int n,m;
bool com(S a,S b)
{
if (a.l==b.l) return a.r<b.r;
return a.l<b.l;
} typedef struct Node ///节点的结构体
{
Node *l,*r;
int val,pri; ///节点的值和优先级
int sz; ///节点子树的节点数
Node(int x) ///初始化节点
{
l=r=NULL;
val=x;
pri=rand();
sz=;
}
}Node;
Node *root; int Tsize(Node *T) ///计算子树的叶子节点
{
if (T==NULL) return ;
return T->sz;
}
Node *L_rotate(Node *T) ///右节点的优先级大于当前节点的优先级,进行左旋转
{
Node *A=T->r; ///A表示有节点
T->r=A->l;
A->l=T;
A->sz-T->sz; ///交换后A变成当前节点,所以它的子树的节点数等于原来节点的节点数
T->sz=Tsize(T->l)+Tsize(T->r)+;
return A;
}
Node *R_rotate(Node *T) ///左节点的优先级大于当前节点的优先级,进行右旋转
{
Node *A=T->l;
T->l=A->r;
A->r=T;
A->sz=T->sz;
T->sz=Tsize(T->l)+Tsize(T->r)+;
return A;
} void inser(Node *&T,int val) ///插入函数,和二叉排序树差不多
{
if (T==NULL)
{
T=new Node(val);
return ;
}
if (T->val>=val)
{
inser(T->l,val);
if ((T->l->pri)<(T->pri)) T=R_rotate(T); ///优先级比较,并旋转
}
else
{
inser(T->r,val);
if ((T->r->pri)<(T->pri)) T=L_rotate(T);
}
T->sz=Tsize(T->l)+Tsize(T->r)+;
} void Delete(Node *&T,int val) ///删除函数
{
if (T->val>val) Delete(T->l,val);
else if (T->val<val) Delete(T->r,val);
else
{
if (T->l==NULL&&T->r==NULL) T=NULL; ///左右节点都为空
else if (T->r==NULL) T=T->l; ///右节点为空
else if(T->l==NULL) T=T->r; ///左节点为空
else ///左右都不空
{
if (T->l->pri<T->r->pri) ///左节点优先级小于右边
{ ///右旋转,并向右子树删除
T=R_rotate(T); ///应为有旋转后,要删除的节点到有子树去了
Delete(T->r,val);
}
else
{
T=L_rotate(T);
Delete(T->l,val);
}
}
}
if (T!=NULL)
{
T->sz=Tsize(T->l)+Tsize(T->r)+;
}
} int Find(Node *T,int k) ///查找第k小的树
{
int temp=Tsize(T->l)+; ///temp小于等于T->val数的个数
if (temp==k) return T->val;
if (temp>k) return Find(T->l,k);
return Find(T->r,k-temp);
} void solve()
{
sort(s+,s+m+,com);
s[].l=-;
s[].r=-;
for (int i=;i<=m;i++)
{
for (int j=s[i-].l;j<=min(s[i-].r,s[i].l-);j++)
Delete(root,a[j]); ///删除比要计算区间多的数
for (int j=max(s[i-].r+,s[i].l);j<=s[i].r;j++)
inser(root,a[j]); ///插入该区间剩下的数
ans[s[i].di]=Find(root,s[i].k);
}
for (int j=s[m].l;j<=s[m].r;j++) Delete(root,a[j]);
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
for (int i=;i<=m;i++)
{
int l,r;
scanf("%d%d%d",&l,&r,&s[i].k);
s[i].l=min(l,r);
s[i].r=max(l,r);
s[i].di=i;
}
root=NULL;
solve();
for (int i=;i<=m;i++) printf("%d\n",ans[i]);
}
poj 2761 Feed the dogs (treap树)的更多相关文章
- POJ 2761 Feed the dogs(平衡树or划分树or主席树)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- POJ 2761 Feed the dogs (主席树)(K-th 值)
Feed the dogs Time Limit: 6000MS Memor ...
- POJ 2761 Feed the dogs
主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- POJ 题目2761 Feed the dogs(主席树||划分树)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 16860 Accepted: 5273 De ...
- [POJ2761] Feed the dogs (Treap)
题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...
- 划分树---Feed the dogs
POJ 2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...
- poj 2761 主席树的应用(查询区间第k小值)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 22084 Accepted: 7033 De ...
- 【POJ2761】【区间第k大】Feed the dogs(吐槽)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- Poj2761-Feed the dogs(伸展树求名次)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
随机推荐
- Cacti修改采集精度为1分钟
前言: 基础知识:建议先了解一下 rrdtool 及 rrd 数据库的工作原理,参考阅读<rrdtool学习笔记> cacti主要使用了rrdtool这个工具来绘图,所以看上去比zabbi ...
- XP局域网内专用消息队列
网上能找到DELPHI消息队列的方法,在XP下试了总是不成功,后来在2003上试就行了,对比发现消息队列属性->安全 2003中多了个用户ANONYMOUS_LOGON. 然后在XP下消息队列属 ...
- JAVA EE中session的理解
转自[互动百科]http://www.baike.com/wiki/Session Session Session:在计算机中,尤其是在网络应用中,称为“会话”.Session直接翻译成中文比较困 ...
- RX(Reactive Extinsion)和IX(Interactive Extinsion)库改名了
RX和IX库是我经常用到的库,今天写一个小程序的时候想用IX库的时候,发现却找不到了.在它的源码的网站上找了一下,发现它们被改名了,具体的变化为为: 对于RX库: Rx-Main -> Syst ...
- maven更新远程仓库速度太慢解决方法
1.maven在更新下载jar包的时候,因为jar包默认是从国外服务器上下载的,所以速度特别慢 2.通过设置镜像的方法加快jar包下载 3.在maven安装目录下,/config/settings.x ...
- css小知识之伪元素
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- adb无法使用,提示error: unknown host service的解决办法
此时,需要辨别电脑的5037端口被哪个应用程序占用的方法:(使用adb时需要5037端口是空闲的) 1. 打开命令行,输入命令:netstat -ano |findstr "5037&quo ...
- BeanUtils.populate的作用
它是在org.apache.commons.beanutils.BeanUtils包中的一个方法. 方法的作用:将一些 key-value 的值(例如 hashmap)映射到 bean 中的属性. ...
- Ubuntu 命令大全
一.文件目录类 1.建立目录:mkdir 目录名 2.删除空目录:rmdir 目录名 3.无条件删除子目录: rm -rf 目录名 4.改变当前目录:cd 目录名 (进入用户home目录:cd ~:进 ...
- egrep 及扩展正则表达式
grep -E 表示支持扩展的正则表达式 grep -E = egrep 一.字符匹配: 扩展模式下的字符匹配与基本正则表达式的字符匹配相同,如: . 表示任意单个字符 [] 表示范围内人任意单个字符 ...