zoukankan      html  css  js  c++  java
  • poj 2104 K-th Number(主席树)

    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.

    【思路】

      主席树 

      戳这 click here

    【代码】

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
     6 using namespace std;
     7 
     8 typedef long long ll;
     9 const int N = 1e5+10;
    10 const int M = 2*1e6+10;
    11 
    12 int n,m,tot,sz;
    13 int v[N],hash[N],root[N];
    14 int sum[M],ls[M],rs[M];
    15 
    16 ll read() {
    17     char c=getchar();
    18     ll x=0,f=1;
    19     while(!isdigit(c)) {
    20         if(c=='-') f=-1; c=getchar();
    21     }
    22     while(isdigit(c))
    23         x=x*10+c-'0',c=getchar();
    24     return x*f;
    25 }
    26 void update(int l,int r,int x,int& y,int num) 
    27 {
    28     y=++sz;
    29     sum[y]=sum[x]+1;
    30     if(l==r) return ;
    31     ls[y]=ls[x],rs[y]=rs[x];
    32     int mid=(l+r)>>1;
    33     if(num<=mid) update(l,mid,ls[x],ls[y],num);
    34     else update(mid+1,r,rs[x],rs[y],num);
    35 }
    36 int query(int x,int y,int rk) 
    37 {
    38     int a=root[x-1],b=root[y];
    39     int l=1,r=tot;
    40     while(l<r) {
    41         int mid=(l+r)>>1;
    42         int now=sum[ls[b]]-sum[ls[a]];
    43         if(rk<=now) r=mid,a=ls[a],b=ls[b];
    44         else l=mid+1,rk-=now,a=rs[a],b=rs[b];
    45     }
    46     return hash[l];
    47 }
    48 int main() {
    49     n=read(),m=read();
    50     FOR(i,1,n)
    51         v[i]=read(),hash[i]=v[i];
    52     sort(hash+1,hash+n+1);
    53     tot=unique(hash+1,hash+n+1)-hash-1;
    54     FOR(i,1,n)
    55         v[i]=lower_bound(hash+1,hash+tot+1,v[i])-hash;
    56     FOR(i,1,n) {
    57         update(1,tot,root[i-1],root[i],v[i]);
    58     }
    59     int x,y,z;
    60     FOR(i,1,m) {
    61         x=read(),y=read(),z=read();
    62         printf("%d",query(x,y,z));
    63         if(i!=m) puts("");
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    WPF:简洁为美
    WPF工作笔记:本地化支持、主进程通知、两种最常用异步编程方式
    WPF:将HTML RGB颜色值转化为Color对象的两种方式
    WPF:定制Checkbox样式,让“正确”绿得好看,让“错误”红的显眼
    Is C# a clone of a Microsoft replacement for Java?
    原创的基于HTML/CSS/JavaScript的层级目录树
    Android笔记——BaseAdapter的使用
    C#开源持久层框架
    经典.net面试题目
    C#:实体框架EF(entity framework)
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5242636.html
Copyright © 2011-2022 走看看