QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#536275 | #365. Railway Trip | makrav# | 0 | 1ms | 3620kb | C++20 | 2.1kb | 2024-08-28 22:03:28 | 2024-08-28 22:03:30 |
answer
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define pb push_back
void solve() {
int n, k, q; cin >> n >> k >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<vector<int>> g(n);
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (!st.empty()) {
g[i].pb(st.top());
g[st.top()].pb(i);
}
st.push(i);
}
while (!st.empty()) st.pop();
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (!st.empty()) {
g[i].pb(st.top());
g[st.top()].pb(i);
}
st.push(i);
}
while (q--) {
int l, r; cin >> l >> r;
l--; r--;
int pmx = l;
for (int i = min(l, r); i <= max(l, r); i++) {
if (a[i] > a[pmx]) pmx = i;
}
vector<int> dist(n, -1);
dist[l] = 0;
queue<int> q;
q.push(l);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto &u : g[v]) {
if (dist[u] == -1) {
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
int S = dist[pmx];
dist.assign(n, -1);
dist[r] = 0;
q.push(r);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto &u : g[v]) {
if (dist[u] == -1) {
dist[u] = dist[v] + 1;
q.push(u);
}
}
}
S += dist[pmx];
cout << S - 1 << '\n';
}
}
signed main() {
int tt = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> tt;
#else
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#endif
while (tt--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 1ms
memory: 3560kb
input:
100 100 50 100 86 39 28 49 22 79 14 83 100 15 26 37 51 53 18 74 15 96 72 47 80 10 46 62 88 20 36 46 29 40 28 37 88 91 41 24 63 14 92 24 31 99 61 62 96 94 51 51 21 72 97 59 96 97 94 66 88 32 96 58 26 53 1 100 31 85 30 42 69 40 62 54 94 49 62 13 20 82 74 20 44 54 69 65 34 78 64 48 69 19 35 8 92 100 87...
output:
3 1 3 5 3 3 3 0 2 5 2 6 1 3 1 3 5 5 3 5 4 7 3 6 3 4 4 4 7 0 2 3 2 4 5 5 6 5 5 6 4 3 2 4 5 2 5 4 2 2
result:
ok 50 lines
Test #2:
score: 0
Wrong Answer
time: 1ms
memory: 3620kb
input:
100 100 50 100 85 82 7 50 49 51 45 30 3 29 99 71 93 5 68 70 52 12 44 1 35 99 80 76 34 23 28 62 91 80 77 59 57 30 15 23 13 16 21 58 23 38 49 44 73 7 47 24 53 97 83 14 71 16 75 61 24 17 96 51 41 74 53 25 2 42 36 73 57 53 45 10 12 11 79 68 2 78 44 47 67 21 99 25 68 60 71 23 92 9 2 97 37 43 64 32 28 7 1...
output:
2 0 5 4 2 4 2 4 3 5 4 3 3 6 4 4 3 3 3 4 2 3 4 3 5 5 5 2 3 3 5 4 3 4 2 2 3 5 4 6 2 5 4 2 2 4 4 3 4 7
result:
wrong answer 23rd lines differ - expected: '3', found: '4'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%