#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define fr first
#define sc second
#define pb push_back
int l[200005], r[200005], x[200005];
int nrpas1[200005], nrpasn[200005], ans[200005];
vector<int> vec[200005];
int n, Q;
int dist[2505][2505];
void bfs1()
{
queue<int> q;
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto i : vec[nod])
{
if(i != 1 && nrpas1[i] == 0)
{
nrpas1[i] = nrpas1[nod] + 1;
q.push(i);
}
}
}
for(int i = 2; i <= n; i ++)
if(nrpas1[i] == 0)
nrpas1[i] = -1;
}
void bfsn()
{
queue<int> q;
q.push(n);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto i : vec[nod])
{
if(i != n && nrpasn[i] == 0)
{
nrpasn[i] = nrpasn[nod] + 1;
q.push(i);
}
}
}
for(int i = 1; i < n; i ++)
if(nrpasn[i] == 0)
nrpasn[i] = -1;
}
void calcdist(int nod)
{
int st = l[nod], dr = r[nod], stp = nod, drp = nod, sus = 0;
ans[nod] = nrpas1[nod] + nrpasn[nod];
if(nrpas1[nod] == -1 || nrpasn[nod] == -1)
{
ans[nod] = -1;
return;
}
// cout << '\n';
while(st != stp || dr != drp)
{
sus ++;
// cout << st << " " << dr << " " << stp << " " << drp << " " << sus << '\n';
int sta = stp, dra = drp;
stp = st, drp = dr;
for(int i = sta - 1; i >= stp; i --)
st = min(st, l[i]), dr = max(dr, r[i]), dist[nod][i] = sus;
for(int i = dra + 1; i <= drp; i ++)
dr = max(dr, r[i]), st = min(st, l[i]), dist[nod][i] = sus;
}
for(int i = 1; i < st; i ++)
dist[nod][i] = 1e9;
for(int i = dr + 1; i <= n; i ++)
dist[nod][i] = 1e9;
for(int i = 1; i <= n; i ++)
if(nrpas1[i] != -1 && nrpasn[i] != -1)
{
int sus = nrpas1[i] + nrpasn[i] + dist[nod][i];
if(nrpas1[i] != 0 && nrpasn[i] != 0)
sus --;
ans[nod] = min(ans[nod], sus);
}
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> l[i] >> r[i];
for(int j = l[i]; j <= r[i]; j ++)
vec[j].pb(i);
}
cin >> Q;
for(int i = 1; i <= Q; i ++)
cin >> x[i];
bfs1();
bfsn();
for(int i = 1; i <= Q; i ++)
calcdist(x[i]), cout << ans[x[i]] << '\n';
// for(int i = 1; i <= n; i ++)
// cout << i << " " << nrpas1[i] << " " << nrpasn[i] << '\n';
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 1; j <= n; j ++)
// cout << dist[i][j] << " ";
// cout << '\n';
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Execution timed out |
2096 ms |
868520 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
2 ms |
12732 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
2 ms |
12732 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12832 KB |
Output is correct |
16 |
Correct |
13 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
12720 KB |
Output is correct |
18 |
Correct |
26 ms |
29532 KB |
Output is correct |
19 |
Correct |
27 ms |
28912 KB |
Output is correct |
20 |
Correct |
3 ms |
12636 KB |
Output is correct |
21 |
Correct |
6 ms |
14940 KB |
Output is correct |
22 |
Correct |
23 ms |
30680 KB |
Output is correct |
23 |
Correct |
20 ms |
26968 KB |
Output is correct |
24 |
Correct |
22 ms |
27584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
2 ms |
12732 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12832 KB |
Output is correct |
16 |
Correct |
13 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
12720 KB |
Output is correct |
18 |
Correct |
26 ms |
29532 KB |
Output is correct |
19 |
Correct |
27 ms |
28912 KB |
Output is correct |
20 |
Correct |
3 ms |
12636 KB |
Output is correct |
21 |
Correct |
6 ms |
14940 KB |
Output is correct |
22 |
Correct |
23 ms |
30680 KB |
Output is correct |
23 |
Correct |
20 ms |
26968 KB |
Output is correct |
24 |
Correct |
22 ms |
27584 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
31 ms |
42220 KB |
Output is correct |
28 |
Correct |
22 ms |
33624 KB |
Output is correct |
29 |
Correct |
25 ms |
33496 KB |
Output is correct |
30 |
Correct |
16 ms |
27228 KB |
Output is correct |
31 |
Correct |
29 ms |
43336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10588 KB |
Output is correct |
4 |
Execution timed out |
2096 ms |
868520 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |