Submission #828976

# Submission time Handle Problem Language Result Execution time Memory
828976 2023-08-17T21:52:29 Z CSQ31 Passport (JOI23_passport) C++17
100 / 100
244 ms 16080 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 2e5+5;
int a[MAXN],b[MAXN],c[MAXN];
int dist[4][MAXN];

int mn[4*MAXN],mx[4*MAXN];
void build(int v,int l,int r){
	if(l==r){
		mn[v] = a[l];
		mx[v] = b[l];
		return;
	}
	int tm = (l+r)/2;
	build(2*v,l,tm);
	build(2*v+1,tm+1,r);
	mn[v] = min(mn[2*v],mn[2*v+1]);
	mx[v] = max(mx[2*v],mx[2*v+1]);
}
pii cur;
vector<int>add;
void getmx(int v,int l,int r,int tl,int tr){
	if(l>r)return;
	if(mx[v] < cur.se)return;
	if(tl==tr){
		if(dist[cur.fi][l] > dist[cur.fi][cur.se]+1){
			dist[cur.fi][l] = dist[cur.fi][cur.se]+1;
			add.pb(l);
		}
		mx[v] = -1;
		mn[v] = 1e9;
		return;
	}
	int tm = (tl+tr)/2;
	if(mx[2*v] >= cur.se)getmx(2*v,l,min(r,tm),tl,tm);
	if(mx[2*v+1] >= cur.se)getmx(2*v+1,max(l,tm+1),r,tm+1,tr);
	
	mn[v] = min(mn[2*v],mn[2*v+1]);
	mx[v] = max(mx[2*v],mx[2*v+1]);
}
void getmn(int v,int l,int r,int tl,int tr){
	if(l>r)return;
	if(mn[v] > cur.se)return;
	if(tl==tr){
		if(dist[cur.fi][l] > dist[cur.fi][cur.se]+1){
			dist[cur.fi][l] = dist[cur.fi][cur.se]+1;
			add.pb(l);
		}
		mx[v] = -1;
		mn[v] = 1e9;
		return;
	}
	int tm = (tl+tr)/2;
	if(mn[2*v] <= cur.se)getmn(2*v,l,min(r,tm),tl,tm);
	if(mn[2*v+1] <= cur.se)getmn(2*v+1,max(l,tm+1),r,tm+1,tr);
	mn[v] = min(mn[2*v],mn[2*v+1]);
	mx[v] = max(mx[2*v],mx[2*v+1]);
}
int main()
{
	owo
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)dist[0][i] = dist[1][i] = dist[2][i] = dist[3][i] = 1e9;
	for(int i=1;i<=n;i++){
		cin>>a[i]>>b[i];
		if(a[i]==1)c[i]++;
		if(b[i]==n)c[i]+=2;
		dist[c[i]][i] = 1;
	}	
	queue<int>q;
	build(1,1,n);
	for(int i=1;i<=n;i++){if(c[i]==1)q.push(i);}
	while(!q.empty()){
		int v = q.front();
		q.pop();
		add.clear();
		cur = {1,v};
		getmx(1,1,v,1,n);
		getmn(1,v,n,1,n);
		for(int x:add)q.push(x);	
	}
	
	
	build(1,1,n);
	for(int i=1;i<=n;i++){if(c[i]==2)q.push(i);}
	while(!q.empty()){
		int v = q.front();
		q.pop();
		add.clear();
		cur = {2,v};
		getmx(1,1,v,1,n);
		getmn(1,v,n,1,n);
		for(int x:add)q.push(x);	
	}
	/////
	priority_queue<pii,vector<pii>,greater<pii>>pq;
	for(int i=1;i<=n;i++){
		dist[3][i] = min(dist[3][i],dist[1][i]+dist[2][i]-1);
		if(dist[3][i] <= n)pq.push({dist[3][i],i});
	}
	build(1,1,n);
	while(!pq.empty()){
		int d = pq.top().fi;
		int v = pq.top().se;
		pq.pop();
		if(d!=dist[3][v])continue;
		add.clear();
		cur = {3,v};
		getmx(1,1,v,1,n);
		getmn(1,v,n,1,n);
		for(int x:add)pq.push({dist[3][x],x});	
		
	}
	int Q;
	cin>>Q;
	while(Q--){
		int i;
		cin>>i;
		if(dist[3][i]==1e9)cout<<-1<<'\n';
		else cout<<dist[3][i]<<'\n';
	}
	
	
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 205 ms 14412 KB Output is correct
5 Correct 183 ms 13808 KB Output is correct
6 Correct 210 ms 13824 KB Output is correct
7 Correct 42 ms 13608 KB Output is correct
8 Correct 27 ms 10596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 328 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 328 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 328 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 328 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 572 KB Output is correct
24 Correct 1 ms 568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 328 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 328 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 572 KB Output is correct
24 Correct 1 ms 568 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 328 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 3 ms 596 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 205 ms 14412 KB Output is correct
5 Correct 183 ms 13808 KB Output is correct
6 Correct 210 ms 13824 KB Output is correct
7 Correct 42 ms 13608 KB Output is correct
8 Correct 27 ms 10596 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 328 KB Output is correct
19 Correct 1 ms 328 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 328 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 600 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 572 KB Output is correct
32 Correct 1 ms 568 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 328 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 2 ms 592 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 244 ms 15892 KB Output is correct
41 Correct 210 ms 15796 KB Output is correct
42 Correct 174 ms 15604 KB Output is correct
43 Correct 114 ms 16080 KB Output is correct
44 Correct 222 ms 15936 KB Output is correct
45 Correct 173 ms 14360 KB Output is correct
46 Correct 76 ms 6728 KB Output is correct
47 Correct 171 ms 15960 KB Output is correct
48 Correct 68 ms 15128 KB Output is correct