Submission #576186

#TimeUsernameProblemLanguageResultExecution timeMemory
576186cheissmartEvent Hopping (BOI22_events)C++14
0 / 100
1595 ms1584 KiB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
	cerr << to_string(h);
	if(sizeof ...(t)) cerr << ", ";
	DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif

const int INF = 1e9 + 7, N = 1e5 + 7;

int l[N], r[N], who[N], p[N][20];

signed main()
{
	IO_OP;

	int n, q;
	cin >> n >> q;
	for(int i = 0; i < n; i++)
		cin >> l[i] >> r[i];

	for(int i = 0; i < n; i++) {
		who[i] = i;
		for(int j = 0; j < n; j++) if(l[i] <= r[j] && r[j] <= r[i]) {
			if(l[j] < l[who[i]])
				who[i] = j;
		}
		p[i][0] = who[i];
	}
	for(int j = 1; j < 20; j++)
		for(int i = 0; i < n; i++)
			p[i][j] = p[p[i][j - 1]][j - 1];

	while(q--) {
		int s, e;
		cin >> s >> e, s--, e--;	
		if(s == e) {
			cout << "0\n";
			continue;
		}
		if(r[s] > r[e]) {
			cout << "impossible\n";
			continue;
		}
		int ans = 0;
		for(int j = 19; j >= 0; j--) {
			if(l[p[e][j]] > r[s]) {
				ans += 1 << j;
				e = p[e][j];
			}
		}
		if(l[who[e]] <= r[s]) {
			ans += 2;
			cout << ans << '\n';
		} else {
			cout << "impossible\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...