zoukankan      html  css  js  c++  java
  • poj2104 Kth-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.


    题意:给你一个大小为n的序列和m个询问,每次询问求[L,R]区间第k大的值。

    正解:主席树或整体二分

    以前一直觉得主席树是一个很高深的东西,自己写了以后才知道原来主席树代码极短。。

    建n颗值域线段树,第i颗线段树保存1-i对应区间的结点个数,如根结点为1-maxa的个数,左右儿子分别二分得到。

    那么,这颗主席树就能满足前缀和的性质,在查询时,只需将第r颗树上的贡献减去第l-1颗树上的贡献就好。查询贡献时,判断当前两结点之差与k的关系。如果当前差<=k,那么就查询左子树,否则查询右子树,查询到叶子结点时就得出答案。。

    还有一个问题,如果真的直接开n颗线段树肯定会MLE,那么我们可以利用可持久化技术,每次插入一棵线段树时只加入与之前的树不同的一条链,其他的结点不变,那么我们每次插入的空间复杂度为O(logN),总复杂度为O(NlogN),可以接受。

    不多说了,看代码吧。。

     1 //It is made by wfj_2048~
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <cstdlib>
     6 #include <cstdio>
     7 #include <vector>
     8 #include <cmath>
     9 #include <queue>
    10 #include <stack>
    11 #include <map>
    12 #include <set>
    13 #define inf 1<<30
    14 #define il inline
    15 #define RG register
    16 #define ll long long
    17 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    18 
    19 using namespace std;
    20 
    21 int root[100010],a[100010],num[100010],hash[100010],s[8000010],ls[8000010],rs[8000010],n,m,sz,tot;
    22 
    23 il int gi(){
    24     RG int x=0,q=0; RG char ch=getchar();
    25     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
    26     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
    27 }
    28 
    29 il void insert(RG int l,RG int r,RG int x,RG int &y,RG int k){
    30     y=++sz,s[y]=s[x]+1; if (l==r) return; ls[y]=ls[x],rs[y]=rs[x]; RG int mid=(l+r)>>1;
    31     if (k<=mid) insert(l,mid,ls[x],ls[y],k); else insert(mid+1,r,rs[x],rs[y],k); return;
    32 }
    33 
    34 il int ask(RG int l,RG int r,RG int x,RG int y,RG int k){
    35     while (l<r){
    36     RG int mid=(l+r)>>1;
    37     if (s[ls[y]]-s[ls[x]]>=k) r=mid,x=ls[x],y=ls[y];
    38     else l=mid+1,k-=s[ls[y]]-s[ls[x]],x=rs[x],y=rs[y];
    39     }
    40     return l;
    41 }
    42 
    43 il void work(){
    44     n=gi(),m=gi(); for (RG int i=1;i<=n;++i) num[i]=a[i]=gi(); sort(num+1,num+n+1);
    45     hash[++tot]=num[1]; for (RG int i=2;i<=n;++i) if (num[i]!=num[i-1]) hash[++tot]=num[i];
    46     for (RG int i=1;i<=n;++i) insert(1,tot,root[i-1],root[i],lower_bound(hash+1,hash+tot+1,a[i])-hash);
    47     for (RG int i=1;i<=m;++i){ RG int l=gi(),r=gi(),k=gi(); printf("%d
    ",hash[ask(1,tot,root[l-1],root[r],k)]); }
    48     return;
    49 }
    50 
    51 int main(){
    52     File("kth");
    53     work();
    54     return 0;
    55 }
  • 相关阅读:
    【BZOJ1801】【AHOI2009】中国象棋(动态规划)
    【BZOJ3436】小K的农场(差分约束)
    【BZOJ2330】【SDOI2012】糖果(差分约束,SPFA)
    【BZOJ4010】【HNOI2015】菜肴制作(拓扑排序)
    【BZOJ2684】【CEOI2004】锯木厂选址(斜率优化,动态规划)
    【BZOJ1096】【ZJOI2007】仓库建设(斜率优化,动态规划)
    吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
    耐得住寂寞,才能守得住繁华
    想成功,就把这九个公式背下来!
    惊人的社会定律(建议收藏!)
  • 原文地址:https://www.cnblogs.com/wfj2048/p/6416608.html
Copyright © 2011-2022 走看看