답안 #1098656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098656 2024-10-09T16:22:42 Z flyingkite Passport (JOI23_passport) C++17
100 / 100
1855 ms 361856 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();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 141140 KB Output is correct
2 Correct 90 ms 141288 KB Output is correct
3 Correct 56 ms 141140 KB Output is correct
4 Correct 1785 ms 347832 KB Output is correct
5 Correct 985 ms 306324 KB Output is correct
6 Correct 797 ms 298132 KB Output is correct
7 Correct 934 ms 283688 KB Output is correct
8 Correct 736 ms 295036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 141136 KB Output is correct
2 Correct 85 ms 141232 KB Output is correct
3 Correct 76 ms 141136 KB Output is correct
4 Correct 78 ms 141176 KB Output is correct
5 Correct 65 ms 141132 KB Output is correct
6 Correct 60 ms 141136 KB Output is correct
7 Correct 61 ms 141356 KB Output is correct
8 Correct 65 ms 141140 KB Output is correct
9 Correct 61 ms 141140 KB Output is correct
10 Correct 66 ms 141140 KB Output is correct
11 Correct 79 ms 141648 KB Output is correct
12 Correct 88 ms 141652 KB Output is correct
13 Correct 55 ms 141648 KB Output is correct
14 Correct 63 ms 141652 KB Output is correct
15 Correct 76 ms 141644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 141136 KB Output is correct
2 Correct 85 ms 141232 KB Output is correct
3 Correct 76 ms 141136 KB Output is correct
4 Correct 78 ms 141176 KB Output is correct
5 Correct 65 ms 141132 KB Output is correct
6 Correct 60 ms 141136 KB Output is correct
7 Correct 61 ms 141356 KB Output is correct
8 Correct 65 ms 141140 KB Output is correct
9 Correct 61 ms 141140 KB Output is correct
10 Correct 66 ms 141140 KB Output is correct
11 Correct 79 ms 141648 KB Output is correct
12 Correct 88 ms 141652 KB Output is correct
13 Correct 55 ms 141648 KB Output is correct
14 Correct 63 ms 141652 KB Output is correct
15 Correct 76 ms 141644 KB Output is correct
16 Correct 79 ms 143560 KB Output is correct
17 Correct 74 ms 143568 KB Output is correct
18 Correct 93 ms 143824 KB Output is correct
19 Correct 77 ms 143900 KB Output is correct
20 Correct 67 ms 143576 KB Output is correct
21 Correct 88 ms 143564 KB Output is correct
22 Correct 68 ms 143396 KB Output is correct
23 Correct 75 ms 143676 KB Output is correct
24 Correct 65 ms 143568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 141136 KB Output is correct
2 Correct 85 ms 141232 KB Output is correct
3 Correct 76 ms 141136 KB Output is correct
4 Correct 78 ms 141176 KB Output is correct
5 Correct 65 ms 141132 KB Output is correct
6 Correct 60 ms 141136 KB Output is correct
7 Correct 61 ms 141356 KB Output is correct
8 Correct 65 ms 141140 KB Output is correct
9 Correct 61 ms 141140 KB Output is correct
10 Correct 66 ms 141140 KB Output is correct
11 Correct 79 ms 141648 KB Output is correct
12 Correct 88 ms 141652 KB Output is correct
13 Correct 55 ms 141648 KB Output is correct
14 Correct 63 ms 141652 KB Output is correct
15 Correct 76 ms 141644 KB Output is correct
16 Correct 79 ms 143560 KB Output is correct
17 Correct 74 ms 143568 KB Output is correct
18 Correct 93 ms 143824 KB Output is correct
19 Correct 77 ms 143900 KB Output is correct
20 Correct 67 ms 143576 KB Output is correct
21 Correct 88 ms 143564 KB Output is correct
22 Correct 68 ms 143396 KB Output is correct
23 Correct 75 ms 143676 KB Output is correct
24 Correct 65 ms 143568 KB Output is correct
25 Correct 63 ms 141180 KB Output is correct
26 Correct 61 ms 141176 KB Output is correct
27 Correct 68 ms 143616 KB Output is correct
28 Correct 66 ms 143568 KB Output is correct
29 Correct 64 ms 143572 KB Output is correct
30 Correct 78 ms 143568 KB Output is correct
31 Correct 76 ms 143584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 141140 KB Output is correct
2 Correct 90 ms 141288 KB Output is correct
3 Correct 56 ms 141140 KB Output is correct
4 Correct 1785 ms 347832 KB Output is correct
5 Correct 985 ms 306324 KB Output is correct
6 Correct 797 ms 298132 KB Output is correct
7 Correct 934 ms 283688 KB Output is correct
8 Correct 736 ms 295036 KB Output is correct
9 Correct 74 ms 141136 KB Output is correct
10 Correct 85 ms 141232 KB Output is correct
11 Correct 76 ms 141136 KB Output is correct
12 Correct 78 ms 141176 KB Output is correct
13 Correct 65 ms 141132 KB Output is correct
14 Correct 60 ms 141136 KB Output is correct
15 Correct 61 ms 141356 KB Output is correct
16 Correct 65 ms 141140 KB Output is correct
17 Correct 61 ms 141140 KB Output is correct
18 Correct 66 ms 141140 KB Output is correct
19 Correct 79 ms 141648 KB Output is correct
20 Correct 88 ms 141652 KB Output is correct
21 Correct 55 ms 141648 KB Output is correct
22 Correct 63 ms 141652 KB Output is correct
23 Correct 76 ms 141644 KB Output is correct
24 Correct 79 ms 143560 KB Output is correct
25 Correct 74 ms 143568 KB Output is correct
26 Correct 93 ms 143824 KB Output is correct
27 Correct 77 ms 143900 KB Output is correct
28 Correct 67 ms 143576 KB Output is correct
29 Correct 88 ms 143564 KB Output is correct
30 Correct 68 ms 143396 KB Output is correct
31 Correct 75 ms 143676 KB Output is correct
32 Correct 65 ms 143568 KB Output is correct
33 Correct 63 ms 141180 KB Output is correct
34 Correct 61 ms 141176 KB Output is correct
35 Correct 68 ms 143616 KB Output is correct
36 Correct 66 ms 143568 KB Output is correct
37 Correct 64 ms 143572 KB Output is correct
38 Correct 78 ms 143568 KB Output is correct
39 Correct 76 ms 143584 KB Output is correct
40 Correct 1855 ms 349500 KB Output is correct
41 Correct 1085 ms 308096 KB Output is correct
42 Correct 1220 ms 361404 KB Output is correct
43 Correct 1321 ms 361856 KB Output is correct
44 Correct 790 ms 298128 KB Output is correct
45 Correct 1002 ms 312084 KB Output is correct
46 Correct 495 ms 207848 KB Output is correct
47 Correct 1367 ms 314588 KB Output is correct
48 Correct 1227 ms 329396 KB Output is correct