#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], dis2[maxn], dis3[maxn];
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";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21584 KB |
Output is correct |
2 |
Correct |
15 ms |
21584 KB |
Output is correct |
3 |
Correct |
15 ms |
21404 KB |
Output is correct |
4 |
Runtime error |
302 ms |
112160 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21596 KB |
Output is correct |
2 |
Correct |
15 ms |
21596 KB |
Output is correct |
3 |
Correct |
15 ms |
21616 KB |
Output is correct |
4 |
Correct |
15 ms |
21600 KB |
Output is correct |
5 |
Correct |
16 ms |
21600 KB |
Output is correct |
6 |
Correct |
16 ms |
21600 KB |
Output is correct |
7 |
Correct |
15 ms |
21600 KB |
Output is correct |
8 |
Correct |
14 ms |
21616 KB |
Output is correct |
9 |
Correct |
15 ms |
21428 KB |
Output is correct |
10 |
Correct |
17 ms |
21596 KB |
Output is correct |
11 |
Correct |
18 ms |
21600 KB |
Output is correct |
12 |
Correct |
16 ms |
21596 KB |
Output is correct |
13 |
Correct |
22 ms |
21584 KB |
Output is correct |
14 |
Correct |
16 ms |
21584 KB |
Output is correct |
15 |
Correct |
15 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21596 KB |
Output is correct |
2 |
Correct |
15 ms |
21596 KB |
Output is correct |
3 |
Correct |
15 ms |
21616 KB |
Output is correct |
4 |
Correct |
15 ms |
21600 KB |
Output is correct |
5 |
Correct |
16 ms |
21600 KB |
Output is correct |
6 |
Correct |
16 ms |
21600 KB |
Output is correct |
7 |
Correct |
15 ms |
21600 KB |
Output is correct |
8 |
Correct |
14 ms |
21616 KB |
Output is correct |
9 |
Correct |
15 ms |
21428 KB |
Output is correct |
10 |
Correct |
17 ms |
21596 KB |
Output is correct |
11 |
Correct |
18 ms |
21600 KB |
Output is correct |
12 |
Correct |
16 ms |
21596 KB |
Output is correct |
13 |
Correct |
22 ms |
21584 KB |
Output is correct |
14 |
Correct |
16 ms |
21584 KB |
Output is correct |
15 |
Correct |
15 ms |
21584 KB |
Output is correct |
16 |
Correct |
23 ms |
22048 KB |
Output is correct |
17 |
Correct |
19 ms |
21960 KB |
Output is correct |
18 |
Correct |
18 ms |
22220 KB |
Output is correct |
19 |
Correct |
19 ms |
22212 KB |
Output is correct |
20 |
Correct |
16 ms |
21840 KB |
Output is correct |
21 |
Correct |
17 ms |
21840 KB |
Output is correct |
22 |
Correct |
17 ms |
22128 KB |
Output is correct |
23 |
Correct |
19 ms |
21940 KB |
Output is correct |
24 |
Correct |
17 ms |
21944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21596 KB |
Output is correct |
2 |
Correct |
15 ms |
21596 KB |
Output is correct |
3 |
Correct |
15 ms |
21616 KB |
Output is correct |
4 |
Correct |
15 ms |
21600 KB |
Output is correct |
5 |
Correct |
16 ms |
21600 KB |
Output is correct |
6 |
Correct |
16 ms |
21600 KB |
Output is correct |
7 |
Correct |
15 ms |
21600 KB |
Output is correct |
8 |
Correct |
14 ms |
21616 KB |
Output is correct |
9 |
Correct |
15 ms |
21428 KB |
Output is correct |
10 |
Correct |
17 ms |
21596 KB |
Output is correct |
11 |
Correct |
18 ms |
21600 KB |
Output is correct |
12 |
Correct |
16 ms |
21596 KB |
Output is correct |
13 |
Correct |
22 ms |
21584 KB |
Output is correct |
14 |
Correct |
16 ms |
21584 KB |
Output is correct |
15 |
Correct |
15 ms |
21584 KB |
Output is correct |
16 |
Correct |
23 ms |
22048 KB |
Output is correct |
17 |
Correct |
19 ms |
21960 KB |
Output is correct |
18 |
Correct |
18 ms |
22220 KB |
Output is correct |
19 |
Correct |
19 ms |
22212 KB |
Output is correct |
20 |
Correct |
16 ms |
21840 KB |
Output is correct |
21 |
Correct |
17 ms |
21840 KB |
Output is correct |
22 |
Correct |
17 ms |
22128 KB |
Output is correct |
23 |
Correct |
19 ms |
21940 KB |
Output is correct |
24 |
Correct |
17 ms |
21944 KB |
Output is correct |
25 |
Correct |
14 ms |
21584 KB |
Output is correct |
26 |
Correct |
14 ms |
21584 KB |
Output is correct |
27 |
Correct |
19 ms |
22116 KB |
Output is correct |
28 |
Correct |
19 ms |
21960 KB |
Output is correct |
29 |
Correct |
17 ms |
21840 KB |
Output is correct |
30 |
Correct |
17 ms |
21840 KB |
Output is correct |
31 |
Correct |
18 ms |
22048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21584 KB |
Output is correct |
2 |
Correct |
15 ms |
21584 KB |
Output is correct |
3 |
Correct |
15 ms |
21404 KB |
Output is correct |
4 |
Runtime error |
302 ms |
112160 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |