-
Notifications
You must be signed in to change notification settings - Fork 0
/
UvA 11235.cpp
148 lines (95 loc) · 5.32 KB
/
UvA 11235.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <bits/stdc++.h>
#define lp(i,n) for(int i=0;i<n;i++)
using namespace std;
//p==index of
const int INF = INT_MAX;
int st[4*(100000+5)];
int A[100000+5];
int cnt=0;
void build(int L, int R, int p){
cnt++;
if(L==R){ st[p]=R; return;}
else {
build(L,(L+R)/2, p*2);
build((L+R)/2 +1, R, p*2+1);
int p1= st[ p*2];
int p2= st[p*2 + 1];
int x=A[p1]; int y=A[p2];
if(A[p1]>A[p2])
st[p]= p1;
else{
st[p]=p2;
}
}
}
int rmq (int L, int R, int i ,int j,int p){
if(j<i) return -1;
if(L>j || R <i) return -1;
if (L >= i && R <= j) return st[p];
int p1 = rmq(L,(L+R)/2, i,j,p*2);
int p2 = rmq((L+R)/2 +1, R, i,j,p*2+1);
if(p1==-1) return p2;
if(p2==-1) return p1;
return A[p1]>A[p2]? p1:p2;
}
int main (){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(true){
int n,ll;
cin>>n;
if(n==0) return 0;
cin>>ll;
int nums[n];
int freq[n];
int first_appearance[n];
int last_appearance[n];
map<int,int> freqmap;
map <int,int> firstmap;
for(int i=0; i<n; i++){
cin>>nums[i];
freqmap[nums[i]]++;
if(firstmap.count(nums[i])==false) firstmap[nums[i]]=i;
}
for(int i=0; i<n; i++){
A[i]=freq[i]=freqmap[nums[i]];
first_appearance[i]=firstmap[nums[i]];
}
last_appearance[n-1]=n-1;
int x=n-1;
for(int i=n-2; i>=0; i--){
if(nums[i]!=nums[i+1]){
x=i;
}
last_appearance[i]=x;
}
build(0,n-1,1);
lp(ssss,ll){
int a,b;
cin>>a>>b;
a--;
b--;
if(nums[a]==nums[b]){
cout<<b-a+1<<endl;
continue;
}
vector <int> v;
int t=0;
if(a!=first_appearance[a]){
t=last_appearance[a]-a+1;
a=last_appearance[a]+1;
}
v.push_back(t);
t=0;
if(b!=last_appearance[b]){
t=b-first_appearance[b]+1;
b=first_appearance[b]-1;
}
v.push_back(t);
int oss=rmq(0,n-1,a,b,1);
if(oss!=-1) v.push_back(A[oss]);
sort(v.begin(), v.end());
cout<<v.back()<<endl;
}
}
}