K-th Number (poj 2104 线段树+二分)

来源:互联网 发布:制作生日快乐的软件 编辑:程序博客网 时间:2024/05/19 17:04

Language:
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 40048 Accepted: 13081Case 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

题意:给定一个数组a1,a2,a3.....an和m个三元组表示的查询。对于每个查询(i,j,k),输出ai,ai+1,.....,aj的升序排列中第k个数。

思路:和以前做的线段树有点不同,这次每个点保存了一个数组。建立线段树的过程和归并排序类似,而每个节点的数列就是其两个儿子节点的数列合并后的结果。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 100005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;struct Tree{    int l,r;    vector<int>d;}tree[maxn<<2];int n,m;int num[maxn];void build(int rt,int l,int r){    tree[rt].l=l;    tree[rt].r=r;    if (tree[rt].l==tree[rt].r)    {        tree[rt].d.push_back(num[tree[rt].l]);        return ;    }    int mid=(tree[rt].l+tree[rt].r)>>1;    build(lson);    build(rson);    tree[rt].d.resize(tree[rt].r-tree[rt].l+1);    //利用STL的merge函数吧两个儿子的数列合并    merge(tree[rt<<1].d.begin(),tree[rt<<1].d.end(),tree[rt<<1|1].d.begin(),tree[rt<<1|1].d.end(),tree[rt].d.begin());}//计算[i,j]中不超过x的个数,k是节点编号int query(int rt,int l,int r,int x){    if (tree[rt].r<l||tree[rt].l>r) //完全不相交        return 0;    else if (l<=tree[rt].l&&tree[rt].r<=r) //完全包含在里面        return upper_bound(tree[rt].d.begin(),tree[rt].d.end(),x)-tree[rt].d.begin();    else    {        int lc=query(rt<<1,l,r,x);        int rc=query(rt<<1|1,l,r,x);        return lc+rc;    }}int solve(int l,int r,int k)        //二分{    int L=1,R=n,ans=1;    while (L<=R)    {        int mid=(L+R)>>1;        int c=query(1,l,r,num[mid]);        if (c>=k)        {            ans=mid;            R=mid-1;        }        else            L=mid+1;    }    return ans;}int main(){    int i,j;    while (~sff(n,m))    {        int l,r,k;        FRE(i,1,n)            sf(num[i]);        build(1,1,n);        sort(num+1,num+n+1);        FRL(i,0,m)        {            //查找[l,r]中的第k个数            sfff(l,r,k);            pf("%d\n",num[solve(l,r,k)]);        }    }    return 0;}



2 0
原创粉丝点击