답안 #599961

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
599961 2022-07-20T10:48:23 Z TimDee Event Hopping (BOI22_events) C++17
0 / 100
135 ms 104640 KB
//#pragma GCC optimize("O1")
//#pragma GCC optimize("O2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define forn(i,n) for (int i=0; i<n; ++i)
#define pb(x) push_back(x);
#define f first
#define s second

void solve() {
	int n, m; cin>>n>>m;
	vector<pair<int,int>> a(n);
	forn(i,n) cin>>a[i].first>>a[i].second;
	//forn(i,n) a[i].f=i+1, a[i].s=i+n+1;
	vector<vector<int>> adj(n);
	if (n<=5000) {

		forn(i,n) {
			forn(j,n) {
				if (i==j) continue;
				if (a[j].f<=a[i].s && a[i].s<=a[j].s) adj[i].pb(j);
			}
		}

	} else {

		set<pair<int,int>> s;
		forn(i,n) s.insert({a[i].s,i});

		auto p = s.end(); --p;
		for (;;--p) {
			auto x=*p; int i=x.s;
			auto it = s.upper_bound({a[i].f,-1});
			auto z=*it; if (z.f<a[x.s].f) ++it;
			while (it!=s.end() && (*it).f<=x.f) {
				if ((*it).s!=i) adj[(*it).s].pb(i);
				++it;
			}

			if (p==s.begin()) break;
		}

	}
	
	vector<vector<int>> dp(min(n,5000),vector<int>(min(n,5000),1e9));
	if (n<=5000) {
		vector<int> vis(n,0);
		forn(i,n) {
			vis.assign(n,0);
			queue<int> q;
			dp[i][i]=0;
			q.push(i);
			vis[i]=1;
			while (!q.empty()) {
				int u=q.front(); q.pop();
				if (vis[u]) continue;
				vis[u]=1;
				for (auto v:adj[u]) {
					if (!vis[v]) {
						vis[v]=1;
						dp[i][v]=min(dp[i][v],dp[i][u]+1);
						q.push(v);
					}
				}
			}
		}
	}

	//forn(i,n) {
	//	cout<<i<<": ";
	//	for (auto x:adj[i]) cout<<x<<' '; cout<<'\n';
	//}

	forn(i,m) {
		int u,v; cin>>u>>v;
		--u, --v;
		//int u=0, v=min(4999,n-1);
		if (n<=5000) {
			if (dp[u][v]==-1) cout<<"impossible\n";
			else cout<<dp[u][v]<<'\n';
		} else {

		}
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 135 ms 104640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -