# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
125497 | choikiwon | Railway Trip (JOI17_railway_trip) | C++17 | 287 ms | 19284 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MN = 100010;
int N, K, Q;
int L[MN];
int le[20][MN], ri[20][MN];
int main() {
scanf("%d %d %d", &N, &K, &Q);
for(int i = 0; i < N; i++) {
scanf("%d", &L[i]);
L[i]--;
}
stack<pii> stk;
for(int i = 0; i < N; i++) {
while(!stk.empty() && stk.top().first < L[i]) stk.pop();
if(!stk.empty()) le[0][i] = stk.top().second;
stk.push(pii(L[i], i));
}
while(!stk.empty()) stk.pop();
for(int i = N - 1; i >= 0; i--) {
while(!stk.empty() && stk.top().first < L[i]) stk.pop();
if(!stk.empty()) ri[0][i] = stk.top().second;
stk.push(pii(L[i], i));
}
le[0][0] = 0;
ri[0][N - 1] = N - 1;
for(int i = 1; i < 20; i++) {
for(int j = 0; j < N; j++) {
int t = le[i - 1][j];
int d = ri[i - 1][j];
le[i][j] = min(le[i - 1][t], le[i - 1][d]);
ri[i][j] = max(ri[i - 1][t], ri[i - 1][d]);
}
}
for(int i = 0; i < Q; i++) {
int a, b; scanf("%d %d", &a, &b);
a--; b--;
if(a > b) swap(a, b);
int ans = 0;
int l = a;
int r = a;
for(int j = 20; j--;) {
if(max(ri[j][l], ri[j][r]) < b) {
ans += (1 << j);
int nl = min(le[j][l], le[j][r]);
int nr = max(ri[j][l], ri[j][r]);
l = nl;
r = nr;
}
}
a = r;
l = b;
r = b;
for(int j = 20; j--;) {
if(min(le[j][l], le[j][r]) > a) {
ans += (1 << j);
int nl = min(le[j][l], le[j][r]);
int nr = max(ri[j][l], ri[j][r]);
l = nl;
r = nr;
}
}
printf("%d\n", ans);
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |