zoukankan      html  css  js  c++  java
  • POJ2104 K-th Number —— 静态区间第k小

    题目链接:http://poj.org/problem?id=2104

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 64277   Accepted: 22615
    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

    划分树:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int mod = 1e9+7;
    17 const int MAXN = 1e5+10;
    18 
    19 int tree[20][MAXN];
    20 int sorted[MAXN];
    21 int toleft[20][MAXN];
    22 
    23 void build(int l, int r, int dep)
    24 {
    25     if(l==r) return;
    26     int mid = (l+r)>>1;
    27     int same = mid-l+1;
    28     for(int i = l; i<=r; i++)
    29         if(tree[dep][i]<sorted[mid])
    30             same--;
    31 
    32     int lpos = l, rpos = mid+1;
    33     for(int i = l; i<=r; i++)
    34     {
    35         if(tree[dep][i]<sorted[mid])
    36             tree[dep+1][lpos++] = tree[dep][i];
    37         else if(tree[dep][i]==sorted[mid] && same>0)
    38         {
    39             tree[dep+1][lpos++] = tree[dep][i];
    40             same--;
    41         }
    42         else
    43             tree[dep+1][rpos++] = tree[dep][i];
    44         toleft[dep][i] = toleft[dep][l-1] + lpos - l;
    45     }
    46 
    47     build(l, mid, dep+1);
    48     build(mid+1, r, dep+1);
    49 }
    50 
    51 int query(int L, int R, int l, int r, int dep, int k)
    52 {
    53     if(l==r) return tree[dep][l];
    54     int mid = (L+R)>>1;
    55     int cnt = toleft[dep][r] - toleft[dep][l-1];
    56 
    57     if(cnt>=k)
    58     {
    59         int newl = L + toleft[dep][l-1] - toleft[dep][L-1];
    60         int newr = newl + cnt - 1;
    61         return query(L, mid, newl, newr, dep+1, k);
    62     }
    63     else
    64     {
    65         int newr = r + toleft[dep][R] - toleft[dep][r];
    66         int newl = newr - (r-l-cnt);
    67         return query(mid+1, R, newl, newr, dep+1, k-cnt);
    68     }
    69 }
    70 
    71 int main()
    72 {
    73     int n, m;
    74     while(scanf("%d%d",&n,&m)!=EOF)
    75     {
    76         memset(tree, 0, sizeof(tree));
    77         for(int i = 1; i<=n; i++)
    78         {
    79             scanf("%d",&tree[0][i]);
    80             sorted[i] = tree[0][i];
    81         }
    82         sort(sorted+1, sorted+1+n);
    83         build(1, n, 0);
    84         int s, t, k;
    85         while(m--)
    86         {
    87             scanf("%d%d%d",&s,&t,&k);
    88             printf("%d
    ", query(1,n,s,t,0,k));
    89         }
    90     }
    91     return 0;
    92 }
    View Code

    主席树(循环):

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int MOD = 1e9+7;
     17 const int MAXN = 1e5+10;
     18 const int M = MAXN*30;
     19 
     20 int n, q, m, tot;
     21 int a[MAXN], t[MAXN];
     22 int T[MAXN], lson[M], rson[M], c[M];
     23 
     24 void Init_hash()
     25 {
     26     for(int i = 1; i<=n; i++)
     27         t[i] = a[i];
     28     sort(t+1,t+1+n);
     29     m = unique(t+1,t+1+n)-(t+1);
     30 }
     31 
     32 int hash(int x)
     33 {
     34     return lower_bound(t+1,t+1+m, x)-t;
     35 }
     36 
     37 int build(int l, int r)
     38 {
     39     int root = tot++;
     40     c[root] = 0;
     41     if(l!=r)
     42     {
     43         int mid = (l+r)>>1;
     44         lson[root] = build(l,mid);
     45         rson[root] = build(mid+1,r);
     46     }
     47     return root;
     48 }
     49 
     50 int update(int root, int pos, int val)
     51 {
     52     int newroot = tot++, tmp = newroot;
     53     c[newroot] = c[root] + val;
     54     int l = 1, r = m;
     55     while(l<r)
     56     {
     57         int mid = (l+r)>>1;
     58         if(pos<=mid)
     59         {
     60             lson[newroot] = tot++; rson[newroot] = rson[root];
     61             newroot = lson[newroot]; root = lson[root];
     62             r = mid;
     63         }
     64         else
     65         {
     66             rson[newroot] = tot++; lson[newroot] = lson[root];
     67             newroot = rson[newroot]; root = rson[root];
     68             l = mid + 1;
     69         }
     70         c[newroot] = c[root] + val;
     71     }
     72     return tmp;
     73 }
     74 
     75 int query(int left_root, int right_root, int k)
     76 {
     77     int l = 1, r = m;
     78     while(l<r)
     79     {
     80         int mid = (l+r)>>1;
     81         if(c[lson[left_root]]-c[lson[right_root]]>=k)
     82         {
     83             r = mid;
     84             left_root = lson[left_root];
     85             right_root = lson[right_root];
     86         }
     87         else
     88         {
     89             l = mid + 1;
     90             k -= c[lson[left_root]]-c[lson[right_root]];
     91             left_root = rson[left_root];
     92             right_root = rson[right_root];
     93         }
     94     }
     95     return l;
     96 }
     97 
     98 int main()
     99 {
    100     while(scanf("%d%d",&n,&q)==2)
    101     {
    102         tot = 0;
    103         for(int i = 1; i<=n; i++)
    104             scanf("%d",&a[i]);
    105         Init_hash();
    106         T[n+1] = build(1,m);
    107         for(int i = n; i; i--)
    108         {
    109             int pos = hash(a[i]);
    110             T[i] = update(T[i+1],pos,1);
    111         }
    112         while(q--)
    113         {
    114             int l, r, k;
    115             scanf("%d%d%d",&l,&r,&k);
    116             printf("%d
    ", t[query(T[l],T[r+1],k)]);
    117         }
    118     }
    119 }
    View Code

    主席树(递归):

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int MOD = 1e9+7;
     17 const int MAXN = 1e5+10;
     18 const int M = MAXN*30;
     19 
     20 int n, q, m, tot;
     21 int a[MAXN], t[MAXN];
     22 int T[MAXN], lson[M], rson[M], c[M];
     23 
     24 void Init_hash()
     25 {
     26     for(int i = 1; i<=n; i++)
     27         t[i] = a[i];
     28     sort(t+1,t+1+n);
     29     m = unique(t+1,t+1+n)-(t+1);
     30 }
     31 
     32 int hash(int x)
     33 {
     34     return lower_bound(t+1,t+1+m, x)-t;
     35 }
     36 
     37 int build(int l, int r)
     38 {
     39     int root = tot++;
     40     c[root] = 0;
     41     if(l!=r)
     42     {
     43         int mid = (l+r)>>1;
     44         lson[root] = build(l,mid);
     45         rson[root] = build(mid+1,r);
     46     }
     47     return root;
     48 }
     49 
     50 int update(int root, int l, int r, int pos, int val)
     51 {
     52     int newroot = tot++;
     53     if(l==r)
     54     {
     55         c[newroot] = c[root] + val;
     56         return newroot;
     57     }
     58     int mid = (l+r)>>1;
     59     if(pos<=mid)
     60     {
     61         lson[newroot] = update(lson[root],l,mid,pos,val);
     62         rson[newroot] = rson[root];
     63     }
     64     else
     65     {
     66         rson[newroot] = update(rson[root],mid+1,r,pos,val);
     67         lson[newroot] = lson[root];
     68     }
     69     c[newroot] = c[lson[newroot]] + c[rson[newroot]];
     70     return newroot;
     71 }
     72 
     73 int query(int left_root, int right_root, int l, int r, int k)
     74 {
     75     if(l==r) return l;
     76 
     77     int mid = (l+r)>>1;
     78     if(c[lson[left_root]]-c[lson[right_root]]>=k)
     79         return query(lson[left_root],lson[right_root],l,mid,k);
     80     else
     81         return query(rson[left_root],rson[right_root],mid+1,r,k-(c[lson[left_root]]-c[lson[right_root]]));
     82 }
     83 
     84 int main()
     85 {
     86     while(scanf("%d%d",&n,&q)!=EOF)
     87     {
     88         tot = 0;
     89         for(int i = 1; i<=n; i++)
     90             scanf("%d",&a[i]);
     91         Init_hash();
     92         T[n+1] = build(1,m);
     93         for(int i = n; i; i--)
     94         {
     95             int pos = hash(a[i]);
     96             T[i] = update(T[i+1],1,m,pos,1);
     97         }
     98         while(q--)
     99         {
    100             int l, r, k;
    101             scanf("%d%d%d",&l,&r,&k);
    102             printf("%d
    ", t[query(T[l],T[r+1],1,m,k)]);
    103         }
    104     }
    105 }
    View Code


  • 相关阅读:
    Xcode8 pod install 报错 “Generating Pods project Abort trap
    适配iOS10 的相关权限设置
    YTKNetworkConfig配置HTTPS请求
    HTTPS学习总结
    设置导航栏nav全透明
    更新 Python 库文件
    有道词典的本地/扩展/离线词库
    三国群英传2修改MOD基础
    添加/删除/修改Windows 7右键的“打开方式”
    Sublime Text安装Package Control
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538604.html
Copyright © 2011-2022 走看看