#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MN = 100010;
int N, K, Q;
int L[MN];
vector<int> V[MN];
int le[20][MN], ri[20][MN];
int le2[MN], ri2[MN];
struct BIT {
vector<int> tree;
void init() {
tree = vector<int>(4 * N);
build(0, N - 1, 1);
}
void build(int l, int r, int n) {
if(l == r) {
tree[n] = L[l];
return;
}
int m = (l + r)>>1;
build(l, m, 2*n);
build(m + 1, r, 2*n + 1);
tree[n] = max(tree[2*n], tree[2*n + 1]);
}
int quer(int a, int b, int l, int r, int n) {
if(b < l || r < a) return -1e9;
if(a <= l && r <= b) return tree[n];
int m = (l + r)>>1;
int L = quer(a, b, l, m, 2*n);
int R = quer(a, b, m + 1, r, 2*n + 1);
return max(L, R);
}
} bit;
int calc(int u, int v) {
if(u == v) return 0;
int ret = 0;
if(u < v) {
int l = u;
int r = u;
for(int i = 20; i--;) {
if(max(ri[i][l], ri[i][r]) < v) {
ret += (1 << i);
int nl = min(le[i][l], le[i][r]);
int nr = max(ri[i][l], ri[i][r]);
l = nl;
r = nr;
}
}
}
else {
int l = u;
int r = u;
for(int i = 20; i--;) {
if(min(le[i][l], le[i][r]) > v) {
ret += (1 << i);
int nl = min(le[i][l], le[i][r]);
int nr = max(ri[i][l], ri[i][r]);
l = nl;
r = nr;
}
}
}
return ret + 1;
}
int main() {
scanf("%d %d %d", &N, &K, &Q);
for(int i = 0; i < N; i++) {
scanf("%d", &L[i]);
V[ --L[i] ].push_back(i);
}
memset(le2, -1, sizeof(le2));
memset(ri2, -1, sizeof(ri2));
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));
}
while(!stk.empty()) stk.pop();
for(int i = 0; i < N; i++) {
while(!stk.empty() && stk.top().first <= L[i]) stk.pop();
if(!stk.empty()) le2[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()) ri2[i] = stk.top().second;
stk.push(pii(L[i], i));
}
while(!stk.empty()) stk.pop();
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]);
}
}
bit.init();
for(int i = 0; i < Q; i++) {
int a, b; scanf("%d %d", &a, &b);
a--; b--;
if(a > b) swap(a, b);
int x = bit.quer(a, b, 0, N - 1, 1);
int t = lower_bound(V[x].begin(), V[x].end(), a) - V[x].begin();
int d = V[x][d];
int ans = 1e9;
if(le2[d] != -1 && ri2[d] != -1) {
ans = min(ans, calc(a, le2[d]) + calc(b, ri2[d]) + 1);
}
ans = min(ans, calc(a, d) + calc(b, d));
printf("%d\n", ans - 1);
}
}
Compilation message
railway_trip.cpp: In function 'int main()':
railway_trip.cpp:73:10: 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:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &L[i]);
~~~~~^~~~~~~~~~~~~
railway_trip.cpp:125:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b; scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
17 ms |
7160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
7544 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
71 ms |
43460 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
122 ms |
46544 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |