1
#include
<
stdio.h
>
2
#include
<
string
.h
>
3
#include
<
algorithm
>
4
5
using
namespace
std;
6
7
inline
int
MID(
int
s,
int
e) {
return
(s
+
e)
>>
1
;}
8
9
const
int
MaxN
=
100010
;
10
int
merge_tree[
20
][MaxN], n, m, dep[MaxN];
11
int
u, v, k, flag;
12
13
void
Merge(
int
begin,
int
end,
int
d) {
14
if
(begin
+
1
==
end) {
15
merge_tree[d][begin]
=
merge_tree[
0
][begin];
16
dep[begin]
=
d;
17
return
;
18
}
19
int
Mid
=
MID(begin, end);
20
Merge(begin, Mid, d
+
1
);
21
Merge(Mid, end, d
+
1
);
22
int
i, j, k;
23
for
(i
=
begin, j
=
Mid, k
=
begin; i
!=
Mid
&&
j
!=
end;) {
24
if
(merge_tree[d
+
1
][i]
<
merge_tree[d
+
1
][j])
25
merge_tree[d][k
++
]
=
merge_tree[d
+
1
][i
++
];
26
else
27
merge_tree[d][k
++
]
=
merge_tree[d
+
1
][j
++
];
28
}
29
if
(i
!=
Mid) {
30
for
(; i
!=
Mid;
++
i) merge_tree[d][k
++
]
=
merge_tree[d
+
1
][i];
31
}
32
if
(j
!=
end) {
33
for
(; j
!=
end;
++
j) merge_tree[d][k
++
]
=
merge_tree[d
+
1
][j];
34
}
35
}
36
37
int
search(
int
begin,
int
end,
int
s,
int
e,
int
x,
int
d) {
38
int
Mid;
39
if
(s
==
begin
&&
end
==
e) {
40
if
(x
>
merge_tree[d][end
-
1
])
41
return
end
-
begin;
42
if
(x
<
merge_tree[d][begin])
43
return
0
;
44
int
u
=
begin
-
1
, v
=
end;
45
while
(u
+
1
<
v) {
46
Mid
=
MID(u, v);
47
if
(merge_tree[d][Mid]
<=
x) {
48
if
(merge_tree[d][Mid]
==
x)
49
flag
++
;
50
u
=
Mid;
51
}
52
else
53
v
=
Mid;
54
}
55
return
u
-
begin
+
1
;
56
}
57
Mid
=
MID(begin, end);
58
if
(s
>=
Mid)
59
return
search(Mid, end, s, e, x, d
+
1
);
60
if
(e
<=
Mid)
61
return
search(begin, Mid, s, e, x, d
+
1
);
62
return
search(begin, Mid, s, Mid, x, d
+
1
)
+
search(Mid, end, Mid, e, x, d
+
1
);
63
}
64
65
void
work() {
66
int
begin
=
-
1
, end
=
n, Mid;
67
while
(begin
+
1
<
end) {
68
Mid
=
MID(begin, end);
69
flag
=
0
;
70
int
sum
=
search(
0
, n, u, v
+
1
, merge_tree[
0
][Mid],
0
);
71
if
(flag
&&
sum
-
flag
<
k
&&
sum
>=
k) {
72
printf(
"
%d\n
"
, merge_tree[
0
][Mid]);
73
return
;
74
}
75
else
if
(sum
<
k)
76
begin
=
Mid;
77
else
78
end
=
Mid;
79
}
80
}
81
82
int
main() {
83
while
(scanf(
"
%d%d
"
,
&
n,
&
m)
!=
EOF) {
84
for
(
int
i
=
0
; i
<
n;
++
i) scanf(
"
%d
"
, merge_tree[
0
]
+
i);
85
Merge(
0
, n,
0
);
86
for
(
int
i
=
0
; i
<
m;
++
i) {
87
scanf(
"
%d%d%d
"
,
&
u,
&
v,
&
k);
88
u
--
; v
--
;
89
work();
90
}
91
}
92
return
0
;
93
}
94