# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pii pair <int,int>
#define pb push_back
const int N = 3e5 + 5;
int n, l[N], r[N];
pii tree[4 * N];
multiset <pii> ms[4 * N];
void update(int node, int le, int ri, int idx, int val, int id) {
if (le > idx || ri < idx) return ;
if (le == ri) {
// cout<<"upd "<<le<<" "<<ri<<" "<<node<<"\n";
if (val == 0) {
if (!ms[node].size()) {
// cout<<node<<" "<<idx<<" "<<val<<" "<<id<<"\n";
// exit(0);
assert(false);
}
assert(ms[node].size());
ms[node].erase(--ms[node].end());
if (!ms[node].size()) {
tree[node] = {0, 0}; return ;
}
}
if (val != 0) ms[node].insert({val, id});
auto it = --ms[node].end();
tree[node] = {(*it).f, (*it).s};
return ;
}
int mid = (le + ri) / 2;
update(2 * node, le, mid, idx, val, id);
update(2 * node + 1, mid + 1, ri, idx, val, id);
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
pii getans(int node, int le, int ri, int start, int end) {
if (le > end || ri < start) return {0, 0};
if (le >= start && ri <= end) return tree[node];
int mid = (le + ri) / 2;
return max(getans(2 * node, le, mid, start, end), getans(2 * node + 1, mid + 1, ri ,start, end));
}
int dp[2][N];
void bfs(int a, int ty) {
for (int i = 0; i < 4 * N; i++) {
tree[i] = {0, 0};
ms[i].clear();
}
for (int i = 1; i <= n; i++) {
if (i == a) continue;
dp[ty][i] = 1e9;
update(1, 1, n, l[i], r[i], i);
}
queue <int> q;
q.push(a);
dp[ty][a] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
while (getans(1, 1, n, 1, x).f >= x) {
pii sth = getans(1, 1, n, 1, x);
q.push(sth.s);
dp[ty][sth.s] = dp[ty][x] + 1;
update(1, 1, n, l[sth.s], 0, sth.s);
}
}
}
int tree_mn[4 * N];
void upd_mn(int node, int le, int ri, int idx, int val) {
if (le > idx || ri < idx) return ;
if (le == ri) {
tree_mn[node] = val; return ;
}
int mid = (le + ri) / 2;
upd_mn(2 * node, le, mid, idx,val);
upd_mn(2 * node + 1, mid + 1, ri, idx,val);
tree_mn[node] = min(tree_mn[2 * node], tree_mn[2 * node + 1]);
}
int get_mn(int node, int le, int ri, int start, int end) {
if (le > end || ri < start) return 1e9;
if (le >= start && ri <= end) return tree_mn[node];
int mid = (le + ri) / 2;
return min(get_mn(2 * node, le ,mid, start, end), get_mn(2 * node + 1, mid + 1, ri, start, end));
}
signed main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>l[i]>>r[i];
}
bfs(1, 0);
bfs(n, 1);
vector <pii> vec;
for (int i = 1; i <= n; i++) {
vec.pb({dp[0][i] + dp[1][i] - (i != 1 && i != n), i});
}
sort(vec.begin(), vec.end());
for (pii sth : vec) {
upd_mn(1, 1, n, sth.s, sth.f);
}
vector <int> res(n + 5, 0);
for (int i = 0; i < (int)vec.size(); i++) {
int id = vec[i].s;
res[id] = min(vec[i].f, get_mn(1, 1, n, l[id], r[id]) + 1);
upd_mn(1, 1, n, id, res[id]);
}
int q;
cin>>q;
while(q--) {
int idx; cin>>idx;
cout<<(res[idx] >= 1e9 ? -1 : res[idx])<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
66028 KB |
Output is correct |
2 |
Correct |
41 ms |
66052 KB |
Output is correct |
3 |
Correct |
41 ms |
66024 KB |
Output is correct |
4 |
Correct |
656 ms |
84156 KB |
Output is correct |
5 |
Correct |
447 ms |
85304 KB |
Output is correct |
6 |
Correct |
432 ms |
83136 KB |
Output is correct |
7 |
Correct |
371 ms |
82020 KB |
Output is correct |
8 |
Correct |
244 ms |
80784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
65996 KB |
Output is correct |
2 |
Correct |
42 ms |
66100 KB |
Output is correct |
3 |
Correct |
41 ms |
66084 KB |
Output is correct |
4 |
Correct |
42 ms |
66068 KB |
Output is correct |
5 |
Correct |
48 ms |
66080 KB |
Output is correct |
6 |
Correct |
42 ms |
66068 KB |
Output is correct |
7 |
Correct |
40 ms |
66004 KB |
Output is correct |
8 |
Correct |
41 ms |
66080 KB |
Output is correct |
9 |
Correct |
42 ms |
66036 KB |
Output is correct |
10 |
Correct |
42 ms |
66000 KB |
Output is correct |
11 |
Correct |
50 ms |
66088 KB |
Output is correct |
12 |
Correct |
47 ms |
66072 KB |
Output is correct |
13 |
Correct |
49 ms |
66112 KB |
Output is correct |
14 |
Correct |
41 ms |
66080 KB |
Output is correct |
15 |
Correct |
42 ms |
66104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
65996 KB |
Output is correct |
2 |
Correct |
42 ms |
66100 KB |
Output is correct |
3 |
Correct |
41 ms |
66084 KB |
Output is correct |
4 |
Correct |
42 ms |
66068 KB |
Output is correct |
5 |
Correct |
48 ms |
66080 KB |
Output is correct |
6 |
Correct |
42 ms |
66068 KB |
Output is correct |
7 |
Correct |
40 ms |
66004 KB |
Output is correct |
8 |
Correct |
41 ms |
66080 KB |
Output is correct |
9 |
Correct |
42 ms |
66036 KB |
Output is correct |
10 |
Correct |
42 ms |
66000 KB |
Output is correct |
11 |
Correct |
50 ms |
66088 KB |
Output is correct |
12 |
Correct |
47 ms |
66072 KB |
Output is correct |
13 |
Correct |
49 ms |
66112 KB |
Output is correct |
14 |
Correct |
41 ms |
66080 KB |
Output is correct |
15 |
Correct |
42 ms |
66104 KB |
Output is correct |
16 |
Correct |
45 ms |
66252 KB |
Output is correct |
17 |
Correct |
46 ms |
66192 KB |
Output is correct |
18 |
Correct |
53 ms |
66236 KB |
Output is correct |
19 |
Correct |
53 ms |
66380 KB |
Output is correct |
20 |
Correct |
43 ms |
66268 KB |
Output is correct |
21 |
Correct |
49 ms |
66260 KB |
Output is correct |
22 |
Correct |
43 ms |
66256 KB |
Output is correct |
23 |
Correct |
43 ms |
66264 KB |
Output is correct |
24 |
Correct |
44 ms |
66288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
65996 KB |
Output is correct |
2 |
Correct |
42 ms |
66100 KB |
Output is correct |
3 |
Correct |
41 ms |
66084 KB |
Output is correct |
4 |
Correct |
42 ms |
66068 KB |
Output is correct |
5 |
Correct |
48 ms |
66080 KB |
Output is correct |
6 |
Correct |
42 ms |
66068 KB |
Output is correct |
7 |
Correct |
40 ms |
66004 KB |
Output is correct |
8 |
Correct |
41 ms |
66080 KB |
Output is correct |
9 |
Correct |
42 ms |
66036 KB |
Output is correct |
10 |
Correct |
42 ms |
66000 KB |
Output is correct |
11 |
Correct |
50 ms |
66088 KB |
Output is correct |
12 |
Correct |
47 ms |
66072 KB |
Output is correct |
13 |
Correct |
49 ms |
66112 KB |
Output is correct |
14 |
Correct |
41 ms |
66080 KB |
Output is correct |
15 |
Correct |
42 ms |
66104 KB |
Output is correct |
16 |
Correct |
45 ms |
66252 KB |
Output is correct |
17 |
Correct |
46 ms |
66192 KB |
Output is correct |
18 |
Correct |
53 ms |
66236 KB |
Output is correct |
19 |
Correct |
53 ms |
66380 KB |
Output is correct |
20 |
Correct |
43 ms |
66268 KB |
Output is correct |
21 |
Correct |
49 ms |
66260 KB |
Output is correct |
22 |
Correct |
43 ms |
66256 KB |
Output is correct |
23 |
Correct |
43 ms |
66264 KB |
Output is correct |
24 |
Correct |
44 ms |
66288 KB |
Output is correct |
25 |
Correct |
40 ms |
66092 KB |
Output is correct |
26 |
Correct |
41 ms |
65988 KB |
Output is correct |
27 |
Correct |
47 ms |
66236 KB |
Output is correct |
28 |
Correct |
53 ms |
66252 KB |
Output is correct |
29 |
Correct |
47 ms |
66212 KB |
Output is correct |
30 |
Correct |
44 ms |
66204 KB |
Output is correct |
31 |
Correct |
45 ms |
66232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
66028 KB |
Output is correct |
2 |
Correct |
41 ms |
66052 KB |
Output is correct |
3 |
Correct |
41 ms |
66024 KB |
Output is correct |
4 |
Correct |
656 ms |
84156 KB |
Output is correct |
5 |
Correct |
447 ms |
85304 KB |
Output is correct |
6 |
Correct |
432 ms |
83136 KB |
Output is correct |
7 |
Correct |
371 ms |
82020 KB |
Output is correct |
8 |
Correct |
244 ms |
80784 KB |
Output is correct |
9 |
Correct |
42 ms |
65996 KB |
Output is correct |
10 |
Correct |
42 ms |
66100 KB |
Output is correct |
11 |
Correct |
41 ms |
66084 KB |
Output is correct |
12 |
Correct |
42 ms |
66068 KB |
Output is correct |
13 |
Correct |
48 ms |
66080 KB |
Output is correct |
14 |
Correct |
42 ms |
66068 KB |
Output is correct |
15 |
Correct |
40 ms |
66004 KB |
Output is correct |
16 |
Correct |
41 ms |
66080 KB |
Output is correct |
17 |
Correct |
42 ms |
66036 KB |
Output is correct |
18 |
Correct |
42 ms |
66000 KB |
Output is correct |
19 |
Correct |
50 ms |
66088 KB |
Output is correct |
20 |
Correct |
47 ms |
66072 KB |
Output is correct |
21 |
Correct |
49 ms |
66112 KB |
Output is correct |
22 |
Correct |
41 ms |
66080 KB |
Output is correct |
23 |
Correct |
42 ms |
66104 KB |
Output is correct |
24 |
Correct |
45 ms |
66252 KB |
Output is correct |
25 |
Correct |
46 ms |
66192 KB |
Output is correct |
26 |
Correct |
53 ms |
66236 KB |
Output is correct |
27 |
Correct |
53 ms |
66380 KB |
Output is correct |
28 |
Correct |
43 ms |
66268 KB |
Output is correct |
29 |
Correct |
49 ms |
66260 KB |
Output is correct |
30 |
Correct |
43 ms |
66256 KB |
Output is correct |
31 |
Correct |
43 ms |
66264 KB |
Output is correct |
32 |
Correct |
44 ms |
66288 KB |
Output is correct |
33 |
Correct |
40 ms |
66092 KB |
Output is correct |
34 |
Correct |
41 ms |
65988 KB |
Output is correct |
35 |
Correct |
47 ms |
66236 KB |
Output is correct |
36 |
Correct |
53 ms |
66252 KB |
Output is correct |
37 |
Correct |
47 ms |
66212 KB |
Output is correct |
38 |
Correct |
44 ms |
66204 KB |
Output is correct |
39 |
Correct |
45 ms |
66232 KB |
Output is correct |
40 |
Correct |
678 ms |
86888 KB |
Output is correct |
41 |
Correct |
466 ms |
88028 KB |
Output is correct |
42 |
Correct |
474 ms |
85404 KB |
Output is correct |
43 |
Correct |
504 ms |
85048 KB |
Output is correct |
44 |
Correct |
431 ms |
85888 KB |
Output is correct |
45 |
Correct |
408 ms |
85376 KB |
Output is correct |
46 |
Correct |
230 ms |
74096 KB |
Output is correct |
47 |
Correct |
451 ms |
84736 KB |
Output is correct |
48 |
Correct |
520 ms |
86592 KB |
Output is correct |