#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int INF = 1e18;
vector<pair<int,int>> seg;
vector<int> ans;
vector<int> dpl,dpr;
vector<int> ind;
vector<pair<int,int>> segTree;
vector<int> vis;
vector<int> d;
vector<pair<int,int>> segTree2;
map<int,set<int>> id;
void modify(int i,int x,int l,int r,int indV)
{
if(l > i || r < i)
return ;
else if(l == r)
{
segTree[indV] = {x,i};
return ;
}
int m = (l+r)/2;
modify(i,x,l,m,indV*2+1);
modify(i,x,m+1,r,indV*2+2);
segTree[indV] = min(segTree[indV*2+1],segTree[indV*2+2]);
return ;
}
pair<int,int> get(int lx,int rx,int l,int r,int indV)
{
if(l > rx || r < lx)
return {INF,-1};
else if(l >= lx && r <= rx)
{
return segTree[indV];
}
int m = (l+r)/2;
pair<int,int> sl = get(lx,rx,l,m,indV*2+1);
pair<int,int> sr = get(lx,rx,m+1,r,indV*2+2);
return min(sl,sr);
}
void modify2(int i,int x,int l,int r,int indV)
{
if(l > i || r < i)
return ;
else if(l == r)
{
segTree2[indV] = {x,i};
return ;
}
int m = (l+r)/2;
modify2(i,x,l,m,indV*2+1);
modify2(i,x,m+1,r,indV*2+2);
segTree2[indV] = max(segTree2[indV*2+1],segTree2[indV*2+2]);
return ;
}
pair<int,int> get2(int lx,int rx,int l,int r,int indV)
{
// cout << l << ' ' << r << ' ' << segTree2[indV].first << ' ' << segTree2[indV].second << "\n";
if(l > rx || r < lx)
return {-1,-1};
else if(l >= lx && r <= rx)
{
return segTree2[indV];
}
int m = (l+r)/2;
pair<int,int> sl = get2(lx,rx,l,m,indV*2+1);
pair<int,int> sr = get2(lx,rx,m+1,r,indV*2+2);
return max(sl,sr);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
seg.resize(n);
ind.resize(n);
for(int i = 0;i < n;++i)
{
ind[i] = i;
cin >> seg[i].first >> seg[i].second;
seg[i].first--;
seg[i].second--;
}
ans.resize(n);
for(int i = 0;i < n;++i)
ans[i] = -2;
dpl.resize(n);
dpr.resize(n);
sort(ind.begin(),ind.end(),[&](int a,int b){return seg[a].first < seg[b].first;});
segTree.resize(4*n);
for(int i = 0;i < 4*n;++i)
{
segTree[i] = {INF,-1};
}
for(int i = 0;i < n;++i)
{
if(seg[ind[i]].first == 0)
{
dpl[ind[i]] = 0;
}
else
{
dpl[ind[i]] = get(seg[ind[i]].first,seg[ind[i]].second,0,n-1,0).first+1;
}
modify(ind[i],dpl[ind[i]],0,n-1,0);
}
sort(ind.begin(),ind.end(),[&](int a,int b){return seg[a].second > seg[b].second;});
segTree.clear();
segTree.resize(4*n);
for(int i = 0;i < 4*n;++i)
{
segTree[i] = {INF,-1};
}
for(int i = 0;i < n;++i)
{
if(seg[ind[i]].second == n-1)
{
dpr[ind[i]] = 0;
}
else
{
dpr[ind[i]] = get(seg[ind[i]].first,seg[ind[i]].second,0,n-1,0).first+1;
}
modify(ind[i],dpr[ind[i]],0,n-1,0);
}
vis.resize(n);
for(int i = 0;i < n;++i)
{
vis[i] = 0;
}
d.resize(n);
//for(int i = 0;i < n;++i)
// {
// cout << dpl[i] << ' ' << dpr[i] << "\n";
// }
for(int i = 0;i < n;++i)
d[i] = dpl[i] + dpr[i];
segTree.clear();
segTree.resize(4*n);
for(int i = 0;i < n;++i)
{
modify(i,seg[i].first,0,n-1,0);
}
segTree2.resize(4*n);
for(int i = 0;i < n;++i)
{
modify2(i,seg[i].second,0,n-1,0);
}
for(int i = 0;i < n;++i)
{
id[d[i]].insert(i);
}
while(id.size())
{
set<int> cc = (*id.begin()).second;
if(cc.size() == 0)
{
id.erase(id.begin());
continue;
}
int td = d[*cc.begin()];
if(td >= INF)
{
break;
}
id.erase(id.begin());
vector<int> t;
for(auto v:cc)
{
// cout << v << ' ';
if(!vis[v])
{
vis[v] = 1;
modify(v,INF,0,n-1,0);
modify2(v,-1,0,n-1,0);
}
ans[v] = d[v];
t.push_back(v);
}
//cout << "\n";
int old = 0;
for(int i = 0;i < t.size();++i)
{
while(true)
{
pair<int,int> y = get2(old,t[i]-1,0,n-1,0);
//cout << old << ' ' << t[i] << ' ' << y.first << ' ' << y.second << endl;
if(y.first < t[i])
break;
vis[y.second] = 1;
id[d[y.second]].erase(y.second);
d[y.second] = td+1;
id[d[y.second]].insert(y.second);
modify(y.second,INF,0,n-1,0);
modify2(y.second,-1,0,n-1,0);
}
old = t[i]+1;
}
old = n-1;
for(int i = int(t.size())-1;i >= 0;--i)
{
while(true)
{
//cout << "!";
pair<int,int> y = get(t[i]+1,old,0,n-1,0);
if(y.first > t[i])
break;
vis[y.second] = 1;
id[d[y.second]].erase(y.second);
d[y.second] = td+1;
id[d[y.second]].insert(y.second);
modify(y.second,INF,0,n-1,0);
modify2(y.second,-1,0,n-1,0);
}
old = t[i]-1;
}
}
int q;
cin >> q;
while(q--)
{
int x;
cin >> x;
x--;
cout << ans[x]+1 << "\n";
}
return 0;
}
Compilation message
passport.cpp: In function 'int main()':
passport.cpp:193:25: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
193 | for(int i = 0;i < t.size();++i)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
636 ms |
53284 KB |
Output is correct |
5 |
Correct |
440 ms |
47032 KB |
Output is correct |
6 |
Correct |
353 ms |
59216 KB |
Output is correct |
7 |
Correct |
286 ms |
56916 KB |
Output is correct |
8 |
Correct |
169 ms |
46028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
560 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
376 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
560 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
376 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
5 ms |
976 KB |
Output is correct |
18 |
Correct |
6 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
560 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
376 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
5 ms |
976 KB |
Output is correct |
18 |
Correct |
6 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
856 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1116 KB |
Output is correct |
23 |
Correct |
5 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
6 ms |
1116 KB |
Output is correct |
28 |
Correct |
5 ms |
1116 KB |
Output is correct |
29 |
Correct |
4 ms |
1116 KB |
Output is correct |
30 |
Correct |
4 ms |
1116 KB |
Output is correct |
31 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
636 ms |
53284 KB |
Output is correct |
5 |
Correct |
440 ms |
47032 KB |
Output is correct |
6 |
Correct |
353 ms |
59216 KB |
Output is correct |
7 |
Correct |
286 ms |
56916 KB |
Output is correct |
8 |
Correct |
169 ms |
46028 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
560 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
376 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
5 ms |
1116 KB |
Output is correct |
25 |
Correct |
5 ms |
976 KB |
Output is correct |
26 |
Correct |
6 ms |
860 KB |
Output is correct |
27 |
Correct |
5 ms |
856 KB |
Output is correct |
28 |
Correct |
4 ms |
1116 KB |
Output is correct |
29 |
Correct |
4 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
5 ms |
1116 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
6 ms |
1116 KB |
Output is correct |
36 |
Correct |
5 ms |
1116 KB |
Output is correct |
37 |
Correct |
4 ms |
1116 KB |
Output is correct |
38 |
Correct |
4 ms |
1116 KB |
Output is correct |
39 |
Correct |
4 ms |
1116 KB |
Output is correct |
40 |
Correct |
680 ms |
56404 KB |
Output is correct |
41 |
Correct |
476 ms |
52444 KB |
Output is correct |
42 |
Correct |
558 ms |
53332 KB |
Output is correct |
43 |
Correct |
538 ms |
55636 KB |
Output is correct |
44 |
Correct |
365 ms |
62036 KB |
Output is correct |
45 |
Correct |
405 ms |
56844 KB |
Output is correct |
46 |
Correct |
157 ms |
21120 KB |
Output is correct |
47 |
Correct |
421 ms |
55648 KB |
Output is correct |
48 |
Correct |
501 ms |
56148 KB |
Output is correct |