COGS 1534 [NEERC 2004]K小数 主席树题解

来源:互联网 发布:python buffer类型使用 编辑:程序博客网 时间:2024/05/19 17:48

时间限制:2 s 内存限制:512 MB

【中文题意】

给出一个长度为n的序列a1~an,有m次询问(x,y,k),每次询问a[x]~a[y]内的第k小数。
输入第一行为n,m,第二行为a1~an,接下来m行是m个(x,y,k)。
由于数据较大,请使用C风格的输入输出。
1<=n<=100000,1<=m<=5000

【题目描述】

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.

【输入格式】

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).

【输出格式】

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

【样例输入】

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

【样例输出】

5
6
3

【提示】

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

【来源】

Northeastern Europe 2004, Northern Subregion

【题目来源】

北京大学 POJ 2104


显然是主席树模板题


#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<queue>#include<set>const int MAXN=100000*20;using namespace std;int sz,root[MAXN],from,to,kk,n,m,a[MAXN];vector<int>v;struct Tree{    int lson,rson,sum;}tree[MAXN];int id(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}void modify(int x,int &y,int l,int r,int loc){    tree[++sz]=tree[x];y=sz;tree[y].sum++;    if(l==r) return;    int mid=(l+r)>>1;    if(loc<=mid) modify(tree[x].lson,tree[y].lson,l,mid,loc);    else         modify(tree[x].rson,tree[y].rson,mid+1,r,loc); }int query(int x,int y,int k,int l,int r){    if(l==r) return l;    int mid=(l+r)>>1;    int t=tree[tree[y].lson].sum-tree[tree[x].lson].sum;    if(t>=k) query(tree[x].lson,tree[y].lson,k,l,mid);    else     query(tree[x].rson,tree[y].rson,k-t,mid+1,r);}int main(){    freopen("kthnumber.in","r",stdin);    freopen("kthnumber.out","w",stdout);    scanf("%d%d",&n,&m);    for(register int i=1;i<=n;i++)scanf("%d",&a[i]),v.push_back(a[i]);    sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());    for(register int i=1;i<=n;i++)modify(root[i-1],root[i],1,n,id(a[i]));    while(m--){        scanf("%d%d%d",&from,&to,&kk);        printf("%d\n",v[query(root[from-1],root[to],kk,1,n)-1]);    }    return 0;}

这里写图片描述

原创粉丝点击