Submission #763251

# Submission time Handle Problem Language Result Execution time Memory
763251 2023-06-22T07:13:23 Z sentheta Passport (JOI23_passport) C++17
100 / 100
435 ms 40780 KB
// author : sentheta aka vanwij
#ifdef VANWIJ
	#include"/home/user/code/bits/stdc++.h"
#else
	#include"bits/stdc++.h"
#endif
using namespace std;

#define Int long long
#define V vector
#define pii pair<int,int>
#define ff first
#define ss second

static mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pow2(x) (1LL<<(x))
#define msb(x) (63-__builtin_clzll(x))
#define bitcnt(x) (__builtin_popcountll(x))

#define nl '\n'
#define _ << ' ' <<
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define dbg(x) if(1) cout << "?" << #x << " : " << (x) << endl << flush;

// #define int long long
// const Int MOD = 1e9+7;
const Int MOD = 998244353;
Int bpow(Int a,Int b){
	Int ret = 1;
	for(;b; a=a*a%MOD,b/=2) if(b&1) ret = ret*a%MOD;
	return ret;
}
Int inv(Int a){return bpow(a,MOD-2);}

void solve(); int TC, ALLTC;
signed main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(7);
	// cin >> ALLTC; for(TC=1; TC<=ALLTC; TC++) solve(); return 0;
	solve();
}












const int INF = 1e9;
const int N = 2e5+5;

int n;
int a[N], b[N];

int to1[N], ton[N];
int ans[N];

bool vis[N];

// {-dist, node}
priority_queue<pii> pq;

// segtree
#define lc (v+1)
#define rc (v+2*(mid-tl+1))
#define mid (tl+tr)/2
V<int> segs[2*N];
bool stvis[2*N];
void insert(int i,int v=0,int tl=1,int tr=n){
	if(b[i]<tl || tr<a[i]) return;
	if(a[i]<=tl && tr<=b[i]){
		// cout << tl _ tr << nl;
		segs[v].push_back(i); return;
	}
	insert(i, lc,tl,mid);
	insert(i, rc,mid+1,tr);
}

void dijk(int *d){
	rep(v,0,2*N) stvis[v] = 0;

	rep(i,1,n+1){
		vis[i] = 0;
		if(d[i] <= n){
			pq.push({-d[i], i});
		}
	}

	while(!pq.empty()){
		auto[dist,x] = pq.top(); pq.pop();
		if(vis[x]) continue;
		vis[x] = 1;
		dist *= -1;
		// if(d==ans){dbg(x); dbg(dist);}

		int v=0,tl=1,tr=n;
		while(1){
			assert(tl<=x && x<=tr);
			if(!stvis[v]){
				stvis[v] = 1;

				for(int y : segs[v]) if(d[y] > d[x]+1){
					d[y] = d[x]+1;
					pq.push({-d[y], y});
				}
			}

			if(tl==tr) break;
			if(x<=mid){
				v=lc; tr=mid;
			}
			else{
				v=rc; tl=mid+1;
			}
		}
	}
}

