K-th Number

区间第K大

题目链接

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <iostream>
 5 using namespace std;
 6 typedef long long ll;
 7 inline int read()
 8 {
 9     int x=0,f=1;char ch=getchar();
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
11     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
12     return x*f;
13 }
14 
15 /********************************************************************/
16 
17 const int maxn = 1e5+7;
18 int n, m, cnt, root[maxn], a[maxn], x, y, k;
19 
20 struct node{
21     int l, r, sum;
22 }T[maxn*40];
23 
24 vector<int>v;
25 int getid(int x){return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;}    //离散化
26         
27 void update(int l, int r, int &x, int y, int pos){
28     T[++cnt] = T[y], T[cnt].sum++, x = cnt;
29     if(l == r) return ;
30     int mid = (l+r)>>1;
31     if(mid >= pos) update(l, mid, T[x].l, T[y].l, pos);
32     else update(mid+1, r, T[x].r, T[y].r, pos);
33 }
34 
35 int query(int l, int r, int x, int y, int k){
36     if(l == r) return l;
37     int mid = (l+r)>>1;
38     int sum = T[T[y].l].sum - T[T[x].l].sum;
39     if(sum >= k) return query(l, mid, T[x].l, T[y].l, k);
40     else return query(mid+1, r, T[x].r, T[y].r, k-sum);
41 }
42 
43 int main(){
44     n = read(); m = read();
45     for(int i = 1;i <= n;i++){
46         a[i] = read();
47         v.push_back(a[i]);
48     }
49     //离散化
50     sort(v.begin(), v.end());
51     v.erase(unique(v.begin(), v.end()), v.end());
52     //unique 去重
53     for(int i = 1;i <= n;i++) update(1, n, root[i], root[i-1], getid(a[i]));
54     for(int i = 1;i <= m;i++){
55         x = read(); y = read(); k = read();
56         printf("%d\n", v[query(1, n, root[x-1], root[y], k) - 1]);  //离散化回来
57     }
58     return 0;
59 }

 

posted @ 2018-09-24 22:32  ouyang_wsgwz  阅读(103)  评论(0编辑  收藏  举报