/*************************************************************
题目: 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树)的更多相关文章

  1. 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 ...

  2. POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs Time Limit: 6000MS   Memor ...

  3. POJ 2761 Feed the dogs

    主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

  4. POJ 题目2761 Feed the dogs(主席树||划分树)

    Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16860   Accepted: 5273 De ...

  5. [POJ2761] Feed the dogs (Treap)

    题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...

  6. 划分树---Feed the dogs

    POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...

  7. poj 2761 主席树的应用(查询区间第k小值)

    Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 22084   Accepted: 7033 De ...

  8. 【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 ...

  9. Poj2761-Feed the dogs(伸展树求名次)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...

随机推荐

  1. STL之序列式容器list与forward_list

    List (双向链表) 与 forwardlist (单向链表) 算是非常基础的数据结构了,这里只是简单介绍下其结构及应用. 以list为例: 其节点模板: template <class T& ...

  2. Could not find a getter for orderItems in class

    ::, ERROR ContextLoader: - Context initialization failed org.springframework.beans.factory.BeanCreat ...

  3. leetcode 172

    172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: ...

  4. OC中property的有关属性

    property的有关属性: (1)readwrite是可读可写特征:需要生成getter方法和setter方法: (2)readonly是只读特性只会生成getter方法不会生成setter方法: ...

  5. Android启动标记

    1. Task是包含一系列Activity的堆栈, 遵循先进后出原则. 2. Task默认行为: (1) 前提: Activity A和Activity B在同一个应用中. 操作: Activity ...

  6. Spring urlMapping

    背景 某url性能测试表明,qps单机最高只有4000多,虽然靠堆机器可以解决问题,但是显然不是什么优雅的方案. 试着把controller里的所有的逻辑都屏蔽,只是简单的返回hello world, ...

  7. 安装zabbix报错configure: error: libcurl library not found

    libcurl 和libcurl-devel都已经安装 且大于7.13.1 但还是报错,这时需要在configure 指定curl路径即可 ./configure --prefix=/usr/loca ...

  8. VS2010安装帮助文档出现错误

    安装VS2010后的帮助文档安装出现错误:未能在指定文件夹中创建本地存储区 安装完VS2010后,出现错误,取消后 再安装MSDN 打开“Help Library 管理器 - Microsoft He ...

  9. curl php 小记

    /* 开发用到. */ https 需加入: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查

  10. asp.net mvc return file result

    asp.net mvc返回文件: public ActionResult ExportReflection(string accessToken) { var reflections = GetCms ...