您的位置:首页 > 编程语言

POJ2104 K-th Number(划分树和主席树代码)

2015-06-12 09:26 363 查看

K-th Number

Time Limit: 20000MSMemory Limit: 65536K
Total Submissions: 41198Accepted: 13469
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

Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
Source
Northeastern Europe 2004, Northern Subregion

题目大意:

给定一个序列,然后再给定一个区间,区间内第k小的数。

解题思路:

本题可以用划分数,归并树,主席树来解,据说归并树再poj上过不了?主席树等写完博客再去学,今天学习了划分树,挺巧妙的,这东西。划分树可以说是线段树的变种吧。
用来求第k大类似的题目。

划分树:

推荐一个博客:/article/7071463.html,感觉划分树理解的难点在查询部分,建议大家还是要花时间好好理解下。

代码模版(划分树只适用于静态求第k大数):

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

const int N = 100005;
int a
,as
; //原数组,排序后的数组
int n,m;
int sum[20]
; //记录第i层的1~j划分到左子树的元素个数(包括j)
int tree[20]
; //记录第i层元素序列

void build(int c,int l,int r)
{
int i;
int mid = (l + r) >> 1;
int lm = mid - l + 1; //左边有几个
int lp = l;
int rp = mid + 1;
for(i=l;i<=mid;i++)
{
if(as[i] < as[mid])
{
lm--;
/*
先假设左边的(mid - l + 1)个数都等于as[mid],然后把实际小于的减去,剩下的就是等于的
*/
}
}
for(i=l;i<=r;i++)
{
if(i == l)
{
sum[c][i] = 0; //sum[i]表示[l,i]内有多少个分到左边,dp思想
}
else
{
sum[c][i] = sum[c][i-1];
}
if(tree[c][i] == as[mid]) //处理相等的情况
{
if(lm)
{
lm--; //相等的统计放在左子树
sum[c][i]++;
tree[c+1][lp++] = tree[c][i];
}
else //否则放在右子树
{
tree[c+1][rp++] = tree[c][i];
}
}
else if(tree[c][i] < as[mid]) //处理小于mid的情况
{
sum[c][i]++;
tree[c+1][lp++] = tree[c][i];
}
else //处理大于mid的情况,放在右子树
{
tree[c+1][rp++] = tree[c][i];
}
}
if(l == r) return;
build(c+1,l,mid);
build(c+1,mid+1,r);
}

int query(int c,int l,int r,int ql,int qr,int k) //[ql,qr]为当前要查找的区间
{
int s; //[l,ql]内将被划分到左子树的元素数目
int ss; //[ql,qr]内将被划分到左子树的元素数目
int mid = (l + r) >> 1;
if(l == r)  //叶子
{
return tree[c][l];
}
if(l == ql) //这里有特殊处理
{
s = 0; //前面没有左子树
ss = sum[c][qr]; //
}
else
{
s = sum[c][ql - 1]; //处理后就可以从2开始了,如果为1会等于-1
ss = sum[c][qr] - s;
}
if(k <= ss) //如果在左子树那么递归,l+s代表跳过不属于查询区间的值
{
return query(c+1,l,mid,l+s,l+s+ss-1,k); //下一层[l+s,l+ss+ss-1]左子树缩小范围
}
else //右子树的情况
{
return query(c+1,mid+1,r,mid-l+1+ql-s,mid-l+1+qr-s-ss,k-ss); //第k大减去第ss大,就是右子树要找的第几大
}  //[mid-l+1+ql-s,mid-l+1+qr-s-ss]右子树缩小范围,mid - l + 1代表左区间长度
}

int main()
{
int m,n;
int i;
int c,d,f;
//freopen("1.txt","r",stdin);
while(cin>>n>>m)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
tree[0][i] = as[i] = a[i]; //初始化
}
sort(as+1,as+n+1);
build(0,1,n);
while(m--)
{
scanf("%d%d%d",&c,&d,&f);
printf("%d\n",query(0,1,n,c,d,f));
}
}
return 0;
}


下面是主席树的代码(适用于动态求第K大数,可做更新等):

代码和注释转自: 点击打开链接
</pre><pre name="code" class="cpp">/*
主席树:对于序列的每一个前缀建一棵以序列里的值为下标的线段树(所以要先离散化),
记录该前缀序列里出现的值的次数;
记离散后的标记为1~n; (下面值直接用1~n代替;)
对于区间[x,y]的第k大的值,那么从root[x-1],root[y]开始,
t=root[y].[1,mid]-root[x-1].[1,mid] ,t表示区间[x,y]内值在[1,mid]的个数
先判断t是否大于K,如果t大于k,那么说明在区间[x,y]内存在[1,mid]的数的个数大于k,
也就是第k大的值在[1,mid]中,否则在[mid+1,r]中;

这样我们依次同时从root[x-1],root[y]往下走
当l==r时 第k大的值就是离散后标记为l的值;

如果每棵线段都建完整的化,n*(n<<2)肯定会mle,
我们发现对于前缀[1,i]和前缀[1,i+1]的线段树,如果b[i+1]<=mid (b[i+1]表示a[i+1]离散后的标记)
那么线段树i和线段树i+1的左边是完全相同的,根本不需要在建,只需要用指针指一下就好;
那么对于一棵新的线段树其实我们最多要建的节点数为log(n);nlog(n)的节点数还是可以忍受的;

*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define w(i) T[(i)].w
#define ls(i) T[(i)].ls
#define rs(i) T[(i)].rs
using namespace std;
const int N=100000+10;
struct node{
int ls,rs,w;
node(){ls=rs=w=0;}
}T[N*20];
int a
,b
,p
,root
,sz;
int cmp(int i,int j){
return a[i]<a[j];
}
int n,m;
void ins(int &i,int l,int r,int x){
T[++sz]=T[i]; i=sz;
w(i)++;
if (l==r) return;
int m=(l+r)>>1;
if (x<=m) ins(ls(i),l,m,x);
else ins(rs(i),m+1,r,x);
}
int query(int i,int j,int l,int r,int k){
if (l==r) return l;
int t=w(ls(j))-w(ls(i));
int m=(l+r)>>1;
if (t>=k) return query(ls(i),ls(j),l,m,k);
else return query(rs(i),rs(j),m+1,r,k-t);
}
int main(){
int Cas;scanf("%d",&Cas);
while (Cas--){
root[0]=0;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++){
scanf("%d",&a[i]);p[i]=i;
}
sort(p+1,p+1+n,cmp);//间接排序,p[i]表示第i小的值在a[]中的下标;
for (int i=1;i<=n;i++) b[p[i]]=i;//离散化
/*
for (int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
for (int i=1;i<=n;i++) cout<<p[i]<<" ";cout<<endl;
for (int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl;
*/
sz=0;
for (int i=1;i<=n;i++){
root[i]=root[i-1];
ins(root[i],1,n,b[i]);
}
for (int i=0;i<m;i++){
int x,y,k;scanf("%d%d%d",&x,&y,&k);
int t=query(root[x-1],root[y],1,n,k);
printf("%d\n",a[p[t]]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: