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(主席树)的更多相关文章

  1. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  2. 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs

    先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...

  3. [POJ2761] Feed the dogs (Treap)

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

  4. [POJ2761]Feed the dogs

    Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...

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

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

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

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

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

  8. 【poj2761】 Feed the dogs

    http://poj.org/problem?id=2761 (题目链接) 题意 求区间第K大. Solution 和poj2104一模一样. 主席树代码 // poj2761 #include< ...

  9. 划分树---Feed the dogs

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

随机推荐

  1. JAVA实现向文本文件中多次写入数据

    使用这种方式,当指定的文件不存在时会自动创建. //此处设置为true即可追加 FileWriter out = new FileWriter("D:\\1.txt",true); ...

  2. android 开发-HttpClient状态码定义

    TP 定义的状态代码的值(.net HttpWebResponse.HttpStatusCode 成员名称 说明 Continue 等效于 HTTP 状态 100.Continue 指示客户端可能继续 ...

  3. springboot 学习笔记(八)

    springboot整合activemq,实现queue,topic同时支持 1.JMS中定义了两种消息模型:点对点(point to point, queue)和发布/订阅(publish/subs ...

  4. Java面向对象的练习。动物乐园

    本次项目是:以面向对象的思想设计动物乐园系统. 动物乐园中有猫,狗,鸭子等成员,还可以增加新成员. 猫和鸭子都有自己的名字,都有自己的腿,但腿的条数不同,猫和鸭子会发出叫声,猫的叫声是:喵喵喵……,鸭 ...

  5. 如何进行大规模在线数据迁移(来自Stripe公司的经验)

    首发于笔者的微信公众号:技术心流FollowFlows 原文地址:Stripe Engineering Blog   各工程团队常面临一项共同挑战:重新设计数据模型以支持清晰准确的抽象和更复杂的功能. ...

  6. mysql通过event和存储过程实时更新简单Demo

    今天想稍微了解一下存储过程和EVENT事件,最好的方法还是直接做一个简单的demo吧 首先可以在mysql表中创建一个users表 除了设置一些username,password等必要字段以外还要设立 ...

  7. WPF中的拖放1

    实现了WPF的不同层级间的元素之间的拖放,例子虽小却很经典,引申一下也许可以实现类VS界面的浮动依靠面板. 拖放前: 拖放后: 代码如下: <Window x:Class="WpfAp ...

  8. 用C#来控制高级安全Windows防火墙

    有的时候我们需要在自己的产品中检测<高级安全Windows防火墙>的状态,并有可能需要加入一些规则甚至需要关闭掉高级安全Windows防火墙. 下面就告诉如何来做: <高级安全Win ...

  9. django Q条件

    #q条件from django.db.models import Qq = Q(name__startswith="p") | Q(name__startswith="l ...

  10. 逐步解读String类(一)

    一句题外话 面试刚入行的Java新手,侧重基础知识:面试有多年工作经验的老鸟,多侧重对具体问题的解决策略. 从一类面试题说起 考察刚入行菜鸟对基础知识的掌握程度,面试官提出关于String类的内容挺常 ...