#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define aint(x) x.begin(), x.end()
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 2e5 + 5;
const int inf = 1e9;
int pos[NM], n, ans[NM];
vector<pair<int, int>> g[4 * NM];
bool imp[4 * NM];
void build(int x = 1, int l = 1, int r = n)
{
if (l == r)
{
imp[x] = (l != 1 && l != n);
pos[l] = x;
return;
}
int m = l + r >> 1;
build(x << 1, l, m);
build(x << 1 | 1, m + 1, r);
g[x << 1].emplace_back(x, 0);
g[x << 1 | 1].emplace_back(x, 0);
// cout << (x << 1) << ' ' << x << ' ' << 0 << endl;
// cout << (x << 1 | 1) << ' ' << x << ' ' << 0 << endl;
}
void upd(int t, int l, int r, int x = 1, int lx = 1, int rx = n)
{
if (l > rx || lx > r)
return;
if (lx >= l && rx <= r)
{
g[x].emplace_back(t, 1);
// cout << x << ' ' << t << ' ' << 1 << endl;
return;
}
int m = lx + rx >> 1;
upd(t, l, r, x << 1, lx, m);
upd(t, l, r, x << 1 | 1, m + 1, rx);
}
pair<int, int> a[NM];
vector<int> bfs(int s)
{
vector<int> dist(4 * n + 1, inf);
deque<pair<int, int>> dq;
dist[s] = 0;
dq.push_front({0, s});
while (!dq.empty())
{
auto u = dq.front();
dq.pop_front();
if (u.fi != dist[u.se])
continue;
for (auto v : g[u.se])
if (dist[v.fi] > u.fi + v.se)
{
dist[v.fi] = u.fi + v.se;
if (v.se)
dq.push_back({dist[v.fi], v.fi});
else
dq.push_front({dist[v.fi], v.fi});
}
}
return dist;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
void solve()
{
cin >> n;
build();
for (int i = 1; i <= n; i++)
{
cin >> a[i].fi >> a[i].se;
upd(pos[i], a[i].fi, a[i].se);
}
vector<int> dist1 = bfs(pos[1]), dist2 = bfs(pos[n]);
vector<int> dist(4 * n + 1);
for (int i = 1; i < dist.size(); i++)
{
dist[i] = dist1[i] + dist2[i] - imp[i];
if (dist[i] < inf)
pq.push({dist[i], i});
}
while (!pq.empty())
{
auto u = pq.top();
pq.pop();
if (u.fi != dist[u.se])
continue;
for (auto v : g[u.se])
if (dist[v.fi] > u.fi + v.se)
{
dist[v.fi] = u.fi + v.se;
pq.push({dist[v.fi], v.fi});
}
}
for (int i = 1; i <= n; i++)
{
if (i == 1)
ans[i] = (dist2[pos[1]] < inf ? dist2[pos[1]] : -1);
else if (i == n)
ans[i] = (dist1[pos[n]] < inf ? dist1[pos[n]] : -1);
else
ans[i] = (dist[pos[i]] < inf ? dist[pos[i]] : -1);
}
int q; cin >> q;
while (q--)
{
int x;
cin >> x;
cout << ans[x] << endl;
}
}
signed main()
{
fastIO
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int tc = 1; // cin >> tc;
while (tc--)
solve();
}
Compilation message
passport.cpp: In function 'void build(long long int, long long int, long long int)':
passport.cpp:33:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int m = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
passport.cpp:52:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int m = lx + rx >> 1;
| ~~~^~~~
passport.cpp: In function 'void solve()':
passport.cpp:97:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 1; i < dist.size(); i++)
| ~~^~~~~~~~~~~~~
passport.cpp: In function 'int main()':
passport.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
passport.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19032 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
582 ms |
124756 KB |
Output is correct |
5 |
Correct |
304 ms |
84528 KB |
Output is correct |
6 |
Correct |
188 ms |
73916 KB |
Output is correct |
7 |
Correct |
147 ms |
67652 KB |
Output is correct |
8 |
Correct |
101 ms |
74160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19032 KB |
Output is correct |
2 |
Correct |
9 ms |
19032 KB |
Output is correct |
3 |
Correct |
9 ms |
19232 KB |
Output is correct |
4 |
Correct |
12 ms |
19260 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
9 ms |
19232 KB |
Output is correct |
7 |
Correct |
10 ms |
19156 KB |
Output is correct |
8 |
Correct |
9 ms |
19036 KB |
Output is correct |
9 |
Correct |
9 ms |
19036 KB |
Output is correct |
10 |
Correct |
9 ms |
19220 KB |
Output is correct |
11 |
Correct |
9 ms |
19292 KB |
Output is correct |
12 |
Correct |
9 ms |
19208 KB |
Output is correct |
13 |
Correct |
9 ms |
19420 KB |
Output is correct |
14 |
Correct |
9 ms |
19292 KB |
Output is correct |
15 |
Correct |
10 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19032 KB |
Output is correct |
2 |
Correct |
9 ms |
19032 KB |
Output is correct |
3 |
Correct |
9 ms |
19232 KB |
Output is correct |
4 |
Correct |
12 ms |
19260 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
9 ms |
19232 KB |
Output is correct |
7 |
Correct |
10 ms |
19156 KB |
Output is correct |
8 |
Correct |
9 ms |
19036 KB |
Output is correct |
9 |
Correct |
9 ms |
19036 KB |
Output is correct |
10 |
Correct |
9 ms |
19220 KB |
Output is correct |
11 |
Correct |
9 ms |
19292 KB |
Output is correct |
12 |
Correct |
9 ms |
19208 KB |
Output is correct |
13 |
Correct |
9 ms |
19420 KB |
Output is correct |
14 |
Correct |
9 ms |
19292 KB |
Output is correct |
15 |
Correct |
10 ms |
19292 KB |
Output is correct |
16 |
Correct |
12 ms |
20316 KB |
Output is correct |
17 |
Correct |
14 ms |
20060 KB |
Output is correct |
18 |
Correct |
11 ms |
20572 KB |
Output is correct |
19 |
Correct |
16 ms |
20572 KB |
Output is correct |
20 |
Correct |
11 ms |
20312 KB |
Output is correct |
21 |
Correct |
11 ms |
20060 KB |
Output is correct |
22 |
Correct |
9 ms |
19800 KB |
Output is correct |
23 |
Correct |
12 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19032 KB |
Output is correct |
2 |
Correct |
9 ms |
19032 KB |
Output is correct |
3 |
Correct |
9 ms |
19232 KB |
Output is correct |
4 |
Correct |
12 ms |
19260 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
9 ms |
19232 KB |
Output is correct |
7 |
Correct |
10 ms |
19156 KB |
Output is correct |
8 |
Correct |
9 ms |
19036 KB |
Output is correct |
9 |
Correct |
9 ms |
19036 KB |
Output is correct |
10 |
Correct |
9 ms |
19220 KB |
Output is correct |
11 |
Correct |
9 ms |
19292 KB |
Output is correct |
12 |
Correct |
9 ms |
19208 KB |
Output is correct |
13 |
Correct |
9 ms |
19420 KB |
Output is correct |
14 |
Correct |
9 ms |
19292 KB |
Output is correct |
15 |
Correct |
10 ms |
19292 KB |
Output is correct |
16 |
Correct |
12 ms |
20316 KB |
Output is correct |
17 |
Correct |
14 ms |
20060 KB |
Output is correct |
18 |
Correct |
11 ms |
20572 KB |
Output is correct |
19 |
Correct |
16 ms |
20572 KB |
Output is correct |
20 |
Correct |
11 ms |
20312 KB |
Output is correct |
21 |
Correct |
11 ms |
20060 KB |
Output is correct |
22 |
Correct |
9 ms |
19800 KB |
Output is correct |
23 |
Correct |
12 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20060 KB |
Output is correct |
25 |
Correct |
10 ms |
19036 KB |
Output is correct |
26 |
Correct |
8 ms |
19036 KB |
Output is correct |
27 |
Correct |
16 ms |
20316 KB |
Output is correct |
28 |
Correct |
12 ms |
20140 KB |
Output is correct |
29 |
Correct |
10 ms |
20180 KB |
Output is correct |
30 |
Correct |
11 ms |
20056 KB |
Output is correct |
31 |
Correct |
11 ms |
20060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19032 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
9 ms |
19036 KB |
Output is correct |
4 |
Correct |
582 ms |
124756 KB |
Output is correct |
5 |
Correct |
304 ms |
84528 KB |
Output is correct |
6 |
Correct |
188 ms |
73916 KB |
Output is correct |
7 |
Correct |
147 ms |
67652 KB |
Output is correct |
8 |
Correct |
101 ms |
74160 KB |
Output is correct |
9 |
Correct |
8 ms |
19032 KB |
Output is correct |
10 |
Correct |
9 ms |
19032 KB |
Output is correct |
11 |
Correct |
9 ms |
19232 KB |
Output is correct |
12 |
Correct |
12 ms |
19260 KB |
Output is correct |
13 |
Correct |
10 ms |
19032 KB |
Output is correct |
14 |
Correct |
9 ms |
19232 KB |
Output is correct |
15 |
Correct |
10 ms |
19156 KB |
Output is correct |
16 |
Correct |
9 ms |
19036 KB |
Output is correct |
17 |
Correct |
9 ms |
19036 KB |
Output is correct |
18 |
Correct |
9 ms |
19220 KB |
Output is correct |
19 |
Correct |
9 ms |
19292 KB |
Output is correct |
20 |
Correct |
9 ms |
19208 KB |
Output is correct |
21 |
Correct |
9 ms |
19420 KB |
Output is correct |
22 |
Correct |
9 ms |
19292 KB |
Output is correct |
23 |
Correct |
10 ms |
19292 KB |
Output is correct |
24 |
Correct |
12 ms |
20316 KB |
Output is correct |
25 |
Correct |
14 ms |
20060 KB |
Output is correct |
26 |
Correct |
11 ms |
20572 KB |
Output is correct |
27 |
Correct |
16 ms |
20572 KB |
Output is correct |
28 |
Correct |
11 ms |
20312 KB |
Output is correct |
29 |
Correct |
11 ms |
20060 KB |
Output is correct |
30 |
Correct |
9 ms |
19800 KB |
Output is correct |
31 |
Correct |
12 ms |
20060 KB |
Output is correct |
32 |
Correct |
10 ms |
20060 KB |
Output is correct |
33 |
Correct |
10 ms |
19036 KB |
Output is correct |
34 |
Correct |
8 ms |
19036 KB |
Output is correct |
35 |
Correct |
16 ms |
20316 KB |
Output is correct |
36 |
Correct |
12 ms |
20140 KB |
Output is correct |
37 |
Correct |
10 ms |
20180 KB |
Output is correct |
38 |
Correct |
11 ms |
20056 KB |
Output is correct |
39 |
Correct |
11 ms |
20060 KB |
Output is correct |
40 |
Correct |
622 ms |
128456 KB |
Output is correct |
41 |
Correct |
329 ms |
86712 KB |
Output is correct |
42 |
Correct |
425 ms |
138496 KB |
Output is correct |
43 |
Correct |
376 ms |
138548 KB |
Output is correct |
44 |
Correct |
235 ms |
76108 KB |
Output is correct |
45 |
Correct |
255 ms |
87156 KB |
Output is correct |
46 |
Correct |
99 ms |
45240 KB |
Output is correct |
47 |
Correct |
340 ms |
93872 KB |
Output is correct |
48 |
Correct |
262 ms |
102948 KB |
Output is correct |