题目描述

看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2,A3,...,AN,

现在有M个询问,每个询问都是Ai...Aj中第k小的数等于多少。

输入格式
第一行两个正整数N,M。
第二行N个数,表示序列A1,A2,...,AN。
紧着的M行,每行三个正整数i,j,k(k≤j-i+1),表示

询问Ai...Aj中第k小的数等于多少。

输出格式

共输出M行,第i行输出第i个询问的答案。

样例输入1:

4 3

4 1 2 3

1 3 1

2 4 3

1 4 4

样例输出1:
1
3
4
样例输入2:
5 5
4 2 9 9 10
1 3 1
2 4 3
1 4 4
3 5 2
2 5 2
样例输出2:
2
9
9
9
9
注释:
询问区间的第k小值并非严格第k小,例如样例2中第4个询问,询问3到5中第2小的数,
答案输出9,并不是严格第2小的10。
数据范围:
在50%的数据中,1<=N<=10000,1<=M<=10000,A[i]<=100000;
在100%的数据中,1<=N<=100000,1<=M<=100000,A[i]<=1000000;
主席树求静态区间k大值模板

屠龙宝刀点击就送

#include <algorithm>
#include <cstdio>
#define N 100500
using namespace std;
struct cmt
{
int l,r,size;
}tr[N*];
int n,m,rt[N],tot,a[N],b[N],size;
int build(int l,int r)
{
int now=++tot;
tr[now].size=;
if(l==r) return now;
int mid=(l+r)>>;
tr[now].l=build(l,mid);
tr[now].r=build(mid+,r);
return now;
}
inline int Rank(int x)
{
return lower_bound(b+,b++size,x)-b;
}
void update(int l,int r,int x,int &y,int t)
{
y=++tot;
tr[y].size=tr[x].size+;
if(l==r) return;
tr[y].l=tr[x].l;
tr[y].r=tr[x].r;
int mid=(l+r)>>;
if(t<=mid) update(l,mid,tr[x].l,tr[y].l,t);
else update(mid+,r,tr[x].r,tr[y].r,t);
}
int ask(int l,int r,int lx,int rx,int k)
{
if(l==r) return l;
int mid=(l+r)>>;
if(tr[tr[rx].l].size-tr[tr[lx].l].size>=k) return ask(l,mid,tr[lx].l,tr[rx].l,k);
else {k-=tr[tr[rx].l].size-tr[tr[lx].l].size;return ask(mid+,r,tr[lx].r,tr[rx].r,k);}
}
int Main()
{
freopen("kth.in","r",stdin);
freopen("kth.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i) scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n);
size=unique(b+,b++n)-b-;
rt[]=build(,size);
for(int i=;i<=n;++i) update(,size,rt[i-],rt[i],Rank(a[i]));
for(int l,r,k;m--;)
{
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[ask(,size,rt[l-],rt[r],k)]);
}
return ;
}
int sb=Main();
int main(int argc,char *argv[]) {;}

COGS 930. [河南省队2012] 找第k小的数的更多相关文章

  1. COGS 930. [河南省队2012] 找第k小的数 主席树

    主席树裸板子 #include<cstdio> #include<iostream> #include<algorithm> #define MAXN 100005 ...

  2. 【COGS 1534】 [NEERC 2004]K小数 &&【COGS 930】 [河南省队2012] 找第k小的数 可持久化01Trie

    板子题,只是记得负数加fix最方便 #include <cstdio> ,N=; namespace FIFO { <<],*S=B,*T=B; #define getc() ...

  3. [河南省队2012] 找第k小的数

    ★★☆   输入文件:kth.in   输出文件:kth.out   简单对比时间限制:1 s   内存限制:128 MB 题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2 ...

  4. [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  5. cogs930找第k小的数(k-th number)

    cogs930找第k小的数(k-th number) 原题链接 题解 好题... 终极版是bzoj3065(然而并不会) 先讲这个题... 维护\(n+1\)个值域线段树(用主席树),标号\(0\) ...

  6. [LeetCode] 719. Find K-th Smallest Pair Distance 找第K小的数对儿距离

    Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...

  7. 快速排序算法的实现 && 随机生成区间里的数 && O(n)找第k小 && O(nlogk)找前k大

    思路:固定一个数,把这个数放到合法的位置,然后左边的数都是比它小,右边的数都是比它大 固定权值选的是第一个数,或者一个随机数 因为固定的是左端点,所以一开始需要在右端点开始,找一个小于权值的数,从左端 ...

  8. 17082 两个有序数序列中找第k小

    17082 两个有序数序列中找第k小 时间限制:1000MS  内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题   语言: 无限制 Description 已知两个已经排好序(非减 ...

  9. 找第k大的数

    (找第k大的数) 给定一个长度为1,000,000的无序正整数序列,以及另一个数n(1<=n<=1000000),接下来以类似快速排序的方法找到序列中第n大的数(关于第n大的数:例如序列{ ...

随机推荐

  1. 面试6-----11 const和静态变量那些事儿

    6 看看const和指针的那些事儿 const在int*左边 const在int*右边 const在int*两边------>请看代码注释 (1)代码 #include <stdio.h& ...

  2. CodeForces - 377A Maze BFS逆思维

    Maze Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, ...

  3. 福建工程学院第十四届ACM程序设计大赛 - E - 外传:小晋逃生记

    http://www.fjutacm.com/Contest.jsp?cid=705#P4 其实想清楚了就很简单,之前想了很多种方法,以为是二分什么的,看起来就像是一个单峰函数.但是发现直接暴力一波就 ...

  4. tableView刷新指定的cell 或section和滚动到指定的位置

    转自:http://blog.csdn.net/tianyou_code/article/details/54426494 //一个section刷新 NSIndexSet *indexSet=[[N ...

  5. 数据结构关于AOV与AOE网的区别

    AOV网,顶点表示活动,弧表示活动间的优先关系的有向图. 即如果a->b,那么a是b的先决条件. AOE网,边表示活动,是一个带权的有向无环图, 其中顶点表示事件,弧表示活动,权表示活动持续时间 ...

  6. POJ3268【最短路】

    题意: n个点m条有向边,每个点有一头牛,每头牛会沿着各自的最短路先到x点,然后又从x点到各自的点,求这些牛中间最短路程最大的牛. 思路: 从x点到各点的最短路不用说了,裸的最短路: 但是从所有点到x ...

  7. IT兄弟连 JavaWeb教程 JSP内置对象3

    读取web.xml配置信息的config对象 config对象主要用于取得服务器的配置信息.通过pageContxt对象的getServletConfig()方法获取一个config对象.当一个Ser ...

  8. linux 和windows 的定时任务

    linux http://www.cnblogs.com/thinksasa/archive/2013/06/06/3121030.html windows http://www.myhack58.c ...

  9. st表求区间最大值

    Input 第一行给出一个数字N,接下来N+1行,每行给出一个数字Ai,(0<=i<=N<=1E6)接来给出一个数字Q(Q<=7000),代表有Q个询问每组询问格式为a,b即询 ...

  10. unicode官网 unicode码表和标准下载