#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
using lint = long long;
using vint = vector<int>;
using pii = pair<int,int>;
const int MAX_N=200010;
int n,q;
int la[MAX_N],ra[MAX_N];
int vi[MAX_N<<2];
vint edge[MAX_N<<2][2];
int dt[MAX_N<<2];
int idx[MAX_N];
int seg[MAX_N<<2];
vint bfs[MAX_N];
void init_edge(int i,int s,int e)
{
vi[i]=(s+1==e) ? s : i+n;
if(i>1)
edge[vi[i]][0].push_back(vi[i>>1]);
if(s+1==e)return;
init_edge(i<<1,s,(s+e)>>1);
init_edge(i<<1|1,(s+e)>>1,e);
}
void update_edge(int i,int s,int e,int l,int r,int v)
{
if(s>=r || e<=l)return;
if(l<=s && e<=r)
{
edge[vi[i]][1].push_back(v);
return;
}
update_edge(i<<1,s,(s+e)>>1,l,r,v);
update_edge(i<<1|1,(s+e)>>1,e,l,r,v);
}
void init_seg(int i,int s,int e)
{
seg[i]=MAX_N;
if(s+1==e)return;
init_seg(i<<1,s,(s+e)>>1);
init_seg(i<<1|1,(s+e)>>1,e);
}
void update_(int i,int s,int e,int x,int v)
{
if(s>x || e<=x)return;
if(s==x && x+1==e)
{
seg[i]=v;
return;
}
update_(i<<1,s,(s+e)>>1,x,v);
update_(i<<1|1,(s+e)>>1,e,x,v);
seg[i]=min(seg[i<<1],seg[i<<1|1]);
}
int find_(int i,int s,int e,int l,int r)
{
if(s>=r || e<=l)return MAX_N;
if(l<=s && e<=r)return seg[i];
return min(find_(i<<1,s,(s+e)>>1,l,r),find_(i<<1|1,(s+e)>>1,e,l,r));
}
bool cpl(const int &i,const int &j)
{
return la[i]<la[j];
}
bool cpr(const int &i,const int &j)
{
return ra[i]>ra[j];
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
init_edge(1,0,n);
for(int i=0;i<n;i++)
{
cin >> la[i] >> ra[i];
la[i]--;
update_edge(1,0,n,la[i],ra[i],i);
idx[i]=i;
}
fill(dt,dt+(MAX_N<<2),MAX_N);
fill(dt,dt+n,1);
for(int t=0;t<2;t++)
{
init_seg(1,0,n);
if(t)sort(idx,idx+n,cpr);
else sort(idx,idx+n,cpl);
for(int j=0;j<n;j++)
{
int i=idx[j],v;
if((!t && la[i]==0) || (t && ra[i]==n))v=0;
else v=find_(1,0,n,la[i],ra[i]);
dt[i]+=v;
update_(1,0,n,i,v+1);
}
}
for(int i=0;i<n;i++)
if(dt[i]<MAX_N)bfs[dt[i]].push_back(i);
for(int d=1;d<=n;d++)
{
while(!bfs[d].empty())
{
int p=bfs[d].back();
bfs[d].pop_back();
if(dt[p]<d)continue;
for(auto v : edge[p][0])
if(dt[v]>d)
{
dt[v]=d;
bfs[d].push_back(v);
}
for(auto v : edge[p][1])
if(dt[v]>d+1)
{
dt[v]=d+1;
bfs[d+1].push_back(v);
}
}
}
cin >> q;
while(q--)
{
int i;
cin >> i;i--;
cout << (dt[i]>=MAX_N ? -1 : dt[i]) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
51960 KB |
Output is correct |
2 |
Correct |
8 ms |
51688 KB |
Output is correct |
3 |
Correct |
9 ms |
51928 KB |
Output is correct |
4 |
Correct |
437 ms |
88172 KB |
Output is correct |
5 |
Correct |
258 ms |
78612 KB |
Output is correct |
6 |
Correct |
187 ms |
78288 KB |
Output is correct |
7 |
Correct |
105 ms |
68832 KB |
Output is correct |
8 |
Correct |
93 ms |
64932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51800 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
8 ms |
51804 KB |
Output is correct |
4 |
Correct |
7 ms |
51804 KB |
Output is correct |
5 |
Correct |
7 ms |
51804 KB |
Output is correct |
6 |
Correct |
7 ms |
51800 KB |
Output is correct |
7 |
Correct |
8 ms |
51888 KB |
Output is correct |
8 |
Correct |
7 ms |
51800 KB |
Output is correct |
9 |
Correct |
7 ms |
51804 KB |
Output is correct |
10 |
Correct |
8 ms |
51932 KB |
Output is correct |
11 |
Correct |
8 ms |
51928 KB |
Output is correct |
12 |
Correct |
8 ms |
51804 KB |
Output is correct |
13 |
Correct |
8 ms |
51804 KB |
Output is correct |
14 |
Correct |
8 ms |
51804 KB |
Output is correct |
15 |
Correct |
8 ms |
51804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51800 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
8 ms |
51804 KB |
Output is correct |
4 |
Correct |
7 ms |
51804 KB |
Output is correct |
5 |
Correct |
7 ms |
51804 KB |
Output is correct |
6 |
Correct |
7 ms |
51800 KB |
Output is correct |
7 |
Correct |
8 ms |
51888 KB |
Output is correct |
8 |
Correct |
7 ms |
51800 KB |
Output is correct |
9 |
Correct |
7 ms |
51804 KB |
Output is correct |
10 |
Correct |
8 ms |
51932 KB |
Output is correct |
11 |
Correct |
8 ms |
51928 KB |
Output is correct |
12 |
Correct |
8 ms |
51804 KB |
Output is correct |
13 |
Correct |
8 ms |
51804 KB |
Output is correct |
14 |
Correct |
8 ms |
51804 KB |
Output is correct |
15 |
Correct |
8 ms |
51804 KB |
Output is correct |
16 |
Correct |
12 ms |
52060 KB |
Output is correct |
17 |
Correct |
11 ms |
52188 KB |
Output is correct |
18 |
Correct |
11 ms |
52316 KB |
Output is correct |
19 |
Correct |
10 ms |
52060 KB |
Output is correct |
20 |
Correct |
11 ms |
52060 KB |
Output is correct |
21 |
Correct |
9 ms |
52196 KB |
Output is correct |
22 |
Correct |
8 ms |
52060 KB |
Output is correct |
23 |
Correct |
10 ms |
52176 KB |
Output is correct |
24 |
Correct |
11 ms |
52312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51800 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
8 ms |
51804 KB |
Output is correct |
4 |
Correct |
7 ms |
51804 KB |
Output is correct |
5 |
Correct |
7 ms |
51804 KB |
Output is correct |
6 |
Correct |
7 ms |
51800 KB |
Output is correct |
7 |
Correct |
8 ms |
51888 KB |
Output is correct |
8 |
Correct |
7 ms |
51800 KB |
Output is correct |
9 |
Correct |
7 ms |
51804 KB |
Output is correct |
10 |
Correct |
8 ms |
51932 KB |
Output is correct |
11 |
Correct |
8 ms |
51928 KB |
Output is correct |
12 |
Correct |
8 ms |
51804 KB |
Output is correct |
13 |
Correct |
8 ms |
51804 KB |
Output is correct |
14 |
Correct |
8 ms |
51804 KB |
Output is correct |
15 |
Correct |
8 ms |
51804 KB |
Output is correct |
16 |
Correct |
12 ms |
52060 KB |
Output is correct |
17 |
Correct |
11 ms |
52188 KB |
Output is correct |
18 |
Correct |
11 ms |
52316 KB |
Output is correct |
19 |
Correct |
10 ms |
52060 KB |
Output is correct |
20 |
Correct |
11 ms |
52060 KB |
Output is correct |
21 |
Correct |
9 ms |
52196 KB |
Output is correct |
22 |
Correct |
8 ms |
52060 KB |
Output is correct |
23 |
Correct |
10 ms |
52176 KB |
Output is correct |
24 |
Correct |
11 ms |
52312 KB |
Output is correct |
25 |
Correct |
8 ms |
51800 KB |
Output is correct |
26 |
Correct |
9 ms |
51800 KB |
Output is correct |
27 |
Correct |
12 ms |
52304 KB |
Output is correct |
28 |
Correct |
10 ms |
52060 KB |
Output is correct |
29 |
Correct |
10 ms |
52216 KB |
Output is correct |
30 |
Correct |
10 ms |
52056 KB |
Output is correct |
31 |
Correct |
9 ms |
52056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
51960 KB |
Output is correct |
2 |
Correct |
8 ms |
51688 KB |
Output is correct |
3 |
Correct |
9 ms |
51928 KB |
Output is correct |
4 |
Correct |
437 ms |
88172 KB |
Output is correct |
5 |
Correct |
258 ms |
78612 KB |
Output is correct |
6 |
Correct |
187 ms |
78288 KB |
Output is correct |
7 |
Correct |
105 ms |
68832 KB |
Output is correct |
8 |
Correct |
93 ms |
64932 KB |
Output is correct |
9 |
Correct |
8 ms |
51800 KB |
Output is correct |
10 |
Correct |
7 ms |
51804 KB |
Output is correct |
11 |
Correct |
8 ms |
51804 KB |
Output is correct |
12 |
Correct |
7 ms |
51804 KB |
Output is correct |
13 |
Correct |
7 ms |
51804 KB |
Output is correct |
14 |
Correct |
7 ms |
51800 KB |
Output is correct |
15 |
Correct |
8 ms |
51888 KB |
Output is correct |
16 |
Correct |
7 ms |
51800 KB |
Output is correct |
17 |
Correct |
7 ms |
51804 KB |
Output is correct |
18 |
Correct |
8 ms |
51932 KB |
Output is correct |
19 |
Correct |
8 ms |
51928 KB |
Output is correct |
20 |
Correct |
8 ms |
51804 KB |
Output is correct |
21 |
Correct |
8 ms |
51804 KB |
Output is correct |
22 |
Correct |
8 ms |
51804 KB |
Output is correct |
23 |
Correct |
8 ms |
51804 KB |
Output is correct |
24 |
Correct |
12 ms |
52060 KB |
Output is correct |
25 |
Correct |
11 ms |
52188 KB |
Output is correct |
26 |
Correct |
11 ms |
52316 KB |
Output is correct |
27 |
Correct |
10 ms |
52060 KB |
Output is correct |
28 |
Correct |
11 ms |
52060 KB |
Output is correct |
29 |
Correct |
9 ms |
52196 KB |
Output is correct |
30 |
Correct |
8 ms |
52060 KB |
Output is correct |
31 |
Correct |
10 ms |
52176 KB |
Output is correct |
32 |
Correct |
11 ms |
52312 KB |
Output is correct |
33 |
Correct |
8 ms |
51800 KB |
Output is correct |
34 |
Correct |
9 ms |
51800 KB |
Output is correct |
35 |
Correct |
12 ms |
52304 KB |
Output is correct |
36 |
Correct |
10 ms |
52060 KB |
Output is correct |
37 |
Correct |
10 ms |
52216 KB |
Output is correct |
38 |
Correct |
10 ms |
52056 KB |
Output is correct |
39 |
Correct |
9 ms |
52056 KB |
Output is correct |
40 |
Correct |
459 ms |
92372 KB |
Output is correct |
41 |
Correct |
267 ms |
83796 KB |
Output is correct |
42 |
Correct |
357 ms |
94264 KB |
Output is correct |
43 |
Correct |
341 ms |
92936 KB |
Output is correct |
44 |
Correct |
217 ms |
83344 KB |
Output is correct |
45 |
Correct |
267 ms |
86352 KB |
Output is correct |
46 |
Correct |
88 ms |
64340 KB |
Output is correct |
47 |
Correct |
237 ms |
84472 KB |
Output is correct |
48 |
Correct |
315 ms |
83668 KB |
Output is correct |