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
| #include <bits/stdc++.h> using namespace std;
const int maxc = 1e4 + 5, maxn = 3e5 + 5, maxm = 1e4 + 5;
struct node { int l, r; int id; };
node quary[maxm]; int book[maxc], color[maxn], belong[maxn], anss[maxm]; int n, m, c, sqrtn;
bool compare(node a, node b) { return belong[a.l] != belong[b.l] ? belong[a.l] < belong[b.l] : (belong[a.l] & 1 ? a.r < b.r : a.r > b.r); }
int main() { scanf("%d %d", &n, &c); sqrtn = pow(n, 0.5); for (int i = 1; i <= n; i++) { scanf("%d", color + i); belong[i] = i / sqrtn; } scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d %d", &quary[i].l, &quary[i].r); quary[i].id = i; } sort(quary + 1, quary + m + 1, compare); int l = 1, r = 0, maxi; for (int i = 1; i <= m; i++) { node q = quary[i]; while (r < q.r) { r++; book[color[r]]++; } while (l > q.l) { l--; book[color[l]]++; } while (r > q.r) { book[color[r]]--; r--; } while (l < q.l) { book[color[l]]--; l++; } int maxid = 0; for (int j = 1; j <= c; j++) { if (book[j] > book[maxid]) maxid = j; } if (book[maxid] > (r - l + 1) >> 1) { anss[q.id] = maxid; } else anss[q.id] = -1; } for (int i = 1; i <= m; i++) { if (anss[i] == -1) printf("no\n"); else printf("yes %d\n", anss[i]); } return 0; }
|