Submission #1214632

#TimeUsernameProblemLanguageResultExecution timeMemory
1214632thelegendary08Passport (JOI23_passport)C++17
48 / 100
2118 ms951608 KiB
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define pout(a) cout<<a.first<<' '<<a.second<<'\n'
#define vout(v) for(auto u : v){cout<<u<<' ';} cout<<'\n'
#define dout(a) cout<<a<<' '<<#a<<'\n'
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<'\n'
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
const int mxn = 2e5 + 5;
vi label(mxn);
vector<vpii> adj(mxn * 4);
void build(int node, int l, int r){
	if(l == r){
		label[l] = node;
		return;
	}	
	int mid = (l + r) / 2;
	build(node * 2, l, mid);
	build(node * 2 + 1, mid + 1, r);
	adj[node * 2].pb(mp(node, 0));
	adj[node * 2 + 1].pb(mp(node, 0));
}

void draw_edges(int node, int l, int r, int tl, int tr, int leaf){
	if(l > r)return;
	if(l == r && tl == tr){
		adj[node].pb(mp(leaf, 1));
		return;
	}
	int tm = (tl + tr) / 2;
	draw_edges(node * 2, l, min(r, tm), tl, tm, leaf);
	draw_edges(node * 2 + 1, max(l, tm + 1), r, tm + 1, tr, leaf);
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	//ifstream cin(".in");
	//ofstream cout(".out");
	
	in(n);
	vpii v;
	f0r(i,n){
		in2(a,b);
		a--; b--;
		v.pb(mp(a,b));
	}
	
	build(1, 0, n-1);
	//draw edge i -> j for all j in [l_i, r_i]
	f0r(i,n){
		draw_edges(1, v[i].first, v[i].second, 0, n-1, label[i]);
	}
	
	vi dist(n * 4 + 5, 1e9);
	deque<int>q;
	q.push_front(label[0]);
	dist[label[0]] = 0;
	while(!q.empty()){
		int node = q.front();
		q.pop_front();
		for(auto u : adj[node]){
			if(dist[u.first] > dist[node] + u.second){
				if(u.second == 0){
					q.push_front(u.first);
					dist[u.first] = dist[node];
				}
				else{
					q.push_back(u.first);
					dist[u.first] = dist[node] + 1;
				}
			}
			
		}
	}
	// vout(dist);
	vi da(n);
	f0r(i, n){
		da[i] = dist[label[i]];
	}
	
	f0r(i, n * 4 + 5){
		dist[i] = 1e9;
	}
	vi db(n);
	q.push_front(label[n-1]);
	dist[label[n-1]] = 0;
	while(!q.empty()){
		int node = q.front();
		q.pop_front();
		for(auto u : adj[node]){
			if(dist[u.first] > dist[node] + u.second){
				if(u.second == 0){
					q.push_front(u.first);
					dist[u.first] = dist[node];
				}
				else{
					q.push_back(u.first);
					dist[u.first] = dist[node] + 1;
				}
			}
			
		}
	}
	f0r(i, n){
		db[i] = dist[label[i]];
	}
	FOR(i, 1, n){
		if(da[i] != 1e9)da[i]--;
	}
	f0r(i, n-1){
		if(db[i] != 1e9)db[i]--;
	}
	// vout(da);
	// vout(db);
	f0r(i,n){
		adj[n*4 + 4].pb(mp(label[i], da[i] + db[i]));
	}
	f0r(i, n * 4 + 5){
		dist[i] = 1e9;
	}
	dist[n * 4 + 4] = 0;
	priority_queue<pii>que;
	que.push(mp(0, n * 4 + 4));
	while(!que.empty()){
		int node = que.top().second;
		que.pop();
		for(auto u : adj[node]){
			int b = u.first; int w = u.second;
			if(dist[b] > dist[node] + w){
				dist[b] = dist[node] + w;
				que.push(mp(-dist[b], b));
			}
		}
	}
	
	in(Q);
	while(Q--){
		in(x); x--; 
		if(dist[label[x]] == 1e9){
			out(-1);
		}
		else{
			out(dist[label[x]] + 1);
		}
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...