#include <bits/stdc++.h>
using namespace std;
#define int long long
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
constexpr int INF = 1e9;
vector<pair<vector<int>,int>> tree;
int tree_shift = 0;
int N, Q;
vector<int> L, R;
vector<int> X;
vector<vector<int>> dp;
void resetadj() {
for (auto& k : tree) k.second = 0;
}
struct Adjacency {
int itree, ivec = 0;
Adjacency(int u) {
itree = tree_shift+u;
ivec = tree[itree].second;
correctidx();
}
inline void correctidx() {
while (itree != 0 && ivec >= (int)tree[itree].first.size()) {
itree >>= 1;
ivec = tree[itree].second;
}
}
Adjacency begin() {
return Adjacency(itree-tree_shift);
}
Adjacency end() {
return Adjacency(-tree_shift);
}
void operator++() {
ivec++;
tree[itree].second++;
correctidx();
}
bool operator!=(const Adjacency& other) const {
return itree != other.itree || ivec != other.ivec;
}
int operator*() const {
return tree[itree].first[ivec];
}
};
void bfs(vector<int>& dist) {
vector<vector<int>> q(2*N+10);
for (int i = 0; i < (int)dist.size(); i++) {
if (dist[i] < INF) q[dist[i]].push_back(i);
}
for (int d = 0; d < (int)q.size(); d++) {
for (int u : q[d]) {
if (dist[u] != d) continue;
for (int v : Adjacency(u)) {
if (u == v) continue;
if (dist[u]+1 < dist[v]) {
dist[v] = dist[u]+1;
q[dist[v]].push_back(v);
}
}
}
}
resetadj();
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> N;
L.resize(N), R.resize(N);
tree_shift = 1<<__lg(2*N-1);
tree.resize(2*tree_shift);
for (int i = 0; i < N; i++) {
cin >> L[i] >> R[i], L[i]--, R[i]--;
for (int l = tree_shift+L[i], r = tree_shift+R[i]+1; l < r; l >>= 1, r >>= 1) {
if (l&1) tree[l++].first.push_back(i);
if (r&1) tree[--r].first.push_back(i);
}
}
cin >> Q;
X.resize(Q);
for (int i = 0; i < Q; i++) {
cin >> X[i], X[i]--;
}
vector<int> ddeb(N, INF);
ddeb[0] = 0;
bfs(ddeb);
ddeb[0]++;
vector<int> dfin(N, INF);
dfin[N-1] = 0;
bfs(dfin);
dfin[N-1]++;
vector<int> ans(N);
for (int i = 0; i < N; i++) {
ans[i] = ddeb[i]+dfin[i];
}
bfs(ans);
for (int i = 0; i < N; i++) if (ans[i] >= INF) ans[i] = -1; else ans[i]--;
for (int i = 0; i < Q; i++) {
cout << ans[X[i]] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
327 ms |
69404 KB |
Output is correct |
5 |
Correct |
165 ms |
59396 KB |
Output is correct |
6 |
Correct |
101 ms |
50496 KB |
Output is correct |
7 |
Correct |
98 ms |
55620 KB |
Output is correct |
8 |
Correct |
62 ms |
51160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
980 KB |
Output is correct |
18 |
Correct |
2 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
2 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
327 ms |
69404 KB |
Output is correct |
5 |
Correct |
165 ms |
59396 KB |
Output is correct |
6 |
Correct |
101 ms |
50496 KB |
Output is correct |
7 |
Correct |
98 ms |
55620 KB |
Output is correct |
8 |
Correct |
62 ms |
51160 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
980 KB |
Output is correct |
25 |
Correct |
2 ms |
980 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
980 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
980 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
3 ms |
1108 KB |
Output is correct |
36 |
Correct |
2 ms |
980 KB |
Output is correct |
37 |
Correct |
2 ms |
980 KB |
Output is correct |
38 |
Correct |
2 ms |
980 KB |
Output is correct |
39 |
Correct |
2 ms |
980 KB |
Output is correct |
40 |
Correct |
371 ms |
75844 KB |
Output is correct |
41 |
Correct |
187 ms |
63756 KB |
Output is correct |
42 |
Correct |
238 ms |
85888 KB |
Output is correct |
43 |
Correct |
219 ms |
84084 KB |
Output is correct |
44 |
Correct |
127 ms |
54780 KB |
Output is correct |
45 |
Correct |
149 ms |
60440 KB |
Output is correct |
46 |
Correct |
55 ms |
23788 KB |
Output is correct |
47 |
Correct |
163 ms |
62260 KB |
Output is correct |
48 |
Correct |
152 ms |
62700 KB |
Output is correct |