zoukankan      html  css  js  c++  java
  • POJ 2104主席树

    Online JudgeProblem SetAuthorsOnline ContestsUser
    Web Board
    Home Page
    F.A.Qs
    Statistical Charts
    Problems
    Submit Problem
    Online Status
    Prob.ID:
    Register
    Update your info
    Authors ranklist
    Current Contest
    Past Contests
    Scheduled Contests
    Award Contest
    20134947      Log Out
    Mail:0(0)
    Login Log      Archive
    Language:
    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 38720   Accepted: 12591
    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

    [Submit]   [Go Back]   [Status]   [Discuss]

    Home Page   Go Back  To top


    All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
    Any problem, Please Contact Administrator

    静态区间第k大查询,学习主席树第一题

     1 /*************************************************************************
     2     > File Name: cf.cpp
     3     > Author: acvcla
     4     > QQ:
     5     > Mail: acvcla@gmail.com
     6     > Created Time: 1949Äê10ÔÂ1ÈÕ ÐÇÆÚÒ» 0ʱ0·Ö0Ãë
     7  ************************************************************************/
     8 #include<iostream>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<vector>
    12 #include<cstring>
    13 #include<map>
    14 #include<queue>
    15 #include<stack>
    16 #include<string>
    17 #include<cstdlib>
    18 #include<ctime>
    19 #include<set>
    20 #include<math.h>
    21 using namespace std;
    22 typedef long long LL;
    23 const int maxn = 1e5 + 10;
    24 #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    25 #define pb push_back
    26 int tot,n,m,date[maxn],sorted[maxn],root[maxn];
    27 struct chairTnode
    28 {
    29     int s,rc,lc;
    30     chairTnode(int s=0,int lc=0,int rc=0):s(s),lc(lc),rc(rc){}
    31 };
    32 chairTnode chairT[maxn*20];
    33 int newnode(int sum,int lson,int rson)
    34 {
    35     int rt=++tot;
    36     chairT[rt]=chairTnode(sum,lson,rson);
    37     return rt;
    38 }
    39 void Insert(int &rt,int pre_rt,int pos,int L,int R)
    40 {
    41     chairTnode &t=chairT[pre_rt];
    42     rt=newnode(t.s+1,t.lc,t.rc);
    43     if(L==R)return;
    44     int mid=(L+R)>>1;
    45     if(pos<=mid)Insert(chairT[rt].lc,t.lc,pos,L,mid);
    46     else Insert(chairT[rt].rc,t.rc,pos,mid+1,R);
    47 }
    48 int Query(int ql,int qr,int L,int R,int k)
    49 {
    50     if(L==R)return L;
    51     int sum=chairT[chairT[qr].lc].s-chairT[chairT[ql].lc].s;
    52     int mid=(L+R)>>1;
    53     if(k<=sum)return Query(chairT[ql].lc,chairT[qr].lc,L,mid,k);
    54     return Query(chairT[ql].rc,chairT[qr].rc,mid+1,R,k-sum);
    55 }
    56 int main()
    57 {
    58     while(~scanf("%d%d",&n,&m))
    59     {
    60         rep(i,1,n){
    61             scanf("%d",date+i);
    62             sorted[i]=date[i];
    63         }
    64         tot=root[0]=0;
    65         sort(sorted+1,sorted+1+n);
    66         int cnt=unique(sorted+1,sorted+1+n)-sorted-1;
    67         for(int i=1;i<=n;i++){
    68             int pos=lower_bound(sorted+1,sorted+1+n,date[i])-sorted;
    69             Insert(root[i],root[i-1],pos,1,cnt);
    70         }
    71         int ql,qr,k;
    72         while(m--)
    73         {
    74             scanf("%d%d%d",&ql,&qr,&k);
    75             printf("%d
    ",sorted[Query(root[ql-1],root[qr],1,cnt,k)]);
    76         }
    77     }
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    css基础
    html常用标签
    自写一个ant-design-pro AnimationList组件
    设计一个A表数据抽取到B表的抽取过程
    垂直、水平方向观看楼层天际线不变情况下能增加的建筑物高度最大总和
    JavaScript 新旧替换一:变量声明
    前端异常类型及捕获方式
    自定义 create-react-app
    Git Commit 规范参考
    JavaScript 团队规范参考
  • 原文地址:https://www.cnblogs.com/nowornever/p/4139017.html
Copyright © 2011-2022 走看看