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

    K-th Number

    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 59327   Accepted: 20660
    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

     

    //题意: n 个数,m 次询问,求 l,r,中第 k 小的数是什么

    题解:有很多方法可以写,线段树配上归并,我用来当做主席树模板来做,学习!

      1 # include <cstdio>
      2 # include <cstring>
      3 # include <cstdlib>
      4 # include <iostream>
      5 # include <vector>
      6 # include <queue>
      7 # include <stack>
      8 # include <map>
      9 # include <bitset>
     10 # include <sstream>
     11 # include <set>
     12 # include <cmath>
     13 # include <algorithm>
     14 # pragma  comment(linker,"/STACK:102400000,102400000")
     15 using namespace std;
     16 # define LL          long long
     17 # define pb          push_back
     18 # define pr          pair
     19 # define mkp         make_pair
     20 # define lowbit(x)   ((x)&(-x))
     21 # define PI          acos(-1.0)
     22 # define INF         0x3f3f3f3f3f3f3f3f
     23 # define eps         1e-8
     24 # define MOD         1000000007
     25 
     26 inline int scan() {
     27     int x=0,f=1; char ch=getchar();
     28     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
     29     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
     30     return x*f;
     31 }
     32 inline void Out(int a) {
     33     if(a<0) {putchar('-'); a=-a;}
     34     if(a>=10) Out(a/10);
     35     putchar(a%10+'0');
     36 }
     37 # define MX 100005
     38 /**************************/
     39 struct Node
     40 {
     41     int l,r;
     42     int sum;
     43 }tree[MX*40];
     44 
     45 int n,m;
     46 int newn,tcnt;
     47 int dat[MX];
     48 int root[MX];
     49 int cpy[MX];
     50 int getid(int x){return lower_bound(cpy+1,cpy+1+n,x)-cpy;}
     51 
     52 int update(int l,int r,int y,int pos)
     53 {
     54     tcnt++;
     55     tree[tcnt].sum = tree[y].sum+1;
     56     int sav = tcnt;
     57 
     58     if (l==r) return sav;
     59     int mid = (l+r)/2;
     60     if (pos<=mid)
     61     {
     62         tree[sav].l = update(l,mid,tree[y].l,pos);
     63         tree[sav].r = tree[y].r;
     64     }
     65     else
     66     {
     67         tree[sav].l = tree[y].l;
     68         tree[sav].r = update(mid+1,r,tree[y].r,pos);
     69     }
     70     return sav;
     71 }
     72 
     73 int inqy(int l,int r,int x,int y,int k)
     74 {
     75     if (l==r) return l;
     76     int all =tree[ tree[y].l ].sum -  tree[ tree[x].l ].sum;
     77     int mid = (l+r)/2;
     78     if (k<=all) return inqy(l,mid,tree[x].l,tree[y].l,k);
     79     else return inqy(mid+1,r,tree[x].r,tree[y].r,k-all);
     80 
     81 }
     82 
     83 
     84 int main ()
     85 {
     86     while (scanf("%d%d",&n,&m)!=EOF)
     87     {
     88         for (int i=1;i<=n;i++)
     89         {
     90             dat[i] = scan();
     91             cpy[i] = dat[i];
     92         }
     93         sort(cpy+1,cpy+1+n);
     94         newn = unique(cpy+1,cpy+1+n)-cpy;
     95         tcnt=0;
     96         for (int i=1;i<=n;i++) root[i]=update(1,n,root[i-1],getid(dat[i]));
     97         while (m--)
     98         {
     99             int a = scan();
    100             int b = scan();
    101             int k = scan();
    102             printf("%d
    ",cpy[inqy(1,n,root[a-1],root[b],k)]);
    103         }
    104     }
    105     return 0;
    106 }
    View Code
  • 相关阅读:
    SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
    恢复SQL Server被误删除的数据(再扩展)
    Windows server 2012 添加中文语言包(英文转为中文)(离线)
    SQL Server技术内幕笔记合集
    将表里的数据批量生成INSERT语句的存储过程 增强版
    在SQL2008查找某数据库中的列是否存在某个值
    SQLSERVER走起 APP隆重推出
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7368042.html
Copyright © 2011-2022 走看看