#include <bits/stdc++.h>
using namespace std;
vector <int> V[101010];
int D[101010], T[101010];
int L[101010], CL[101010], R[101010], CR[101010];
int S[101010], sp;
int n, k, q;
void update(int p, int v)
{
for(; p<=n; p+=p&-p) T[p] += v;
}
int get_sum(int p)
{
int ret = 0;
for(; p; p-=p&-p) ret += T[p];
return ret;
}
int dist(int a, int b)
{
if(a == 0 || b == 0) return 1e8;
if(a > b) swap(a, b);
return get_sum(b - 1) - get_sum(a);
}
int main()
{
int i, a, b, c, ans;
int la, ra, lb, rb;
int cla, cra, clb, crb;
scanf("%d%d%d", &n, &k, &q);
if(q > 50) return 0;
for(i=1; i<=n; i++){
scanf("%d ", D+i);
V[D[i]].push_back(i);
}
for(i=1; i<=n; i++){
c = 1;
for(; sp && D[S[sp]] <= D[i]; ){
if(D[S[sp]] < D[i]) sp --;
if(D[S[sp]] == D[i]) c += CL[S[sp]], sp --;
}
if(sp) L[i] = S[sp], CL[i] = c;
else CL[i] = 1e8;
S[++sp] = i;
}
sp = 0;
for(i=n; i>=1; i--){
c = 1;
for(; sp && D[S[sp]] <= D[i]; ){
if(D[S[sp]] < D[i]) sp --;
if(D[S[sp]] == D[i]) c += CR[S[sp]], sp --;
}
if(sp) R[i] = S[sp], CR[i] = c;
else CR[i] = 1e8;
S[++sp] = i;
}
for(i=1; i<=n; i++){
// printf("%d %d %d %d\n", L[i], CL[i], R[i], CR[i]);
}
for(; q--; ){
scanf("%d%d", &a, &b);
// printf("%d %d\n", a, b);
la = ra = a; cla = cra = 0;
lb = rb = b; clb = crb = 0;
ans = 1e8;
for(i=1; i<=n; i++) update(i, 1);
for(i=1; i<=k; i++){
if(D[la] < i) cla += CL[la], la = L[la];
if(D[ra] < i) cra += CR[ra], ra = R[ra];
cla = min(cla, cra + 1);
cra = min(cra, cla + 1);
if(D[lb] < i) clb += CL[lb], lb = L[lb];
if(D[rb] < i) crb += CR[rb], rb = R[rb];
clb = min(clb, crb + 1);
crb = min(crb, clb + 1);
// printf("%d : %d(%d) %d(%d) , %d(%d) %d(%d)\n", i, la, cla, ra, cra, lb, clb, rb, crb);
ans = min(ans, cla + clb + dist(la, lb));
ans = min(ans, cra + clb + dist(ra, lb));
ans = min(ans, cla + crb + dist(la, rb));
ans = min(ans, cra + crb + dist(ra, rb));
for(int t: V[i]) update(t, -1);
}
printf("%d\n", ans);
}
return 0;
}
Compilation message
railway_trip.cpp: In function 'int main()':
railway_trip.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &k, &q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d ", D+i);
~~~~~^~~~~~~~~~~~
railway_trip.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2792 KB |
Output is correct |
3 |
Correct |
7 ms |
3000 KB |
Output is correct |
4 |
Correct |
6 ms |
3036 KB |
Output is correct |
5 |
Correct |
7 ms |
3036 KB |
Output is correct |
6 |
Correct |
6 ms |
3036 KB |
Output is correct |
7 |
Correct |
6 ms |
3036 KB |
Output is correct |
8 |
Correct |
6 ms |
3036 KB |
Output is correct |
9 |
Correct |
6 ms |
3036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
3060 KB |
Output is correct |
2 |
Correct |
321 ms |
6052 KB |
Output is correct |
3 |
Correct |
299 ms |
6468 KB |
Output is correct |
4 |
Correct |
268 ms |
6592 KB |
Output is correct |
5 |
Correct |
270 ms |
6848 KB |
Output is correct |
6 |
Correct |
346 ms |
6904 KB |
Output is correct |
7 |
Correct |
1319 ms |
8448 KB |
Output is correct |
8 |
Correct |
587 ms |
8448 KB |
Output is correct |
9 |
Correct |
768 ms |
8448 KB |
Output is correct |
10 |
Correct |
430 ms |
8448 KB |
Output is correct |
11 |
Correct |
1053 ms |
8448 KB |
Output is correct |
12 |
Correct |
569 ms |
8448 KB |
Output is correct |
13 |
Correct |
516 ms |
8448 KB |
Output is correct |
14 |
Correct |
987 ms |
8448 KB |
Output is correct |
15 |
Correct |
652 ms |
8448 KB |
Output is correct |
16 |
Correct |
496 ms |
8448 KB |
Output is correct |
17 |
Correct |
708 ms |
8448 KB |
Output is correct |
18 |
Correct |
744 ms |
8448 KB |
Output is correct |
19 |
Correct |
883 ms |
9340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
9340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
9340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |