Submission #786892

# Submission time Handle Problem Language Result Execution time Memory
786892 2023-07-18T14:16:53 Z penguin133 Passport (JOI23_passport) C++17
100 / 100
812 ms 142680 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q, L[200005], R[200005], cl[600005], cr[600005];

struct node{
	int s, e, m, val;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		val = 0;
		if(s != e)l = new node(s, m), r = new node(m+1, e);
	}
	
	void upd(int p, int v){
		if(s == e){
			val = v;
			return;
		}
		if(p <= m)l->upd(p, v);
		else r->upd(p, v);
		val = min(l->val, r->val);
	}
	
	int qry(int a, int b){
		if(s == a && b == e)return val;
		else if(b <= m)return l->qry(a, b);
		else if(a > m)return r->qry(a, b);
		else return min(l->qry(a, m), r->qry(m+1, b));
	}
}*root;

vector <pi> adj[600005];
int dist[600005];
int cnt;

struct node2{
	int s, e, m, nd;
	node2  *l, *r;
	node2(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		
		if(s != e){
			nd = ++cnt;
			l = new node2(s, m);
			r = new node2(m+1, e);
			adj[l->nd].push_back({nd, 0});
			adj[r->nd].push_back({nd, 0});
		}
		else nd = s;
	}
	
	void upd(int a, int b, int u){
		if(s == a && e == b){
			adj[nd].push_back({u, 1});
			return;
		}
		if(b <= m)l->upd(a, b, u);
		else if(a > m)r->upd(a, b, u);
		else l->upd(a, m, u), r->upd(m+1, b, u);
	}
}*root2;

