-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKTHNUMBER
More file actions
61 lines (61 loc) · 1.59 KB
/
KTHNUMBER
File metadata and controls
61 lines (61 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//SOURCE: HACKERRANK
#include <bits/stdc++.h>
using namespace std;
vector <int> indices[100005]; int arr[100005];
struct node
{
node * first; node * second; int count;
node (node * a, node * b, int c){first=a; second=b; count=c;}
};
node * root = new node (NULL, NULL, 0);
node * x[100005];
node * insert (int start, int end, node * r, int num)
{
if (start<=num && end>=num)
{
if (r==NULL) r=new node (NULL, NULL, 0);
if (start==end)
{
return new node (r->first, r->second, r->count+1);
}
node * n=insert(start, (start+end)/2, r->first, num);
node * m=insert((start+end)/2+1, end, r->second, num);
int z= (n==NULL)?0:n->count;
int zz=(m==NULL)?0:m->count;
return new node (n, m, z+zz);
}
return r;
}
int query (int start, int end, node * a, int kth)
{
if (start==end) return start;
int z=(a->first==NULL)?0:a->first->count;
if (z>=kth)
{
return query(start, (start+end)/2, a->first, kth);
}
else return query((start+end)/2+1, end, a->second, kth-z);
}
int main()
{
int a,b; cin >> a >> b;
for (int g=1; g<=a; g++)
{
int c; cin >> c; arr[g]=c; indices[c].push_back(g);
}
x[100001]=root;
for (int g=100000; g>=1; g--)
{
x[g]=x[g+1];
for (int y=0; y<indices[g].size(); y++)
{
x[g]=insert(1, 100000, x[g], indices[g][y]);
}
}
for (int g=0; g<b; g++)
{
int above, kth; cin >> above >> kth;
cout << arr[query(1, 100000, x[above], kth)]<< '\n';
}
return 0;
}