#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define lg long long
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 1e6+5;
vector<array<lg, 2>> adj[N];
lg dis[2][N], dist[N], id, n, in_queue[N];
void propagate(lg node, lg lr, lg hr, lg idx, lg lq, lg hq)
{
if(lq > hr || lr > hq) return;
if(lq <= lr && hr <= hq)
{
adj[node].push_back({idx, 0});
return;
}
lg mid = (lr+hr)/2;
propagate(node << 1, lr, mid, idx, lq, hq);
propagate(node << 1 | 1, mid+1, hr, idx, lq, hq);
return;
}
void dijkstra(lg t, lg src)
{
memset(dis[t], 0x3f, sizeof(dis[t]));
dis[t][src] = 0;
priority_queue<array<lg, 2>> pq;
pq.push({0, src});
in_queue[src] = true;
while(pq.size())
{
lg u = pq.top()[1];
in_queue[u] = false;
pq.pop();
for(auto [it, c] : adj[u])
{
if(dis[t][it] > dis[t][u]+c)
{
dis[t][it] = dis[t][u]+c;
if(in_queue[it]) continue;
pq.push({-dis[t][it], it});
in_queue[it] = true;
}
}
}
return;
}
int main()
{
fastio;
cin >> n;
lg sn = 1;
while(sn < n) sn *= 2;
id = sn*2+1;
for(int i = 1; i <= n; i++)
{
lg x, y;
cin >> x >> y;
id++;
adj[id].push_back({i-1+sn, 1});
propagate(1, 1, sn, id, x, y);
}
for(int i = 1; i < sn; i++)
{
adj[i*2].push_back({i, 0});
adj[i*2+1].push_back({i, 0});
}
// for(int i = 1; i <= id; i++)
// {
// for(auto [it, c] : adj[i])
// {
// cout << i << " " << it << ' ' << c << '\n';
// }
// }
dijkstra(0, sn);
dijkstra(1, sn+n-1);
priority_queue<array<lg, 2>> pq;
for(int i = 1; i <= id; i++) dist[i] += dis[0][i]+dis[1][i];
for(int i = 1; i <= id; i++)
{
if(dist[i] > 1e9) continue;
pq.push({-dist[i], i}), in_queue[i] = 1;
}
while(pq.size())
{
lg u = pq.top()[1];
in_queue[u] = false;
pq.pop();
for(auto [it, c] : adj[u])
{
if(dist[it] > dist[u]+c)
{
dist[it] = dist[u]+c;
if(in_queue[it]) continue;
pq.push({-dist[it], it});
in_queue[it] = true;
}
}
}
lg q;
cin >> q;
while(q--)
{
lg node;
cin >> node;
if(dist[node+sn-1] > 1e9) dist[node+sn-1] = -1;
cout << dist[node+sn-1] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
39380 KB |
Output is correct |
2 |
Correct |
20 ms |
39432 KB |
Output is correct |
3 |
Correct |
18 ms |
39392 KB |
Output is correct |
4 |
Correct |
981 ms |
144396 KB |
Output is correct |
5 |
Correct |
502 ms |
107572 KB |
Output is correct |
6 |
Correct |
275 ms |
100652 KB |
Output is correct |
7 |
Correct |
364 ms |
123608 KB |
Output is correct |
8 |
Correct |
192 ms |
107464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
39428 KB |
Output is correct |
2 |
Correct |
20 ms |
39380 KB |
Output is correct |
3 |
Correct |
19 ms |
39380 KB |
Output is correct |
4 |
Correct |
19 ms |
39476 KB |
Output is correct |
5 |
Correct |
20 ms |
39432 KB |
Output is correct |
6 |
Correct |
21 ms |
39560 KB |
Output is correct |
7 |
Correct |
19 ms |
39428 KB |
Output is correct |
8 |
Correct |
20 ms |
39484 KB |
Output is correct |
9 |
Correct |
19 ms |
39380 KB |
Output is correct |
10 |
Correct |
20 ms |
39372 KB |
Output is correct |
11 |
Correct |
20 ms |
39544 KB |
Output is correct |
12 |
Correct |
20 ms |
39508 KB |
Output is correct |
13 |
Correct |
21 ms |
39620 KB |
Output is correct |
14 |
Correct |
20 ms |
39576 KB |
Output is correct |
15 |
Correct |
20 ms |
39556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
39428 KB |
Output is correct |
2 |
Correct |
20 ms |
39380 KB |
Output is correct |
3 |
Correct |
19 ms |
39380 KB |
Output is correct |
4 |
Correct |
19 ms |
39476 KB |
Output is correct |
5 |
Correct |
20 ms |
39432 KB |
Output is correct |
6 |
Correct |
21 ms |
39560 KB |
Output is correct |
7 |
Correct |
19 ms |
39428 KB |
Output is correct |
8 |
Correct |
20 ms |
39484 KB |
Output is correct |
9 |
Correct |
19 ms |
39380 KB |
Output is correct |
10 |
Correct |
20 ms |
39372 KB |
Output is correct |
11 |
Correct |
20 ms |
39544 KB |
Output is correct |
12 |
Correct |
20 ms |
39508 KB |
Output is correct |
13 |
Correct |
21 ms |
39620 KB |
Output is correct |
14 |
Correct |
20 ms |
39576 KB |
Output is correct |
15 |
Correct |
20 ms |
39556 KB |
Output is correct |
16 |
Correct |
24 ms |
40524 KB |
Output is correct |
17 |
Correct |
25 ms |
40404 KB |
Output is correct |
18 |
Correct |
24 ms |
40732 KB |
Output is correct |
19 |
Correct |
25 ms |
40660 KB |
Output is correct |
20 |
Correct |
24 ms |
40260 KB |
Output is correct |
21 |
Correct |
22 ms |
40164 KB |
Output is correct |
22 |
Correct |
21 ms |
40436 KB |
Output is correct |
23 |
Correct |
27 ms |
40440 KB |
Output is correct |
24 |
Correct |
22 ms |
40404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
39428 KB |
Output is correct |
2 |
Correct |
20 ms |
39380 KB |
Output is correct |
3 |
Correct |
19 ms |
39380 KB |
Output is correct |
4 |
Correct |
19 ms |
39476 KB |
Output is correct |
5 |
Correct |
20 ms |
39432 KB |
Output is correct |
6 |
Correct |
21 ms |
39560 KB |
Output is correct |
7 |
Correct |
19 ms |
39428 KB |
Output is correct |
8 |
Correct |
20 ms |
39484 KB |
Output is correct |
9 |
Correct |
19 ms |
39380 KB |
Output is correct |
10 |
Correct |
20 ms |
39372 KB |
Output is correct |
11 |
Correct |
20 ms |
39544 KB |
Output is correct |
12 |
Correct |
20 ms |
39508 KB |
Output is correct |
13 |
Correct |
21 ms |
39620 KB |
Output is correct |
14 |
Correct |
20 ms |
39576 KB |
Output is correct |
15 |
Correct |
20 ms |
39556 KB |
Output is correct |
16 |
Correct |
24 ms |
40524 KB |
Output is correct |
17 |
Correct |
25 ms |
40404 KB |
Output is correct |
18 |
Correct |
24 ms |
40732 KB |
Output is correct |
19 |
Correct |
25 ms |
40660 KB |
Output is correct |
20 |
Correct |
24 ms |
40260 KB |
Output is correct |
21 |
Correct |
22 ms |
40164 KB |
Output is correct |
22 |
Correct |
21 ms |
40436 KB |
Output is correct |
23 |
Correct |
27 ms |
40440 KB |
Output is correct |
24 |
Correct |
22 ms |
40404 KB |
Output is correct |
25 |
Correct |
18 ms |
39420 KB |
Output is correct |
26 |
Correct |
18 ms |
39428 KB |
Output is correct |
27 |
Correct |
25 ms |
40596 KB |
Output is correct |
28 |
Correct |
24 ms |
40404 KB |
Output is correct |
29 |
Correct |
22 ms |
40336 KB |
Output is correct |
30 |
Correct |
23 ms |
40200 KB |
Output is correct |
31 |
Correct |
22 ms |
40504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
39380 KB |
Output is correct |
2 |
Correct |
20 ms |
39432 KB |
Output is correct |
3 |
Correct |
18 ms |
39392 KB |
Output is correct |
4 |
Correct |
981 ms |
144396 KB |
Output is correct |
5 |
Correct |
502 ms |
107572 KB |
Output is correct |
6 |
Correct |
275 ms |
100652 KB |
Output is correct |
7 |
Correct |
364 ms |
123608 KB |
Output is correct |
8 |
Correct |
192 ms |
107464 KB |
Output is correct |
9 |
Correct |
20 ms |
39428 KB |
Output is correct |
10 |
Correct |
20 ms |
39380 KB |
Output is correct |
11 |
Correct |
19 ms |
39380 KB |
Output is correct |
12 |
Correct |
19 ms |
39476 KB |
Output is correct |
13 |
Correct |
20 ms |
39432 KB |
Output is correct |
14 |
Correct |
21 ms |
39560 KB |
Output is correct |
15 |
Correct |
19 ms |
39428 KB |
Output is correct |
16 |
Correct |
20 ms |
39484 KB |
Output is correct |
17 |
Correct |
19 ms |
39380 KB |
Output is correct |
18 |
Correct |
20 ms |
39372 KB |
Output is correct |
19 |
Correct |
20 ms |
39544 KB |
Output is correct |
20 |
Correct |
20 ms |
39508 KB |
Output is correct |
21 |
Correct |
21 ms |
39620 KB |
Output is correct |
22 |
Correct |
20 ms |
39576 KB |
Output is correct |
23 |
Correct |
20 ms |
39556 KB |
Output is correct |
24 |
Correct |
24 ms |
40524 KB |
Output is correct |
25 |
Correct |
25 ms |
40404 KB |
Output is correct |
26 |
Correct |
24 ms |
40732 KB |
Output is correct |
27 |
Correct |
25 ms |
40660 KB |
Output is correct |
28 |
Correct |
24 ms |
40260 KB |
Output is correct |
29 |
Correct |
22 ms |
40164 KB |
Output is correct |
30 |
Correct |
21 ms |
40436 KB |
Output is correct |
31 |
Correct |
27 ms |
40440 KB |
Output is correct |
32 |
Correct |
22 ms |
40404 KB |
Output is correct |
33 |
Correct |
18 ms |
39420 KB |
Output is correct |
34 |
Correct |
18 ms |
39428 KB |
Output is correct |
35 |
Correct |
25 ms |
40596 KB |
Output is correct |
36 |
Correct |
24 ms |
40404 KB |
Output is correct |
37 |
Correct |
22 ms |
40336 KB |
Output is correct |
38 |
Correct |
23 ms |
40200 KB |
Output is correct |
39 |
Correct |
22 ms |
40504 KB |
Output is correct |
40 |
Correct |
1068 ms |
144780 KB |
Output is correct |
41 |
Correct |
525 ms |
107752 KB |
Output is correct |
42 |
Correct |
626 ms |
159308 KB |
Output is correct |
43 |
Correct |
624 ms |
158316 KB |
Output is correct |
44 |
Correct |
306 ms |
100556 KB |
Output is correct |
45 |
Correct |
358 ms |
102440 KB |
Output is correct |
46 |
Correct |
248 ms |
66964 KB |
Output is correct |
47 |
Correct |
665 ms |
121968 KB |
Output is correct |
48 |
Correct |
439 ms |
117744 KB |
Output is correct |