// na hanooz omidi vjood dare... hanooz ye karayi hast ke bknm :)
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "(" << (#x) << "): " << (x) << endl;
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
const int maxn5 = 2e5 + 10;
const int maxnt = 1e6 + 10;
const int lg = 20;
int suf[maxn5], pre[maxn5], ans[maxn5];
int l[maxn5], r[maxn5];
bool mark[maxn5], done[maxn5];
int dp[maxn5];
vector <int> adj[maxn5], rt, av[maxn5];
namespace rmq{
pair <int, int> mn[lg][maxn5];
void build(int n){
for(int i = 1; i < lg; i++) for(int j = 0; j + (1 << i) - 1 < n; j++)
mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
}
pair <int, int> get_min(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
return min(mn[k][l], mn[k][r - (1 << k) + 1]);
}
}
namespace seg{
pair <int, int> mn[maxnt], mx[maxnt];
void build(int l, int r, int v){
if(r - l == 1){
mn[v] = {::l[l], l};
mx[v] = {::r[l], l};
return;
}
int mid = (l + r) >> 1;
build(l, mid, v * 2);
build(mid, r, v * 2 + 1);
mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
}
void rem(int l, int r, int id, int v){
if(r - l == 1){
mn[v] = {maxn5, l};
mx[v] = {-1, l};
return;
}
int mid = (l + r) >> 1;
if(id < mid)
rem(l, mid, id, v * 2);
else
rem(mid, r, id, v * 2 + 1);
mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
}
pair <int, int> get_max(int l, int r, int lq, int rq, int v){
if(rq <= l || r <= lq)
return mp(-1, -1);
if(lq <= l && r <= rq)
return mx[v];
int mid = (l + r) >> 1;
return max(get_max(l, mid, lq, rq, v * 2), get_max(mid, r, lq, rq, v * 2 + 1));
}
pair <int, int> get_min(int l, int r, int lq, int rq, int v){
if(rq <= l || r <= lq)
return mp(maxn5, -1);
if(lq <= l && r <= rq)
return mn[v];
int mid = (l + r) >> 1;
return min(get_min(l, mid, lq, rq, v * 2), get_min(mid, r, lq, rq, v * 2 + 1));
}
}
void dfs(int v){
for(auto u : adj[v]){
dp[u] = min(dp[u], dp[v] + 1);
dfs(u);
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 0; i < n; i++){
cin >> l[i] >> r[i];
l[i]--; r[i]--;
rmq::mn[0][i] = {l[i], i};
}
rmq::build(n);
fill(dp, dp + n + 5, maxn5);
for(int i = 0; i < n; i++){
int v = rmq::get_min(l[i], r[i]).se;
if(l[i] == 0)
dp[i] = 0;
if(i != v)
adj[v].pb(i);
else
rt.pb(i);
}
for(auto u : rt)
dfs(u);
for(int i = 0; i < n; i++){
pre[i] = dp[i];
adj[i].clear();
}
rt.clear();
for(int i = 0; i < n; i++)
rmq::mn[0][i] = {-r[i], i};
rmq::build(n);
fill(dp, dp + n + 5, maxn5);
for(int i = 0; i < n; i++){
int v = rmq::get_min(l[i], r[i]).se;
if(r[i] == n - 1)
dp[i] = 0;
if(i != v)
adj[v].pb(i);
else
rt.pb(i);
}
for(auto u : rt)
dfs(u);
for(int i = 0; i < n; i++){
suf[i] = dp[i];
adj[i].clear();
ans[i] = min(maxn5, pre[i] + suf[i]);
if(ans[i] < maxn5)
av[ans[i]].pb(i);
}
seg::build(0, n, 1);
for(int i = 0; i < maxn5; i++) for(int j = 0; j < int(av[i].size()); j++){
int v = av[i][j];
if(done[v])
continue;
done[v] = mark[v] = true;
//debug(v);
//debug(ans[v]);
pair <int, int> best;
while((best = seg::get_max(0, n, 0, v, 1)).fi >= v){
int u = best.se;
//debug(u);
if(!mark[u]){
mark[u] = true;
ans[u] = min(ans[u], ans[v] + 1);
av[ans[u]].pb(u);
}
seg::rem(0, n, u, 1);
}
while((best = seg::get_min(0, n, v + 1, n, 1)).fi <= v){
int u = best.se;
//debug(u);
if(!mark[u]){
mark[u] = true;
ans[u] = min(ans[u], ans[v] + 1);
av[ans[u]].pb(u);
}
seg::rem(0, n, u, 1);
}
}
int q; cin >> q;
for(int i = 0; i < q; i++){
int x; cin >> x;
x--;
cout << (ans[x] >= maxn5 ? -1 : ans[x] + 1) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
212 ms |
52488 KB |
Output is correct |
5 |
Correct |
179 ms |
55176 KB |
Output is correct |
6 |
Correct |
167 ms |
66788 KB |
Output is correct |
7 |
Correct |
136 ms |
51016 KB |
Output is correct |
8 |
Correct |
45 ms |
42448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
7 ms |
10308 KB |
Output is correct |
17 |
Correct |
7 ms |
10324 KB |
Output is correct |
18 |
Correct |
8 ms |
10324 KB |
Output is correct |
19 |
Correct |
6 ms |
10196 KB |
Output is correct |
20 |
Correct |
6 ms |
10452 KB |
Output is correct |
21 |
Correct |
6 ms |
10324 KB |
Output is correct |
22 |
Correct |
5 ms |
10196 KB |
Output is correct |
23 |
Correct |
8 ms |
10324 KB |
Output is correct |
24 |
Correct |
8 ms |
10196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9812 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
7 ms |
10308 KB |
Output is correct |
17 |
Correct |
7 ms |
10324 KB |
Output is correct |
18 |
Correct |
8 ms |
10324 KB |
Output is correct |
19 |
Correct |
6 ms |
10196 KB |
Output is correct |
20 |
Correct |
6 ms |
10452 KB |
Output is correct |
21 |
Correct |
6 ms |
10324 KB |
Output is correct |
22 |
Correct |
5 ms |
10196 KB |
Output is correct |
23 |
Correct |
8 ms |
10324 KB |
Output is correct |
24 |
Correct |
8 ms |
10196 KB |
Output is correct |
25 |
Correct |
5 ms |
9684 KB |
Output is correct |
26 |
Correct |
6 ms |
9684 KB |
Output is correct |
27 |
Correct |
7 ms |
10196 KB |
Output is correct |
28 |
Correct |
7 ms |
10324 KB |
Output is correct |
29 |
Correct |
6 ms |
10388 KB |
Output is correct |
30 |
Correct |
6 ms |
10324 KB |
Output is correct |
31 |
Correct |
6 ms |
10324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9728 KB |
Output is correct |
4 |
Correct |
212 ms |
52488 KB |
Output is correct |
5 |
Correct |
179 ms |
55176 KB |
Output is correct |
6 |
Correct |
167 ms |
66788 KB |
Output is correct |
7 |
Correct |
136 ms |
51016 KB |
Output is correct |
8 |
Correct |
45 ms |
42448 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9812 KB |
Output is correct |
24 |
Correct |
7 ms |
10308 KB |
Output is correct |
25 |
Correct |
7 ms |
10324 KB |
Output is correct |
26 |
Correct |
8 ms |
10324 KB |
Output is correct |
27 |
Correct |
6 ms |
10196 KB |
Output is correct |
28 |
Correct |
6 ms |
10452 KB |
Output is correct |
29 |
Correct |
6 ms |
10324 KB |
Output is correct |
30 |
Correct |
5 ms |
10196 KB |
Output is correct |
31 |
Correct |
8 ms |
10324 KB |
Output is correct |
32 |
Correct |
8 ms |
10196 KB |
Output is correct |
33 |
Correct |
5 ms |
9684 KB |
Output is correct |
34 |
Correct |
6 ms |
9684 KB |
Output is correct |
35 |
Correct |
7 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10324 KB |
Output is correct |
37 |
Correct |
6 ms |
10388 KB |
Output is correct |
38 |
Correct |
6 ms |
10324 KB |
Output is correct |
39 |
Correct |
6 ms |
10324 KB |
Output is correct |
40 |
Correct |
272 ms |
56684 KB |
Output is correct |
41 |
Correct |
213 ms |
60512 KB |
Output is correct |
42 |
Correct |
215 ms |
60220 KB |
Output is correct |
43 |
Correct |
191 ms |
56964 KB |
Output is correct |
44 |
Correct |
195 ms |
71716 KB |
Output is correct |
45 |
Correct |
153 ms |
65816 KB |
Output is correct |
46 |
Correct |
67 ms |
28396 KB |
Output is correct |
47 |
Correct |
184 ms |
58732 KB |
Output is correct |
48 |
Correct |
149 ms |
55296 KB |
Output is correct |