Submission #918364

# Submission time Handle Problem Language Result Execution time Memory
918364 2024-01-29T17:41:36 Z denniskim Passport (JOI23_passport) C++17
100 / 100
743 ms 168324 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

ll n;
pll a[200010];
ll q;
ll X;
vector<pll> vec[1000010], yuk[1000010];
ll num[1000010];
ll dist[1000010];
ll A[1000010], B[1000010];
ll siz;
ll ans[1000010];

void init(ll no, ll s, ll e)
{
	siz = max(siz, no);
	
	if(s == e)
	{
		num[s] = no;
		return;
	}
	
	vec[no << 1].push_back({no, 0});
	vec[no << 1 | 1].push_back({no, 0});
	
	init(no << 1, s, (s + e) >> 1);
	init(no << 1 | 1, ((s + e) >> 1) + 1, e);
}

void update(ll no, ll s, ll e, ll l, ll r, ll V)
{
	if(e < l || r < s)
		return;
	
	if(l <= s && e <= r)
	{
		vec[no].push_back({V, 1});
		return;
	}
	
	update(no << 1, s, (s + e) >> 1, l, r, V);
	update(no << 1 | 1, ((s + e) >> 1) + 1, e, l, r, V);
}

void f(ll sta)
{
	for(ll i = 0 ; i <= siz ; i++)
		dist[i] = INF / (siz + 10);
	
	deque<ll> dq;
	
	dist[sta] = 0;
	dq.push_back(sta);
	
	while(!dq.empty())
	{
		ll qq = dq.front();
		dq.pop_front();
		
		for(auto &i : vec[qq])
		{
			if(dist[i.fi] > dist[qq] + i.se)
			{
				if(i.se == 0)
				{
					dist[i.fi] = dist[qq];
					dq.push_front(i.fi);
				}
				
				else
				{
					dist[i.fi] = dist[qq] + 1;
					dq.push_back(i.fi);
				}
			}
		}
	}
}

void f2(ll sta)
{
	priority_queue<pll> pq;
	
	for(ll i = 1 ; i <= siz ; i++)
		dist[i] = INF;
	
	dist[sta] = 0;
	pq.push({0, sta});
	
	while(!pq.empty())
	{
		pll qq = pq.top();
		pq.pop();
		
		for(auto &i : vec[qq.se])
		{
			if(dist[i.fi] > dist[qq.se] + i.se)
			{
				dist[i.fi] = dist[qq.se] + i.se;
				pq.push({-dist[i.fi], i.fi});
			}
		}
	}
}

