zoukankan      html  css  js  c++  java
  • POJ --2104

    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 34935   Accepted: 11134
    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
    划分树
    AC代码:
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #define MAX 100005
      5 using namespace std;
      6 class TreeNode
      7 {
      8     public:
      9         int left;
     10         int right;
     11         int mid;
     12 };
     13 int ToLeft[30][MAX];
     14 int val[30][MAX];
     15 TreeNode node[3*MAX];
     16 int sorted[MAX];
     17 void BuildTree(int k, int d, int l, int r)
     18 {
     19     node[k].left = l;
     20     node[k].right = r;
     21     node[k].mid = (l + r) >> 1;
     22     int mid = (l + r) >> 1;
     23     if(l == r)
     24         return ;
     25     int lsame = mid - l + 1;
     26     for(int i = l;i <= r; i ++)
     27     {
     28         if(val[d][i] < sorted[mid])
     29             lsame --;
     30     }
     31     int lpos = l;
     32     int rpos = mid + 1;
     33     for(int i = l;i <= r;i ++)
     34     {
     35         if(i == l)
     36             ToLeft[d][i] == 0;
     37         else
     38             ToLeft[d][i] = ToLeft[d][i-1];
     39         if(val[d][i] < sorted[mid])
     40         {
     41             ToLeft[d][i] ++;
     42             val[d+1][lpos++] = val[d][i];
     43         }
     44         else if(val[d][i] > sorted[mid])
     45             val[d+1][rpos++] = val[d][i];
     46         else
     47         {
     48             if(lsame)
     49             {
     50                 ToLeft[d][i] ++;
     51                 val[d+1][lpos++] = val[d][i];
     52                 lsame --;
     53             }
     54             else
     55                 val[d+1][rpos++] = val[d][i];
     56         }
     57     }
     58     BuildTree(k << 1, d + 1, l, mid);
     59     BuildTree(k << 1|1, d+1, mid + 1, r);
     60 }
     61 
     62 int Query(int l, int r, int k, int d, int idx)
     63 {
     64     if(l == r)
     65         return val[d][l];
     66     int s;
     67     int ss;
     68     if(node[idx].left == l)
     69     {
     70         s = ToLeft[d][r];
     71         ss = 0;
     72     }
     73     else
     74     {
     75         s = ToLeft[d][r] - ToLeft[d][l-1];
     76         ss = ToLeft[d][l-1];
     77     }
     78     if(s >= k)
     79     {
     80         int newl = node[idx].left + ss;
     81         int newr = node[idx].left + ss + s - 1;
     82         return Query(newl, newr, k, d + 1, idx << 1);
     83     }
     84     else
     85     {
     86         int bb = l - node[idx].left - ss;
     87         int b = r- l - s + 1;
     88         int newl = node[idx].mid + bb + 1;
     89         int newr = node[idx].mid + bb + b;
     90         return Query(newl, newr, k - s, d + 1, idx << 1|1);
     91     }
     92 }
     93 
     94 int main(int argc, char const *argv[])
     95 {
     96     int n, m;
     97     int l, r, k;
     98     //freopen("in.c", "r", stdin);
     99     while(~scanf("%d%d", &n, &m))
    100     {
    101         for(int i = 1;i <= n;i ++)
    102         {
    103             scanf("%d", &val[0][i]);
    104             sorted[i] = val[0][i];
    105         }
    106         sort(sorted+1, sorted+n+1);
    107         BuildTree(1, 0, 1, n);
    108         for(int i = 0;i < m;i ++)
    109         {
    110             scanf("%d%d%d", &l, &r, &k);
    111             printf("%d
    ", Query(l, r, k, 0, 1));
    112         }
    113     }
    114     return 0;
    115 }
     
  • 相关阅读:
    Java守护线程Daemon
    在for循环中创建双向链表
    Java泛型-官方教程
    大自然搬运工
    转 curl命令
    HashMap扩容问题及了解散列均分
    mysql 分组查询并取出各个分组中时间最新的数据
    CNN 模型复杂度分析
    Attention机制
    深度学习之目标检测
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3580886.html
Copyright © 2011-2022 走看看