zoukankan      html  css  js  c++  java
  • HDU 2665(主席树,无修改第k小)

                                    Kth number

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

    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
    Source
    说实话  我不知道我该怎么去解释 因为这就是主席树的板子
    无区间修改的主席树 不解释
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cctype>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<map>
     8 #include<stack>
     9 #include<set>
    10 #include<vector>
    11 #include<algorithm>
    12 #include<string.h>
    13 #define ll long long
    14 #define LL unsigned long long
    15 using namespace std;
    16 const int INF=0x3f3f3f3f;
    17 const double eps=0.0000000001;
    18 const int N=100000+10;
    19 struct node{
    20     int left,right;
    21     int val;
    22 }tree[N*40];
    23 int cnt;
    24 int a[N],b[N];
    25 int root[N];
    26 int build(int l,int r){
    27     int pos=++cnt;
    28     tree[pos].val=0;
    29     if(l==r)return pos;
    30     int mid=(l+r)>>1;
    31     tree[pos].left=build(l,mid);
    32     tree[pos].right=build(mid+1,r);
    33     return pos;
    34 }
    35 void update(int pre,int &now,int x,int l,int r){
    36     tree[++cnt]=tree[pre];
    37     now=cnt;
    38     tree[now].val++;
    39     if(l==r)return;
    40     int mid=(l+r)>>1;
    41     if(x<=mid){
    42         update(tree[pre].left,tree[now].left,x,l,mid);
    43     }
    44     else{
    45         update(tree[pre].right,tree[now].right,x,mid+1,r);
    46     }
    47 
    48 }
    49 int query(int L,int R,int k,int l,int r){
    50     if(l==r) return l;
    51     int mid=(l+r)>>1;
    52     int sum=tree[tree[R].left].val-tree[tree[L].left].val;
    53     if(k<=sum){
    54         return query(tree[L].left,tree[R].left,k,l,mid);
    55     }
    56     else{
    57         return query(tree[L].right,tree[R].right,k-sum,mid+1,r);
    58     }
    59 
    60 }
    61 int main()
    62 {
    63     int t;
    64     scanf("%d",&t);
    65     while(t--){
    66         memset(root,0,sizeof(root));
    67         memset(a,0,sizeof(a));
    68         memset(b,0,sizeof(b));
    69         int n,m;
    70         scanf("%d%d",&n,&m);
    71         cnt=0;
    72         for(int i=1;i<=n;i++) {
    73             scanf("%d",&a[i]);
    74             b[i]=a[i];
    75         }
    76         sort(b+1,b+1+n);
    77         int tt = unique(b+1,b+1+n)-b-1;
    root[0]=build(1,tt);
    78 for(int i=1;i<=n;i++) { 79 a[i]=lower_bound(b+1,b+1+tt,a[i])-b;//二分找到a[i]的位置 81 update(root[i-1],root[i],a[i],1,tt);//root[i-1]表示上一个版本的线段树 82 } 83 for(int i=1;i<=m;i++) { 84 int l,r,k; 85 scanf("%d%d%d",&l,&r,&k); 86 int ans=query(root[l-1],root[r],k,1,tt); 87 printf("%d ", b[ans]); 88 } 89 } 90 return 0; 91 }
  • 相关阅读:
    牛客多校第九场 && ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解
    2019牛客多校第九场B Quadratic equation(二次剩余定理)题解
    URAL 1132 Square Root(二次剩余定理)题解
    牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解
    配置android.support.v7.widget.Toolbar 搜索框样式
    Google之路
    Editplus 竖选,竖插入技巧
    JNDI
    Spring Hibernate Transaction示例
    AOP 入门
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6794098.html
Copyright © 2011-2022 走看看