#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>
using namespace std;
int tt = 1, n;
int a[200005], lf[200005], rg[200005];
int mn[4 * 200005], mx[4 * 200005];
void up(int l, int r, int pos, int num, int num1, int vv){
if(l > pos || r < pos) return;
if(l == r){
mn[vv] = num;
mx[vv] = num1;
return;
}
int mid = (l + r) / 2;
up(l, mid, pos, num, num1, vv * 2);
up(mid + 1, r, pos, num, num1, vv * 2 + 1);
mn[vv] = min(mn[vv * 2], mn[vv * 2 + 1]);
mx[vv] = max(mx[vv * 2], mx[vv * 2 + 1]);
}
int sbor(int l, int r, int l1, int r1, int vv){
if(l > r1 || l1 > r) return 1e9;
if(l >= l1 && r1 >= r) return mn[vv];
int mid = (l + r) / 2;
return min(sbor(l, mid, l1, r1, vv * 2), sbor(mid + 1, r, l1, r1, vv * 2 + 1));
}
int sbor1(int l, int r, int l1, int r1, int vv){
if(l > r1 || l1 > r) return 0;
if(l >= l1 && r1 >= r) return mx[vv];
int mid = (l + r) / 2;
return max(sbor1(l, mid, l1, r1, vv * 2), sbor1(mid + 1, r, l1, r1, vv * 2 + 1));
}
int dp[200005];
vector<int> g1[200005], g2[200005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> lf[i] >> rg[i];
up(1, n, i, lf[i], rg[i], 1);
}
cin >> tt;
while(tt--){
int x;
cin >> x;
bool ok = 0;
set<pair<int, int>> st1, st2;
for(int i = 1; i <= n; i++){
dp[i] = 1e9;
g1[i].clear();
g2[i].clear();
}
dp[x] = 0;
int l = x - 1, r = x + 1, pos = x;
st1.insert({dp[x], x});
st2.insert({dp[x], x});
g1[lf[x]].push_back(x);
g2[rg[x]].push_back(x);
int mn1 = l, mx1 = r;
if(lf[x] == x) st1.erase({dp[x], x});
if(rg[x] == x) st2.erase({dp[x], x});
g1[x].clear();
g2[x].clear();
while(l >= 1 || r <= n){
bool f1 = 0, f2 = 0;
if(l > 0 && !st1.empty()){
dp[l] = st1.begin()->first + 1;
f1 = 1;
}
if(r <= n && !st2.empty()){
dp[r] = st2.begin()->first + 1;
f2 = 1;
}
// cout << l << " " << r << "\n";
// cout << f1 << " " << f2 << "\n";
if(!f1 && !f2){
ok = 1;
break;
}
if(f1 == 1 && l > 0 && rg[l] >= r && r <= n) dp[r] = min(dp[r], dp[l] + 1);
if(f2 == 1 && r <= n && lf[r] <= l && l > 0) dp[l] = min(dp[l], dp[r] + 1);
if(f1 == 1){
if(lf[l] != l){
g1[lf[l]].push_back(l);
st1.insert({dp[l], l});
}
if(r <= n && rg[l] >= r){
st2.insert({dp[l], l});
g2[rg[l]].push_back(l);
}
}
if(f2 == 1 && r <= n){
if(rg[r] != r){
g2[rg[r]].push_back(r);
st2.insert({dp[r], r});
}
if(l > 0 && lf[r] <= l){
st1.insert({dp[r], r});
g1[lf[r]].push_back(r);
}
}
if(f1 == 1){
for(int to : g1[l])
st1.erase({dp[to], to});
g1[l].clear();
}
if(f2 == 1){
for(int to : g2[r])
st2.erase({dp[to], to});
g2[r].clear();
}
// cout << dp[l] << " " << dp[r] << "\n" << "\n";
if(f1 == 1) l--;
if(f2 == 1) r++;
}
if(ok){
cout << -1 << "\n";
continue;
}
int ans = 0;
for(int i = 1; i <= n; i++) ans = max(ans, dp[i]);
cout << ans << "\n";
}
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:80:35: warning: unused variable 'pos' [-Wunused-variable]
80 | int l = x - 1, r = x + 1, pos = x;
| ^~~
passport.cpp:85:13: warning: unused variable 'mn1' [-Wunused-variable]
85 | int mn1 = l, mx1 = r;
| ^~~
passport.cpp:85:22: warning: unused variable 'mx1' [-Wunused-variable]
85 | int mn1 = l, mx1 = r;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
124 ms |
25244 KB |
Output is correct |
5 |
Correct |
82 ms |
24240 KB |
Output is correct |
6 |
Correct |
86 ms |
26748 KB |
Output is correct |
7 |
Correct |
93 ms |
29996 KB |
Output is correct |
8 |
Correct |
59 ms |
23904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Incorrect |
4 ms |
16988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Incorrect |
4 ms |
16988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Incorrect |
4 ms |
16988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
124 ms |
25244 KB |
Output is correct |
5 |
Correct |
82 ms |
24240 KB |
Output is correct |
6 |
Correct |
86 ms |
26748 KB |
Output is correct |
7 |
Correct |
93 ms |
29996 KB |
Output is correct |
8 |
Correct |
59 ms |
23904 KB |
Output is correct |
9 |
Correct |
3 ms |
16984 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Incorrect |
4 ms |
16988 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |