真正的危机不是机器人像人一样思考,而是人像机器一样思考。 ——凉宫春日的忧郁

[HDU 2665]Kth number

[HDU 2665]Kth number

题目

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

OUTPUT

2

解题报告

初识主席树

虽然只会静态的2333

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 //#define mem(x) memset((x),0,sizeof(x))
 7 inline int read(){
 8     int sum(0);
 9     char ch(getchar());
10     for(;ch<'0'||ch>'9';ch=getchar());
11     for(;ch>='0'&&ch<='9';sum=sum*10+(ch^48),ch=getchar());
12     return sum;
13 }
14 int T;
15 int n,m;
16 int a[100005],num[100005];
17 int cnt,size;
18 int rt[2000005],sum[2000005],lch[2000005],rch[2000005];
19 inline void clear(){
20 //    mem(a),mem(num),mem(rt),mem(sum),mem(lch),mem(rch);
21     cnt=size=n=m=0;
22 }
23 inline void build(int &x,int l,int r){
24     x=++cnt;
25     sum[x]=0;
26     if(l==r){
27         lch[x]=rch[x]=0;
28         return;
29     }
30     int mid((l+r)>>1);
31     build(lch[x],l,mid);
32     build(rch[x],mid+1,r);
33 }
34 inline void update(int &x,int las,int pos,int l,int r){
35     x=++cnt;
36     lch[x]=lch[las];
37     rch[x]=rch[las];
38     sum[x]=sum[las]+1;
39     if(l==r)
40         return;
41     int mid((l+r)>>1);
42     if(pos<=mid)
43         update(lch[x],lch[las],pos,l,mid);
44     else
45         update(rch[x],rch[las],pos,mid+1,r);
46 }
47 inline int query(int ll,int rr,int l,int r,int k){
48     if(l==r)
49         return l;
50     int mid((l+r)>>1);
51     int cnt(sum[lch[rr]]-sum[lch[ll]]);
52     if(k<=cnt)
53         return query(lch[ll],lch[rr],l,mid,k);
54     return query(rch[ll],rch[rr],mid+1,r,k-cnt);
55 }
56 int main(){
57     T=read();
58     while(T--){
59         clear();
60         n=read(),m=read();
61         for(int i=1;i<=n;++i)
62             num[i]=a[i]=read();
63         sort(num+1,num+n+1);
64         size=unique(num+1,num+n+1)-num-1;
65         for(int i=1;i<=n;++i)
66             a[i]=lower_bound(num+1,num+size+1,a[i])-num;
67         build(rt[0],1,size);
68         for(int i=1;i<=n;++i)
69             update(rt[i],rt[i-1],a[i],1,size);
70         for(int i=1;i<=m;++i){
71             int l(read()),r(read()),k(read());
72             int ans(query(rt[l-1],rt[r],1,size,k));
73             printf("%d\n",num[ans]);
74         }
75     }
76 }
View Code

 

posted @ 2017-09-23 16:32  Hzoi_Mafia  阅读(162)  评论(0编辑  收藏  举报
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。 ——死神