void solve(){
	
	cin >> n;

	rep(i,1,n+1){
		cin >> a[i] >> b[i];

		to1[i] = ton[i] = ans[i] = INF;
		// dbg(i);
		insert(i);
	}
	// cout << nl;

	// find distance to 1
	to1[1] = 0;
	dijk(to1);
	// rep(i,1,n+1) cout << to1[i] << " ";
	// cout << nl;

	// find distance to n
	ton[n] = 0;
	dijk(ton);
	// rep(i,1,n+1) cout << ton[i] << " ";
	// cout << nl;

	// find optimal path
	rep(i,1,n+1){
		ans[i] = to1[i] + ton[i];

		// double counted your inital passport
		if(1<i && i<n) ans[i]--;

		if(a[i]==1 && b[i]==n) ans[i] = 1;
	}
	dijk(ans);
	

	int q; cin >> q;
	while(q--){
		int i; cin >> i;
		int out = ans[i];
		// if(1<i && i<n && out>1) out--;
		if(out>n) out = -1;
		cout << out << nl;
	}
	
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10080 KB Output is correct
2 Correct 5 ms 10112 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 402 ms 38956 KB Output is correct
5 Correct 202 ms 29272 KB Output is correct
6 Correct 135 ms 29256 KB Output is correct
7 Correct 119 ms 18724 KB Output is correct
8 Correct 76 ms 21596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10068 KB Output is correct
2 Correct 6 ms 10116 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 5 ms 10068 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 5 ms 10068 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10108 KB Output is correct
10 Correct 5 ms 10068 KB Output is correct
11 Correct 5 ms 10108 KB Output is correct
12 Correct 5 ms 10120 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 5 ms 10196 KB Output is correct
15 Correct 5 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10068 KB Output is correct
2 Correct 6 ms 10116 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 5 ms 10068 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 5 ms 10068 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10108 KB Output is correct
10 Correct 5 ms 10068 KB Output is correct
11 Correct 5 ms 10108 KB Output is correct
12 Correct 5 ms 10120 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 5 ms 10196 KB Output is correct
15 Correct 5 ms 10112 KB Output is correct
16 Correct 7 ms 10324 KB Output is correct
17 Correct 7 ms 10400 KB Output is correct
18 Correct 8 ms 10400 KB Output is correct
19 Correct 7 ms 10376 KB Output is correct
20 Correct 7 ms 10324 KB Output is correct
21 Correct 8 ms 10252 KB Output is correct
22 Correct 6 ms 10196 KB Output is correct
23 Correct 8 ms 10392 KB Output is correct
24 Correct 7 ms 10324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10068 KB Output is correct
2 Correct 6 ms 10116 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 5 ms 10068 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 5 ms 10068 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10108 KB Output is correct
10 Correct 5 ms 10068 KB Output is correct
11 Correct 5 ms 10108 KB Output is correct
12 Correct 5 ms 10120 KB Output is correct
13 Correct 6 ms 10068 KB Output is correct
14 Correct 5 ms 10196 KB Output is correct
15 Correct 5 ms 10112 KB Output is correct
16 Correct 7 ms 10324 KB Output is correct
17 Correct 7 ms 10400 KB Output is correct
18 Correct 8 ms 10400 KB Output is correct
19 Correct 7 ms 10376 KB Output is correct
20 Correct 7 ms 10324 KB Output is correct
21 Correct 8 ms 10252 KB Output is correct
22 Correct 6 ms 10196 KB Output is correct
23 Correct 8 ms 10392 KB Output is correct
24 Correct 7 ms 10324 KB Output is correct
25 Correct 6 ms 10068 KB Output is correct
26 Correct 5 ms 10068 KB Output is correct
27 Correct 8 ms 10388 KB Output is correct
28 Correct 7 ms 10324 KB Output is correct
29 Correct 7 ms 10384 KB Output is correct
30 Correct 7 ms 10324 KB Output is correct
31 Correct 10 ms 10384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10080 KB Output is correct
2 Correct 5 ms 10112 KB Output is correct
3 Correct 5 ms 10068 KB Output is correct
4 Correct 402 ms 38956 KB Output is correct
5 Correct 202 ms 29272 KB Output is correct
6 Correct 135 ms 29256 KB Output is correct
7 Correct 119 ms 18724 KB Output is correct
8 Correct 76 ms 21596 KB Output is correct
9 Correct 5 ms 10068 KB Output is correct
10 Correct 6 ms 10116 KB Output is correct
11 Correct 5 ms 10068 KB Output is correct
12 Correct 5 ms 10068 KB Output is correct
13 Correct 5 ms 10068 KB Output is correct
14 Correct 5 ms 10068 KB Output is correct
15 Correct 6 ms 10068 KB Output is correct
16 Correct 6 ms 10068 KB Output is correct
17 Correct 6 ms 10108 KB Output is correct
18 Correct 5 ms 10068 KB Output is correct
19 Correct 5 ms 10108 KB Output is correct
20 Correct 5 ms 10120 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 5 ms 10196 KB Output is correct
23 Correct 5 ms 10112 KB Output is correct
24 Correct 7 ms 10324 KB Output is correct
25 Correct 7 ms 10400 KB Output is correct
26 Correct 8 ms 10400 KB Output is correct
27 Correct 7 ms 10376 KB Output is correct
28 Correct 7 ms 10324 KB Output is correct
29 Correct 8 ms 10252 KB Output is correct
30 Correct 6 ms 10196 KB Output is correct
31 Correct 8 ms 10392 KB Output is correct
32 Correct 7 ms 10324 KB Output is correct
33 Correct 6 ms 10068 KB Output is correct
34 Correct 5 ms 10068 KB Output is correct
35 Correct 8 ms 10388 KB Output is correct
36 Correct 7 ms 10324 KB Output is correct
37 Correct 7 ms 10384 KB Output is correct
38 Correct 7 ms 10324 KB Output is correct
39 Correct 10 ms 10384 KB Output is correct
40 Correct 435 ms 40496 KB Output is correct
41 Correct 235 ms 31372 KB Output is correct
42 Correct 295 ms 40780 KB Output is correct
43 Correct 302 ms 40424 KB Output is correct
44 Correct 173 ms 31420 KB Output is correct
45 Correct 164 ms 30984 KB Output is correct
46 Correct 83 ms 17628 KB Output is correct
47 Correct 298 ms 32296 KB Output is correct
48 Correct 190 ms 31436 KB Output is correct