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. 用R理解统计学

    1.随机变量( random variable)概念的引入 该数据来自杰克逊实验室.2组数据,每组12只老鼠,一组普通食物,另一组高脂肪(hf)饮食.几周后,科学家们称了每只老鼠的体重,得到了这个数据 ...

  2. Real Time Rendering 2

    [Real Time Rendering 2] 1.The light vector l is usually defined pointing in a direction opposite to ...

  3. Building Projects with Native Code

    [Building Projects with Native Code] 1.安装Node(v4.0以上).Python2.JDK(v8.0以上). 添加 JAVA_HOME环境变量,指向 JDK 的 ...

  4. java正则验证

    String regex ="[A-Z]{4}[0-9]{7}"; List<String> non= new ArrayList<String>(); f ...

  5. 第八篇:Jmeter的分布式测试

    一: 由于Jmeter本身的瓶颈,当模拟数以千计的用户并发的时候,使用单台机器会有些力不从心,甚至还会引起Java内存溢出的错误,要解决这个问题,就要使用分布式测试,运行多台机器,也就是所谓的Agen ...

  6. css3修改滚动条样式

    /*滚动条整体样式*/ /*高宽分别对应横竖滚动条的尺寸*/ .content-box::-webkit-scrollbar{ width: 4px; height: 4px; } /*滚动条里面小方 ...

  7. pta l2-16(愿天下有情人都是失散多年的兄妹)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 题意:两个异性的人五服之内不得通婚 ...

  8. Unity之Application.runInBackground = true

    默认是False, 设置 Application.runInBackground = true; 则 void OnApplicationPause(bool pause) 不再起作用

  9. vue滚动行为控制——页面跳转返回上一个页面保留滚动位置

    需求分析: 一般这个功能在后台管理系统用的比较多,因为后台页面都是在当前页面打开,对于某些列表筛选页,如果列表数据比较多,页面就会滚动.当页面发生滚动,对列表数据进行查看或者编辑的时候,跳转到下一级页 ...

  10. Djang的model创建的字段和参数复习

    class test_orm(models.Model): id = models.AutoField(primary_key=True) # int自增列,必须填入参数primary_key=Tru ...