「BZOJ3524」[POI2014]Couriers

Description

给一个长度为nn的序列aa1ain1\leq a_i \leq n
mm组询问,每次询问一个区间[l,r][l,r],是否存在一个数在[l,r][l,r]中出现的次数大于(rl+1)/2(r-l+1)/2。如果存在,输出这个数,否则输出00

Input

第一行两个数nnmm
第二行nn个数,aia_i
接下来mm行,每行两个数l,rl,r,表示询问[l,r][l,r]这个区间。

Output

mm行,每行对应一个答案。

Sample Input

1
2
3
4
5
6
7
7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6

Sample Output

1
2
3
4
5
1
0
3
0
4

HINT

n,m500000n,m\leq 500000

Solution

随便在主席树上二分就行了~

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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#define mk make_pair
#define fi first
#define nd second
#define pii pair<int,int>
#define pb push_back
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
inline ll read() {ll x = 0; char ch = getchar(), w = 1;while(ch < '0' || ch > '9') {if(ch == '-') w = -1;
ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * w;}
void write(ll x) {if(x < 0) putchar('-'), x = -x;if(x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(ll x) {write(x);puts("");}
const int N = 510000;
int n, m;
struct Tre {
int l, r, s;
}t[N * 35];
int tot, rt[N];
void ins(int pre,int &o, int l, int r, int x) {
o = ++tot;
t[o] = t[pre];
++t[o].s;
if(l == r) return;
int mid = (l + r) >> 1;
if(x <= mid) ins(t[pre].l, t[o].l, l, mid, x);
else ins(t[pre].r, t[o].r, mid+1,r,x);
}
int query(int pre,int o, int l, int r, int val) {
if(!o || t[o].s - t[pre].s <= val) return 0;
if(l == r) return l;
int mid = (l + r) >> 1;
if(t[t[o].l].s - t[t[pre].l].s> val) return query(t[pre].l, t[o].l, l ,mid, val);
else return query(t[pre].r, t[o].r, mid + 1, r, val);
}
int a[N];
int main() {
n = read(), m = read();
for(int i = 1; i <= n; ++i) {
a[i] = read();
ins(rt[i - 1], rt[i], 1, 1e9 + 7, a[i]);
}
for(int i = 1; i <= m; ++i) {
int x = read(), y = read();
int ans = query(rt[x - 1], rt[y], 1, 1e9 + 7, (y - x +1) / 2);
writeln(ans);
}
return 0;
}