#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double dou;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define MASK(x) (1LL<<(x))
#define BITj(x, j) (((x)>>(j))&1)
template<typename T> bool maximize(T &x, const T &y) {
if (x < y) {x = y; return 1;}
return 0;
}
template<typename T> bool minimize(T &x, const T &y) {
if (x > y) {x = y; return 1;}
return 0;
}
const int maxn = 2e5+5, INF = (int)1e9+7;
int n, trsz;
vector<int> adj[maxn*4];
int dis1[maxn*4], dis2[maxn*4], dis3[maxn*4];
namespace SMT{
void init() {
trsz = 1;
while (trsz < n) trsz <<= 1;
for (int i = trsz-1; i; i--) {
int lc = i<<1, rc = (i<<1)|1;
adj[lc].pb(i);
adj[rc].pb(i);
}
}
void buildedge(int u, int l, int r){
l += trsz - 1;
r += trsz;
u += trsz-1;
while (l < r){
if (l&1) {
adj[l++].pb(u);
}
if (r&1) {
adj[--r].pb(u);
}
l >>= 1;
r >>= 1;
}
}
}
void dijkstra(int dis[]) {
priority_queue<pii> q;
for (int i= 1; i < (trsz<<1); i++) {
q.push({-dis[i], i});
}
while (!q.empty()) {
int u = q.top().se, w = -q.top().fi;
q.pop();
if (w != dis[u]) continue;
if (dis[u] >= INF) continue;
for (int i= 0; i < (int)adj[u].size(); i++) {
int v = adj[u][i];
if (minimize(dis[v], dis[u]+(v>=trsz))) {
q.push({-dis[v], v});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// #define name "task"
// if (fopen(name".inp", "r")) {
// freopen(name".inp", "r", stdin);
// freopen(name".out", "w", stdout);
// }
//input + init SMT
cin >> n;
SMT::init();
//build graph
for (int i= 1; i <= n; i++) {
int l, r;
cin >> l >> r;
SMT::buildedge(i, l, r);
}
//dijkstra 12
memset(dis1, 0x3f, sizeof(dis1));
dis1[trsz] = 0;
dijkstra(dis1);
memset(dis2, 0x3f, sizeof(dis2));
dis2[trsz+n-1] = 0;
dijkstra(dis2);
//merge
memset(dis3, 0x3f, sizeof(dis3));
for (int i = 1; i <= n; i++) {
int u = i+trsz-1;
if (max(dis1[u], dis2[u]) >= INF) {
continue;
}
dis3[u] = dis1[u]+dis2[u]-1;
if (i == 1 || i == n) dis3[u]++;
}
//dijkstra 3
dijkstra(dis3);
// for (int i = 1; i < trsz*2; i++) {
// cout << "node " << i << ' ' << dis1[i] << ' ' << dis2[i] << ' ' << dis3[i] << "\n";
// }
//answer queries
int q;
cin >> q;
while (q--) {
int u;
cin >> u;
u += trsz - 1;
if (dis3[u] >= INF) cout << "-1\n";
else cout << dis3[u] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
28496 KB |
Output is correct |
2 |
Correct |
20 ms |
28656 KB |
Output is correct |
3 |
Correct |
19 ms |
28496 KB |
Output is correct |
4 |
Correct |
601 ms |
70776 KB |
Output is correct |
5 |
Correct |
410 ms |
62956 KB |
Output is correct |
6 |
Correct |
366 ms |
55588 KB |
Output is correct |
7 |
Correct |
340 ms |
65932 KB |
Output is correct |
8 |
Correct |
263 ms |
68680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28496 KB |
Output is correct |
2 |
Correct |
19 ms |
28496 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
19 ms |
28496 KB |
Output is correct |
5 |
Correct |
21 ms |
28628 KB |
Output is correct |
6 |
Correct |
19 ms |
28496 KB |
Output is correct |
7 |
Correct |
20 ms |
28496 KB |
Output is correct |
8 |
Correct |
19 ms |
28496 KB |
Output is correct |
9 |
Correct |
19 ms |
28572 KB |
Output is correct |
10 |
Correct |
19 ms |
28552 KB |
Output is correct |
11 |
Correct |
21 ms |
28496 KB |
Output is correct |
12 |
Correct |
21 ms |
28704 KB |
Output is correct |
13 |
Correct |
21 ms |
28496 KB |
Output is correct |
14 |
Correct |
20 ms |
28496 KB |
Output is correct |
15 |
Correct |
20 ms |
28644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28496 KB |
Output is correct |
2 |
Correct |
19 ms |
28496 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
19 ms |
28496 KB |
Output is correct |
5 |
Correct |
21 ms |
28628 KB |
Output is correct |
6 |
Correct |
19 ms |
28496 KB |
Output is correct |
7 |
Correct |
20 ms |
28496 KB |
Output is correct |
8 |
Correct |
19 ms |
28496 KB |
Output is correct |
9 |
Correct |
19 ms |
28572 KB |
Output is correct |
10 |
Correct |
19 ms |
28552 KB |
Output is correct |
11 |
Correct |
21 ms |
28496 KB |
Output is correct |
12 |
Correct |
21 ms |
28704 KB |
Output is correct |
13 |
Correct |
21 ms |
28496 KB |
Output is correct |
14 |
Correct |
20 ms |
28496 KB |
Output is correct |
15 |
Correct |
20 ms |
28644 KB |
Output is correct |
16 |
Correct |
23 ms |
29112 KB |
Output is correct |
17 |
Correct |
23 ms |
29128 KB |
Output is correct |
18 |
Correct |
25 ms |
29132 KB |
Output is correct |
19 |
Correct |
23 ms |
29124 KB |
Output is correct |
20 |
Correct |
24 ms |
29008 KB |
Output is correct |
21 |
Correct |
23 ms |
29008 KB |
Output is correct |
22 |
Correct |
22 ms |
29072 KB |
Output is correct |
23 |
Correct |
23 ms |
29108 KB |
Output is correct |
24 |
Correct |
28 ms |
29112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
28496 KB |
Output is correct |
2 |
Correct |
19 ms |
28496 KB |
Output is correct |
3 |
Correct |
20 ms |
28544 KB |
Output is correct |
4 |
Correct |
19 ms |
28496 KB |
Output is correct |
5 |
Correct |
21 ms |
28628 KB |
Output is correct |
6 |
Correct |
19 ms |
28496 KB |
Output is correct |
7 |
Correct |
20 ms |
28496 KB |
Output is correct |
8 |
Correct |
19 ms |
28496 KB |
Output is correct |
9 |
Correct |
19 ms |
28572 KB |
Output is correct |
10 |
Correct |
19 ms |
28552 KB |
Output is correct |
11 |
Correct |
21 ms |
28496 KB |
Output is correct |
12 |
Correct |
21 ms |
28704 KB |
Output is correct |
13 |
Correct |
21 ms |
28496 KB |
Output is correct |
14 |
Correct |
20 ms |
28496 KB |
Output is correct |
15 |
Correct |
20 ms |
28644 KB |
Output is correct |
16 |
Correct |
23 ms |
29112 KB |
Output is correct |
17 |
Correct |
23 ms |
29128 KB |
Output is correct |
18 |
Correct |
25 ms |
29132 KB |
Output is correct |
19 |
Correct |
23 ms |
29124 KB |
Output is correct |
20 |
Correct |
24 ms |
29008 KB |
Output is correct |
21 |
Correct |
23 ms |
29008 KB |
Output is correct |
22 |
Correct |
22 ms |
29072 KB |
Output is correct |
23 |
Correct |
23 ms |
29108 KB |
Output is correct |
24 |
Correct |
28 ms |
29112 KB |
Output is correct |
25 |
Correct |
21 ms |
28508 KB |
Output is correct |
26 |
Correct |
19 ms |
28496 KB |
Output is correct |
27 |
Correct |
24 ms |
29112 KB |
Output is correct |
28 |
Correct |
23 ms |
29128 KB |
Output is correct |
29 |
Correct |
22 ms |
29008 KB |
Output is correct |
30 |
Correct |
23 ms |
28992 KB |
Output is correct |
31 |
Correct |
22 ms |
29108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
28496 KB |
Output is correct |
2 |
Correct |
20 ms |
28656 KB |
Output is correct |
3 |
Correct |
19 ms |
28496 KB |
Output is correct |
4 |
Correct |
601 ms |
70776 KB |
Output is correct |
5 |
Correct |
410 ms |
62956 KB |
Output is correct |
6 |
Correct |
366 ms |
55588 KB |
Output is correct |
7 |
Correct |
340 ms |
65932 KB |
Output is correct |
8 |
Correct |
263 ms |
68680 KB |
Output is correct |
9 |
Correct |
20 ms |
28496 KB |
Output is correct |
10 |
Correct |
19 ms |
28496 KB |
Output is correct |
11 |
Correct |
20 ms |
28544 KB |
Output is correct |
12 |
Correct |
19 ms |
28496 KB |
Output is correct |
13 |
Correct |
21 ms |
28628 KB |
Output is correct |
14 |
Correct |
19 ms |
28496 KB |
Output is correct |
15 |
Correct |
20 ms |
28496 KB |
Output is correct |
16 |
Correct |
19 ms |
28496 KB |
Output is correct |
17 |
Correct |
19 ms |
28572 KB |
Output is correct |
18 |
Correct |
19 ms |
28552 KB |
Output is correct |
19 |
Correct |
21 ms |
28496 KB |
Output is correct |
20 |
Correct |
21 ms |
28704 KB |
Output is correct |
21 |
Correct |
21 ms |
28496 KB |
Output is correct |
22 |
Correct |
20 ms |
28496 KB |
Output is correct |
23 |
Correct |
20 ms |
28644 KB |
Output is correct |
24 |
Correct |
23 ms |
29112 KB |
Output is correct |
25 |
Correct |
23 ms |
29128 KB |
Output is correct |
26 |
Correct |
25 ms |
29132 KB |
Output is correct |
27 |
Correct |
23 ms |
29124 KB |
Output is correct |
28 |
Correct |
24 ms |
29008 KB |
Output is correct |
29 |
Correct |
23 ms |
29008 KB |
Output is correct |
30 |
Correct |
22 ms |
29072 KB |
Output is correct |
31 |
Correct |
23 ms |
29108 KB |
Output is correct |
32 |
Correct |
28 ms |
29112 KB |
Output is correct |
33 |
Correct |
21 ms |
28508 KB |
Output is correct |
34 |
Correct |
19 ms |
28496 KB |
Output is correct |
35 |
Correct |
24 ms |
29112 KB |
Output is correct |
36 |
Correct |
23 ms |
29128 KB |
Output is correct |
37 |
Correct |
22 ms |
29008 KB |
Output is correct |
38 |
Correct |
23 ms |
28992 KB |
Output is correct |
39 |
Correct |
22 ms |
29108 KB |
Output is correct |
40 |
Correct |
624 ms |
73724 KB |
Output is correct |
41 |
Correct |
436 ms |
62992 KB |
Output is correct |
42 |
Correct |
488 ms |
78280 KB |
Output is correct |
43 |
Correct |
476 ms |
78340 KB |
Output is correct |
44 |
Correct |
397 ms |
55776 KB |
Output is correct |
45 |
Correct |
397 ms |
59932 KB |
Output is correct |
46 |
Correct |
199 ms |
45320 KB |
Output is correct |
47 |
Correct |
441 ms |
66596 KB |
Output is correct |
48 |
Correct |
364 ms |
67448 KB |
Output is correct |