Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11394    Accepted Submission(s): 3465

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases. 
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere. 
The second line contains n integers, describe the sequence. 
Each of following m lines contains three integers s, t, k. 
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10
1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
Source
题意:求区间第K小(区间升序第k个数)
思路:归并树。二分答案x。判断不超过x的数在区间内多少个。假设x是区间第k小,那么在区间内不超过x的数不少于k个。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
using namespace std;
#define PI acos(-1.0)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e13+;
inline int get_int()
{
int num=;
char ch;
while((ch=getchar())!=' '&&ch!='\n')
num=num*+(ch-'');
return num;
}
/****************************/
struct edge
{
int from,to;
int cost;
};
edge es[maxm];
struct node
{
int num;
int k;
};
node sign[maxn];
int a[maxn];
int cmp(node x,node y)
{
return x.num<y.num;
}
vector<node>tree[maxn<<];
void build(int l,int r,int pos)
{
if(l==r) return;
int mid=(l+r)/;
for(int i=; i<tree[pos].size(); i++)
{
int k=tree[pos][i].k;
if(l<=k&&k<=mid) tree[pos<<].push_back(tree[pos][i]);
else tree[pos<<|].push_back(tree[pos][i]);
}
build(l,mid,pos<<);
build(mid+,r,pos<<|);
}
int query(int L,int R,int w,int l,int r,int pos)
{
if(L<=l&&r<=R)
{
int s=,e=tree[pos].size()-;
int cou=-;
while(s<=e)
{
int md=(s+e)/;
if(tree[pos][md].num<=w) s=md+,cou=md;
else e=md-;
}
return cou+;
}
int mid=(l+r)/;
int ans=;
if(L<=mid) ans+=query(L,R,w,l,mid,pos<<);
if(R>mid) ans+=query(L,R,w,mid+,r,pos<<|);
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=; i<=*n; i++) tree[i].clear();
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
sign[i].num=a[i],sign[i].k=i+;
}
sort(sign,sign+n,cmp);
for(int i=; i<n; i++) tree[].push_back(sign[i]);
build(,n,);
while(q--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int L=,R=n-;
int ans=n-;
while(L<=R)
{
int mid=(L+R)/;
int w=sign[mid].num;
if((query(l,r,w,,n,))>=k) R=mid-,ans=mid;
else L=mid+;
}
printf("%d\n",sign[ans].num);
}
}
return ;
}

区间第K小

HDU 2665.Kth number 区间第K小的更多相关文章

  1. POJ 2014.K-th Number 区间第k小 (归并树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57543   Accepted: 19893 Ca ...

  2. POJ2104 K-th Number —— 区间第k小 整体二分

    题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit: 20000MS   Memory Limit: 65536K Tota ...

  3. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

  4. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  5. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  6. HDU 2665 Kth number(主席树静态区间第K大)题解

    题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...

  7. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  8. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

  9. HDU 2665 Kth number(可持续化线段树)

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

随机推荐

  1. table tr 加入背景色之后 去掉td之间的空隙

    给table加样式  border-collapse:collapse;

  2. Retrofit 打印log时,中文会显示类似%E8%BE%BD字符

    https://blog.csdn.net/honghailiang888/article/details/54289632?utm_source=blogxgwz6 参照Android Retrof ...

  3. git 使用说明

    git使用的一些说明 关于新用户1. 到优居客群里下载git客户端2. 安装之后打开git bash3. 生成密钥,执行如下命令:    ssh-keygen -t rsa -C "你的邮箱 ...

  4. FP增加的索引

    1.优化FP_BOM中第839行执行过慢问题,且会出现ORA-01652: 无法通过 128 (在表空间 STGTEMP 中) 扩展 temp 段ORA-06512: 在 "STG.FP_B ...

  5. tomcat启动时引用非JAVA_HOME的指定路径

    参考 https://jingyan.baidu.com/article/066074d62d371cc3c21cb0ec.html 先查看bin/catalina.bat 再查看bin/setcla ...

  6. codeblocks安装(自带gcc编译器)

    下载安装自带c编译器的的codeblocks. 网址:http://www.codeblocks.org/downloads/26 自带gcc编译器的版本 codeblocks-16.01mingw- ...

  7. django的优缺点(非原创)

    Django 大包大揽,用它来快速开发一些 Web 应用是不错的.如果你顺着 Django 的设计哲学来,你会觉得 Django 很好用,越用越顺手:相反,你如果不能融入或接受 Django 的设计哲 ...

  8. css控制div上浮下落

    CSS3 示例:http://www.w3school.com.cn/cssref/pr_keyframes.asp 以下是代码: <!DOCTYPE html> <html> ...

  9. [leetcode]428. Serialize and Deserialize N-ary Tree序列化与反序列化N叉树

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. SeekBar

    <SeekBar android:id=”@+id/seek”android:layout_width=”match_parent”android:layout_height=”wrap_con ...