#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(100001,1000000);
vector<int> dp2(100001,1000000);
vector<int> dp(100001,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 |
20 ms |
89180 KB |
Output is correct |
2 |
Correct |
20 ms |
89120 KB |
Output is correct |
3 |
Correct |
20 ms |
89180 KB |
Output is correct |
4 |
Runtime error |
323 ms |
229708 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
89180 KB |
Output is correct |
2 |
Correct |
20 ms |
89320 KB |
Output is correct |
3 |
Correct |
21 ms |
89180 KB |
Output is correct |
4 |
Correct |
20 ms |
89180 KB |
Output is correct |
5 |
Correct |
19 ms |
89080 KB |
Output is correct |
6 |
Correct |
20 ms |
89316 KB |
Output is correct |
7 |
Correct |
20 ms |
89180 KB |
Output is correct |
8 |
Correct |
20 ms |
89180 KB |
Output is correct |
9 |
Correct |
20 ms |
89180 KB |
Output is correct |
10 |
Correct |
21 ms |
89124 KB |
Output is correct |
11 |
Correct |
20 ms |
89108 KB |
Output is correct |
12 |
Correct |
20 ms |
89348 KB |
Output is correct |
13 |
Correct |
22 ms |
89180 KB |
Output is correct |
14 |
Correct |
20 ms |
89260 KB |
Output is correct |
15 |
Correct |
20 ms |
89360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
89180 KB |
Output is correct |
2 |
Correct |
20 ms |
89320 KB |
Output is correct |
3 |
Correct |
21 ms |
89180 KB |
Output is correct |
4 |
Correct |
20 ms |
89180 KB |
Output is correct |
5 |
Correct |
19 ms |
89080 KB |
Output is correct |
6 |
Correct |
20 ms |
89316 KB |
Output is correct |
7 |
Correct |
20 ms |
89180 KB |
Output is correct |
8 |
Correct |
20 ms |
89180 KB |
Output is correct |
9 |
Correct |
20 ms |
89180 KB |
Output is correct |
10 |
Correct |
21 ms |
89124 KB |
Output is correct |
11 |
Correct |
20 ms |
89108 KB |
Output is correct |
12 |
Correct |
20 ms |
89348 KB |
Output is correct |
13 |
Correct |
22 ms |
89180 KB |
Output is correct |
14 |
Correct |
20 ms |
89260 KB |
Output is correct |
15 |
Correct |
20 ms |
89360 KB |
Output is correct |
16 |
Correct |
25 ms |
89468 KB |
Output is correct |
17 |
Correct |
22 ms |
89436 KB |
Output is correct |
18 |
Correct |
21 ms |
89436 KB |
Output is correct |
19 |
Correct |
22 ms |
89596 KB |
Output is correct |
20 |
Correct |
24 ms |
89692 KB |
Output is correct |
21 |
Correct |
21 ms |
89436 KB |
Output is correct |
22 |
Correct |
23 ms |
89180 KB |
Output is correct |
23 |
Correct |
21 ms |
89436 KB |
Output is correct |
24 |
Correct |
22 ms |
89412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
89180 KB |
Output is correct |
2 |
Correct |
20 ms |
89320 KB |
Output is correct |
3 |
Correct |
21 ms |
89180 KB |
Output is correct |
4 |
Correct |
20 ms |
89180 KB |
Output is correct |
5 |
Correct |
19 ms |
89080 KB |
Output is correct |
6 |
Correct |
20 ms |
89316 KB |
Output is correct |
7 |
Correct |
20 ms |
89180 KB |
Output is correct |
8 |
Correct |
20 ms |
89180 KB |
Output is correct |
9 |
Correct |
20 ms |
89180 KB |
Output is correct |
10 |
Correct |
21 ms |
89124 KB |
Output is correct |
11 |
Correct |
20 ms |
89108 KB |
Output is correct |
12 |
Correct |
20 ms |
89348 KB |
Output is correct |
13 |
Correct |
22 ms |
89180 KB |
Output is correct |
14 |
Correct |
20 ms |
89260 KB |
Output is correct |
15 |
Correct |
20 ms |
89360 KB |
Output is correct |
16 |
Correct |
25 ms |
89468 KB |
Output is correct |
17 |
Correct |
22 ms |
89436 KB |
Output is correct |
18 |
Correct |
21 ms |
89436 KB |
Output is correct |
19 |
Correct |
22 ms |
89596 KB |
Output is correct |
20 |
Correct |
24 ms |
89692 KB |
Output is correct |
21 |
Correct |
21 ms |
89436 KB |
Output is correct |
22 |
Correct |
23 ms |
89180 KB |
Output is correct |
23 |
Correct |
21 ms |
89436 KB |
Output is correct |
24 |
Correct |
22 ms |
89412 KB |
Output is correct |
25 |
Correct |
20 ms |
89180 KB |
Output is correct |
26 |
Correct |
20 ms |
89180 KB |
Output is correct |
27 |
Correct |
24 ms |
89372 KB |
Output is correct |
28 |
Correct |
21 ms |
89436 KB |
Output is correct |
29 |
Correct |
21 ms |
89692 KB |
Output is correct |
30 |
Correct |
21 ms |
89428 KB |
Output is correct |
31 |
Correct |
21 ms |
89436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
89180 KB |
Output is correct |
2 |
Correct |
20 ms |
89120 KB |
Output is correct |
3 |
Correct |
20 ms |
89180 KB |
Output is correct |
4 |
Runtime error |
323 ms |
229708 KB |
Execution killed with signal 6 |
5 |
Halted |
0 ms |
0 KB |
- |