답안 #603168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
603168 2022-07-23T16:26:14 Z DanerZein Event Hopping (BOI22_events) C++14
0 / 100
230 ms 8292 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int MAX_N=1e5+10;
const int MAX=1e9;
vector<vi> G;
vii ev;
int dis[MAX_N];
int n;
bool isq[MAX_N];
void bfs(int u){
  for(int i=0;i<n;i++) dis[i]=MAX;
  dis[u]=0; isq[u]=1;
  queue<int> q; q.push(u);
  while(!q.empty()){
    int x=q.front(); q.pop();
    isq[x]=0;
    for(auto &v:G[x]){
      if(dis[v]>dis[x]+1){
	dis[v]=dis[x]+1;
	if(!isq[v]){
	  isq[v]=1;
	  q.push(v);
	}
      }
    }
  }
}
int in[MAX_N];
int pa[MAX_N][32];
int le[MAX_N];
void dfs(int u,int p){
  pa[u][0]=p;
  for(int i=1;i<=20;i++){
    pa[u][i]=pa[pa[u][i-1]][i-1];
  }
  for(auto &v:G[u]){
    if(v!=u) {
      le[v]=le[u]+1;
      dfs(v,u);
    }
  }
}
bool father(int u,int v){
  int dif=le[u]-le[v];
  if(dif<0) return -1;
  for(int i=0;i<=20;i++){
    if(dif&(1<<i)) u=pa[u][i];
  }
  if(u!=v) return -1;
  return dif;
}
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  int q; cin>>n>>q;
  vii enp;
  for(int i=0;i<n;i++){
    int a,b; cin>>a>>b;
    ev.push_back(ii(a,b));
    enp.push_back(ii(b,i));
  }
  sort(enp.begin(),enp.end());
  G.resize(n+1);
  for(int i=0;i<n;i++){
    auto l=lower_bound(enp.begin(),enp.end(),ii(ev[i].first,0));
    auto r=upper_bound(enp.begin(),enp.end(),ii(ev[i].second,0));
    for(int i=(l-enp.begin());i<=(r-enp.begin());i++){
      G[i].push_back(enp[i].second);
      in[enp[i].second]=1;
    }
  }
  for(int i=0;i<n;i++){
    if(!in[i]) dfs(i,i);
  }
  
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(ev[j].first<=ev[i].second && ev[i].second<=ev[j].second){
	G[i].push_back(j);
      }
    }
    }*/
  while(q--){
    int s,e; cin>>s>>e;
    s--; e--;
    int ans=father(s,e);
    if(ans!=-1) cout<<ans<<endl;
    else cout<<"impossible\n";
      /*bfs(s);
    if(dis[e]>=MAX) cout<<"impossible\n";
    else cout<<dis[e]<<endl;*/
  }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 230 ms 8292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -