#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>> haha(200001);
vector<pair<int,int>> sm(1000001);
vector<pair<int,int>> big(1000001);
vector<int> seg[1000001];
vector<int> dp1(200001,1000000);
vector<int> dp2(200001,1000000);
vector<int> dp(200001,1000000);
vector<int> wow[1000001];
void build(int l, int r, int x) {
if(l == r) {
big[x] = {haha[l].second,l};
sm[x] = {haha[l].first,l};
return;
}
int mid = (l+r)/2;
build(l,mid,x*2);
build(mid+1,r,x*2+1);
big[x] = max(big[x*2],big[x*2+1]);
sm[x] = min(sm[x*2],sm[x*2+1]);
}
pair<int,int> calc(int l, int r, int ql, int qr, int x, bool yeah) {
if(ql == l && qr == r) {
if(yeah) {
return sm[x];
}
else {
return big[x];
}
}
int mid = (l+r)/2;
if(qr <= mid) {
return calc(l,mid,ql,qr,x*2,yeah);
}
else if(ql > mid) {
return calc(mid+1,r,ql,qr,x*2+1,yeah);
}
else {
pair<int,int> a = calc(l,mid,ql,mid,x*2,yeah),b = calc(mid+1,r,mid+1,qr,x*2+1,yeah);
if(yeah) {
return min(a,b);
}
else {
return max(a,b);
}
}
}
void dfs(int a, bool yeah, int d) {
if(yeah) {
dp1[a] = d;
}
else {
dp2[a] = d;
}
for(int v: wow[a]) {
dfs(v,yeah,d+1);
}
}
void dude(int l, int r, int ql, int qr, int x, int c) {
if(ql == l && qr == r) {
seg[x].push_back(c);
return;
}
int mid = (l+r)/2;
if(qr <= mid) {
dude(l,mid,ql,qr,x*2,c);
}
else if(ql > mid) {
dude(mid+1,r,ql,qr,x*2+1,c);
}
else {
dude(l,mid,ql,mid,x*2,c);
dude(mid+1,r,mid+1,qr,x*2+1,c);
}
}
vector<int> troll[1000001];
void banana(int l, int r, int x, int p, int br) {
for(int c: seg[x]) {
if(dp[c] > br) {
dp[c] = br;
troll[br].push_back(c);
}
}
seg[x].clear();
if(l == r) {
return;
}
int mid = (l+r)/2;
if(p <= mid) {
banana(l,mid,x*2,p,br);
}
else {
banana(mid+1,r,x*2+1,p,br);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,l,r;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> l >> r;
haha[i] = {l,r};
}
build(1,n,1);
for(int i = 1; i <= n; i++) {
pair<int,int> a = calc(1,n,haha[i].first,haha[i].second,1,1);
if(a.first < haha[i].first) {
wow[a.second].push_back(i);
}
else if(a.first == 1) {
dp1[i] = 0;
}
}
for(int i = 1; i <= n; i++) {
if(dp1[i] == 0) {
dfs(i,1,0);
}
}
for(int i = 1; i <= n; i++) {
wow[i].clear();
}
for(int i = 1; i <= n; i++) {
pair<int,int> a = calc(1,n,haha[i].first,haha[i].second,1,0);
if(a.first > haha[i].second) {
wow[a.second].push_back(i);
}
else if(a.second == n) {
dp2[i] = 0;
}
}
for(int i = 1; i <= n; i++) {
if(dp2[i] == 0) {
dfs(i,0,0);
}
}
for(int i = 1; i <= n; i++) {
dp[i] = dp1[i]+dp2[i]+1;
if(dp[i] <= n) {
troll[dp[i]].push_back(i);
}
}
for(int i = 1; i <= n; i++) {
dude(1,n,haha[i].first,haha[i].second,1,i);
}
for(int i = 0; i <= n; i++) {
for(int v: troll[i]) {
if(dp[v] == i) {
banana(1,n,1,v,dp[v]+1);
}
}
}
int q,x;
cin >> q;
for(int i = 0; i < q; i++) {
cin >> x;
if(dp[x] <= n) {
cout << dp[x] << "\n";
}
else {
cout << -1 << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
90460 KB |
Output is correct |
2 |
Correct |
19 ms |
90368 KB |
Output is correct |
3 |
Correct |
19 ms |
90444 KB |
Output is correct |
4 |
Correct |
272 ms |
113816 KB |
Output is correct |
5 |
Correct |
145 ms |
109392 KB |
Output is correct |
6 |
Correct |
111 ms |
119784 KB |
Output is correct |
7 |
Correct |
49 ms |
94416 KB |
Output is correct |
8 |
Correct |
67 ms |
97948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
90456 KB |
Output is correct |
2 |
Correct |
19 ms |
90456 KB |
Output is correct |
3 |
Correct |
22 ms |
90460 KB |
Output is correct |
4 |
Correct |
19 ms |
90280 KB |
Output is correct |
5 |
Correct |
20 ms |
90460 KB |
Output is correct |
6 |
Correct |
19 ms |
90460 KB |
Output is correct |
7 |
Correct |
20 ms |
90412 KB |
Output is correct |
8 |
Correct |
19 ms |
90268 KB |
Output is correct |
9 |
Correct |
20 ms |
90308 KB |
Output is correct |
10 |
Correct |
21 ms |
90460 KB |
Output is correct |
11 |
Correct |
25 ms |
90276 KB |
Output is correct |
12 |
Correct |
19 ms |
90528 KB |
Output is correct |
13 |
Correct |
20 ms |
90460 KB |
Output is correct |
14 |
Correct |
23 ms |
90488 KB |
Output is correct |
15 |
Correct |
20 ms |
90460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
90456 KB |
Output is correct |
2 |
Correct |
19 ms |
90456 KB |
Output is correct |
3 |
Correct |
22 ms |
90460 KB |
Output is correct |
4 |
Correct |
19 ms |
90280 KB |
Output is correct |
5 |
Correct |
20 ms |
90460 KB |
Output is correct |
6 |
Correct |
19 ms |
90460 KB |
Output is correct |
7 |
Correct |
20 ms |
90412 KB |
Output is correct |
8 |
Correct |
19 ms |
90268 KB |
Output is correct |
9 |
Correct |
20 ms |
90308 KB |
Output is correct |
10 |
Correct |
21 ms |
90460 KB |
Output is correct |
11 |
Correct |
25 ms |
90276 KB |
Output is correct |
12 |
Correct |
19 ms |
90528 KB |
Output is correct |
13 |
Correct |
20 ms |
90460 KB |
Output is correct |
14 |
Correct |
23 ms |
90488 KB |
Output is correct |
15 |
Correct |
20 ms |
90460 KB |
Output is correct |
16 |
Correct |
21 ms |
90712 KB |
Output is correct |
17 |
Correct |
20 ms |
90456 KB |
Output is correct |
18 |
Correct |
21 ms |
90788 KB |
Output is correct |
19 |
Correct |
20 ms |
90716 KB |
Output is correct |
20 |
Correct |
20 ms |
90836 KB |
Output is correct |
21 |
Correct |
21 ms |
90712 KB |
Output is correct |
22 |
Correct |
19 ms |
90460 KB |
Output is correct |
23 |
Correct |
21 ms |
90712 KB |
Output is correct |
24 |
Correct |
24 ms |
90456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
90456 KB |
Output is correct |
2 |
Correct |
19 ms |
90456 KB |
Output is correct |
3 |
Correct |
22 ms |
90460 KB |
Output is correct |
4 |
Correct |
19 ms |
90280 KB |
Output is correct |
5 |
Correct |
20 ms |
90460 KB |
Output is correct |
6 |
Correct |
19 ms |
90460 KB |
Output is correct |
7 |
Correct |
20 ms |
90412 KB |
Output is correct |
8 |
Correct |
19 ms |
90268 KB |
Output is correct |
9 |
Correct |
20 ms |
90308 KB |
Output is correct |
10 |
Correct |
21 ms |
90460 KB |
Output is correct |
11 |
Correct |
25 ms |
90276 KB |
Output is correct |
12 |
Correct |
19 ms |
90528 KB |
Output is correct |
13 |
Correct |
20 ms |
90460 KB |
Output is correct |
14 |
Correct |
23 ms |
90488 KB |
Output is correct |
15 |
Correct |
20 ms |
90460 KB |
Output is correct |
16 |
Correct |
21 ms |
90712 KB |
Output is correct |
17 |
Correct |
20 ms |
90456 KB |
Output is correct |
18 |
Correct |
21 ms |
90788 KB |
Output is correct |
19 |
Correct |
20 ms |
90716 KB |
Output is correct |
20 |
Correct |
20 ms |
90836 KB |
Output is correct |
21 |
Correct |
21 ms |
90712 KB |
Output is correct |
22 |
Correct |
19 ms |
90460 KB |
Output is correct |
23 |
Correct |
21 ms |
90712 KB |
Output is correct |
24 |
Correct |
24 ms |
90456 KB |
Output is correct |
25 |
Correct |
21 ms |
90460 KB |
Output is correct |
26 |
Correct |
19 ms |
90476 KB |
Output is correct |
27 |
Correct |
21 ms |
90712 KB |
Output is correct |
28 |
Correct |
21 ms |
90716 KB |
Output is correct |
29 |
Correct |
21 ms |
90712 KB |
Output is correct |
30 |
Correct |
21 ms |
90712 KB |
Output is correct |
31 |
Correct |
21 ms |
90452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
90460 KB |
Output is correct |
2 |
Correct |
19 ms |
90368 KB |
Output is correct |
3 |
Correct |
19 ms |
90444 KB |
Output is correct |
4 |
Correct |
272 ms |
113816 KB |
Output is correct |
5 |
Correct |
145 ms |
109392 KB |
Output is correct |
6 |
Correct |
111 ms |
119784 KB |
Output is correct |
7 |
Correct |
49 ms |
94416 KB |
Output is correct |
8 |
Correct |
67 ms |
97948 KB |
Output is correct |
9 |
Correct |
19 ms |
90456 KB |
Output is correct |
10 |
Correct |
19 ms |
90456 KB |
Output is correct |
11 |
Correct |
22 ms |
90460 KB |
Output is correct |
12 |
Correct |
19 ms |
90280 KB |
Output is correct |
13 |
Correct |
20 ms |
90460 KB |
Output is correct |
14 |
Correct |
19 ms |
90460 KB |
Output is correct |
15 |
Correct |
20 ms |
90412 KB |
Output is correct |
16 |
Correct |
19 ms |
90268 KB |
Output is correct |
17 |
Correct |
20 ms |
90308 KB |
Output is correct |
18 |
Correct |
21 ms |
90460 KB |
Output is correct |
19 |
Correct |
25 ms |
90276 KB |
Output is correct |
20 |
Correct |
19 ms |
90528 KB |
Output is correct |
21 |
Correct |
20 ms |
90460 KB |
Output is correct |
22 |
Correct |
23 ms |
90488 KB |
Output is correct |
23 |
Correct |
20 ms |
90460 KB |
Output is correct |
24 |
Correct |
21 ms |
90712 KB |
Output is correct |
25 |
Correct |
20 ms |
90456 KB |
Output is correct |
26 |
Correct |
21 ms |
90788 KB |
Output is correct |
27 |
Correct |
20 ms |
90716 KB |
Output is correct |
28 |
Correct |
20 ms |
90836 KB |
Output is correct |
29 |
Correct |
21 ms |
90712 KB |
Output is correct |
30 |
Correct |
19 ms |
90460 KB |
Output is correct |
31 |
Correct |
21 ms |
90712 KB |
Output is correct |
32 |
Correct |
24 ms |
90456 KB |
Output is correct |
33 |
Correct |
21 ms |
90460 KB |
Output is correct |
34 |
Correct |
19 ms |
90476 KB |
Output is correct |
35 |
Correct |
21 ms |
90712 KB |
Output is correct |
36 |
Correct |
21 ms |
90716 KB |
Output is correct |
37 |
Correct |
21 ms |
90712 KB |
Output is correct |
38 |
Correct |
21 ms |
90712 KB |
Output is correct |
39 |
Correct |
21 ms |
90452 KB |
Output is correct |
40 |
Correct |
296 ms |
118412 KB |
Output is correct |
41 |
Correct |
168 ms |
111700 KB |
Output is correct |
42 |
Correct |
215 ms |
121540 KB |
Output is correct |
43 |
Correct |
209 ms |
117992 KB |
Output is correct |
44 |
Correct |
129 ms |
122332 KB |
Output is correct |
45 |
Correct |
158 ms |
118524 KB |
Output is correct |
46 |
Correct |
62 ms |
97632 KB |
Output is correct |
47 |
Correct |
150 ms |
111104 KB |
Output is correct |
48 |
Correct |
174 ms |
108116 KB |
Output is correct |