-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpoj_MKTHNUM.cpp
135 lines (113 loc) · 2.91 KB
/
Spoj_MKTHNUM.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Spoj problem: MKTHNUM - K-th Number
Find kth smallest by Merge-Sort-Tree in given range (i, j) of array
Here you will learn implementation of Merge_Sort_Tree.
Happy_Coding(^_^)
*/
/* pmaik
░▒▓█►─═ Maneesh ═─◄█▓▒░
( •̪●)
███████]▄▄▄▄▄▄▄▄▃ ▃ ▃
▂▄▅█████████▅▄▃▂
[███████████████████]
...◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...... */
#include<bits/stdc++.h>
using namespace std;
#define inf() ifstream cin("in00.txt")
#define onf() ofstream cout("out00.txt")
#define ll long long int
#define pb push_back
#define pf push_front
#define mkp make_pair
#define pii pair<ll,ll>
#define fi first
#define se second
#define M 1000000007
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_set1 tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define inff -1000000000000000
vector<ll>st[300000];
vector<pair<ll,ll>>vec;
void build_tree(ll si, ll ss, ll se)
{
if(ss==se)
{
st[si].push_back(vec[ss].second);
return;
}
ll mid=(ss+se)/2;
build_tree(2*si, ss, mid);
build_tree(2*si+1, mid+1, se);
ll i=0,j=0;
while(i<st[2*si].size() && j<st[2*si+1].size())
{
if(st[2*si][i]<st[2*si+1][j])
{
st[si].push_back(st[2*si][i]);
i++;
}
else
{
st[si].push_back(st[2*si+1][j]);
j++;
}
}
while(i<st[2*si].size())
{
st[si].push_back(st[2*si][i]);
i++;
}
while(j<st[2*si+1].size())
{
st[si].push_back(st[2*si+1][j]);
j++;
}
}
ll find_kth(ll si, ll ss, ll se, ll l, ll r, ll k)
{
if(ss==se)
return st[si][0];
ll mid=(ss+se)/2;
ll pos1=lower_bound(st[2*si].begin(), st[2*si].end(), l)-st[2*si].begin();
ll pos2=upper_bound(st[2*si].begin(), st[2*si].end(), r)-st[2*si].begin();
ll c=pos2-pos1;
if(c>=k)
return find_kth(2*si, ss, mid, l, r, k);
else
return find_kth(2*si+1, mid+1, se, l, r, k-c);
}
void solve(ll tc)
{
ll n,q;
cin>>n>>q;
ll i,j,k,a[n+5];
vec.push_back(make_pair(inff, 0));
for(i=1; i<=n; i++)
{
cin>>a[i];
vec.push_back(make_pair(a[i], i));
}
sort(vec.begin(), vec.end());
build_tree(1, 1, n);
while(q--)
{
cin>>i>>j>>k;
ll idx=find_kth(1, 1, n, i, j, k);
cout<<a[idx]<<endl;
}
}
int main()
{
fastio;
ll t=1;
// cin>>t;
for(ll tc=1; tc<=t; tc++)
{
solve(tc);
}
return 0;
}