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

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 60557   Accepted: 21228
    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
     
    终于攻克主席树第一个难关:) 这题额外的学会了stl的二分,stl的去重还有stl的离散化:> 反正收获多多啦
    最后查询的时候应该是work(y)-work(x-1)才行,我一开始是work(y)-work(x)然后只在work里面的一个地方用了x-1所以错了,很尴尬
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstdlib>
     4 #include <queue>
     5 #include <iostream>
     6 #include "algorithm"
     7 using namespace std;
     8 typedef long long LL;
     9 const int MAX=1e5+5;
    10 int n,m;
    11 int a[MAX],b[MAX],lb,root[MAX],cnt;
    12 struct Node{
    13     int l,r;
    14     int sum;
    15     Node (){l=r=sum=0;}
    16 }t[MAX*40];
    17 inline int read(){
    18     int an=0,x=1;char c=getchar();
    19     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
    20     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
    21     return an*x;
    22 }
    23 void update(int l,int r,int &x,int y,int pos){
    24     t[++cnt]=t[y],t[cnt].sum++;x=cnt;
    25     if (l==r) return;
    26     int mid=(l+r)>>1;
    27     if (pos<=mid) update(l,mid,t[x].l,t[y].l,pos);
    28     else update(mid+1,r,t[x].r,t[y].r,pos);
    29 }
    30 int query(int l,int r,int x,int y,int k){
    31     if (l==r) return l;
    32     int sum=t[t[y].l].sum-t[t[x].l].sum;
    33     int mid=(l+r)>>1;
    34     if (sum>=k) return query(l,mid,t[x].l,t[y].l,k);
    35     else return query(mid+1,r,t[x].r,t[y].r,k-sum);
    36 }
    37 int main(){
    38     freopen ("chairman.in","r",stdin);
    39     freopen ("chairman.out","w",stdout);
    40     int i,j;
    41     int x,y,z;
    42     n=read();m=read();
    43     for (i=1;i<=n;i++){a[i]=read();b[i]=a[i];}sort(b+1,b+n+1);
    44     lb=unique(b+1,b+n+1)-(b+1);
    45     for (i=1;i<=n;i++){
    46         int pos=lower_bound(b+1,b+lb+1,a[i])-b;
    47         update(1,lb,root[i],root[i-1],pos);
    48     }
    49     for (i=1;i<=m;i++){
    50         x=read();y=read();z=read();
    51         printf("%d
    ",b[query(1,lb,root[x-1],root[y],z)]);
    52     }
    53     return 0;
    54 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    AtCoder AGC036C GP 2 (组合计数)
    Luogu P4708 画画 (Burnside引理、组合计数、划分数)
    BZOJ 1488 Luogu P4727 [HNOI2009]图的同构 (Burnside引理、组合计数)
    BZOJ 2655 calc (组合计数、DP、多项式、拉格朗日插值)
    POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)
    BZOJ 4555 Luogu P4091 [HEOI2016/TJOI2016]求和 (第二类斯特林数)
    Luogu P4707 重返现世 (拓展Min-Max容斥、DP)
    LOJ #6358 前夕 (组合计数、容斥原理)
    BZOJ 3622 Luogu P4859 已经没有什么好害怕的了 (容斥原理、DP)
    Java基本的程序结构设计 大数操作
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7599755.html
Copyright © 2011-2022 走看看