void solve(){
	cin >> n;
	for(int i=1;i<=n;i++)cin >> L[i] >> R[i];
	/*
	root = new node(1, n);
	cl[1] = cr[n] = 1;
	for(int i=2;i<=n;i++){
		if(L[i] == i){
			cl[i] = 1e18;
		}
		else cl[i] = root->qry(L[i], i - 1) + 1;
		root->upd(i, cl[i]);
	}
	root = new node(1, n);
	for(int i=n-1;i>=1;i--){
		if(R[i] == i){
			cr[i] = 1e18;
		}
		else cr[i] = root->qry(i + 1, R[i]) + 1;
		root->upd(i, cr[i]);
	}
	*/
	cnt = n;
	root2 = new node2(1, n);
	for(int i=1;i<=n;i++)root2->upd(L[i], R[i], i);
	priority_queue <pi, vector <pi>, greater <pi> > pq;
	for(int i=1;i<=cnt;i++)cl[i] = 1e18;
	cl[1] = 0;
	pq.push({0, 1});
	while(!pq.empty()){
		int x = pq.top().fi, y = pq.top().se;
		pq.pop();
		if(cl[y] < x)continue;
		for(auto [i, j] : adj[y]){
			if(cl[i] > x + j)cl[i] = x + j, pq.push({cl[i], i});
		}
	}
	for(int i=1;i<=cnt;i++)cr[i] = 1e18;
	cr[n] = 0;
	pq.push({0, n});
	while(!pq.empty()){
		int x = pq.top().fi, y = pq.top().se;
		pq.pop();
		if(cr[y] < x)continue;
		for(auto [i, j] : adj[y]){
			if(cr[i] > x + j)cr[i] = x + j, pq.push({cr[i], i});
		}
	}
	for(int i=1;i<=n;i++){
		if(i == 1)dist[i] = cr[i];
		else if(i == n)dist[i] = cl[i];
		else dist[i] = cl[i] + cr[i] - 1;
		//cout << cl[i] << ' ' << cr[i] << ' ' << dist[i] << '\n';
		//if(L[i] == 1 && R[i] == n)dist[i] = 1;
		//cout << dist[i] << ' ';
		pq.push({dist[i], i});
	}
	for(int i=n+1;i<=cnt;i++)dist[i] = 1e18;
	while(!pq.empty()){
		int x = pq.top().fi, y = pq.top().se;
		pq.pop();
		if(dist[y] < x)continue;
		for(auto [i, j] : adj[y]){
			if(dist[i] > x + j)dist[i] = x + j, pq.push({dist[i], i});
		}
	}
	//for(int i=1;i<=cnt;i++)cout << dist[i] << ' ';
	//cout << '\n';
	cin >> q;
	while(q--){
		int x; cin >> x;
		if(dist[x] > 1e9)cout << -1 << '\n';
		else cout << dist[x] << '\n';
	}
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

passport.cpp:150:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  150 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14364 KB Output is correct
4 Correct 777 ms 126956 KB Output is correct
5 Correct 315 ms 86760 KB Output is correct
6 Correct 232 ms 76944 KB Output is correct
7 Correct 230 ms 69420 KB Output is correct
8 Correct 149 ms 78660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 6 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 6 ms 14400 KB Output is correct
5 Correct 7 ms 14344 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 7 ms 14368 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14408 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 7 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 6 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 6 ms 14400 KB Output is correct
5 Correct 7 ms 14344 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 7 ms 14368 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14408 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 7 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 13 ms 15496 KB Output is correct
17 Correct 9 ms 15452 KB Output is correct
18 Correct 12 ms 15784 KB Output is correct
19 Correct 10 ms 15840 KB Output is correct
20 Correct 10 ms 15316 KB Output is correct
21 Correct 10 ms 15444 KB Output is correct
22 Correct 8 ms 15136 KB Output is correct
23 Correct 10 ms 15444 KB Output is correct
24 Correct 9 ms 15572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 6 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 6 ms 14400 KB Output is correct
5 Correct 7 ms 14344 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 7 ms 14368 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14408 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14548 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 7 ms 14548 KB Output is correct
15 Correct 7 ms 14548 KB Output is correct
16 Correct 13 ms 15496 KB Output is correct
17 Correct 9 ms 15452 KB Output is correct
18 Correct 12 ms 15784 KB Output is correct
19 Correct 10 ms 15840 KB Output is correct
20 Correct 10 ms 15316 KB Output is correct
21 Correct 10 ms 15444 KB Output is correct
22 Correct 8 ms 15136 KB Output is correct
23 Correct 10 ms 15444 KB Output is correct
24 Correct 9 ms 15572 KB Output is correct
25 Correct 7 ms 14412 KB Output is correct
26 Correct 7 ms 14420 KB Output is correct
27 Correct 10 ms 15636 KB Output is correct
28 Correct 10 ms 15444 KB Output is correct
29 Correct 9 ms 15316 KB Output is correct
30 Correct 9 ms 15444 KB Output is correct
31 Correct 9 ms 15316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 7 ms 14364 KB Output is correct
4 Correct 777 ms 126956 KB Output is correct
5 Correct 315 ms 86760 KB Output is correct
6 Correct 232 ms 76944 KB Output is correct
7 Correct 230 ms 69420 KB Output is correct
8 Correct 149 ms 78660 KB Output is correct
9 Correct 9 ms 14420 KB Output is correct
10 Correct 6 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 6 ms 14400 KB Output is correct
13 Correct 7 ms 14344 KB Output is correct
14 Correct 6 ms 14420 KB Output is correct
15 Correct 7 ms 14368 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 6 ms 14420 KB Output is correct
18 Correct 7 ms 14408 KB Output is correct
19 Correct 7 ms 14548 KB Output is correct
20 Correct 7 ms 14548 KB Output is correct
21 Correct 7 ms 14548 KB Output is correct
22 Correct 7 ms 14548 KB Output is correct
23 Correct 7 ms 14548 KB Output is correct
24 Correct 13 ms 15496 KB Output is correct
25 Correct 9 ms 15452 KB Output is correct
26 Correct 12 ms 15784 KB Output is correct
27 Correct 10 ms 15840 KB Output is correct
28 Correct 10 ms 15316 KB Output is correct
29 Correct 10 ms 15444 KB Output is correct
30 Correct 8 ms 15136 KB Output is correct
31 Correct 10 ms 15444 KB Output is correct
32 Correct 9 ms 15572 KB Output is correct
33 Correct 7 ms 14412 KB Output is correct
34 Correct 7 ms 14420 KB Output is correct
35 Correct 10 ms 15636 KB Output is correct
36 Correct 10 ms 15444 KB Output is correct
37 Correct 9 ms 15316 KB Output is correct
38 Correct 9 ms 15444 KB Output is correct
39 Correct 9 ms 15316 KB Output is correct
40 Correct 812 ms 134540 KB Output is correct
41 Correct 351 ms 92472 KB Output is correct
42 Correct 429 ms 142680 KB Output is correct
43 Correct 461 ms 142660 KB Output is correct
44 Correct 247 ms 82024 KB Output is correct
45 Correct 286 ms 95956 KB Output is correct
46 Correct 139 ms 42968 KB Output is correct
47 Correct 425 ms 99212 KB Output is correct
48 Correct 435 ms 111412 KB Output is correct