#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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28508 KB |
Output is correct |
2 |
Correct |
14 ms |
28692 KB |
Output is correct |
3 |
Correct |
14 ms |
28508 KB |
Output is correct |
4 |
Correct |
1061 ms |
181020 KB |
Output is correct |
5 |
Correct |
612 ms |
144052 KB |
Output is correct |
6 |
Correct |
336 ms |
136628 KB |
Output is correct |
7 |
Correct |
427 ms |
126292 KB |
Output is correct |
8 |
Correct |
264 ms |
116364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28508 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28504 KB |
Output is correct |
4 |
Correct |
13 ms |
28636 KB |
Output is correct |
5 |
Correct |
14 ms |
28764 KB |
Output is correct |
6 |
Correct |
16 ms |
28588 KB |
Output is correct |
7 |
Correct |
15 ms |
28508 KB |
Output is correct |
8 |
Correct |
13 ms |
28508 KB |
Output is correct |
9 |
Correct |
13 ms |
28464 KB |
Output is correct |
10 |
Correct |
13 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28764 KB |
Output is correct |
12 |
Correct |
13 ms |
28764 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
13 ms |
28764 KB |
Output is correct |
15 |
Correct |
14 ms |
28764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28508 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28504 KB |
Output is correct |
4 |
Correct |
13 ms |
28636 KB |
Output is correct |
5 |
Correct |
14 ms |
28764 KB |
Output is correct |
6 |
Correct |
16 ms |
28588 KB |
Output is correct |
7 |
Correct |
15 ms |
28508 KB |
Output is correct |
8 |
Correct |
13 ms |
28508 KB |
Output is correct |
9 |
Correct |
13 ms |
28464 KB |
Output is correct |
10 |
Correct |
13 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28764 KB |
Output is correct |
12 |
Correct |
13 ms |
28764 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
13 ms |
28764 KB |
Output is correct |
15 |
Correct |
14 ms |
28764 KB |
Output is correct |
16 |
Correct |
19 ms |
30196 KB |
Output is correct |
17 |
Correct |
18 ms |
30040 KB |
Output is correct |
18 |
Correct |
18 ms |
30424 KB |
Output is correct |
19 |
Correct |
20 ms |
30396 KB |
Output is correct |
20 |
Correct |
16 ms |
29912 KB |
Output is correct |
21 |
Correct |
17 ms |
30072 KB |
Output is correct |
22 |
Correct |
15 ms |
29912 KB |
Output is correct |
23 |
Correct |
18 ms |
30020 KB |
Output is correct |
24 |
Correct |
16 ms |
30172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28508 KB |
Output is correct |
2 |
Correct |
13 ms |
28508 KB |
Output is correct |
3 |
Correct |
13 ms |
28504 KB |
Output is correct |
4 |
Correct |
13 ms |
28636 KB |
Output is correct |
5 |
Correct |
14 ms |
28764 KB |
Output is correct |
6 |
Correct |
16 ms |
28588 KB |
Output is correct |
7 |
Correct |
15 ms |
28508 KB |
Output is correct |
8 |
Correct |
13 ms |
28508 KB |
Output is correct |
9 |
Correct |
13 ms |
28464 KB |
Output is correct |
10 |
Correct |
13 ms |
28508 KB |
Output is correct |
11 |
Correct |
12 ms |
28764 KB |
Output is correct |
12 |
Correct |
13 ms |
28764 KB |
Output is correct |
13 |
Correct |
13 ms |
28764 KB |
Output is correct |
14 |
Correct |
13 ms |
28764 KB |
Output is correct |
15 |
Correct |
14 ms |
28764 KB |
Output is correct |
16 |
Correct |
19 ms |
30196 KB |
Output is correct |
17 |
Correct |
18 ms |
30040 KB |
Output is correct |
18 |
Correct |
18 ms |
30424 KB |
Output is correct |
19 |
Correct |
20 ms |
30396 KB |
Output is correct |
20 |
Correct |
16 ms |
29912 KB |
Output is correct |
21 |
Correct |
17 ms |
30072 KB |
Output is correct |
22 |
Correct |
15 ms |
29912 KB |
Output is correct |
23 |
Correct |
18 ms |
30020 KB |
Output is correct |
24 |
Correct |
16 ms |
30172 KB |
Output is correct |
25 |
Correct |
14 ms |
28508 KB |
Output is correct |
26 |
Correct |
18 ms |
28492 KB |
Output is correct |
27 |
Correct |
20 ms |
30168 KB |
Output is correct |
28 |
Correct |
17 ms |
30040 KB |
Output is correct |
29 |
Correct |
18 ms |
29912 KB |
Output is correct |
30 |
Correct |
18 ms |
30104 KB |
Output is correct |
31 |
Correct |
23 ms |
30044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28508 KB |
Output is correct |
2 |
Correct |
14 ms |
28692 KB |
Output is correct |
3 |
Correct |
14 ms |
28508 KB |
Output is correct |
4 |
Correct |
1061 ms |
181020 KB |
Output is correct |
5 |
Correct |
612 ms |
144052 KB |
Output is correct |
6 |
Correct |
336 ms |
136628 KB |
Output is correct |
7 |
Correct |
427 ms |
126292 KB |
Output is correct |
8 |
Correct |
264 ms |
116364 KB |
Output is correct |
9 |
Correct |
14 ms |
28508 KB |
Output is correct |
10 |
Correct |
13 ms |
28508 KB |
Output is correct |
11 |
Correct |
13 ms |
28504 KB |
Output is correct |
12 |
Correct |
13 ms |
28636 KB |
Output is correct |
13 |
Correct |
14 ms |
28764 KB |
Output is correct |
14 |
Correct |
16 ms |
28588 KB |
Output is correct |
15 |
Correct |
15 ms |
28508 KB |
Output is correct |
16 |
Correct |
13 ms |
28508 KB |
Output is correct |
17 |
Correct |
13 ms |
28464 KB |
Output is correct |
18 |
Correct |
13 ms |
28508 KB |
Output is correct |
19 |
Correct |
12 ms |
28764 KB |
Output is correct |
20 |
Correct |
13 ms |
28764 KB |
Output is correct |
21 |
Correct |
13 ms |
28764 KB |
Output is correct |
22 |
Correct |
13 ms |
28764 KB |
Output is correct |
23 |
Correct |
14 ms |
28764 KB |
Output is correct |
24 |
Correct |
19 ms |
30196 KB |
Output is correct |
25 |
Correct |
18 ms |
30040 KB |
Output is correct |
26 |
Correct |
18 ms |
30424 KB |
Output is correct |
27 |
Correct |
20 ms |
30396 KB |
Output is correct |
28 |
Correct |
16 ms |
29912 KB |
Output is correct |
29 |
Correct |
17 ms |
30072 KB |
Output is correct |
30 |
Correct |
15 ms |
29912 KB |
Output is correct |
31 |
Correct |
18 ms |
30020 KB |
Output is correct |
32 |
Correct |
16 ms |
30172 KB |
Output is correct |
33 |
Correct |
14 ms |
28508 KB |
Output is correct |
34 |
Correct |
18 ms |
28492 KB |
Output is correct |
35 |
Correct |
20 ms |
30168 KB |
Output is correct |
36 |
Correct |
17 ms |
30040 KB |
Output is correct |
37 |
Correct |
18 ms |
29912 KB |
Output is correct |
38 |
Correct |
18 ms |
30104 KB |
Output is correct |
39 |
Correct |
23 ms |
30044 KB |
Output is correct |
40 |
Correct |
1129 ms |
181996 KB |
Output is correct |
41 |
Correct |
684 ms |
145068 KB |
Output is correct |
42 |
Correct |
765 ms |
189680 KB |
Output is correct |
43 |
Correct |
743 ms |
189700 KB |
Output is correct |
44 |
Correct |
377 ms |
136884 KB |
Output is correct |
45 |
Correct |
545 ms |
147872 KB |
Output is correct |
46 |
Correct |
240 ms |
67304 KB |
Output is correct |
47 |
Correct |
707 ms |
149524 KB |
Output is correct |
48 |
Correct |
578 ms |
161552 KB |
Output is correct |