#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);
}
}
void sol_penala()
{
int dr = 1, ans = 0, rmax = r[1];
while(dr != n && dr != rmax)
{
int pdr = dr;
dr = rmax;
ans ++;
for(int i = pdr + 1; i <= dr; i ++)
rmax = max(rmax, r[i]);
}
if(dr == n)
cout << ans;
else
cout << -1;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> l[i] >> r[i];
cin >> Q;
for(int i = 1; i <= Q; i ++)
cin >> x[i];
if(Q == 1 && x[1] == 1)
{
sol_penala();
return 0;
}
for(int i = 1; i <= n; i ++)
for(int j = l[i]; j <= r[i]; j ++)
vec[j].pb(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 |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
93 ms |
14048 KB |
Output is correct |
5 |
Correct |
94 ms |
14040 KB |
Output is correct |
6 |
Correct |
95 ms |
14032 KB |
Output is correct |
7 |
Correct |
73 ms |
13192 KB |
Output is correct |
8 |
Correct |
65 ms |
12884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10680 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 |
10684 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 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 |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10680 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 |
10684 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 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 |
12636 KB |
Output is correct |
16 |
Correct |
12 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
27 ms |
29820 KB |
Output is correct |
19 |
Correct |
25 ms |
28768 KB |
Output is correct |
20 |
Correct |
3 ms |
12640 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
23 ms |
30816 KB |
Output is correct |
23 |
Correct |
22 ms |
26976 KB |
Output is correct |
24 |
Correct |
22 ms |
27480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10584 KB |
Output is correct |
3 |
Correct |
2 ms |
10680 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 |
10684 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 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 |
12636 KB |
Output is correct |
16 |
Correct |
12 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
27 ms |
29820 KB |
Output is correct |
19 |
Correct |
25 ms |
28768 KB |
Output is correct |
20 |
Correct |
3 ms |
12640 KB |
Output is correct |
21 |
Correct |
6 ms |
15196 KB |
Output is correct |
22 |
Correct |
23 ms |
30816 KB |
Output is correct |
23 |
Correct |
22 ms |
26976 KB |
Output is correct |
24 |
Correct |
22 ms |
27480 KB |
Output is correct |
25 |
Correct |
2 ms |
10592 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
29 ms |
42268 KB |
Output is correct |
28 |
Correct |
22 ms |
33624 KB |
Output is correct |
29 |
Correct |
27 ms |
33628 KB |
Output is correct |
30 |
Correct |
16 ms |
27228 KB |
Output is correct |
31 |
Correct |
29 ms |
43260 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 |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
93 ms |
14048 KB |
Output is correct |
5 |
Correct |
94 ms |
14040 KB |
Output is correct |
6 |
Correct |
95 ms |
14032 KB |
Output is correct |
7 |
Correct |
73 ms |
13192 KB |
Output is correct |
8 |
Correct |
65 ms |
12884 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
2 ms |
10680 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10684 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12632 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12636 KB |
Output is correct |
24 |
Correct |
12 ms |
20828 KB |
Output is correct |
25 |
Correct |
4 ms |
12892 KB |
Output is correct |
26 |
Correct |
27 ms |
29820 KB |
Output is correct |
27 |
Correct |
25 ms |
28768 KB |
Output is correct |
28 |
Correct |
3 ms |
12640 KB |
Output is correct |
29 |
Correct |
6 ms |
15196 KB |
Output is correct |
30 |
Correct |
23 ms |
30816 KB |
Output is correct |
31 |
Correct |
22 ms |
26976 KB |
Output is correct |
32 |
Correct |
22 ms |
27480 KB |
Output is correct |
33 |
Correct |
2 ms |
10592 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
29 ms |
42268 KB |
Output is correct |
36 |
Correct |
22 ms |
33624 KB |
Output is correct |
37 |
Correct |
27 ms |
33628 KB |
Output is correct |
38 |
Correct |
16 ms |
27228 KB |
Output is correct |
39 |
Correct |
29 ms |
43260 KB |
Output is correct |
40 |
Execution timed out |
2130 ms |
832844 KB |
Time limit exceeded |
41 |
Halted |
0 ms |
0 KB |
- |