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
| // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
struct Node { int color; int count; };
Node mergeNode(const Node &L, const Node &R) {
if (L.color == -1) return R;
if (R.color == -1) return L;
if (L.color == R.color) return {L.color, L.count + R.count};
if (L.count > R.count) return {L.color, L.count - R.count};
return {R.color, R.count - L.count};
}
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);
int N,C; if(!(cin>>N>>C)) return 0;
vector<int> a(N+1);
vector<vector<int>> pos(C+1);
for(int i=1;i<=N;++i){cin>>a[i]; if(1<=a[i] && a[i]<=C) pos[a[i]].push_back(i);}
vector<Node> seg(4*N+4, {-1,0});
function<void(int,int,int)> build = [&](int idx,int l,int r){
if(l==r){ seg[idx]={a[l],1}; return; }
int m=(l+r)>>1; build(idx<<1,l,m); build(idx<<1|1,m+1,r);
seg[idx]=mergeNode(seg[idx<<1],seg[idx<<1|1]);
};
function<Node(int,int,int,int,int)> query = [&](int idx,int l,int r,int ql,int qr)->Node{
if(qr<l||r<ql) return Node{-1,0};
if(ql<=l && r<=qr) return seg[idx];
int m=(l+r)>>1; Node L=query(idx<<1,l,m,ql,qr), R=query(idx<<1|1,m+1,r,ql,qr);
return mergeNode(L,R);
};
auto countInRange = [&](const vector<int>& v, int L, int R){
auto itL=lower_bound(v.begin(),v.end(),L);
auto itR=upper_bound(v.begin(),v.end(),R);
return (int)(itR-itL);
};
build(1,1,N);
int M; cin>>M;
while(M--){
int A,B; cin>>A>>B; if(A>B) swap(A,B);
Node cand=query(1,1,N,A,B);
int color=cand.color; int len=B-A+1; int cnt=0;
if(color!=-1 && 1<=color && color<=C) cnt=countInRange(pos[color],A,B);
if(cnt>len/2) cout<<"yes "<<color<<'\n'; else cout<<"no\n";
}
return 0; }
|