POJ 2014.K-th Number 区间第k小 (归并树)
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 57543 | Accepted: 19893 | |
| Case Time Limit: 2000MS | ||
Description
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
Hint
#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=5e5+,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)
{
///cout<<l<<" "<<r<<endl;
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-;
}
///cout<<cou+1<<endl;
return cou+1;
}
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 n,q;
scanf("%d%d",&n,&q);
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=-;
while(L<=R)
{
int mid=(L+R)/;
int w=sign[mid].num;
///cout<<"x="<<w<<endl;
if((query(l,r,w,1,n,1))>=k) R=mid-1,ans=mid;
else L=mid+;
}
cout<<sign[ans].num<<endl;
}
return ;
}
归并树
POJ 2014.K-th Number 区间第k小 (归并树)的更多相关文章
- POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)
题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- HDU 2665.Kth number 区间第K小
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ2104 K-th Number —— 区间第k小 整体二分
题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit: 20000MS Memory Limit: 65536K Tota ...
- BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 418 Solved: 235 [ Submit][ ...
- POJ-2104-K-th Number(区间第K大+主席树模板题)
Description You are working for Macrohard company in data structures department. After failing your ...
- poj2104 K-th Number区间第k小值 主席树
原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- 【POJ】2104 K-th Number(区间k大+主席树)
http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...
随机推荐
- Real Time Rendering 2
[Real Time Rendering 2] 1.The light vector l is usually defined pointing in a direction opposite to ...
- dns隧道 dns2tcpd
有些网络的防火墙设置只允许端口53的UDP流量,就不能通过tcp搭建隧道,这种情况下我们可以通过UDP搭建DNS隧道,通过搭建一个DNS服务器委派的子域,这个子域因为使我们自己搭建的主机,这时候就可以 ...
- 安装scrapy框架
前提安装好python.setuptools. 1.安装Python 安装完了记得配置环境,将python目录和python目录下的Scripts目录添加到系统环境变量的Path里.在cmd中输入py ...
- Laravel5.1 与 Laypage 结合进行分页
demo地址:http://lara.ytlwin.top/orm 路由 Route::match(array('get','post'),'/orm','StuController@orm'); 控 ...
- 【scrapy】其他问题2
今天爬取豆瓣电影的是时候,出现了两个问题: 1.数据无法爬取并输出Retrying <GET https://movie.douban.com/robots.txt> 看起来像是被拦截了. ...
- pta l1-49(天梯赛座位分配)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805081289900032 题意:给定n个高校,每个高校m[i ...
- FPGA基础知识1
1.乘法 在FPGA中,乘法运算可以分为 1)信号与信号之间的运算,用乘法器核实现: 2)常数与信号之间的运算,利用移位及加减法实现. A x 16 = A左移4位: A x 20 = A x 16 ...
- QT信号与槽
参考: 简单例子: http://www.cnblogs.com/MuyouSome/p/3515941.html 基本了解: http://blog.csdn.net/harbinzju/artic ...
- Wannafly挑战赛14 C.可达性(tarjan缩点)
题目描述 给出一个 0 ≤ N ≤ 105 点数.0 ≤ M ≤ 105 边数的有向图, 输出一个尽可能小的点集,使得从这些点出发能够到达任意一点,如果有多个这样的集合,输出这些集合升序排序后字典序最 ...
- python 中 类型转换 bytes
https://www.cnblogs.com/sesshoumaru/p/5980090.html