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. jquery iframe父子框架中的元素访问方法

    在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或者在iframe框架中使用父窗口的元素 js 在父窗口中获取iframe中的元素 1. 格式:window. ...

  2. Java http请求工具类

    该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...

  3. 大型运输行业实战_day15_1_全文检索之Lucene

    1.引入 全文检索简介: 非结构化数据又一种叫法叫全文数据.从全文数据(文本)中进行检索就叫全文检索. 2.数据库搜索的弊端 案例 :     select  *  from product  whe ...

  4. 联想电脑 Wifi开关开不了

    "VirtualBox Host-Only Network" 没有有效的IP配置  未修复 自己电脑显示 控制面板>网络和Internet>网络连接 VirtualBo ...

  5. Android Studio: Application Installation Failed

    [Android Studio: Application Installation Failed] 参考:http://stackoverflow.com/questions/32718044/and ...

  6. HDU-1004.Let the ballon Rise(STL-map)

    2019-02-28-08:56:03 初次做本题是用字符串硬钢,最近校队训练时又遇到才知道用map是真的舒服.需要注意的是map的用法. clear : 清除map中的所有元素,map.clear( ...

  7. 20. Valid Parentheses (Stack)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. Android studio实现简单的CRUD

    1.打开Android studio ,创建项目DataBase01 2.进行UI设计,如图 目的:实现对姓名及其爱好的增删改查 3.创建DB.java,用于创建数据库,并做相关操作 在该目录下右键- ...

  9. HTML基础学习笔记(1)

    HTML学习笔记(1) 1.常用快捷键 win+d---返回桌面 win+e---我的电脑 win+r---打开运行 Alt+tab---切换软件 ctrl+tab---切换软件文档 F2---重命名 ...

  10. how2j网站前端项目——天猫前端(第一次)学习笔记3

    开始学习分类页面! 站长介绍说,这个项目一共有17个分类页面,每个分类页面的风格都是相似的:由分类图片. 查询.各种排序方式,产品列表.内容很多,拆成3部分学习:1.排序和价格 2.产品列表 3.交互 ...