poj 2104 主席树(区间第k大)
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 44940 | Accepted: 14946 | |
| 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
因为保存的是前i个数的情况,求[a,b]区间时,只需要T[b]-T[a-1]
/*
poj2104
主席树-求解区间第k大
hhh-2016-02-18 13:09:24
*/ #include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std;
const int maxn = 100010;
int tot;
int n,m;
int a[maxn],t[maxn];
int T[maxn],lson[maxn*30],rson[maxn*30],c[maxn*30]; int build(int l,int r)
{
int root = tot++;
if(l != r)
{
int mid = (l+r)>>1;
lson[root] = build(l,mid);
rson[root] = build(mid+1,r);
}
return root;
} void ini_hash() //排序去重
{
for(int i = 1;i <= n;i++)
t[i] = a[i];
sort(t+1,t+n+1);
m = unique(t+1,t+n+1)-(t+1);
} int Hash(int x) //得到位置
{
return lower_bound(t+1,t+m+1,x)-t;
} //如果那里发生改变则新建一个节点而非像平常修改那个节点的值
int update(int root,int pos,int val)
{
int newroot = tot++;
int tmp = newroot;
c[newroot] = c[root] + val;
int l = 1,r = m;
while(l < r)
{
int mid = (l+r)>>1;
if(pos <= mid)
{
lson[newroot] = tot++;rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
lson[newroot] = lson[root]; rson[newroot] = tot++;
newroot = rson[newroot]; root = rson[root];
l = mid+1;
}
c[newroot] = c[root] + val;
}
return tmp;
} int query(int lt,int rt,int k)
{
int l = 1, r = m;
while(l < r)
{
int mid = (l+r)>>1;
if(c[lson[rt]] - c[lson[lt]] >= k)
{
r = mid;
lt = lson[lt];
rt = lson[rt];
}
else
{
l = mid+1;
k -= (c[lson[rt]] - c[lson[lt]]);
lt = rson[lt];
rt = rson[rt];
}
}
return l;
} int main()
{
int q;
while(scanf("%d%d",&n,&q) !=EOF)
{
tot = 0;
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
ini_hash();
T[0] = build(1,m);
for(int i = 1;i <= n;i++)
{
int pos = Hash(a[i]);
T[i] = update(T[i-1],pos,1);
}
while(q--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",t[query(T[l-1],T[r],k)]);
}
}
return 0;
}
poj 2104 主席树(区间第k大)的更多相关文章
- 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大
静态区间第k大的问题,往往可以利用主席树来解决 这是主席树的第一道题 主席树大概可以理解为在n个节点上都建立一棵线段树,但是想想会超出内存 每一个节点保存的线段树都记录当前整段前缀区间的信息 但是因为 ...
- 主席树区间第K大
主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...
- HDU 6278 主席树(区间第k大)+二分
Just h-index Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)To ...
- K-th Number Poj - 2104 主席树
K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...
- [poj 2104]主席树+静态区间第k大
题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...
- POJ 2104 求序列里第K大 主席树裸题
给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...
- POJ 2104(K-th Number-区间第k大-主席树)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 31790 Accepted: 9838 Cas ...
- zoj2112 主席树动态第k大 (主席树&&树状数组)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
随机推荐
- Centos7安装openvpn及客户端配置
1.openvpn介绍 VPN直译就是虚拟专用通道,是提供给企业之间或者个人与公司之间安全数据传输的隧道,使用OpenSSL加密库中的SSLv3/TLSv1协议函数库. 目前OpenVPN能在Sola ...
- Linux进程管理:查杀进程
一.查看进程 Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区别呢? 两者没太大差别,讨论这个问题,要追溯到Unix系统中的两种风格,System V风格 ...
- OrientDB入门(1)Getting Started
Running OrientDB the First Time First, download and extract OrientDB by selecting the appropriate pa ...
- ssh整合之六管理我们的配置文件
1.我们的ssh搭建已经搭建好了,之前是纯xml方式,而且我们的配置文件,是一个框架一个配置文件.这样的话,配置文件中的内容就会很多,这样以后修改起来也会很麻烦,因 此,我们尝试着把这些 ...
- Java8新特性第1章(Lambda表达式)
在介绍Lambda表达式之前,我们先来看只有单个方法的Interface(通常我们称之为回调接口): public interface OnClickListener { void onClick(V ...
- CMDB资产采集
Agent(方式) 1:服务器每台都需要安装Agent 达到采集速度快,简单:造成性能损耗 获取每台服务器的资产并有返回值:v=subprocess.getoutput('dir')或者ipconfi ...
- nginx nfs服务
一.nginx服务 1.二进制安装nginx包 [root@bogon ~]# ls /etc/yum.repos.d/ [root@bogon ~]# cd /etc/yum.repos.d/ [r ...
- 如何在node和vue前后端分离的项目中使用极客验证,用node的方式
1.用express的脚手架和vue-cli的脚手架搭建的项目目录如下图 2.在vue-client的src/component新建一个login.vue文件,作为登录页面,代码如下 <temp ...
- python常见异常
- 字符串分割方法split()函数
>>> data = '1000,小甲鱼,男'>>> data.split(',')['1000', '小甲鱼', '男'] str.split('以什么为标志进行 ...