제출 #1315957

#제출 시각아이디문제언어결과실행 시간메모리
1315957vlomaczkEvent Hopping (BOI22_events)C++20
10 / 100
1601 ms589824 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n, q;
	cin >> n >> q;
	ll inf = 1e9;
	vector<vector<int>> ans(n, vector<int>(n, inf));
	vector<pair<int, int>> seg(n);
	for(int i=0; i<n; ++i) cin >> seg[i].first >> seg[i].second;
	queue<int> Q;
	for(int s=0; s<n; ++s) {
		ans[s][s] = 0;
		Q.push(s);
		while(Q.size())  {
			int v = Q.front(); Q.pop();
			for(int j=0; j<n; ++j) {
				if(seg[j].first <= seg[v].second && seg[v].second <= seg[j].second) {
					if(ans[s][j] == inf) {
						ans[s][j] = ans[s][v] + 1;
						Q.push(j);
					}
				}
			}
		}
	}
	while(q--) {
		int a, b;
		cin >> a >> b;
		--a;--b;
		if(ans[a][b]==inf) cout << "impossible\n";
		else cout << ans[a][b] << "\n";
	}

	return 0;
}
#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...