【POJ2104】【HDU2665】K-th Number 主席树
【POJ2104】【HDU2665】K-th Number
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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
struct node
{
int ls,rs,siz;
}s[4000010];
struct NUM
{
int num,org;
}v[maxn];
int n,m,tot,nm;
int root[maxn],ref[maxn];
bool cmp1(NUM a,NUM b)
{
return a.num<b.num;
}
bool cmp2(NUM a,NUM b)
{
return a.org<b.org;
}
int readin()
{
int ret=0,f=1; char gc;
while(gc<'0'||gc>'9') {if(gc=='-')f=-f;gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void pushup(int x)
{
s[x].siz=s[s[x].ls].siz+s[s[x].rs].siz;
}
void insert(int &x,int &y,int l,int r,int p)
{
y=++tot;
if(l==r)
{
s[y].siz=s[x].siz+1;
return ;
}
int mid=l+r>>1;
if(p<=mid) s[y].rs=s[x].rs,insert(s[x].ls,s[y].ls,l,mid,p);
else s[y].ls=s[x].ls,insert(s[x].rs,s[y].rs,mid+1,r,p);
pushup(y);
}
int query(int x,int y,int l,int r,int p)
{
if(l==r) return ref[l];
int mid=l+r>>1;
if(s[s[y].ls].siz-s[s[x].ls].siz>=p) return query(s[x].ls,s[y].ls,l,mid,p);
return query(s[x].rs,s[y].rs,mid+1,r,p-s[s[y].ls].siz+s[s[x].ls].siz);
}
int main()
{
n=readin(),m=readin();
int i,a,b,c;
for(i=1;i<=n;i++) v[i].num=readin(),v[i].org=i;
sort(v+1,v+n+1,cmp1);
ref[0]=-1<<30;
for(i=1;i<=n;i++)
{
if(v[i].num>ref[nm]) ref[++nm]=v[i].num;
v[i].num=nm;
}
sort(v+1,v+n+1,cmp2);
for(i=1;i<=n;i++)
insert(root[i-1],root[i],1,nm,v[i].num);
for(i=1;i<=m;i++)
{
a=readin(),b=readin(),c=readin();
printf("%d\n",query(root[a-1],root[b],1,nm,c));
}
return 0;
}
【POJ2104】【HDU2665】K-th Number 主席树的更多相关文章
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- poj2104 K-th Number区间第k小值 主席树
原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...
- POJ2104 K-th Number[主席树]【学习笔记】
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 51440 Accepted: 17594 Ca ...
- [poj2104] K-th Number (主席树)
主席树 Description You are working for Macrohard company in data structures department. After failing y ...
- 主席树:POJ2104 K-th Number (主席树模板题)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44952 Accepted: 14951 Ca ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- 【poj2104】K-th Number 主席树
题目描述 You are working for Macrohard company in data structures department. After failing your previou ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
随机推荐
- apicloud教程2 (转载)
本帖最后由 中山赢友网络科技有限公司 于 2015-10-17 15:38 编辑 继<APICloud之小白图解教程系列(一):认识APICloud>之后的第二篇教程. 本篇教程有以下知识 ...
- Too Much Money
Too Much Money time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- aX+bY+cZ=n(非负整数解存在性)
题意: a*1234567+b*123456+c*1234=n 非负整数解得存在性. 题解: 看代码. #include<iostream> #include<cstdio> ...
- scull_p_read()函数分析
/* * Data management: read and write */ static ssize_t scull_p_read (struct file *filp, char __user ...
- GameUnity 2.0 发布倒计时
万众期待的 gameunity 网络游戏框架 已经完成了,现在在最后的检验调试阶段. 因为版本 改动非常之大,所以 版本号 从0.2版本 改成 2.0版本. gameunity事件部分,一如既往保持高 ...
- 更换arm-linux-gcc 4.3.2编译器
先创建一个临时目录:mcx@mcx-virtual-machine:/home/work/tools$ mkdir tmp 解压到根目录:mcx@mcx-virtual-machine:/home/w ...
- CentOS下将自编译的Apache添加为系统服务
首先,先谈下对linux服务的理解 1,linux 服务运行方式: 脚本的方式运行,服务脚本存放位置/etc/rc.d/init.d/ 2,linux服务管理软件 chkconfig Red Hat公 ...
- 【啊哈!算法】算法7:Dijkstra最短路算法
上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图 ...
- 基于手机传感器数据使用 CNN 识别用户行为的 Tensroflow 实现
传感器数据集 这个项目使用了 WISDM (Wireless Sensor Data Mining) Lab 实验室公开的 Actitracker 的数据集. WISDM 公开了两个数据集,一个是在实 ...
- haar_adaboost_cascade阅读资料
1,AdaBoost中利用Haar特征进行人脸识别算法分析与总结1——Haar特征与积分图 2,浅谈 Adaboost 算法 3,浅析人脸检测之Haar分类器方法 4,http://wenku.bai ...