#include <bits/stdc++.h>
/// 500 485
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=1e5+100,M=20;
int nxtl[N][M];
int nxtr[N][M];
ll a[N];
vector <int> ja[N];
ll seg[N*4];
void build(ll nod,ll l,ll r){
if (r-l==1){
seg[nod]=a[l];
return ;
}
ll mid=(r+l)/2;
build(nod*2,l,mid);
build(nod*2+1,mid,r);
seg[nod]=max(seg[nod*2],seg[nod*2+1]);
}
ll get(ll nod,ll l,ll r,ll L,ll R){
if (l>=R || L>=r) return 0;
if (l>=L && r<=R){
return seg[nod];
}
ll mid=(r+l)/2;
return max(get(nod*2,l,mid,L,R),get(nod*2+1,mid,r,L,R));
}
ll num(ll l,ll r,ll x){
if (l>r) swap(l,r);
ll z=upper_bound(ja[x].begin(),ja[x].end(),r)-ja[x].begin();
ll t=lower_bound(ja[x].begin(),ja[x].end(),l)-ja[x].begin();
return z-t;
}
int L[N],R[N];
int32_t main(){
sync;
ll n,k,q;
cin >> n >> k >> q ;
for (int i=1;i<=n;i++){
cin >> a[i];
ja[a[i]].pb(i);
}
build(1,1,n+1);
vector <int> agh;
agh.pb(0);
a[0]=N-1;
for (int i=1;i<=n;i++){
while(agh.size() && a[agh.back()]<a[i]) agh.pop_back();
L[i]=agh.back();
agh.pb(i);
}
agh.clear();
agh.pb(n+1);
a[n+1]=N-1;
for (int i=n;i;i--){
while(agh.size() && a[agh.back()]<a[i]) agh.pop_back();
R[i]=agh.back();
agh.pb(i);
}
for (int i=N-1;i;i--){
for (auto u : ja[i]){
nxtl[u][0]=L[u];
nxtr[u][0]=R[u];
}
}
for (int i=0;i<M;i++){
nxtl[0][i]=0;
nxtr[0][i]=0;
nxtl[n+1][i]=n+1;
nxtr[n+1][i]=n+1;
}
for (int j=1;j<M;j++){
for (int i=1;i<=n;i++){
nxtl[i][j]=min(nxtl[nxtl[i][j-1]][j-1],nxtl[nxtr[i][j-1]][j-1]);
nxtr[i][j]=max(nxtr[nxtr[i][j-1]][j-1],nxtr[nxtl[i][j-1]][j-1]);
}
}
while(q--){
ll x,y;
cin >> x >> y;
if (x>y) swap(x,y);
if (x+1==y){
cout << 0 << endl;
continue;
}
/*
ll z=get(1,1,n+1,x+1,y);
if (z<min(a[x],a[y])){
cout << 0 << endl;
continue;
}
*/
ll l=x,r=x;
ll ans=0;
for (int i=M-1;i>-1;i--){
ll newl=l,newr=r;
newl=min(nxtl[l][i],nxtl[r][i]);
newr=max(nxtr[l][i],nxtr[r][i]);
if (newr<y){
l=newl;
r=newr;
ans+=(1<<i);
}
}
//cout << l << " " << r << " " << ans << endl;
ll L=y,R=y;
for (int i=M-1;i>-1;i--){
ll newl=L,newr=R;
newl=min(nxtl[L][i],nxtl[R][i]);
newr=max(nxtr[L][i],nxtr[R][i]);
if (newl>r){
L=newl;
R=newr;
ans+=(1<<i);
}
}
cout << ans<< endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
2 ms |
2796 KB |
Output is correct |
5 |
Correct |
2 ms |
2796 KB |
Output is correct |
6 |
Correct |
2 ms |
2796 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
2 ms |
2796 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3052 KB |
Output is correct |
2 |
Correct |
80 ms |
39916 KB |
Output is correct |
3 |
Correct |
81 ms |
39916 KB |
Output is correct |
4 |
Correct |
82 ms |
39788 KB |
Output is correct |
5 |
Correct |
83 ms |
39788 KB |
Output is correct |
6 |
Correct |
86 ms |
39916 KB |
Output is correct |
7 |
Correct |
105 ms |
41196 KB |
Output is correct |
8 |
Correct |
84 ms |
40300 KB |
Output is correct |
9 |
Correct |
97 ms |
41064 KB |
Output is correct |
10 |
Correct |
94 ms |
40804 KB |
Output is correct |
11 |
Correct |
93 ms |
40936 KB |
Output is correct |
12 |
Correct |
92 ms |
40804 KB |
Output is correct |
13 |
Correct |
94 ms |
40804 KB |
Output is correct |
14 |
Correct |
95 ms |
40940 KB |
Output is correct |
15 |
Correct |
100 ms |
40804 KB |
Output is correct |
16 |
Correct |
92 ms |
40820 KB |
Output is correct |
17 |
Correct |
82 ms |
40044 KB |
Output is correct |
18 |
Correct |
85 ms |
40172 KB |
Output is correct |
19 |
Correct |
92 ms |
41836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
40940 KB |
Output is correct |
2 |
Correct |
199 ms |
41580 KB |
Output is correct |
3 |
Correct |
207 ms |
41452 KB |
Output is correct |
4 |
Correct |
193 ms |
41452 KB |
Output is correct |
5 |
Correct |
191 ms |
41452 KB |
Output is correct |
6 |
Correct |
204 ms |
41792 KB |
Output is correct |
7 |
Correct |
192 ms |
41580 KB |
Output is correct |
8 |
Correct |
237 ms |
41648 KB |
Output is correct |
9 |
Correct |
295 ms |
42092 KB |
Output is correct |
10 |
Correct |
293 ms |
42220 KB |
Output is correct |
11 |
Correct |
300 ms |
42092 KB |
Output is correct |
12 |
Correct |
292 ms |
42092 KB |
Output is correct |
13 |
Correct |
293 ms |
42088 KB |
Output is correct |
14 |
Correct |
259 ms |
41704 KB |
Output is correct |
15 |
Correct |
212 ms |
41204 KB |
Output is correct |
16 |
Correct |
195 ms |
41064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
41708 KB |
Output is correct |
2 |
Correct |
179 ms |
41836 KB |
Output is correct |
3 |
Correct |
180 ms |
41068 KB |
Output is correct |
4 |
Correct |
176 ms |
42092 KB |
Output is correct |
5 |
Correct |
293 ms |
42092 KB |
Output is correct |
6 |
Correct |
261 ms |
42856 KB |
Output is correct |
7 |
Correct |
258 ms |
42856 KB |
Output is correct |
8 |
Correct |
261 ms |
42468 KB |
Output is correct |
9 |
Correct |
242 ms |
42728 KB |
Output is correct |
10 |
Correct |
246 ms |
42604 KB |
Output is correct |
11 |
Correct |
238 ms |
42468 KB |
Output is correct |
12 |
Correct |
241 ms |
42724 KB |
Output is correct |
13 |
Correct |
239 ms |
42604 KB |
Output is correct |
14 |
Correct |
257 ms |
42980 KB |
Output is correct |
15 |
Correct |
267 ms |
43364 KB |
Output is correct |
16 |
Correct |
264 ms |
43748 KB |
Output is correct |
17 |
Correct |
232 ms |
42728 KB |
Output is correct |
18 |
Correct |
243 ms |
42728 KB |
Output is correct |
19 |
Correct |
237 ms |
42728 KB |
Output is correct |
20 |
Correct |
203 ms |
42092 KB |
Output is correct |
21 |
Correct |
155 ms |
41708 KB |
Output is correct |
22 |
Correct |
157 ms |
41580 KB |
Output is correct |
23 |
Correct |
160 ms |
43372 KB |
Output is correct |