#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 200005
#define MAX_NODE 1200005
#define lson (nd << 1)
#define rson (nd << 1 | 1)
int numNode;
int nArr, numQuery;
int l[MAX], r[MAX];
int ID[MAX_NODE];
vector<pair<int, int>> G[MAX_NODE];
void addEdge(int u, int v, int w){
G[u].emplace_back(v, w);
}
void build(int nd, int l, int r){
if (l == r){
addEdge(l, ID[nd], 0);
return;
}
int m = (l + r) >> 1;
build(lson, l, m);
build(rson, m + 1, r);
addEdge(ID[lson], ID[nd], 0);
addEdge(ID[rson], ID[nd], 0);
}
void addEdge(int nd, int l, int r, int u, int v, int w, int from){
if (u > r || v < l) return;
if (u <= l && v >= r){
addEdge(ID[nd], from, w);
return;
}
int m = (l + r) >> 1;
addEdge(lson, l, m, u, v, w, from);
addEdge(rson, m + 1, r, u, v, w, from);
}
void dijkstra(int s, int minDist[]){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.emplace(0, s);
for (int i = 1; i <= numNode; ++i) minDist[i] = INF;
minDist[s] = 0;
while(q.size()){
int du, u; tie(du, u) = q.top(); q.pop();
if (du > minDist[u]) continue;
for (pair<int, int>&x : G[u]){
int v = x.first, w = x.second;
if(minimize(minDist[v], minDist[u] + w)){
q.emplace(minDist[v], v);
}
}
}
}
int minDist[2][MAX_NODE];
int F[MAX_NODE];
void process(void){
cin >> nArr;
numNode = nArr;
for (int i = 1; i <= nArr * 4; ++i) ID[i] = ++numNode;
build(1, 1, nArr);
for (int i = 1; i <= nArr; ++i){
cin >> l[i] >> r[i];
addEdge(1, 1, nArr, l[i], r[i], 0, ++numNode);
addEdge(numNode, i, 1);
}
dijkstra(1, minDist[0]);
dijkstra(nArr, minDist[1]);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int i = 1; i <= numNode; ++i){
F[i] = minDist[0][i] + minDist[1][i];
q.emplace(F[i], i);
}
while(q.size()){
int du, u; tie(du, u) = q.top(); q.pop();
if(du > F[u]) continue;
for (pair<int, int>&x : G[u]){
int v = x.first, w = x.second;
if(minimize(F[v], F[u] + w)){
q.emplace(F[v], v);
}
}
}
cin >> numQuery;
for (int i = 1; i <= numQuery; ++i){
int x; cin >> x;
cout << (F[x] >= INF ? -1 : F[x]) << '\n';
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28604 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28504 KB |
Output is correct |
4 |
Correct |
1061 ms |
181008 KB |
Output is correct |
5 |
Correct |
616 ms |
144040 KB |
Output is correct |
6 |
Correct |
347 ms |
136624 KB |
Output is correct |
7 |
Correct |
425 ms |
126448 KB |
Output is correct |
8 |
Correct |
256 ms |
116304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28508 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
12 ms |
28528 KB |
Output is correct |
4 |
Correct |
14 ms |
28508 KB |
Output is correct |
5 |
Correct |
13 ms |
28800 KB |
Output is correct |
6 |
Correct |
13 ms |
28508 KB |
Output is correct |
7 |
Correct |
13 ms |
28508 KB |
Output is correct |
8 |
Correct |
12 ms |
28508 KB |
Output is correct |
9 |
Correct |
14 ms |
28504 KB |
Output is correct |
10 |
Correct |
13 ms |
28572 KB |
Output is correct |
11 |
Correct |
13 ms |
28760 KB |
Output is correct |
12 |
Correct |
13 ms |
28660 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
12 ms |
28764 KB |
Output is correct |
15 |
Correct |
12 ms |
28764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28508 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
12 ms |
28528 KB |
Output is correct |
4 |
Correct |
14 ms |
28508 KB |
Output is correct |
5 |
Correct |
13 ms |
28800 KB |
Output is correct |
6 |
Correct |
13 ms |
28508 KB |
Output is correct |
7 |
Correct |
13 ms |
28508 KB |
Output is correct |
8 |
Correct |
12 ms |
28508 KB |
Output is correct |
9 |
Correct |
14 ms |
28504 KB |
Output is correct |
10 |
Correct |
13 ms |
28572 KB |
Output is correct |
11 |
Correct |
13 ms |
28760 KB |
Output is correct |
12 |
Correct |
13 ms |
28660 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
12 ms |
28764 KB |
Output is correct |
15 |
Correct |
12 ms |
28764 KB |
Output is correct |
16 |
Correct |
19 ms |
30168 KB |
Output is correct |
17 |
Correct |
21 ms |
30052 KB |
Output is correct |
18 |
Correct |
17 ms |
30424 KB |
Output is correct |
19 |
Correct |
18 ms |
30432 KB |
Output is correct |
20 |
Correct |
17 ms |
29912 KB |
Output is correct |
21 |
Correct |
16 ms |
30044 KB |
Output is correct |
22 |
Correct |
23 ms |
29912 KB |
Output is correct |
23 |
Correct |
18 ms |
30052 KB |
Output is correct |
24 |
Correct |
17 ms |
30428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28508 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
12 ms |
28528 KB |
Output is correct |
4 |
Correct |
14 ms |
28508 KB |
Output is correct |
5 |
Correct |
13 ms |
28800 KB |
Output is correct |
6 |
Correct |
13 ms |
28508 KB |
Output is correct |
7 |
Correct |
13 ms |
28508 KB |
Output is correct |
8 |
Correct |
12 ms |
28508 KB |
Output is correct |
9 |
Correct |
14 ms |
28504 KB |
Output is correct |
10 |
Correct |
13 ms |
28572 KB |
Output is correct |
11 |
Correct |
13 ms |
28760 KB |
Output is correct |
12 |
Correct |
13 ms |
28660 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
12 ms |
28764 KB |
Output is correct |
15 |
Correct |
12 ms |
28764 KB |
Output is correct |
16 |
Correct |
19 ms |
30168 KB |
Output is correct |
17 |
Correct |
21 ms |
30052 KB |
Output is correct |
18 |
Correct |
17 ms |
30424 KB |
Output is correct |
19 |
Correct |
18 ms |
30432 KB |
Output is correct |
20 |
Correct |
17 ms |
29912 KB |
Output is correct |
21 |
Correct |
16 ms |
30044 KB |
Output is correct |
22 |
Correct |
23 ms |
29912 KB |
Output is correct |
23 |
Correct |
18 ms |
30052 KB |
Output is correct |
24 |
Correct |
17 ms |
30428 KB |
Output is correct |
25 |
Correct |
13 ms |
28508 KB |
Output is correct |
26 |
Correct |
13 ms |
28508 KB |
Output is correct |
27 |
Correct |
19 ms |
30164 KB |
Output is correct |
28 |
Correct |
19 ms |
30044 KB |
Output is correct |
29 |
Correct |
17 ms |
29984 KB |
Output is correct |
30 |
Correct |
17 ms |
30044 KB |
Output is correct |
31 |
Correct |
17 ms |
30080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
28604 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28504 KB |
Output is correct |
4 |
Correct |
1061 ms |
181008 KB |
Output is correct |
5 |
Correct |
616 ms |
144040 KB |
Output is correct |
6 |
Correct |
347 ms |
136624 KB |
Output is correct |
7 |
Correct |
425 ms |
126448 KB |
Output is correct |
8 |
Correct |
256 ms |
116304 KB |
Output is correct |
9 |
Correct |
13 ms |
28508 KB |
Output is correct |
10 |
Correct |
13 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28528 KB |
Output is correct |
12 |
Correct |
14 ms |
28508 KB |
Output is correct |
13 |
Correct |
13 ms |
28800 KB |
Output is correct |
14 |
Correct |
13 ms |
28508 KB |
Output is correct |
15 |
Correct |
13 ms |
28508 KB |
Output is correct |
16 |
Correct |
12 ms |
28508 KB |
Output is correct |
17 |
Correct |
14 ms |
28504 KB |
Output is correct |
18 |
Correct |
13 ms |
28572 KB |
Output is correct |
19 |
Correct |
13 ms |
28760 KB |
Output is correct |
20 |
Correct |
13 ms |
28660 KB |
Output is correct |
21 |
Correct |
13 ms |
28764 KB |
Output is correct |
22 |
Correct |
12 ms |
28764 KB |
Output is correct |
23 |
Correct |
12 ms |
28764 KB |
Output is correct |
24 |
Correct |
19 ms |
30168 KB |
Output is correct |
25 |
Correct |
21 ms |
30052 KB |
Output is correct |
26 |
Correct |
17 ms |
30424 KB |
Output is correct |
27 |
Correct |
18 ms |
30432 KB |
Output is correct |
28 |
Correct |
17 ms |
29912 KB |
Output is correct |
29 |
Correct |
16 ms |
30044 KB |
Output is correct |
30 |
Correct |
23 ms |
29912 KB |
Output is correct |
31 |
Correct |
18 ms |
30052 KB |
Output is correct |
32 |
Correct |
17 ms |
30428 KB |
Output is correct |
33 |
Correct |
13 ms |
28508 KB |
Output is correct |
34 |
Correct |
13 ms |
28508 KB |
Output is correct |
35 |
Correct |
19 ms |
30164 KB |
Output is correct |
36 |
Correct |
19 ms |
30044 KB |
Output is correct |
37 |
Correct |
17 ms |
29984 KB |
Output is correct |
38 |
Correct |
17 ms |
30044 KB |
Output is correct |
39 |
Correct |
17 ms |
30080 KB |
Output is correct |
40 |
Correct |
1113 ms |
181384 KB |
Output is correct |
41 |
Correct |
640 ms |
144620 KB |
Output is correct |
42 |
Correct |
740 ms |
189048 KB |
Output is correct |
43 |
Correct |
720 ms |
189056 KB |
Output is correct |
44 |
Correct |
373 ms |
136288 KB |
Output is correct |
45 |
Correct |
511 ms |
147040 KB |
Output is correct |
46 |
Correct |
231 ms |
67304 KB |
Output is correct |
47 |
Correct |
720 ms |
149092 KB |
Output is correct |
48 |
Correct |
527 ms |
161116 KB |
Output is correct |