Poj2761-Feed the dogs(伸展树求名次)
Description
Your task is to help Jiajia calculate which dog ate the food after each feeding.
Input
The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.
Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.
You can assume that n<100001 and m<50001.
Output
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
题意:查询一段区间的第k大值,而且题目保证查询区间是不存在包含关系。 解析:因为不存在包含关系,所以直接离线排序,向右插入元素,向右删除元素。利用treap树实现名次树的功能。 代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int INF=1e9+;
const int maxn=;
int A[maxn],cnt; //A数组保存数,cnt是节点标号,我是用数组模拟的
struct treap
{
treap* son[]; //左右儿子
int v,s,r;
treap(){ v=s=r=; son[]=son[]=NULL; }
treap(int nv,int nr);
int rk(){ return son[]->s+; } //排名,第几个数
int cmp(int k) //比较,如果相等返回-1,小于返回0,大于1
{
if(k==v) return -;
return k<v?:;
}
void pushup(){ s=son[]->s+son[]->s+; } //更新大小
}null,tr[maxn];
treap::treap(int nv,int nr)
{
v=nv; r=nr;
s=;
son[]=son[]=&null;
}
treap* NewNode(int x,int r)//建新节点
{
tr[cnt]=treap(x,r);
return tr+cnt++;
}
struct splaytree
{
int Size;
treap* root;
splaytree(){ Size=; root=&null; }
void Rotate(treap* &t,int d) //翻转操作
{
treap* p=t->son[d^];
t->son[d^]=p->son[d];
p->son[d]=t;
t->pushup(); //要更新
t=p;
t->pushup();
}
void Insert(treap* &t,int x,int r) //插入
{
if(t==&null) //插入
{
t=NewNode(x,r); //申请新节点
return;
}
int d=t->cmp(x);
if(d==-) d=; //往右边走
Insert(t->son[d],x,r);
if(t->son[d]->r > t->r) Rotate(t,d^); //旋转
t->pushup();
}
void Remove(treap* &t,int x) //删除
{
int d=t->cmp(x);
if(d==-)
{
if(t->son[]==&null) t=t->son[];
else if(t->son[]==&null) t=t->son[];
else
{
int d2=(t->son[]->r > t->son[]->r ? : );
Rotate(t,d2);
Remove(t->son[d2],x);
}
}
else Remove(t->son[d],x);
if(t!=&null) t->pushup();
}
int Query(treap* &t,int kth) //查询
{
if(t==&null||kth<=||kth>t->s) return -;
int a=t->rk();
if(kth==a) return t->v;
else if(kth<a) return Query(t->son[],kth);
else return Query(t->son[],kth-a);
}
};
int N,M;
struct Ques
{
int x,y,k,id;
Ques(int x=,int y=,int k=,int id=):x(x),y(y),k(k),id(id){}
bool operator < (const Ques& t) const
{
if(x!=t.x) return x<t.x;
return y<t.y;
}
}q[maxn];
int ans[maxn];
int main()
{
scanf("%d%d",&N,&M);
splaytree spt; cnt=;
for(int i=;i<=N;i++) scanf("%d",&A[i]);
int x,y,k;
for(int i=;i<M;i++) //输入
{
scanf("%d%d%d",&x,&y,&k);
q[i]=Ques(x,y,k,i);
}
sort(q,q+M); //排序
int f=,r=;
for(int i=;i<M;i++)
{
Ques& t=q[i];
int x=t.x,y=t.y,k=t.k;
for(;f<x;f++) if(f<r) spt.Remove(spt.root,A[f]);
if(r<f) r=f;
for(;r<=y;r++) spt.Insert(spt.root,A[r],rand());
ans[t.id]=spt.Query(spt.root,k); //保存答案
}
for(int i=;i<M;i++) printf("%d\n",ans[i]);
return ;
}
Poj2761-Feed the dogs(伸展树求名次)的更多相关文章
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- [Poj2761]Feed the dogs(主席树)
Desciption 题意:求区间第K小(N<=100000) Solution 主席树模板题 Code #include <cstdio> #include <algorit ...
- 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs
先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...
- [POJ2761] Feed the dogs (Treap)
题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...
- [POJ2761]Feed the dogs
Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...
- POJ 题目2761 Feed the dogs(主席树||划分树)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 16860 Accepted: 5273 De ...
- 划分树---Feed the dogs
POJ 2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...
- 【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 ...
- 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 ...
随机推荐
- (greedy)Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- hsql使用架构包启动数据库
一.通常我们平时启动就是直接通过hsql.jar来进行启动 java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing java -cp hsql ...
- 关于static静态
静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用. class Car { private static $speed = ...
- Entify Framewrok - LINQ简单使用
1.如何使用Join: http://www.devcurry.com/2011/01/join-example-in-linq-and-c.html
- lucene 使用教程
原文转自:http://cloudera.iteye.com/blog/656459 1 lucene简介 1.1 什么是lucene Lucene是一个全文搜索框架,而不是应用产品.因此它并不像 ...
- Spring的工作原理核心组件和应用
Spring框架 Spring 是管理多个java类的容器框架,注意是类不管理接口. Spring 的主要功能 Ioc 反转控制和 DI 依赖注入. 注入的方式可以是构造函数赋值也可以是 set方法赋 ...
- testng xml 示例
TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.php 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...
- Direct3D 11的资源
资源(Resource) 如果把渲染流水线比喻成汽车装配线,资源就是流水线上需要输入的东西. 资源可分为两类:Textures(纹理)和Buffers(缓冲区). Textures可以简单地分为1维, ...
- HDU 3622 Bomb Game(2-sat)
HDU 3622 Bomb Game 题目链接 题意:求一个最大半径,使得每一个二元组的点任选一个,能够得到全部圆两两不相交 思路:显然的二分半径,然后2-sat去判定就可以 代码: #include ...
- LinkedList : 双向链表与实现
所谓双向链表: (由此图可见老夫深厚的画功) 链表,就是由一个一个的节点连接组成. 在这里,每一个节点都是由三部分组成:上一个节点.当前节点的元素.下一个节点 当链表中只有一个节点的时候,这个节点指向 ...