K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 44940   Accepted: 14946
Case Time Limit: 2000MS

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
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 first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
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

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

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大)的更多相关文章

  1. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

  2. POJ 2104 静态找区间第k大

    静态区间第k大的问题,往往可以利用主席树来解决 这是主席树的第一道题 主席树大概可以理解为在n个节点上都建立一棵线段树,但是想想会超出内存 每一个节点保存的线段树都记录当前整段前缀区间的信息 但是因为 ...

  3. 主席树区间第K大

    主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...

  4. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  5. K-th Number Poj - 2104 主席树

    K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...

  6. [poj 2104]主席树+静态区间第k大

    题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...

  7. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

  8. POJ 2104(K-th Number-区间第k大-主席树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 31790   Accepted: 9838 Cas ...

  9. zoj2112 主席树动态第k大 (主席树&&树状数组)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

随机推荐

  1. javascript 腾讯ABS云平台面试题及面试经历

    既然说到面试前端肯定是Javascript各种问,只好各种答. 面试题肯定离不了,最近热门的Vue.js,React.js,Angular.js,Gulp,Webpack还有各种Js问题,还有令人头痛 ...

  2. Mysql-5.7.21安装配置

    搞开发多年,其实MySql前前后后安装配置了无数次,但是每次都需要到网上搜教程,折腾半天才搞定,这次索性把整个过程全部记录下来,以便以后查阅. 下载 到MySql官网,导航找到DOWNLOADS> ...

  3. JAVA_SE基础——51.内部类

    在Java中,允许在一个类的内部定义类,这样的类称作内部类,这个内部类所在的类称作外部类.根据内部类的位置.修饰符和定义的方式可分为成员内部类.静态内部类.方法(局部内部类)内部类. 内部类:一个类定 ...

  4. Centos6.7下面配置vim及其插件

    Vim是在vi的基础上升级而来的,比vi更强大,提供代码补全,编译功能 [4]vim Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用 ...

  5. Python-模块使用-Day6

    Python 之路 Day6 - 常用模块学习 本节大纲: 模块介绍time &datetime模块randomossysshutiljson & picleshelvexml处理ya ...

  6. Mosquito集群模式

    参考链接: http://blog.csdn.net/z729685731/article/details/70142182 http://blog.csdn.net/yuhaiyang457288/ ...

  7. LDAP的用户需求

    使用LDAP(ApacheDS)构建统一认证服务(SSO单点登录)   构建团队协作的体系,需要涉及很多个系统,如SVN.Jenkins.Trac.Nexus等,而一般而言每个系统均有其用户体系,当我 ...

  8. Jetty入门(1-1)Jetty入门教程

    一.Jetty是什么? 1.Jetty 是一个Java语言编写的,开源的Servlet容器和应用服务器. Jetty 极度轻量级.高便携性.功能强大.灵活和扩展性好,而且支持各种技术如SPDY.Web ...

  9. OAuth2.0学习(1-2)OAuth2.0的一个企业级应用场景 - 新浪开放平台微博OAuth2.0认证

    http://open.weibo.com/wiki/%E9%A6%96%E9%A1%B5 开发者可以先浏览OAuth2.0的接口文档,熟悉OAuth2.0的接口及参数的含义,然后我们根据应用场景各自 ...

  10. spring8——AOP之Bean的自动代理生成器

    对于上篇博客http://www.cnblogs.com/cdf-opensource-007/p/6464237.html结尾处提到的两个问题,可以使用spring提供的自动代理生成器解决.自动代理 ...