답안 #713528

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
713528 2023-03-22T11:43:42 Z HamletPetrosyan Event Hopping (BOI22_events) C++17
25 / 100
224 ms 44084 KB
#include <bits/stdc++.h>
using namespace std;

#define len(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define fr first
#define sc second

const int INF = 1e9;
const int N = 1e5 + 5, lN = 18;

int n, q;
pii a[N];
int par[N][lN];

vector<int> temp, ind;
map<int, int> mp;

pii t[4 * N];

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

void update(int v, int tl, int tr, int ind, int val, int i){
	if(tl == tr){
		t[v] = min(t[v], {val, i});
		return;
	}
	int tm = (tl + tr) / 2;
	if(ind <= tm) update(2 * v + 0, tl, tm + 0, ind, val, i);
	else update(2 * v + 1, tm + 1, tr, ind, val, i);
	t[v] = min(t[2 * v + 0], t[2 * v + 1]);
}

pii get(int v, int tl, int tr, int l, int r){
	if(l > r) return (pii){INF, -1};
	if(tl == l && tr == r) return t[v];
	int tm = (tl + tr) / 2;
	return min(get(2 * v + 0, tl, tm + 0, l, min(r, tm + 0)),
			   get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> q;
	for(int i = 0; i < n; i++){
		cin >> a[i].fr >> a[i].sc;
		temp.push_back(a[i].fr);
		temp.push_back(a[i].sc);
		ind.push_back(i);
	}
	sort(all(temp));
	sort(all(ind), [](int i, int j){ return a[i] < a[j]; });

	int last = -1, cnt = 0;
	for(int i = 0; i < len(temp); i++){
		if(temp[i] == last) continue;
		cnt++;
		last = temp[i];
		mp[temp[i]] = cnt;
	}

	build(1, 1, cnt);
	for(int i = 0; i < n; i++){
		a[i].fr = mp[a[i].fr];
		a[i].sc = mp[a[i].sc];
		update(1, 1, cnt, a[i].sc, a[i].fr, i);
	}

	for(int i = 0; i < n; i++){
		pii h = get(1, 1, cnt, a[i].fr, a[i].sc);
		par[i][0] = (h.fr >= a[i].fr ? i : h.sc);
	}
	for(auto i : ind){
		for(int j = 1; j < lN; j++){
			par[i][j] = par[par[i][j - 1]][j - 1];
		}
	}

	int x, y;
	while(q--){
		cin >> x >> y;
		x--, y--;
		if(x == y){
			cout << "0\n";
			continue;
		}
		if(a[x].sc > a[y].sc){
			cout << "impossible\n";
			continue;
		}
		if(a[x].sc >= a[y].fr){
			cout << "1\n";
			continue;
		}

		int ans = 2, now = y;
		for(int i = lN - 1; i >= 0; i--){
			if(a[par[now][i]].fr <= a[x].sc) continue;
			ans += (1 << i);
			now = par[now][i];
		}
		if(par[now][0] == -1 || a[par[now][0]].fr > a[x].sc){
			cout << "impossible\n";
			continue;
		}
		cout << ans << "\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 197 ms 16596 KB Output is correct
3 Correct 181 ms 16680 KB Output is correct
4 Correct 211 ms 16668 KB Output is correct
5 Correct 208 ms 17184 KB Output is correct
6 Correct 224 ms 17624 KB Output is correct
7 Correct 216 ms 17768 KB Output is correct
8 Runtime error 218 ms 44084 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 33 ms 1748 KB Output is correct
20 Correct 42 ms 1576 KB Output is correct
21 Correct 32 ms 1920 KB Output is correct
22 Correct 33 ms 1976 KB Output is correct
23 Correct 34 ms 2188 KB Output is correct
24 Correct 23 ms 2132 KB Output is correct
25 Correct 29 ms 1812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 168 ms 15984 KB Output is correct
20 Correct 165 ms 15964 KB Output is correct
21 Correct 198 ms 16068 KB Output is correct
22 Runtime error 190 ms 38072 KB Execution killed with signal 11
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 203 ms 16596 KB Output is correct
2 Correct 198 ms 16648 KB Output is correct
3 Correct 214 ms 16628 KB Output is correct
4 Runtime error 197 ms 44072 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 197 ms 16596 KB Output is correct
3 Correct 181 ms 16680 KB Output is correct
4 Correct 211 ms 16668 KB Output is correct
5 Correct 208 ms 17184 KB Output is correct
6 Correct 224 ms 17624 KB Output is correct
7 Correct 216 ms 17768 KB Output is correct
8 Runtime error 218 ms 44084 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -