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 ...
随机推荐
- 习课的redis配置记录
<!-- redis begin --> <dependency> <groupId>redis.clients</groupId> <artif ...
- 一个最小mybatis
项目结构 package hello; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis ...
- ASP.NET MVC4框架揭秘 源代码下载
http://files.cnblogs.com/artech/asp.net.mvc.4.samples.rar
- CentOS 6下Apache的https虚拟主机实践
题目:1.建立httpd服务器,要求: 提供两个基于名称的虚拟主机: (a)www1.buybybuy.com,页面文件目录为/web/vhosts/www1:错误日志为/var/log/httpd/ ...
- Leetcode5:Longest Palindromic Substring@Python
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- switch(){}default后是有冒号的
switch: function a(c,d){ switch(c+d){ : console.log(); break; : console.log(); break; : console.log( ...
- Jquery--弹窗
<title>弹窗</title> <script src="JS/jquery-1.7.2.js"></script> <s ...
- avalon全选效果分析讲解
全选功能就是 1.点击全选控制循环元素是否选中.(点击全选,下面的所有元素选中,再次点击 所有元素取消选中.) 2.点击循环元素控制全选.(如果当前元素是未选中状态则全选不选中,如果当前元素是选中状态 ...
- BootStrap基本控件
简介 BootStrap是一个用于快速开发web应用程序和网站的前端框架. BootStrap是基于HTML, CSS, JavaScript. BootStrap是由Twitter的Mark Ott ...
- SQL Server执行计划的理解
详细看:http://www.cnblogs.com/kissdodog/p/3160560.html 自己总结: 扫描Scan:逐行遍历数据. 查找Seek:根据查询条件,定位到索引的局部位置,然后 ...