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. 性能测试学习第十天_controller

    集合点设置 controller虚拟多个用户执行脚本启动步骤不一定同步,集合点在脚本的某处设置一个标记,当有虚拟用户运行到这个标记的时候,停下等待所有用户都达到这个标记,再一同进行下面的步骤.这样可以 ...

  2. 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)

    一.环境: Windows: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE Linux(C ...

  3. 菜鸟 学注册机编写之 “sha1”

    1. 首先运行程序随便输入用户与注册码如下图所示: 2.将程序载入OD, 下MessageBoxA函数断点, F9运行程序, 程序运行后随便输入用户名与注册码,点"OK"后断下,F ...

  4. 软件测试Lab 1 Junit and Eclemma

    首先安装eclipse 然后下载hamcrest-core-1.3.jar,下载地址:http://mvnrepository.com/artifact/org.hamcrest/hamcrest-c ...

  5. Eucalyptus常用查询命令

    前言: Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus)  ...

  6. webpack前端构建工具学习总结(二)之loader的使用

    Webpack 本身只能处理 JavaScript 模块,如果要处理其他类型的文件,就需要使用 loader 进行转换. Loader 可以理解为是模块和资源的转换器,它本身是一个函数,接受源文件作为 ...

  7. co-dialog弹出框组件-版本v2.0.0

    co-dialog theme 访问git:co-dialog 版本v2.0.0 主题2 coog.app('.theme2').use({ title: 'JUST CHECKING.', mess ...

  8. UVA 11040 Add bricks in the wall(线性组合)

    砖块上的数字最终都可以看作是最后一行的线性组合,独立变元最多9个. 这类题的一般做法,线性组合都可以列出方程然后高斯消元. 对于这道题,只要确定最后一行剩下的4个变量就好了,对于最后一行的j位置,它对 ...

  9. hdu-2112 HDU Today---dijkstra+标号

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2112. 题目大意: 求起点到终点的最短路 解题思路: 对地名进行编号即可 然后直接dijkstra算 ...

  10. Python02 变量

    变量 因为Python是弱变量类型编程语言,所以变量赋值不需要类型声明. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 变量 ...