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 ...
随机推荐
- jsp Request获取url信息的各种方法比较
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...
- ffmpeg relocation error
在向imx6移植ffmpeg后,一般的编解码操作没有问题,但是当从摄像头录视频时, ffmpeg -f video4linux2 -s 640*480 -r 10 -i /dev/video0 tes ...
- Linux环境下安装Oracle 10g 发生错误 You do not have permission to write to the inventory location
关于安装过程中出现的一些错误,我总结一下,路径没权限,不是该用户组下面的需要创建oracle的用户和用户组及目录 ,并对目录赋予相应权限,可参考下面的例子:这个地方如果简单的按照下面的程序做也能安装成 ...
- Python + PIL 处理支付宝AR红包
思路比较简单:1.对图片进行锐化处理:2.设(r_h, g_h, b_h)为支付宝遮罩黑条的RGB值,以此为中心,查找半径为Diff_radius的范围内所有的色值: 3.对每一行符合步骤2的像素点个 ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
- java程序员需要掌握些什么知识
java程序员需要掌握些什么知识 合格的程序员应具有实际开发能力的Java和J2EE.如今的IT企业需求量大,但人才紧缺的.企业需要大量掌握Java/JEE/Oracle/WebLogic/Websp ...
- 15.linux 中无法输入指令
怎么可以回到倒数第五行的输入状态啊……
- php工作笔记8-并发和数据类型
1.mysql在进行数据的修改时,并发情况下: $RoundsRows=$modelRounds->where("id=$roundsID and (sendMoney + $amou ...
- 敏捷软件开发 VS. 传统软件工程
敏捷软件开发 VS. 传统软件工程 软件工程这一术语1968年被提出,之后美国软件工程专家巴利·玻姆对十多年间研究软件工程的专家学者们提出的一些准则与信条,于1983年对提出软件工程的七条基本定理,将 ...
- 9.openssl ca
用于签名证书请求.生成CRL.维护一个记录已颁发证书和这些证书状态的数据库. 证书请求私用CA的私钥签名之后就是证书. [root@xuexi tmp]# man ca SYNOPSIS openss ...