#include <bits/stdc++.h>
#define int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAX_N = 1 << 18;
const int INF = 1e9;
pii itvs[MAX_N];
vector<int> tree[2*MAX_N];
// inclus
void addItv(int node, int leftNode, int rightNode, int idItv) {
const auto [leftAdd, rightAdd] = itvs[idItv];
if (rightNode < leftAdd || rightAdd < leftNode) {
return;
}
if (leftAdd <= leftNode && rightNode <= rightAdd) {
// dbg(idItv, "added"s, node);
tree[node].push_back(idItv);
return;
}
if (leftNode < rightNode) {
int mid = (leftNode + rightNode) / 2;
addItv(2*node, leftNode, mid, idItv);
addItv(2*node+1, mid+1, rightNode, idItv);
}
}
void popItv(int node, int leftNode, int rightNode, int leaf, vector<int> &acc) {
// dbg(node, leftNode, rightNode, tree[node]);
for (int x : tree[node]) {
acc.push_back(x);
}
tree[node].clear(), tree[node].shrink_to_fit();
if (leftNode < rightNode) {
int mid = (leftNode + rightNode) / 2;
if (leaf <= mid) {
popItv(2*node, leftNode, mid, leaf, acc);
} else {
popItv(2*node+1, mid+1, rightNode, leaf, acc);
}
}
}
int N, Q;
vector<int> calcDist(int source) {
rep(node, 0, N) {
addItv(1, 0, N-1, node);
}
vector<int> dist(N, INF);
dist[source] = 0;
queue<int> bfs;
bfs.push(source);
while (!bfs.empty()) {
int cur = bfs.front();
bfs.pop();
vector<int> todo;
popItv(1, 0, N-1, cur, todo);
for (int voisin : todo) if (dist[voisin] == INF) {
dist[voisin] = dist[cur]+1;
bfs.push(voisin);
}
}
return dist;
}
vector<int> d0, dN;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
rep(i, 0, N) {
cin >> itvs[i].first >> itvs[i].second;
--itvs[i].first, --itvs[i].second;
}
d0 = calcDist(0);
dbg(d0);
dN = calcDist(N-1);
dbg(dN);
d0[0]++, dN[N-1]++;
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<int> dist(N, INF);
rep(node, 0, N) {
addItv(1, 0, N-1, node);
dist[node] = d0[node] + dN[node] - 1;
if (dist[node] < INF)
pq.push({dist[node], node});
}
while (!pq.empty()) {
auto [cd, node] = pq.top();
pq.pop();
if (cd != dist[node]) continue;
vector<int> todo;
popItv(1, 0, N-1, node, todo);
for (int voisin : todo) if (dist[voisin] > dist[node]+1) {
dist[voisin] = dist[node]+1;
pq.push({dist[voisin], voisin});
}
}
for (int &x : dist) if (x >= INF) {
x = -1;
}
dbg(dist);
cin >> Q;
rep(iReq, 0, Q) {
int node; cin >> node;
--node;
cout << dist[node] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12628 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
6 ms |
12628 KB |
Output is correct |
4 |
Correct |
799 ms |
59576 KB |
Output is correct |
5 |
Correct |
406 ms |
37872 KB |
Output is correct |
6 |
Correct |
246 ms |
33940 KB |
Output is correct |
7 |
Correct |
104 ms |
29560 KB |
Output is correct |
8 |
Correct |
110 ms |
49336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12516 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
6 ms |
12628 KB |
Output is correct |
4 |
Correct |
5 ms |
12628 KB |
Output is correct |
5 |
Correct |
6 ms |
12628 KB |
Output is correct |
6 |
Correct |
5 ms |
12628 KB |
Output is correct |
7 |
Correct |
5 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12628 KB |
Output is correct |
9 |
Correct |
6 ms |
12624 KB |
Output is correct |
10 |
Correct |
6 ms |
12628 KB |
Output is correct |
11 |
Correct |
6 ms |
12628 KB |
Output is correct |
12 |
Correct |
6 ms |
12680 KB |
Output is correct |
13 |
Correct |
6 ms |
12628 KB |
Output is correct |
14 |
Correct |
6 ms |
12628 KB |
Output is correct |
15 |
Correct |
6 ms |
12628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12516 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
6 ms |
12628 KB |
Output is correct |
4 |
Correct |
5 ms |
12628 KB |
Output is correct |
5 |
Correct |
6 ms |
12628 KB |
Output is correct |
6 |
Correct |
5 ms |
12628 KB |
Output is correct |
7 |
Correct |
5 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12628 KB |
Output is correct |
9 |
Correct |
6 ms |
12624 KB |
Output is correct |
10 |
Correct |
6 ms |
12628 KB |
Output is correct |
11 |
Correct |
6 ms |
12628 KB |
Output is correct |
12 |
Correct |
6 ms |
12680 KB |
Output is correct |
13 |
Correct |
6 ms |
12628 KB |
Output is correct |
14 |
Correct |
6 ms |
12628 KB |
Output is correct |
15 |
Correct |
6 ms |
12628 KB |
Output is correct |
16 |
Correct |
10 ms |
13088 KB |
Output is correct |
17 |
Correct |
10 ms |
12852 KB |
Output is correct |
18 |
Correct |
10 ms |
13148 KB |
Output is correct |
19 |
Correct |
10 ms |
13132 KB |
Output is correct |
20 |
Correct |
8 ms |
12884 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
6 ms |
12756 KB |
Output is correct |
23 |
Correct |
9 ms |
13004 KB |
Output is correct |
24 |
Correct |
8 ms |
13268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12516 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
6 ms |
12628 KB |
Output is correct |
4 |
Correct |
5 ms |
12628 KB |
Output is correct |
5 |
Correct |
6 ms |
12628 KB |
Output is correct |
6 |
Correct |
5 ms |
12628 KB |
Output is correct |
7 |
Correct |
5 ms |
12628 KB |
Output is correct |
8 |
Correct |
6 ms |
12628 KB |
Output is correct |
9 |
Correct |
6 ms |
12624 KB |
Output is correct |
10 |
Correct |
6 ms |
12628 KB |
Output is correct |
11 |
Correct |
6 ms |
12628 KB |
Output is correct |
12 |
Correct |
6 ms |
12680 KB |
Output is correct |
13 |
Correct |
6 ms |
12628 KB |
Output is correct |
14 |
Correct |
6 ms |
12628 KB |
Output is correct |
15 |
Correct |
6 ms |
12628 KB |
Output is correct |
16 |
Correct |
10 ms |
13088 KB |
Output is correct |
17 |
Correct |
10 ms |
12852 KB |
Output is correct |
18 |
Correct |
10 ms |
13148 KB |
Output is correct |
19 |
Correct |
10 ms |
13132 KB |
Output is correct |
20 |
Correct |
8 ms |
12884 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
6 ms |
12756 KB |
Output is correct |
23 |
Correct |
9 ms |
13004 KB |
Output is correct |
24 |
Correct |
8 ms |
13268 KB |
Output is correct |
25 |
Correct |
5 ms |
12628 KB |
Output is correct |
26 |
Correct |
6 ms |
12628 KB |
Output is correct |
27 |
Correct |
11 ms |
13012 KB |
Output is correct |
28 |
Correct |
9 ms |
12956 KB |
Output is correct |
29 |
Correct |
9 ms |
12844 KB |
Output is correct |
30 |
Correct |
9 ms |
12884 KB |
Output is correct |
31 |
Correct |
9 ms |
13012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12628 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
6 ms |
12628 KB |
Output is correct |
4 |
Correct |
799 ms |
59576 KB |
Output is correct |
5 |
Correct |
406 ms |
37872 KB |
Output is correct |
6 |
Correct |
246 ms |
33940 KB |
Output is correct |
7 |
Correct |
104 ms |
29560 KB |
Output is correct |
8 |
Correct |
110 ms |
49336 KB |
Output is correct |
9 |
Correct |
6 ms |
12516 KB |
Output is correct |
10 |
Correct |
6 ms |
12628 KB |
Output is correct |
11 |
Correct |
6 ms |
12628 KB |
Output is correct |
12 |
Correct |
5 ms |
12628 KB |
Output is correct |
13 |
Correct |
6 ms |
12628 KB |
Output is correct |
14 |
Correct |
5 ms |
12628 KB |
Output is correct |
15 |
Correct |
5 ms |
12628 KB |
Output is correct |
16 |
Correct |
6 ms |
12628 KB |
Output is correct |
17 |
Correct |
6 ms |
12624 KB |
Output is correct |
18 |
Correct |
6 ms |
12628 KB |
Output is correct |
19 |
Correct |
6 ms |
12628 KB |
Output is correct |
20 |
Correct |
6 ms |
12680 KB |
Output is correct |
21 |
Correct |
6 ms |
12628 KB |
Output is correct |
22 |
Correct |
6 ms |
12628 KB |
Output is correct |
23 |
Correct |
6 ms |
12628 KB |
Output is correct |
24 |
Correct |
10 ms |
13088 KB |
Output is correct |
25 |
Correct |
10 ms |
12852 KB |
Output is correct |
26 |
Correct |
10 ms |
13148 KB |
Output is correct |
27 |
Correct |
10 ms |
13132 KB |
Output is correct |
28 |
Correct |
8 ms |
12884 KB |
Output is correct |
29 |
Correct |
8 ms |
12884 KB |
Output is correct |
30 |
Correct |
6 ms |
12756 KB |
Output is correct |
31 |
Correct |
9 ms |
13004 KB |
Output is correct |
32 |
Correct |
8 ms |
13268 KB |
Output is correct |
33 |
Correct |
5 ms |
12628 KB |
Output is correct |
34 |
Correct |
6 ms |
12628 KB |
Output is correct |
35 |
Correct |
11 ms |
13012 KB |
Output is correct |
36 |
Correct |
9 ms |
12956 KB |
Output is correct |
37 |
Correct |
9 ms |
12844 KB |
Output is correct |
38 |
Correct |
9 ms |
12884 KB |
Output is correct |
39 |
Correct |
9 ms |
13012 KB |
Output is correct |
40 |
Correct |
840 ms |
60016 KB |
Output is correct |
41 |
Correct |
401 ms |
38160 KB |
Output is correct |
42 |
Correct |
518 ms |
62584 KB |
Output is correct |
43 |
Correct |
529 ms |
63316 KB |
Output is correct |
44 |
Correct |
276 ms |
35292 KB |
Output is correct |
45 |
Correct |
330 ms |
38996 KB |
Output is correct |
46 |
Correct |
133 ms |
22760 KB |
Output is correct |
47 |
Correct |
373 ms |
42016 KB |
Output is correct |
48 |
Correct |
397 ms |
83948 KB |
Output is correct |