[Coci 2009]PATULJCI
Description
给一个有N个数的序列,有M次询问,每次询问一个区间[l,r]内,是否有一个数出现次数超过一半,如果存在则输出这个数
Sample Input
10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10
Sample Output
no
yes 1
no
yes 1
no
yes 2
no
yes 3
HINT
Notice:输入第二个整数是序列中权值的范围Lim,即1<=ai(1<=i<=n)<=Lim。
1<=Lim<=10000
Code
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500010;
int
n, m, sz, lim,
root[MAXN], lc[MAXN * 20], rc[MAXN * 20], sum[MAXN * 20];
inline int read(){
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
void modify(int l, int r, int x, int &y, int v){
y = ++sz;
sum[y] = sum[x] + 1;
if (l == r) return;
lc[y] = lc[x]; rc[y] = rc[x];
int mid = l + r >> 1;
if (v <= mid) modify(l, mid, lc[x], lc[y], v);
else modify(mid + 1, r, rc[x], rc[y], v);
}
int query(int L, int R){
if (L > R) swap(L, R);
int x = root[L - 1], y = root[R], l = 1, r = lim;
while (l != r){
int mid = l + r >> 1;
if (sum[y] - sum[x] <= (R - L + 1) / 2) return 0;
if (sum[lc[y]] - sum[lc[x]] > (R - L + 1) / 2){
x = lc[x]; y = lc[y]; r = mid;
}else if (sum[rc[y]] - sum[rc[x]] > (R - L + 1) / 2){
x = rc[x]; y = rc[y]; l = mid + 1;
}else return 0;
}
return l;
}
int main(){
n = read(); lim = read();
for (int i = 1; i <= n; i++) modify(1, lim, root[i - 1], root[i], read());
m = read();
while (m--){
int ans = query(read(), read());
if (!ans) puts("no");
else printf("yes %d\n", ans);
}
return 0;
}