您的位置:首页 > 其它

POJ 2104:K-th Number(主席树)

2016-10-08 10:41 429 查看
K-th Number

Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 50411 Accepted: 17188
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


问题概述:n个数,q次查询,每次查询(l,r,k)表示查询区间[l,r]内第k小的数是多少,对于每次查询,输出答案

(http://poj.org/problem?id=2104)

主席树:可持久化线段树/函数式线段树(http://blog.csdn.net/regina8023/article/details/41910615)

实质:建n棵线段树,第i棵线段树包含前i个元素的信息

内存占用过大怎么办?多多利用之前的节点,这样总内存占用就不是n²而是大概nlogn

目的:查询区间第k大,某个数在区间[l,r]内出现的次数等

代码中有题解

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n, m, size, tot, b[100005], t[100005], a[100005];
typedef struct
{
int l, r;
int size;		/*这个节点所存信息量大小(数的个数)*/
}Ctree;
Ctree s[3000005];
int Build(int l, int r);
int Update(int root, int x);
int Query(int lx, int rx, int k);
int main(void)
{
int i, l, r, k;
while(scanf("%d%d", &n, &m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d", &a[i]);
memcpy(b, a, sizeof(a));
sort(b+1, b+n+1);
size = unique(b+1, b+1+n)-(b+1);		/*排序+去重(离散化)*/
for(i=1;i<=n;i++)
a[i] = lower_bound(b+1, b+1+size, a[i])-b;		/*找到元素a[i]为当前第几小*/
t[0] = Build(1, size);	/*初始化一个空树*/
for(i=1;i<=n;i++)
t[i] = Update(t[i-1], a[i]);	/*建n棵线段树,每棵线段树都比上一棵多1个元素,t[i]表示第i棵树的根节点*/
while(m--)		/*↑传入上一棵树的根节点*/
{
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", b[Query(t[l-1], t[r], k)]);		/*通过前l-1个数的情况和前r个数的情况即可计算答案*/
}
}
return 0;
}

int Build(int l, int r)
{
int m, root;
root = ++tot;
m = (l+r)/2;
s[root].size = 0;
if(l==r)
return root;
s[root].l = Build(l, m);
s[root].r = Build(m+1, r);
return root;
}

int Update(int root, int x)
{
int now, tmp, l, r, m;
tmp = now = ++tot;		/*tot为当前根节点的编号*/
l = 1, r = size;
while(l<r)
{
s[now].size = s[root].size+1;
m = (l+r)/2;
if(x<=m)			/*如果要添加的新元素在该节点的左区间*/
{
s[now].l = ++tot;		/*新建左节点*/
s[now].r = s[root].r;		/*充分利用原来相同的结点*/
root = s[root].l;		/*"指针"游历*/
now = tot;
r = m;
}
else			/*同上*/
{
s[now].l = s[root].l;
s[now].r = ++tot;
root = s[root].r;
now = tot;
l = m+1;
}
}
s[now].size = s[root].size+1;
return tmp;
}

int Query(int lx, int rx, int k)	/*通过两棵树的信息量差判断第k小的数*/
{
int l, r, m;
l = 1, r = size;
while(l<r)
{
m = (l+r)/2;
if(s[s[rx].l].size-s[s[lx].l].size>=k)
{
r = m;
lx = s[lx].l;
rx = s[rx].l;
}
else
{
l = m+1;
k -= s[s[rx].l].size-s[s[lx].l].size;
lx = s[lx].r;
rx = s[rx].r;
}
}
return r;	/* return l; */
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: