#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb push_back
#define all(x) (x).begin(),(x).end()
const int nax = 2e5 + 4;
int n, q;
vi L, R, X;
int LG[nax];
pii sp[nax][20], ps[nax][20];
void build()
{
for(int i =0 ; i < n;i++)
sp[i][0] = {R[i], i};
for(int i = 0; i < n; i++)
ps[i][0] = {L[i], i};
//cout << "here" << endl;
for(int j = 1; j < 20; j++)
{
//cout << j << endl;
for(int i = 0; i + (1 << (j - 1)) < n;i ++)
{
sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
ps[i][j] = min(ps[i][j - 1], ps[i + (1 << (j - 1))][j - 1]);
}
}
}
pii RMQ(int l, int r)
{
int j = LG[r - l + 1];
return max(sp[l][j], sp[r- (1 << j) + 1][j]);
}
pii rmq(int l, int r)
{
int j = LG[r - l + 1];
return min(ps[l][j], ps[r - (1 << j) + 1][j]);
}
int dp[2505][2505];
int f(int l, int r)
{
if(dp[l][r] != -1)
return dp[l][r];
if(l == 0 && r == n - 1)
return dp[l][r] = 0;
int ans = n + 2;
pii a = RMQ(l, r);
pii b = rmq(l, r);
int la = min(l, L[a.ss]), ra = max(r, R[a.ss]);
int lb = min(l, L[b.ss]), rb = max(r, R[b.ss]);
if(la < l || ra > r)
ans = min(ans, f(la, ra) + 1);
if(lb < l || rb > r)
ans = min(ans, f(lb, rb) + 1);
return dp[l][r] = ans;
}
void solve()
{
cin >> n;
memset(dp, -1, sizeof(dp));
L.resize(n); R.resize(n);
for(int i =0; i < n; i++)
{
cin >> L[i] >> R[i];
L[i]--; R[i]--;
}
build();
//cout << "here" << endl;
cin >> q;
while(q--)
{
int X; cin >> X;
X--;
int l = L[X], r = R[X];
int ans = f(l, r) + 1;
if(ans >= n + 1)
cout << "-1\n";
else
cout << ans << '\n';
}
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
LG[1] = 0;
for(int i =2; i < nax; i++)
LG[i] = LG[i/2] + 1;
int tt = 1;
while(tt--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
25684 KB |
Output is correct |
2 |
Correct |
11 ms |
25564 KB |
Output is correct |
3 |
Correct |
11 ms |
25560 KB |
Output is correct |
4 |
Correct |
110 ms |
90928 KB |
Output is correct |
5 |
Correct |
117 ms |
92868 KB |
Output is correct |
6 |
Correct |
122 ms |
107984 KB |
Output is correct |
7 |
Correct |
109 ms |
89912 KB |
Output is correct |
8 |
Correct |
88 ms |
77600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
25684 KB |
Output is correct |
2 |
Correct |
10 ms |
25564 KB |
Output is correct |
3 |
Correct |
11 ms |
25676 KB |
Output is correct |
4 |
Correct |
12 ms |
25596 KB |
Output is correct |
5 |
Correct |
11 ms |
25592 KB |
Output is correct |
6 |
Correct |
10 ms |
25684 KB |
Output is correct |
7 |
Correct |
11 ms |
25628 KB |
Output is correct |
8 |
Correct |
11 ms |
25556 KB |
Output is correct |
9 |
Correct |
11 ms |
25684 KB |
Output is correct |
10 |
Correct |
11 ms |
25684 KB |
Output is correct |
11 |
Correct |
11 ms |
25684 KB |
Output is correct |
12 |
Correct |
11 ms |
25752 KB |
Output is correct |
13 |
Correct |
10 ms |
25688 KB |
Output is correct |
14 |
Correct |
11 ms |
25684 KB |
Output is correct |
15 |
Correct |
11 ms |
25684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
25684 KB |
Output is correct |
2 |
Correct |
10 ms |
25564 KB |
Output is correct |
3 |
Correct |
11 ms |
25676 KB |
Output is correct |
4 |
Correct |
12 ms |
25596 KB |
Output is correct |
5 |
Correct |
11 ms |
25592 KB |
Output is correct |
6 |
Correct |
10 ms |
25684 KB |
Output is correct |
7 |
Correct |
11 ms |
25628 KB |
Output is correct |
8 |
Correct |
11 ms |
25556 KB |
Output is correct |
9 |
Correct |
11 ms |
25684 KB |
Output is correct |
10 |
Correct |
11 ms |
25684 KB |
Output is correct |
11 |
Correct |
11 ms |
25684 KB |
Output is correct |
12 |
Correct |
11 ms |
25752 KB |
Output is correct |
13 |
Correct |
10 ms |
25688 KB |
Output is correct |
14 |
Correct |
11 ms |
25684 KB |
Output is correct |
15 |
Correct |
11 ms |
25684 KB |
Output is correct |
16 |
Correct |
12 ms |
26452 KB |
Output is correct |
17 |
Incorrect |
12 ms |
26452 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
25684 KB |
Output is correct |
2 |
Correct |
10 ms |
25564 KB |
Output is correct |
3 |
Correct |
11 ms |
25676 KB |
Output is correct |
4 |
Correct |
12 ms |
25596 KB |
Output is correct |
5 |
Correct |
11 ms |
25592 KB |
Output is correct |
6 |
Correct |
10 ms |
25684 KB |
Output is correct |
7 |
Correct |
11 ms |
25628 KB |
Output is correct |
8 |
Correct |
11 ms |
25556 KB |
Output is correct |
9 |
Correct |
11 ms |
25684 KB |
Output is correct |
10 |
Correct |
11 ms |
25684 KB |
Output is correct |
11 |
Correct |
11 ms |
25684 KB |
Output is correct |
12 |
Correct |
11 ms |
25752 KB |
Output is correct |
13 |
Correct |
10 ms |
25688 KB |
Output is correct |
14 |
Correct |
11 ms |
25684 KB |
Output is correct |
15 |
Correct |
11 ms |
25684 KB |
Output is correct |
16 |
Correct |
12 ms |
26452 KB |
Output is correct |
17 |
Incorrect |
12 ms |
26452 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
25684 KB |
Output is correct |
2 |
Correct |
11 ms |
25564 KB |
Output is correct |
3 |
Correct |
11 ms |
25560 KB |
Output is correct |
4 |
Correct |
110 ms |
90928 KB |
Output is correct |
5 |
Correct |
117 ms |
92868 KB |
Output is correct |
6 |
Correct |
122 ms |
107984 KB |
Output is correct |
7 |
Correct |
109 ms |
89912 KB |
Output is correct |
8 |
Correct |
88 ms |
77600 KB |
Output is correct |
9 |
Correct |
10 ms |
25684 KB |
Output is correct |
10 |
Correct |
10 ms |
25564 KB |
Output is correct |
11 |
Correct |
11 ms |
25676 KB |
Output is correct |
12 |
Correct |
12 ms |
25596 KB |
Output is correct |
13 |
Correct |
11 ms |
25592 KB |
Output is correct |
14 |
Correct |
10 ms |
25684 KB |
Output is correct |
15 |
Correct |
11 ms |
25628 KB |
Output is correct |
16 |
Correct |
11 ms |
25556 KB |
Output is correct |
17 |
Correct |
11 ms |
25684 KB |
Output is correct |
18 |
Correct |
11 ms |
25684 KB |
Output is correct |
19 |
Correct |
11 ms |
25684 KB |
Output is correct |
20 |
Correct |
11 ms |
25752 KB |
Output is correct |
21 |
Correct |
10 ms |
25688 KB |
Output is correct |
22 |
Correct |
11 ms |
25684 KB |
Output is correct |
23 |
Correct |
11 ms |
25684 KB |
Output is correct |
24 |
Correct |
12 ms |
26452 KB |
Output is correct |
25 |
Incorrect |
12 ms |
26452 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |