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. IE6/7下同级只有一个元素浮动,会换行问题

    .myDiv { background-color: red; width: 200px; height: 200px; padding: 10px; } .div1 { background-col ...

  2. DataBinding初探 数据绑定的用法 ,import 集合类型,绑定的表达式,访问集合类型2

    数据绑定的用法 import语法   <data> <import type="android.view.view"/> </data>   如 ...

  3. 基于android-uitableview扩展-uilistview项目

    这个项目是正如标题说的那样,是基于uitableview项目为基础进行二次封装的,目的是实现更多的展现形式,项目地址:点击打开 不过,这个使用起来你还必须得会用uitableview扩展(项目地址:点 ...

  4. Java中的for循环——通过示例学习Java编程(9)

      作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=21 循环用于反复执行同一组语句,直到满足特定条件 ...

  5. 分享到qq空间等代码

    <!-- Baidu Button BEGIN --><div id="bdshare" class="bdshare_t bds_tools get- ...

  6. swift 2特性记录

    swift 团队一直在优化,让大家准备在秋天的时候,迁移到swift2做准备. 一.错误处理 异常处理,不是NSError对象和双指针. 可以使用 throws   来指定方法来抛出一个错误. 调用d ...

  7. leetcode 355 Design Twitte

    题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...

  8. Unity光晕剑效果的Shader简单实现

    最近遇到了一个需求,想要一种在刀剑上带有光晕的酷炫效果,甚至是还想要有闪烁呼吸效果,于是就写了一个简单的叫LightSwrod的Shader去实现,先上图看看效果吧. 简单展示 这是剑本身的样子 这是 ...

  9. POJ - 3111 K Best(二分)

    包含一些ai和bi的集用S来表示,x = max(sigma(ai)/sigma(bi),i 属于S) ,k 表示S的大小,k= |S|. x和k之间具有单调性.k0 < k1 → x0 ≥ x ...

  10. RHEL7 本地yum源配置

    配置yum 源 1.挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别  [root@ mnt]# mount /dev/cdrom /mnt 2.在目录/etc/yum.r ...