[Poj2761]Feed the dogs(主席树)
Desciption
题意:求区间第K小(N<=100000)
Solution
主席树模板题
Code
#include <cstdio>
#include <algorithm>
#define N 100010
using namespace std;
int n,m,A[N],rank[N],T[N],ls[N*20],rs[N*20],sum[N*20],tot;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void update(int last,int p,int l,int r,int &rt){
rt=++tot;
ls[rt]=ls[last],rs[rt]=rs[last],sum[rt]=sum[last]+1;
if(l==r) return;
int m=(l+r)>>1;
if(p<=m) update(ls[last],p,l,m,ls[rt]);
else update(rs[last],p,m+1,r,rs[rt]);
}
int Query(int ss,int tt,int l,int r,int k){
if(l==r) return l;
int m=(l+r)>>1;
int d=sum[ls[tt]]-sum[ls[ss]];
if(k<=d) return Query(ls[ss],ls[tt],l,m,k);
else return Query(rs[ss],rs[tt],m+1,r,k-d);
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;++i) A[i]=rank[i]=read();
sort(rank+1,rank+n+1);
int cnt=unique(rank+1,rank+n+1)-(rank+1);
for(int i=1;i<=n;++i) A[i]=lower_bound(rank+1,rank+cnt+1,A[i])-rank;
for(int i=1;i<=n;++i) update(T[i-1],A[i],1,cnt,T[i]);
while(m--){
int l=read(),r=read(),k=read();
printf("%d\n",rank[Query(T[l-1],T[r],1,cnt,k)]);
}
return 0;
}
[Poj2761]Feed the dogs(主席树)的更多相关文章
- poj 2761 Feed the dogs (treap树)
/************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...
- 【莫队算法】【权值分块】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(平衡树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(主席树||划分树)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 16860 Accepted: 5273 De ...
- 【poj2761】 Feed the dogs
http://poj.org/problem?id=2761 (题目链接) 题意 求区间第K大. Solution 和poj2104一模一样. 主席树代码 // poj2761 #include< ...
- 划分树---Feed the dogs
POJ 2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to fee ...
随机推荐
- asp.net MVC 中枚举创建下拉列表?
我将尝试使用 Html.DropDownList 扩展方法,但不能找出如何使用它的枚举. 让我们说我有一个这样的枚举: public enum ItemTypes { Movie = 1, Game ...
- 在sublime text3下,用快捷键把文件打开到浏览器中
使用背景 在编辑html或者js文件的时候,是否想在浏览器中预览一下, 你的步骤可能是这样的: 找到编辑文件的位置, 右键使用某一浏览器打开.如果是这样,你就out了, 因为在sublime中有更加简 ...
- hadoop集群搭建简要记录
2019/03/09 21:46 准备4台服务器或者虚拟机[centos7],分别设置好静态ip[之所以设置静态ip主要就是为了省心!!!][ centos7下面配置静态IP 参考地址: https ...
- 使用AOP监控用户操作并插入数据库
引入依赖 <!--spring切面aop依赖--> <dependency> <groupId>org.springframework.boot</group ...
- 常见的生成全局唯一id有哪些?他们各有什么优缺点?
分布式系统中全局唯一id是我们经常用到的,生成全局id方法由很多,我们选择的时候也比较纠结.每种方式都有各自的使用场景,如果我们熟悉各种方式及优缺点,使用的时候才会更方便.下面我们就一起来看一下常见的 ...
- springboot 学习笔记(六)
(六)springboot整合activemq 1.现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bi ...
- 使用CSS设置Chrome打印背景色
以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...
- 如何使用cPanel管理域名和数据库
cPanel是一个基于web的基于web的控制面板,它简化了许多常见的系统管理任务,如网站创建.数据库部署和管理等.本指南向您展示了如何使用cPanel用户帐户管理域和数据库.所有这些指令都与位于端口 ...
- JSON.parse()与JSON.stringify()
JSON.parse() 将字符串转成JSON 举个例子 var str = '{"name":"cn","age":"2&quo ...
- 【BZOJ1562】[NOI2009] 变换序列(匈牙利算法)
点此看题面 大致题意: 给你一个长度为\(n\)的序列\(D\),让你找到一个字典序最小的\(n\)的排列\(T\),满足\(D_i=min(|T_i-i|,n-|T_i-i|)\). 建图 我想建图 ...