Submission #1086597

# Submission time Handle Problem Language Result Execution time Memory
1086597 2024-09-11T05:27:45 Z coldbr3w Passport (JOI23_passport) C++17
100 / 100
1608 ms 361944 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<long long, long long>
#define pb push_back
#define F first
#define S second  
#define all(x) (x).begin(), (x).end()

const ll N = 6e5 + 100;
const ll inf = 1e17;
const ll mod = 1e9 + 7;
const ll block = 350;
vector<ll>adj[5 * N], rev[5 * N];
ll d[5 * N][2];
ll dp[5 * N];
ll n,q;
struct cmp{
    bool operator()(const pll &a, const pll &b){
        return a.S > b.S;
    }
};
void dijk(ll s, ll typ){
    priority_queue<pll, vector<pll>, cmp>q;
    for(int i = 1; i <= 9 * n;i++) d[i][typ] = inf;
    d[s][typ] = 0;
    q.push({s, d[s][typ]});
    while(q.size()){
        ll u = q.top().F, c = q.top().S; q.pop();
        if(c != d[u][typ]) continue;
        for(auto v : rev[u]){
            if(d[u][typ] + (v >= 8 * n + 1) < d[v][typ]){
                d[v][typ] = d[u][typ] + (v >= 8 * n + 1);
                q.push({v, d[v][typ]});
            }
        }
    }
}
void finalize(){
    priority_queue<pll, vector<pll>, cmp>q;
    for(int i = 1; i <= 9 * n;i++) dp[i] = d[i][0] + d[i][1] - (i > 8 * n + 1 && i < 9 * n), q.push({i, dp[i]});
    while(q.size()){
        ll u = q.top().F, c = q.top().S; q.pop();
        if(c != dp[u]) continue;
        for(auto v : rev[u]){
            if(dp[u] + (v >= 8 * n + 1) < dp[v]){
                dp[v] = dp[u] + (v >= 8 * n + 1);
                q.push({v, dp[v]});
            }
        }
    }
}
void build1(ll id, ll l, ll r){
    if(l == r){
        adj[id].pb(8 * n + l);
        adj[8 * n + l].pb(id);
		rev[id].pb(8 * n + l);
    	rev[8 * n + l].pb(id);
        return;
    }
    ll mid = (l + r) / 2;
    build1(2 * id, l, mid); build1(2 * id + 1, mid + 1, r);
    adj[id].pb(2 * id); adj[id].pb(2 * id + 1);
	rev[2 * id].pb(id); rev[2 * id + 1].pb(id);
}
void build2(ll id, ll l, ll r){
    if(l == r) {
        adj[4 * n + id].pb(8 * n + l);
        adj[8 * n + l].pb(4 * n + id);
        rev[8 * n + l].pb(4 * n + id);
        rev[4 * n + id].pb(8 * n + l);
        return;
    }
    ll mid = (l + r) / 2;
    build2(2 * id, l, mid); build2(2 * id + 1, mid + 1, r);
    adj[4 * n + 2 * id].pb(4 * n + id);
    rev[4 * n + id].pb(4 * n + 2 * id);
    adj[4 * n + 2 * id + 1].pb(4 * n + id);
    rev[4 * n + id].pb(4 * n + 2 * id + 1);
}
void add_edge2(ll id, ll l, ll r, ll left, ll right, ll v){ 
    if(l > right || r < left) return;
    if(left <= l && r <= right){
        adj[8 * n + v].pb(id);
        rev[id].pb(8 * n + v);
        return;
    }
    ll mid = (l + r) / 2;
    add_edge2(2 * id, l, mid, left, right, v);
    add_edge2(2 * id + 1, mid + 1, r, left, right, v);
}
void to_thic_cau(){
	cin >> n;
	build1(1,1,n); build2(1,1,n);
	for(int i = 1; i <= n;i++){
		ll l,r; cin >> l >> r;
		add_edge2(1,1,n,l,r,i);
	}
	dijk(8 * n + 1, 0); dijk(9 * n, 1); finalize();
    cin >> q;
    while(q--){
        ll x; cin >> x;
        cout << (dp[8 * n + x] >= inf ? -1 : dp[8 * n + x]) << "\n";
    }
}

