제출 #966649

#제출 시각아이디문제언어결과실행 시간메모리
966649aegEvent Hopping (BOI22_events)C++14
100 / 100
84 ms13396 KiB
    #include <bits/stdc++.h>
    using namespace std;
     
    const int N = 1e5 + 10 , L = 18;
    int n , q , l[N] , r[N] , mv[L][N];
    vector <int> vec , st;
     
    bool cmp(int a , int b) {
    	if(r[a] != r[b]) return r[a] < r[b];
    	return l[a] < l[b];
    }
     
    int Bs(int x){
    	int low = -1 , high = st.size();
    	while(high - low > 1) {
    		int mid = (low + high) >> 1;
    		if(r[st[mid]] >= x) high = mid;
    		else low = mid;
    	}
    	return st[high];
    }
     
    int solve(int from , int to , int bit) {
    	if(bit == -1) return 0;
    	int nex = mv[bit][from];
    	if(l[nex] > r[to]) return (1 << bit) + solve(nex , to , bit - 1);
    	return solve(from , to , bit - 1);
    }
     
    signed main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0);  cout.tie(0);
    	cin >> n >> q;
    	for(int i = 1 ; i <= n ; i++) {
    		cin >> l[i] >> r[i];
    		vec.push_back(i);
    	}
    	sort(vec.begin() , vec.end() , cmp);
    	for(int i = 1 ; i <= n ; i++) {
    		int now = vec[i];
    		while(!st.empty() && l[st.back()] >= l[now]) st.pop_back();
    		st.push_back(now);
    		mv[0][now] = Bs(l[now]);
    	}
    	for(int i = 1 ; i < L ; i++)   for(int j = 1 ; j <= n ; j++) mv[i][j] = mv[i - 1][mv[i - 1][j]];
    	while(q--) {
    		int s , e;  cin >> s >> e;
    		if(l[mv[L - 1][e]] > r[s] || r[e] < r[s]) {
    			cout << "impossible" << '\n';
    			continue;
    		}
    		if(s == e) {
    			cout << 0 << '\n';
    			continue;
    		}
    		if(l[e] <= r[s]) {
    			cout << 1 << '\n';
    			continue;
    		}
    		cout << solve(e , s , L - 1) + 2 << '\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...