제출 #600933

#제출 시각아이디문제언어결과실행 시간메모리
600933CSQ31Event Hopping (BOI22_events)C++17
0 / 100
1576 ms2272 KiB
#include <bits/stdc++.h> using namespace std; #define all(a) a.begin(),a.end() #define owo ios_base::sync_with_stdio(0);cin.tie(0); const int MAXN = 1e5+1; int s[MAXN],e[MAXN]; bool check(int i,int j){ //got edge i->j? return s[j] <= e[i] && e[i] <= e[j]; } int main() { owo int n,q; cin>>n>>q; vector<int>crd; for(int i=0;i<n;i++)cin>>s[i]>>e[i]; for(int i=0;i<n;i++){ crd.push_back(s[i]); crd.push_back(e[i]); } sort(all(crd)); crd.resize(unique(all(crd)) - crd.begin()); for(int i=0;i<n;i++){ s[i] = lower_bound(all(crd),s[i]) - crd.begin()+1; e[i] = lower_bound(all(crd),e[i]) - crd.begin()+1; } vector<int>pre(n); for(int i=0;i<n;i++){ pre[i] = i; for(int j=0;j<n;j++){ if(check(i,j) && e[j] > e[pre[i]])pre[i] = j; } } //cout<<'\n'; //binary lift to the nearest guy with e[j] < e[target] //three case //1)can reach target,ans+1 //2)no guy touches target range ,impossible //3)some guy touches ans+2 //always optimal to go max guy if e[j] < e[target] //for(int i=0;i<n;i++)cout<<pre[i]<<" "; //cout<<'\n'; while(q--){ int l,r; cin>>l>>r; l--; r--; int ans = 0; while(l != r){ if(check(l,r)){ l=r; ans++; break; } if(e[l] == e[pre[l]])break; if(e[pre[l]] < e[r])l = pre[l]; else break; ans++; } if(l!=r){ for(int i=0;i<n;i++){ if(check(l,i) && check(i,r)){ ans+=2; l = r; break; } } } if(l!=r)cout<<"impossible"<<'\n'; else cout<<ans<<'\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...