signed main()   
{ 
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll tc = 1;
	//cin >> tc;
	while(tc--) to_thic_cau();
}
# Verdict Execution time Memory Grader output
1 Correct 57 ms 141140 KB Output is correct
2 Correct 59 ms 141136 KB Output is correct
3 Correct 59 ms 141272 KB Output is correct
4 Correct 1577 ms 347984 KB Output is correct
5 Correct 999 ms 306336 KB Output is correct
6 Correct 799 ms 298148 KB Output is correct
7 Correct 769 ms 283712 KB Output is correct
8 Correct 641 ms 294988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 141136 KB Output is correct
2 Correct 58 ms 141136 KB Output is correct
3 Correct 66 ms 141136 KB Output is correct
4 Correct 60 ms 141296 KB Output is correct
5 Correct 58 ms 141136 KB Output is correct
6 Correct 60 ms 141140 KB Output is correct
7 Correct 59 ms 141136 KB Output is correct
8 Correct 69 ms 141396 KB Output is correct
9 Correct 57 ms 141140 KB Output is correct
10 Correct 60 ms 141136 KB Output is correct
11 Correct 60 ms 141648 KB Output is correct
12 Correct 57 ms 141652 KB Output is correct
13 Correct 57 ms 141648 KB Output is correct
14 Correct 58 ms 141660 KB Output is correct
15 Correct 57 ms 141656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 141136 KB Output is correct
2 Correct 58 ms 141136 KB Output is correct
3 Correct 66 ms 141136 KB Output is correct
4 Correct 60 ms 141296 KB Output is correct
5 Correct 58 ms 141136 KB Output is correct
6 Correct 60 ms 141140 KB Output is correct
7 Correct 59 ms 141136 KB Output is correct
8 Correct 69 ms 141396 KB Output is correct
9 Correct 57 ms 141140 KB Output is correct
10 Correct 60 ms 141136 KB Output is correct
11 Correct 60 ms 141648 KB Output is correct
12 Correct 57 ms 141652 KB Output is correct
13 Correct 57 ms 141648 KB Output is correct
14 Correct 58 ms 141660 KB Output is correct
15 Correct 57 ms 141656 KB Output is correct
16 Correct 81 ms 143684 KB Output is correct
17 Correct 62 ms 143564 KB Output is correct
18 Correct 68 ms 143824 KB Output is correct
19 Correct 70 ms 143824 KB Output is correct
20 Correct 66 ms 143560 KB Output is correct
21 Correct 66 ms 143440 KB Output is correct
22 Correct 62 ms 143352 KB Output is correct
23 Correct 66 ms 143576 KB Output is correct
24 Correct 67 ms 143564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 141136 KB Output is correct
2 Correct 58 ms 141136 KB Output is correct
3 Correct 66 ms 141136 KB Output is correct
4 Correct 60 ms 141296 KB Output is correct
5 Correct 58 ms 141136 KB Output is correct
6 Correct 60 ms 141140 KB Output is correct
7 Correct 59 ms 141136 KB Output is correct
8 Correct 69 ms 141396 KB Output is correct
9 Correct 57 ms 141140 KB Output is correct
10 Correct 60 ms 141136 KB Output is correct
11 Correct 60 ms 141648 KB Output is correct
12 Correct 57 ms 141652 KB Output is correct
13 Correct 57 ms 141648 KB Output is correct
14 Correct 58 ms 141660 KB Output is correct
15 Correct 57 ms 141656 KB Output is correct
16 Correct 81 ms 143684 KB Output is correct
17 Correct 62 ms 143564 KB Output is correct
18 Correct 68 ms 143824 KB Output is correct
19 Correct 70 ms 143824 KB Output is correct
20 Correct 66 ms 143560 KB Output is correct
21 Correct 66 ms 143440 KB Output is correct
22 Correct 62 ms 143352 KB Output is correct
23 Correct 66 ms 143576 KB Output is correct
24 Correct 67 ms 143564 KB Output is correct
25 Correct 57 ms 141136 KB Output is correct
26 Correct 60 ms 141140 KB Output is correct
27 Correct 66 ms 143736 KB Output is correct
28 Correct 67 ms 143672 KB Output is correct
29 Correct 68 ms 143572 KB Output is correct
30 Correct 73 ms 143652 KB Output is correct
31 Correct 65 ms 143568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 141140 KB Output is correct
2 Correct 59 ms 141136 KB Output is correct
3 Correct 59 ms 141272 KB Output is correct
4 Correct 1577 ms 347984 KB Output is correct
5 Correct 999 ms 306336 KB Output is correct
6 Correct 799 ms 298148 KB Output is correct
7 Correct 769 ms 283712 KB Output is correct
8 Correct 641 ms 294988 KB Output is correct
9 Correct 58 ms 141136 KB Output is correct
10 Correct 58 ms 141136 KB Output is correct
11 Correct 66 ms 141136 KB Output is correct
12 Correct 60 ms 141296 KB Output is correct
13 Correct 58 ms 141136 KB Output is correct
14 Correct 60 ms 141140 KB Output is correct
15 Correct 59 ms 141136 KB Output is correct
16 Correct 69 ms 141396 KB Output is correct
17 Correct 57 ms 141140 KB Output is correct
18 Correct 60 ms 141136 KB Output is correct
19 Correct 60 ms 141648 KB Output is correct
20 Correct 57 ms 141652 KB Output is correct
21 Correct 57 ms 141648 KB Output is correct
22 Correct 58 ms 141660 KB Output is correct
23 Correct 57 ms 141656 KB Output is correct
24 Correct 81 ms 143684 KB Output is correct
25 Correct 62 ms 143564 KB Output is correct
26 Correct 68 ms 143824 KB Output is correct
27 Correct 70 ms 143824 KB Output is correct
28 Correct 66 ms 143560 KB Output is correct
29 Correct 66 ms 143440 KB Output is correct
30 Correct 62 ms 143352 KB Output is correct
31 Correct 66 ms 143576 KB Output is correct
32 Correct 67 ms 143564 KB Output is correct
33 Correct 57 ms 141136 KB Output is correct
34 Correct 60 ms 141140 KB Output is correct
35 Correct 66 ms 143736 KB Output is correct
36 Correct 67 ms 143672 KB Output is correct
37 Correct 68 ms 143572 KB Output is correct
38 Correct 73 ms 143652 KB Output is correct
39 Correct 65 ms 143568 KB Output is correct
40 Correct 1608 ms 349496 KB Output is correct
41 Correct 958 ms 307876 KB Output is correct
42 Correct 1191 ms 361480 KB Output is correct
43 Correct 1202 ms 361944 KB Output is correct
44 Correct 781 ms 298128 KB Output is correct
45 Correct 968 ms 312168 KB Output is correct
46 Correct 420 ms 207964 KB Output is correct
47 Correct 1344 ms 314764 KB Output is correct
48 Correct 946 ms 329256 KB Output is correct