This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#define S 262144
using namespace std;
vector<pair<int,int>> V[4*S];
bool vis[4*S][3];
int dist[4*S][3];
void BFS(int x, int t){
deque<pair<int,int>> q;
q.push_front({x,0});
while(!q.empty()){
int v = q.front().first;
int d = q.front().second;
q.pop_front();
if(vis[v][t])
continue;
vis[v][t] = 1;
dist[v][t] = d;
for(int i = 0; i < V[v].size(); i++){
int u = V[v][i].first;
int d2 = V[v][i].second;
if(d2 == 0){
q.push_front({u,d});
}else{
q.push_back({u,d+1});
}
}
}
}
void addEdge(int l, int r, int ll, int rr, int w, int v){
if(l > rr || r < ll)
return;
if(l >= ll && r <= rr){
V[w].push_back({v,1});
return;
}
int sr = (l+r)/2;
addEdge(l,sr,ll,rr,w*2,v);
addEdge(sr+1,r,ll,rr,w*2+1,v);
}
int ans[S];
int tree[4*S];
void sett(int w, int x){
w = w+S-1;
while(w != 0){
tree[w] = min(tree[w], x);
w /= 2;
}
}
int getMin(int l, int r, int ll, int rr, int w){
if(l > rr || r < ll)
return 1e9;
if(l >= ll && r <= rr){
return tree[w];
}
int sr = (l+r)/2;
return min(getMin(l,sr,ll,rr,w*2), getMin(sr+1,r,ll,rr,w*2+1));
}
int L[S];
int R[S];
int main(void){
for(int i = 2; i < 2*S; i++){
V[i].push_back({i/2,0});
}
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++){
int l, r;
scanf("%d %d",&l,&r);
L[i] = l;
R[i] = r;
addEdge(1,S,l,r,1,2*S+i);
}
for(int i = 1; i <= n; i++){
V[2*S+i].push_back({i+S-1,0});
}
BFS(2*S+1,1);
BFS(2*S+n,2);
dist[2*S+1][1] = 1;
dist[2*S+n][2] = 1;
for(int i = 1; i < 2*S;i++){
tree[i] = 1e9;
}
vector<pair<int,int>> P;
for(int i = 1; i <= n; i++){
if(!vis[2*S+i][1] || !vis[2*S+i][2]){
ans[i] = 1e9;
}else{
ans[i] = dist[2*S+i][1] + dist[2*S+i][2] - 1;
}
sett(i, ans[i]);
if(ans[i] < 1e8)
P.push_back({ans[i], i});
}
sort(P.begin(), P.end());
for(int i = 0; i < P.size(); i++){
int v = P[i].second;
// printf("%d %d*\n",v,ans[v]);
ans[v] = min(ans[v], 1 + getMin(1,S,L[v], R[v], 1));
sett(v,ans[v]);
// printf("%d %d$\n",v, ans[v]);
}
int q;
scanf("%d", &q);
for(int i = 1; i <= q; i++){
int x;
scanf("%d", &x);
if(ans[x] >= 1e8)
ans[x] = -1;
printf("%d\n",ans[x]);
}
// for(int i = 1; i <= n; i++){
// printf("%d %d\n", dist[2*S+i][1], dist[2*S+i][2]);
// }
return 0;
}
Compilation message (stderr)
passport.cpp: In function 'void BFS(int, int)':
passport.cpp:25:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i = 0; i < V[v].size(); i++){
| ~~^~~~~~~~~~~~~
passport.cpp: In function 'int main()':
passport.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i = 0; i < P.size(); i++){
| ~~^~~~~~~~~~
passport.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
passport.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | scanf("%d %d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~
passport.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
passport.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |