제출 #576198

#제출 시각아이디문제언어결과실행 시간메모리
576198cheissmartEvent Hopping (BOI22_events)C++14
100 / 100
174 ms12400 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], n;
V<pi> aux;

int t[N * 4];

int add(int x, int y) {
	if(l[x] < l[y]) return x;
	else return y;
}

void build(int v = 1, int tl = 0, int tr = n) {
	if(tr - tl == 1) {
		t[v] = aux[tl].S;
		return;
	}
	int tm = (tl + tr) / 2;
	build(v * 2, tl, tm);
	build(v * 2 + 1, tm, tr);
	t[v] = add(t[v * 2], t[v * 2 + 1]);
}

int qry(int l, int r, int v = 1, int tl = 0, int tr = n) {
	if(l <= tl && tr <= r)
		return t[v];
	int tm = (tl + tr) / 2;
	if(r <= tm) return qry(l, r, v * 2, tl, tm);
	else if(l >= tm) return qry(l, r, v * 2 + 1, tm, tr);
	else return add(qry(l, r, v * 2, tl, tm), qry(l, r, v * 2 + 1, tm, tr));
}

signed main()
{
	IO_OP;

	int q;
	cin >> n >> q;
	for(int i = 0; i < n; i++) {
		cin >> l[i] >> r[i];
		aux.EB(r[i], i);
	}
	sort(ALL(aux));

	build();

	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;
		// }
		int lb = lower_bound(ALL(aux), pi(l[i], -INF)) - aux.begin();
		int rb = upper_bound(ALL(aux), pi(r[i], INF)) - aux.begin();
		who[i] = qry(lb, rb);
		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;
		}
		if(l[e] <= r[s] && r[s] <= r[e]) {
			cout << 1 << '\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...