#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int,int>
using namespace std;
const int N = 2e5+10;
const int INF = INT_MAX;
const int mod = 1e9+7;
const string NAME = "solve";
int n,cnt;
int d[3][8*N];
vector<pii> adj[16*N];
int idx[2][8*N],ok[16*N];
int q;
void build(int id = 1, int l = 1, int r = 2*n) {
if(l == r) {
idx[0][id] = idx[1][id] = l;
return;
}
idx[0][id] = ++cnt;
idx[1][id] = ++cnt;
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid+1,r);
adj[idx[0][id]].push_back({idx[0][id << 1],0});
adj[idx[1][id << 1]].push_back({idx[1][id],0});
adj[idx[1][id << 1 | 1]].push_back({idx[1][id],0});
adj[idx[0][id]].push_back({idx[0][id << 1 | 1],0});
}
void update(int root, int u, int v, int val, int id = 1, int l = 1, int r = 2*n) {
if(r < u or v < l) {
return;
}
if(u <= l and r <= v) {
if(val) {
adj[idx[val][id]].push_back({root,val});
}
else {
adj[root].push_back({idx[val][id],val});
}
return;
}
int mid = l + r >> 1;
update(root,u,v,val,id << 1,l,mid);
update(root,u,v,val,id << 1 | 1,mid+1,r);
}
queue<int> pq;
void dijkstra(int d[]) {
while(!pq.empty()) {
int u = pq.front();
pq.pop();
for(auto p:adj[u]) {
int v = p.first;
int w = p.second;
if(d[u] + w < d[v]) {
d[v] = d[u] + w;
pq.push(v);
}
}
}
}
signed main()
{
if (fopen((NAME + ".inp").c_str(), "r")) {
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cnt = 2*n;
build();
for(int i = 1 ; i <= n ; i++) {
int l,r; cin >> l >> r;
update(i+n,l,r,1);
adj[i+n].push_back({i,0});
}
for(int j = 0 ; j < 3 ; j++) {
for(int i = 1 ; i <= cnt ; i++) {
d[j][i] = 1e8;
}
}
d[0][1] = 0;
pq.push(1);
dijkstra(d[0]);
d[1][n] = 0;
pq.push(n);
dijkstra(d[1]);
for(int i = 1 ; i <= cnt ; i++) {
if(i == 1 or i == n) {
d[2][i] = d[0][i] + d[1][i];
}
else {
if(1 <= i and i <= n) {
d[2][i] = d[0][i] + d[1][i] - 1;
}
else {
d[2][i] = d[0][i] + d[1][i];
}
}
pq.push(i);
}
dijkstra(d[2]);
cin >> q;
while(q--) {
int x; cin >> x;
if(d[2][x] >= 1e8) {
d[2][x] = -1;
}
cout << d[2][x] << '\n';
}
return 0;
}
Compilation message
passport.cpp: In function 'void build(int, int, int)':
passport.cpp:26:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'void update(int, int, int, int, int, int, int)':
passport.cpp:48:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int mid = l + r >> 1;
| ~~^~~
passport.cpp: In function 'int main()':
passport.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen((NAME + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
84564 KB |
Output is correct |
2 |
Correct |
17 ms |
84568 KB |
Output is correct |
3 |
Correct |
18 ms |
84520 KB |
Output is correct |
4 |
Correct |
890 ms |
172764 KB |
Output is correct |
5 |
Execution timed out |
2053 ms |
149232 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
84572 KB |
Output is correct |
2 |
Correct |
17 ms |
84572 KB |
Output is correct |
3 |
Correct |
18 ms |
84572 KB |
Output is correct |
4 |
Correct |
17 ms |
84572 KB |
Output is correct |
5 |
Correct |
17 ms |
84576 KB |
Output is correct |
6 |
Correct |
18 ms |
84572 KB |
Output is correct |
7 |
Correct |
17 ms |
84824 KB |
Output is correct |
8 |
Correct |
17 ms |
84496 KB |
Output is correct |
9 |
Correct |
17 ms |
84572 KB |
Output is correct |
10 |
Correct |
18 ms |
84572 KB |
Output is correct |
11 |
Correct |
18 ms |
84572 KB |
Output is correct |
12 |
Correct |
18 ms |
84572 KB |
Output is correct |
13 |
Correct |
18 ms |
84572 KB |
Output is correct |
14 |
Correct |
18 ms |
84480 KB |
Output is correct |
15 |
Correct |
18 ms |
84568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
84572 KB |
Output is correct |
2 |
Correct |
17 ms |
84572 KB |
Output is correct |
3 |
Correct |
18 ms |
84572 KB |
Output is correct |
4 |
Correct |
17 ms |
84572 KB |
Output is correct |
5 |
Correct |
17 ms |
84576 KB |
Output is correct |
6 |
Correct |
18 ms |
84572 KB |
Output is correct |
7 |
Correct |
17 ms |
84824 KB |
Output is correct |
8 |
Correct |
17 ms |
84496 KB |
Output is correct |
9 |
Correct |
17 ms |
84572 KB |
Output is correct |
10 |
Correct |
18 ms |
84572 KB |
Output is correct |
11 |
Correct |
18 ms |
84572 KB |
Output is correct |
12 |
Correct |
18 ms |
84572 KB |
Output is correct |
13 |
Correct |
18 ms |
84572 KB |
Output is correct |
14 |
Correct |
18 ms |
84480 KB |
Output is correct |
15 |
Correct |
18 ms |
84568 KB |
Output is correct |
16 |
Correct |
20 ms |
85224 KB |
Output is correct |
17 |
Correct |
21 ms |
85176 KB |
Output is correct |
18 |
Correct |
26 ms |
85336 KB |
Output is correct |
19 |
Correct |
22 ms |
85340 KB |
Output is correct |
20 |
Correct |
19 ms |
85024 KB |
Output is correct |
21 |
Correct |
75 ms |
83260 KB |
Output is correct |
22 |
Correct |
18 ms |
80984 KB |
Output is correct |
23 |
Correct |
20 ms |
81552 KB |
Output is correct |
24 |
Correct |
20 ms |
81260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
84572 KB |
Output is correct |
2 |
Correct |
17 ms |
84572 KB |
Output is correct |
3 |
Correct |
18 ms |
84572 KB |
Output is correct |
4 |
Correct |
17 ms |
84572 KB |
Output is correct |
5 |
Correct |
17 ms |
84576 KB |
Output is correct |
6 |
Correct |
18 ms |
84572 KB |
Output is correct |
7 |
Correct |
17 ms |
84824 KB |
Output is correct |
8 |
Correct |
17 ms |
84496 KB |
Output is correct |
9 |
Correct |
17 ms |
84572 KB |
Output is correct |
10 |
Correct |
18 ms |
84572 KB |
Output is correct |
11 |
Correct |
18 ms |
84572 KB |
Output is correct |
12 |
Correct |
18 ms |
84572 KB |
Output is correct |
13 |
Correct |
18 ms |
84572 KB |
Output is correct |
14 |
Correct |
18 ms |
84480 KB |
Output is correct |
15 |
Correct |
18 ms |
84568 KB |
Output is correct |
16 |
Correct |
20 ms |
85224 KB |
Output is correct |
17 |
Correct |
21 ms |
85176 KB |
Output is correct |
18 |
Correct |
26 ms |
85336 KB |
Output is correct |
19 |
Correct |
22 ms |
85340 KB |
Output is correct |
20 |
Correct |
19 ms |
85024 KB |
Output is correct |
21 |
Correct |
75 ms |
83260 KB |
Output is correct |
22 |
Correct |
18 ms |
80984 KB |
Output is correct |
23 |
Correct |
20 ms |
81552 KB |
Output is correct |
24 |
Correct |
20 ms |
81260 KB |
Output is correct |
25 |
Correct |
17 ms |
80476 KB |
Output is correct |
26 |
Correct |
17 ms |
80484 KB |
Output is correct |
27 |
Correct |
21 ms |
81384 KB |
Output is correct |
28 |
Correct |
21 ms |
83292 KB |
Output is correct |
29 |
Correct |
19 ms |
83044 KB |
Output is correct |
30 |
Correct |
74 ms |
81240 KB |
Output is correct |
31 |
Correct |
20 ms |
83032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
84564 KB |
Output is correct |
2 |
Correct |
17 ms |
84568 KB |
Output is correct |
3 |
Correct |
18 ms |
84520 KB |
Output is correct |
4 |
Correct |
890 ms |
172764 KB |
Output is correct |
5 |
Execution timed out |
2053 ms |
149232 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |