zoukankan      html  css  js  c++  java
  • HDU 2665 Kth number

    Kth number

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 13726    Accepted Submission(s): 4160


    Problem Description

    Give you a sequence and ask you the kth big number of a inteval.
     

    Input

    The first line is the number of the test cases. 
    For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere. 
    The second line contains n integers, describe the sequence. 
    Each of following m lines contains three integers s, t, k. 
    [s, t] indicates the interval and k indicates the kth big number in interval [s, t]
     

    Output

    For each test case, output m lines. Each line contains the kth big number.
     

    Sample Input

    1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
     

    Sample Output

    2
     

    分析

    求一段区间内的第k大值。

    主席树~

    code

     1 #include<cstdio>
     2 #include<algorithm>
     3 
     4 using namespace std;
     5 
     6 const int N = 100010;
     7 int sum[N*20],ls[N*20],rs[N*20];
     8 int num[N],t[N],Root[N],tot;
     9 
    10 void build(int l,int r,int &rt) {
    11     rt = ++tot;
    12     sum[rt] = 0;
    13     if (l==r) return ;
    14     int m = (l + r) / 2;
    15     build(l,m,ls[rt]);
    16     build(m+1,r,rs[rt]);
    17 }
    18 void update(int l,int r,int &rt,int last,int p) {
    19     rt = ++tot;
    20     ls[rt] = ls[last];rs[rt] = rs[last];
    21     sum[rt] = sum[last] + 1;
    22     if (l==r) return ;
    23     int m = (l + r) / 2;
    24     if (p<=m) update(l,m,ls[rt],ls[last],p);
    25     else update(m+1,r,rs[rt],rs[last],p);
    26 }
    27 int query(int l,int r,int L,int R,int k) {
    28     if (l==r) return l;
    29     int m = (l + r) / 2;
    30     int cnt = sum[ls[R]] - sum[ls[L]];
    31     if (k <= cnt) return query(l,m,ls[L],ls[R],k);
    32     else return query(m+1,r,rs[L],rs[R],k-cnt);
    33 }
    34 
    35 int main () {
    36     int T,n,m;
    37     scanf("%d",&T);
    38     while (T--) {
    39         tot = 0;
    40         scanf("%d%d",&n,&m);
    41         for (int i=1; i<=n; ++i) 
    42             scanf("%d",&num[i]),t[i] = num[i];
    43         sort(t+1,t+n+1);
    44         int c = unique(t+1,t+n+1)-(t+1);
    45         build(1,c,Root[0]);
    46         for (int i=1; i<=n; ++i) {
    47             int x = lower_bound(t+1,t+c+1,num[i])-t;
    48             update(1,c,Root[i],Root[i-1],x);
    49         }
    50         while (m--) {
    51             int l,r,k;
    52             scanf("%d%d%d",&l,&r,&k);
    53             printf("%d
    ",t[query(1,c,Root[l-1],Root[r],k)]);
    54         }
    55     }
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    推荐一本不错的书《Sencha Ext JS 5 Bootcamp in a Book》
    Libgdx 1.6.0发布,跨平台游戏开发框架
    《.NET最佳实践》与Ext JS/Touch的团队开发
    【翻译】Sencha Cmd中脚本压缩方法之比较
    【翻译】Ext JS 6早期访问版本发布
    【翻译】Ext JS 6有什么新东西?
    SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
    Spring boot+Thymeleaf+easyui集成:js创建组件页面报错
    SpringBoot多模块搭建,依赖管理
    IDEA在同一窗口导入多个项目
  • 原文地址:https://www.cnblogs.com/mjtcn/p/8442512.html
Copyright © 2011-2022 走看看