poj 2107 K-th Number(主席树求区间第K大)

来源:互联网 发布:java打印方法签名 编辑:程序博客网 时间:2024/06/07 13:33
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 58888 Accepted: 20450Case 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 31 5 2 6 3 7 42 5 34 4 11 7 3

Sample Output

563

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


题意:

给出n个数 有m个问题让求 区间[l,r]内第k大的数。


分析: 这题因为没有修改,可以用其他方法(划分树)去做

这里学一手主席树,看了一天懂了一点皮毛。

主席树相当于多个线段树去实现类似于前缀和的操作。

每个结点记录的是数的个数,同时每个结点也是一个线段树。

看了挺多大牛对优化的描述,因为在建立主席树的时候是一个点一个点添加的。

所以修改的时候只用修改当前结点的一个儿子结点,那么总有一边是不用修改的,并且每个结点的结构类似

所以没有修改的儿子结点可以直接连接上一个结点的相同儿子结点。

(比如修改的是左儿子,那么就连接右儿子)

那么就可以得到逐个添加的所有历史版本,root[i]代表的是区间[0,i]添加得到的线段树

那么要求 [i,j]的第k大 就相当于 [0,j]-[0,i-1]的第k大。然后去判断左儿子结点记录数量是否大于等于k

是的话第k大在左儿子里,否则在右儿子,递归下去,直到找到区间大小为1.那么得到答案。


AC代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int num[100010],sortnum[100010];struct Tree{int num,lson,rson;}tree[5000500];int root[5000500];int cnt;void bulidtree(int & v,int l,int r) {v=++cnt;tree[v].num=0;if(l==r)return;int mid=(l+r)>>1;bulidtree(tree[v].lson,l,mid);bulidtree(tree[v].rson,mid+1,r);}int Binary_search(int *p,int aim,int r){int l=1;while(l<=r){int mid=(l+r)>>1;if(p[mid]>=aim)r=mid-1;elsel=mid+1;}return l;}void update(int p,int &v,int l,int r,int s){v=++cnt;tree[v]=tree[p];tree[v].num++;if(l==r)return;int mid=(l+r)>>1;if(s>mid)update(tree[p].rson,tree[v].rson,mid+1,r,s);elseupdate(tree[p].lson,tree[v].lson,l,mid,s);}int query(int p,int v,int l,int r,int k){if(l==r)return l;int mid=(l+r)>>1;int left=tree[tree[v].lson].num-tree[tree[p].lson].num;if(k<=left)query(tree[p].lson,tree[v].lson,l,mid,k);elsequery(tree[p].rson,tree[v].rson,mid+1,r,k-left);}int main(){int n,m;while(scanf("%d%d",&n,&m)==2){cnt=0;for(int i=1;i<=n;i++){scanf("%d",&num[i]);sortnum[i]=num[i];}sort(sortnum+1,sortnum+n+1);int t=1;for(int i=2;i<=n;i++)if(sortnum[i]!=sortnum[i-1])sortnum[++t]=sortnum[i];bulidtree(root[0],1,t);for(int i=1;i<=n;i++){int s=Binary_search(sortnum,num[i],t);update(root[i-1],root[i],1,t,s);} for(int i=0;i<m;i++){int l,r,k;scanf("%d%d%d",&l,&r,&k);int ans=query(root[l-1],root[r],1,t,k);printf("%d\n",sortnum[ans]);}}}



阅读全文
0 0
原创粉丝点击