int main(void)
{
	fastio
	
	cin >> n;
	
	for(ll i = 1 ; i <= n ; i++)
		cin >> a[i].fi >> a[i].se;
	
	init(1, 1, n);
	
	for(ll i = 1 ; i <= n ; i++)
		update(1, 1, n, a[i].fi, a[i].se, num[i]);
	
	f(num[1]);
	
	for(ll i = 1 ; i <= siz ; i++)
		A[i] = dist[i];
	
	f(num[n]);
	
	for(ll i = 1 ; i <= siz ; i++)
		B[i] = dist[i];
	
	for(ll i = 1 ; i <= n ; i++)
		vec[siz + 1].push_back({num[i], max(A[num[i]] - 1, 0LL) + max(B[num[i]] - 1, 0LL) + 1});
	
	siz++;
	
	f2(siz);
	
	for(ll i = 1 ; i <= siz ; i++)
		ans[i] = dist[i];
	
	cin >> q;
	
	while(q--)
	{
		cin >> X;
		
		if(ans[num[X]] >= INF / (siz + 10))
			cout << -1 en;
		else
			cout << ans[num[X]] en;
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 55644 KB Output is correct
2 Correct 16 ms 55644 KB Output is correct
3 Correct 15 ms 55644 KB Output is correct
4 Correct 672 ms 154748 KB Output is correct
5 Correct 288 ms 115900 KB Output is correct
6 Correct 209 ms 103688 KB Output is correct
7 Correct 130 ms 100676 KB Output is correct
8 Correct 148 ms 116800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 55852 KB Output is correct
2 Correct 16 ms 55640 KB Output is correct
3 Correct 16 ms 55644 KB Output is correct
4 Correct 14 ms 55644 KB Output is correct
5 Correct 15 ms 55852 KB Output is correct
6 Correct 15 ms 55644 KB Output is correct
7 Correct 16 ms 55788 KB Output is correct
8 Correct 15 ms 55864 KB Output is correct
9 Correct 15 ms 55740 KB Output is correct
10 Correct 17 ms 55640 KB Output is correct
11 Correct 14 ms 55900 KB Output is correct
12 Correct 15 ms 55900 KB Output is correct
13 Correct 14 ms 55900 KB Output is correct
14 Correct 15 ms 56148 KB Output is correct
15 Correct 14 ms 55900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 55852 KB Output is correct
2 Correct 16 ms 55640 KB Output is correct
3 Correct 16 ms 55644 KB Output is correct
4 Correct 14 ms 55644 KB Output is correct
5 Correct 15 ms 55852 KB Output is correct
6 Correct 15 ms 55644 KB Output is correct
7 Correct 16 ms 55788 KB Output is correct
8 Correct 15 ms 55864 KB Output is correct
9 Correct 15 ms 55740 KB Output is correct
10 Correct 17 ms 55640 KB Output is correct
11 Correct 14 ms 55900 KB Output is correct
12 Correct 15 ms 55900 KB Output is correct
13 Correct 14 ms 55900 KB Output is correct
14 Correct 15 ms 56148 KB Output is correct
15 Correct 14 ms 55900 KB Output is correct
16 Correct 18 ms 56668 KB Output is correct
17 Correct 18 ms 56412 KB Output is correct
18 Correct 19 ms 56836 KB Output is correct
19 Correct 18 ms 56892 KB Output is correct
20 Correct 16 ms 56412 KB Output is correct
21 Correct 17 ms 56412 KB Output is correct
22 Correct 16 ms 56156 KB Output is correct
23 Correct 17 ms 56408 KB Output is correct
24 Correct 17 ms 56668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 55852 KB Output is correct
2 Correct 16 ms 55640 KB Output is correct
3 Correct 16 ms 55644 KB Output is correct
4 Correct 14 ms 55644 KB Output is correct
5 Correct 15 ms 55852 KB Output is correct
6 Correct 15 ms 55644 KB Output is correct
7 Correct 16 ms 55788 KB Output is correct
8 Correct 15 ms 55864 KB Output is correct
9 Correct 15 ms 55740 KB Output is correct
10 Correct 17 ms 55640 KB Output is correct
11 Correct 14 ms 55900 KB Output is correct
12 Correct 15 ms 55900 KB Output is correct
13 Correct 14 ms 55900 KB Output is correct
14 Correct 15 ms 56148 KB Output is correct
15 Correct 14 ms 55900 KB Output is correct
16 Correct 18 ms 56668 KB Output is correct
17 Correct 18 ms 56412 KB Output is correct
18 Correct 19 ms 56836 KB Output is correct
19 Correct 18 ms 56892 KB Output is correct
20 Correct 16 ms 56412 KB Output is correct
21 Correct 17 ms 56412 KB Output is correct
22 Correct 16 ms 56156 KB Output is correct
23 Correct 17 ms 56408 KB Output is correct
24 Correct 17 ms 56668 KB Output is correct
25 Correct 15 ms 55640 KB Output is correct
26 Correct 14 ms 55644 KB Output is correct
27 Correct 18 ms 56792 KB Output is correct
28 Correct 17 ms 56412 KB Output is correct
29 Correct 17 ms 56408 KB Output is correct
30 Correct 20 ms 56412 KB Output is correct
31 Correct 16 ms 56412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 55644 KB Output is correct
2 Correct 16 ms 55644 KB Output is correct
3 Correct 15 ms 55644 KB Output is correct
4 Correct 672 ms 154748 KB Output is correct
5 Correct 288 ms 115900 KB Output is correct
6 Correct 209 ms 103688 KB Output is correct
7 Correct 130 ms 100676 KB Output is correct
8 Correct 148 ms 116800 KB Output is correct
9 Correct 15 ms 55852 KB Output is correct
10 Correct 16 ms 55640 KB Output is correct
11 Correct 16 ms 55644 KB Output is correct
12 Correct 14 ms 55644 KB Output is correct
13 Correct 15 ms 55852 KB Output is correct
14 Correct 15 ms 55644 KB Output is correct
15 Correct 16 ms 55788 KB Output is correct
16 Correct 15 ms 55864 KB Output is correct
17 Correct 15 ms 55740 KB Output is correct
18 Correct 17 ms 55640 KB Output is correct
19 Correct 14 ms 55900 KB Output is correct
20 Correct 15 ms 55900 KB Output is correct
21 Correct 14 ms 55900 KB Output is correct
22 Correct 15 ms 56148 KB Output is correct
23 Correct 14 ms 55900 KB Output is correct
24 Correct 18 ms 56668 KB Output is correct
25 Correct 18 ms 56412 KB Output is correct
26 Correct 19 ms 56836 KB Output is correct
27 Correct 18 ms 56892 KB Output is correct
28 Correct 16 ms 56412 KB Output is correct
29 Correct 17 ms 56412 KB Output is correct
30 Correct 16 ms 56156 KB Output is correct
31 Correct 17 ms 56408 KB Output is correct
32 Correct 17 ms 56668 KB Output is correct
33 Correct 15 ms 55640 KB Output is correct
34 Correct 14 ms 55644 KB Output is correct
35 Correct 18 ms 56792 KB Output is correct
36 Correct 17 ms 56412 KB Output is correct
37 Correct 17 ms 56408 KB Output is correct
38 Correct 20 ms 56412 KB Output is correct
39 Correct 16 ms 56412 KB Output is correct
40 Correct 743 ms 160252 KB Output is correct
41 Correct 318 ms 117224 KB Output is correct
42 Correct 455 ms 168324 KB Output is correct
43 Correct 418 ms 168064 KB Output is correct
44 Correct 234 ms 105408 KB Output is correct
45 Correct 313 ms 118840 KB Output is correct
46 Correct 128 ms 82724 KB Output is correct
47 Correct 393 ms 125072 KB Output is correct
48 Correct 368 ms 135596 KB Output is correct