#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 3e5 + 5, kMaxD = 550;
int a[kMaxN], ans[kMaxN], st[kMaxD], ed[kMaxD], p[kMaxN], len, l = 1, r, n, m;
struct M { // 值域分块
int v[kMaxN], t[kMaxD];
// st:块首,ed:块尾,v:i出现次数,p:i所在块编号,t:块最大值
M() {
for (int i = 0; i < kMaxN; i++) {
p[i] = v[i] = 0;
}
}
void I() {
for (int i = 1; i <= len; i++) {
t[i] = 0;
for (int j = st[i]; j <= ed[i]; j++) {
v[j] = 0;
}
}
}
void U(int x) {
v[x]++, t[p[x]] = max(t[p[x]], v[x]);
}
} s[2]; // s[0]:存答案,s[1]:回滚
struct T {
int p, l, r, id, k;
T() {
p = l = r = id = k = 0;
}
bool operator<(const T& a) const {
return a.p == p ? r < a.r : p < a.p;
}
} q[kMaxN];
/* --------------- fast io --------------- */ // begin
namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return '\n';
}
return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
fwrite(buf, 1, S - buf, stdout);
S = buf;
}
inline void putchar(char c) {
*S++ = c;
if (S == T) flush();
}
struct NTR {
~NTR() { flush(); }
} ztr;
} // namespace Fwrite
#ifdef ONLINE_JUDGE
#define getchar Fread ::getchar
#define putchar Fwrite ::putchar
#endif
namespace Fastio {
struct Reader {
template <typename T>
Reader& operator>>(T& x) {
char c = getchar();
T f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
x *= f;
return *this;
}
Reader& operator>>(char& c) {
c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return *this;
}
Reader& operator>>(char* str) {
int len = 0;
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
while (c != ' ' && c != '\n' && c != '\r') { // \r\n in windows
str[len++] = c;
c = getchar();
}
str[len] = '\0';
return *this;
}
Reader() {}
} cin;
const char endl = '\n';
struct Writer {
template <typename T>
Writer& operator<<(T x) {
if (x == 0) {
putchar('0');
return *this;
}
if (x < 0) {
putchar('-');
x = -x;
}
static int sta[45];
int top = 0;
while (x) {
sta[++top] = x % 10;
x /= 10;
}
while (top) {
putchar(sta[top] + '0');
--top;
}
return *this;
}
Writer& operator<<(char c) {
putchar(c);
return *this;
}
Writer& operator<<(char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator<<(const char* str) {
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer() {}
} cout;
} // namespace Fastio
#define cin Fastio ::cin
#define cout Fastio ::cout
#define endl Fastio ::endl
/* --------------- fast io --------------- */ // end
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int d = sqrt(n);
len = n / d + bool(n % d);
for (int i = 1; i <= len; i++) {
st[i] = ed[i - 1] + 1, ed[i] = min(n, ed[i - 1] + d);
for (int j = st[i]; j <= ed[i]; j++) {
p[j] = i;
}
}
for (int i = 1; i <= m; i++) {
cin >> q[i].l >> q[i].r >> q[i].k;
q[i].k = (q[i].r - q[i].l + 1) / q[i].k + 1, q[i].id = i, q[i].p = q[i].l / d + bool(q[i].l % d);
}
sort(q + 1, q + m + 1);
for (register int i = 1, pre = 0; i <= m; i++) {
if (q[i].r / d + bool(q[i].r % d) == q[i].p) {
for (register int j = l; j <= r; j++) {
s[0].v[a[j]] = s[1].v[a[j]] = 0;
s[0].t[p[a[j]]] = s[1].t[p[a[j]]] = 0;
}
for (register int j = q[i].l; j <= q[i].r; j++) {
s[0].U(a[j]);
}
} else {
if (q[i].p != pre) {
for (register int j = l; j <= r; j++) {
s[0].v[a[j]] = s[1].v[a[j]] = 0;
s[0].t[p[a[j]]] = s[1].t[p[a[j]]] = 0;
}
for (register int j = q[i].p * d + 1; j <= q[i].r; j++) {
s[0].U(a[j]), s[1].U(a[j]);
}
for (register int j = q[i].l; j <= q[i].p * d; j++) {
s[0].U(a[j]);
}
} else {
for (; r < q[i].r; r++, s[0].U(a[r]), s[1].U(a[r])) {
}
if (l < q[i].l) {
int ep = q[i].p * d;
for (; l <= ep; s[0].v[a[l]] = s[1].v[a[l]], l++) {
}
for (register int j = 1; j <= len; j++) {
s[0].t[j] = s[1].t[j];
}
}
for (; l > q[i].l; l--, s[0].U(a[l])) {
}
}
pre = q[i].p;
}
int p = -1;
for (register int j = 1; j <= len; j++) {
if (s[0].t[j] >= q[i].k) {
p = j;
break;
}
}
if (p == -1) {
ans[q[i].id] = -1;
} else {
for (register int j = st[p]; j <= ed[p]; j++) {
if (s[0].v[j] >= q[i].k) {
ans[q[i].id] = j;
break;
}
}
}
l = q[i].l, r = q[i].r;
}
for (register int i = 1; i <= m; i++) {
cout << ans[i] << "\n";
}
return 0;
}剪贴板
剪贴板 9shp8yib
剪贴板信息
剪贴板信息
- 收录
- 更新