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
66
67
68
69
70
71
72
73
74
75
76
77
| #include <cstdio>
#include <cctype>
#define mid ((l+r)>>1)
#define pp pair<int,int>
using namespace std;
namespace fast_io {
//...
}using namespace fast_io;
const int MAXN = 600000;
namespace prSegTree{
int val[MAXN*20];int ls[MAXN*20],rs[MAXN*20];
int cnt = 1;
void build(int &nown,int l,int r){
nown = cnt++;
if(l == r)
val[nown] = 0;
else{
build(ls[nown],l,mid);
build(rs[nown],mid+1,r);
}
}
void update(int &nown,int pre,int l,int r,int pos,int d){
nown = cnt++;val[nown] = val[pre]+d;
ls[nown] = ls[pre];rs[nown] = rs[pre];
if(l == r)
return;
else{
if(pos <= mid)
update(ls[nown],ls[pre],l,mid,pos,d);
if(mid+1 <= pos)
update(rs[nown],rs[pre],mid+1,r,pos,d);
}
}
int query(int nowl,int nowr,int l,int r,int limit){
if(l == r)
return l;
else{
if(val[ls[nowr]] - val[ls[nowl]]>=limit)
return query(ls[nowl],ls[nowr],l,mid,limit);
if(val[rs[nowr]] - val[rs[nowl]]>=limit)
return query(rs[nowl],rs[nowr],mid+1,r,limit);
return 0;
}
}
}
int n,m;
int root[MAXN];
void init(){
read(n),read(m);
prSegTree::build(root[0],1,n);
int tmp;
for(int i = 1;i<=n;i++){
read(tmp);
root[i] = root[i-1];
prSegTree::update(root[i],root[i-1],1,n,tmp,1);
}
}
void solve(){
int l,r;
for(int i = 1;i<=m;i++){
read(l),read(r);
print(prSegTree::query(root[l-1],root[r],1,n,(r-l+1)/2+1)),print('\n');
}
}
int main(){
init();
solve();
flush();
return 0;
}
|