#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 1e5+10;
int a[maxn], l[maxn][2], r[maxn][2], dist[2][maxn], mark[maxn], seen[maxn], cnt;
vector<int> pos[maxn];
int d(int x, int y, int k) {
// se x==y ele retorna -1 e é pra retornar isso mesmo
if(x > y) swap(x, y);
return lower_bound(pos[k].begin(), pos[k].end(), y) - lower_bound(pos[k].begin(), pos[k].end(), x) - 1;
}
vector<int> reached[2][maxn];
void dijkstra(int s, int k) {
++cnt;
dist[k][s] = 0;
seen[s] = cnt;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({0, s});
while(q.size()) {
int u = q.top().second; q.pop();
if(mark[u] == cnt) continue;
mark[u] = cnt;
reached[k][a[u]].push_back(u);
int d = dist[k][u];
for(auto [v, w] : {make_pair(l[u][0], l[u][1]), make_pair(r[u][0], r[u][1])}) {
if(seen[v] < cnt || dist[k][v] > d + w) {
seen[v] = cnt;
dist[k][v] = d+w;
q.push({d+w, v});
}
}
}
}
int main() {
int n, k, q; scanf("%d %d %d", &n, &k, &q);
for(int i = 0; i < n; i++) {
scanf("%d", a+i);
for(int j = 0; j <= a[i]; j++)
pos[j].push_back(i);
}
for(int i = 0; i < n; i++) {
if(a[i] == k) { l[i][0] = i; l[i][1] = 0; continue; }
l[i][0] = i-1; l[i][1] = 1;
while(a[l[i][0]] <= a[i]) {
l[i][1] = (a[l[i][0]] == a[i] ? l[l[i][0]][1] : 0) + 1;
l[i][0] = l[l[i][0]][0];
}
}
for(int i = n-1; i >= 0; i--) {
if(a[i] == k) { r[i][0] = i, r[i][1] = 0; continue; }
r[i][0] = i+1; r[i][1] = 1;
while(a[r[i][0]] <= a[i]) {
r[i][1] = (a[r[i][0]] == a[i] ? r[r[i][0]][1] : 0) + 1;
r[i][0] = r[r[i][0]][0];
}
}
while(q--) {
int x, y; scanf("%d %d", &x, &y);
--x, --y;
dijkstra(x, 0); dijkstra(y, 1);
int ans = 0x3f3f3f3f;
for(int lvl = 1; lvl <= k; lvl++) {
for(int x : reached[0][lvl]) for(int y : reached[1][lvl])
ans = min(ans, dist[0][x] + dist[1][y] + d(x, y, lvl));
reached[0][lvl].clear(), reached[1][lvl].clear();
}
printf("%d\n", ans);
}
}
Compilation message
railway_trip.cpp: In function 'int main()':
railway_trip.cpp:42:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | int n, k, q; scanf("%d %d %d", &n, &k, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
railway_trip.cpp:68:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | int x, y; scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
5 ms |
7348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
10324 KB |
Output is correct |
2 |
Correct |
20 ms |
13328 KB |
Output is correct |
3 |
Correct |
22 ms |
15528 KB |
Output is correct |
4 |
Correct |
32 ms |
22132 KB |
Output is correct |
5 |
Correct |
50 ms |
32896 KB |
Output is correct |
6 |
Correct |
339 ms |
224120 KB |
Output is correct |
7 |
Runtime error |
1315 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
234 ms |
15080 KB |
Output is correct |
2 |
Correct |
218 ms |
14076 KB |
Output is correct |
3 |
Correct |
231 ms |
15472 KB |
Output is correct |
4 |
Correct |
228 ms |
15560 KB |
Output is correct |
5 |
Correct |
242 ms |
15732 KB |
Output is correct |
6 |
Correct |
235 ms |
16128 KB |
Output is correct |
7 |
Correct |
237 ms |
16424 KB |
Output is correct |
8 |
Correct |
142 ms |
12488 KB |
Output is correct |
9 |
Correct |
118 ms |
12352 KB |
Output is correct |
10 |
Correct |
321 ms |
12344 KB |
Output is correct |
11 |
Correct |
387 ms |
12444 KB |
Output is correct |
12 |
Correct |
399 ms |
12304 KB |
Output is correct |
13 |
Correct |
402 ms |
12472 KB |
Output is correct |
14 |
Correct |
400 ms |
12304 KB |
Output is correct |
15 |
Correct |
614 ms |
12316 KB |
Output is correct |
16 |
Correct |
608 ms |
14204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1319 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |