#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];
vector<int>adj[MAXN];
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;
for(int j=a[i];j<=b[i];j++)adj[j].pb(i);
}
queue<int>q;
for(int i=1;i<=n;i++){
if(c[i]==1)q.push(i);
}
while(!q.empty()){
int v = q.front();
q.pop();
for(int x:adj[v]){
if(dist[1][x] > dist[1][v] + 1){
dist[1][x] = dist[1][v] + 1;
q.push(x);
}
}
}
for(int i=1;i<=n;i++){
if(c[i]==2)q.push(i);
}
while(!q.empty()){
int v = q.front();
q.pop();
for(int x:adj[v]){
if(dist[2][x] > dist[2][v] + 1){
dist[2][x] = dist[2][v] + 1;
q.push(x);
}
}
}
for(int i=1;i<=n;i++){
if(c[i]==3)q.push(i);
else if(dist[1][i]+dist[2][i] < 1e9){
dist[3][i] = dist[1][i] + dist[2][i] - 1;
q.push(i);
}
}
while(!q.empty()){
int v = q.front();
q.pop();
for(int x:adj[v]){
if(dist[3][x] > dist[3][v] + 1){
dist[3][x] = dist[3][v] + 1;
q.push(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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5048 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Execution timed out |
2102 ms |
962984 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5292 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5292 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
13 ms |
13380 KB |
Output is correct |
17 |
Correct |
4 ms |
5292 KB |
Output is correct |
18 |
Correct |
43 ms |
22232 KB |
Output is correct |
19 |
Correct |
38 ms |
21204 KB |
Output is correct |
20 |
Correct |
3 ms |
5172 KB |
Output is correct |
21 |
Correct |
115 ms |
7508 KB |
Output is correct |
22 |
Correct |
22 ms |
25300 KB |
Output is correct |
23 |
Correct |
25 ms |
19412 KB |
Output is correct |
24 |
Correct |
23 ms |
20044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
5028 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5160 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5292 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
13 ms |
13380 KB |
Output is correct |
17 |
Correct |
4 ms |
5292 KB |
Output is correct |
18 |
Correct |
43 ms |
22232 KB |
Output is correct |
19 |
Correct |
38 ms |
21204 KB |
Output is correct |
20 |
Correct |
3 ms |
5172 KB |
Output is correct |
21 |
Correct |
115 ms |
7508 KB |
Output is correct |
22 |
Correct |
22 ms |
25300 KB |
Output is correct |
23 |
Correct |
25 ms |
19412 KB |
Output is correct |
24 |
Correct |
23 ms |
20044 KB |
Output is correct |
25 |
Correct |
3 ms |
5024 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
15 ms |
13908 KB |
Output is correct |
28 |
Correct |
4 ms |
5292 KB |
Output is correct |
29 |
Correct |
4 ms |
5208 KB |
Output is correct |
30 |
Correct |
110 ms |
7380 KB |
Output is correct |
31 |
Correct |
16 ms |
15152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5048 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Execution timed out |
2102 ms |
962984 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |