답안 #853503

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
853503 2023-09-24T13:02:54 Z esomer Event Hopping (BOI22_events) C++17
100 / 100
491 ms 47156 KB
#include <bits/stdc++.h>

using namespace std;

#define endl "\n"

struct segTree{
	vector<pair<int, int>> mn;
	int sz;
	void init(int n){
		sz = 1;
		while(sz < n) sz *= 2;
		mn.assign(2 * sz, {2e9, 2e9});
	}
	void set(int i, pair<int, int> val, int x, int lx, int rx){
		if(rx - lx == 1){
			mn[x] = val;
			return;
		}
		int m = (lx + rx) / 2;
		if(i < m) set(i, val, 2 * x + 1, lx, m);
		else set(i, val, 2 * x + 2, m, rx);
		mn[x] = min(mn[2 * x + 1], mn[2 * x + 2]);
	}
	void set(int i, pair<int, int> val){
		set(i, val, 0, 0, sz);
	}
	pair<int, int> calc(int l, int r, int x, int lx, int rx){
		if(lx >= l && rx <= r) return mn[x];
		else if(lx >= r || rx <= l) return {2e9, 2e9};
		int m = (lx + rx) / 2;
		pair<int, int> p1 = calc(l, r, 2 * x + 1, lx, m);
		pair<int, int> p2 = calc(l, r, 2 * x + 2, m, rx);
		return min(p1, p2);
	}
	pair<int, int> calc(int l, int r){
		return calc(l, r, 0, 0, sz);
	}
};

bool can_reach(int i, int j, vector<int>& S, vector<int>& E){
	if(S[j] <= E[i] && E[i] <= E[j]) return true;
	else return false;
}

void solve(){
	int N, Q; cin >> N >> Q;
	vector<int> E(N);
	vector<int> S(N);
	vector<int> all;
	for(int i = 0; i < N; i++) {cin >> S[i] >> E[i]; all.push_back(S[i]); all.push_back(E[i]);}
	sort(all.begin(), all.end());
	int curr = 0;
	int lst_x = -1;
	map<int, int> frst;
	map<int, int> lst;
	for(int x : all){
		bool fst = 0;
		if(x != lst_x) fst = 1;
		lst_x = x;
		lst[x] = curr;
		if(fst) frst[x] = curr;
		curr++;
	}
	map<int, int> add;
	segTree st; st.init(2 * N);
	for(int i = 0; i < N; i++){
		st.set(frst[E[i]] + add[E[i]], {S[i], i});
		add[E[i]]++;
	}
	vector<pair<int, int>> adj(N); //Order by E[i], then by i.
	for(int i = 0; i < N; i++){
		adj[i] = st.calc(frst[S[i]], lst[E[i]] + 1);
	}
	vector<vector<pair<int, int>>> dp(20, vector<pair<int, int>>(N));
	for(int k = 0; k < 20; k++){
		for(int i = 0; i < N; i++){
			if(k == 0) dp[k][i] = adj[i];
			else{
				dp[k][i] = dp[k-1][dp[k-1][i].second];
			}
		}
	}
	// for(int i = 0; i < N; i++){
	// 	cout << "i " << i << " adj " << adj[i].first << " " << adj[i].second << endl;
	// }
	// for(int k = 0; k < 3; k++){
	// 	cout << "k " << k << endl;
	// 	for(int i = 0; i < N; i++){
	// 		cout << "i " << i << " " << dp[k][i].first << " " << dp[k][i].second << endl;
	// 	}
	// }
	while(Q--){
		int s, e; cin >> s >> e; s--; e--;
		if(s == e){
			cout << 0 << endl;
			continue;
		}else if(can_reach(s, e, S, E)){
			cout << 1 << endl;
			continue;
		}
		int orge = e;
		int ans = -1;
		int curr = 0;
		for(int k = 19; k >= 0; k--){
			if(dp[k][e].first > E[s]){
				e = dp[k][e].second;
				curr += (1 << k);
			}else ans = curr + (1 << k);
		}
		if(ans != -1 && E[s] <= E[orge]) cout << ans + 1 << endl;
		else cout << "impossible" << endl;
	}
}

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

	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 296 ms 37412 KB Output is correct
3 Correct 310 ms 37324 KB Output is correct
4 Correct 387 ms 37384 KB Output is correct
5 Correct 321 ms 38424 KB Output is correct
6 Correct 309 ms 39192 KB Output is correct
7 Correct 347 ms 39384 KB Output is correct
8 Correct 402 ms 47132 KB Output is correct
9 Correct 382 ms 47028 KB Output is correct
10 Correct 310 ms 37488 KB Output is correct
11 Correct 346 ms 41388 KB Output is correct
12 Correct 152 ms 35440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 856 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 856 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 34 ms 2972 KB Output is correct
20 Correct 32 ms 2624 KB Output is correct
21 Correct 35 ms 3096 KB Output is correct
22 Correct 31 ms 2900 KB Output is correct
23 Correct 30 ms 3412 KB Output is correct
24 Correct 30 ms 3416 KB Output is correct
25 Correct 32 ms 2904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 856 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 656 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 277 ms 37488 KB Output is correct
20 Correct 256 ms 37360 KB Output is correct
21 Correct 312 ms 35064 KB Output is correct
22 Correct 294 ms 40824 KB Output is correct
23 Correct 365 ms 46788 KB Output is correct
24 Correct 399 ms 46776 KB Output is correct
25 Correct 51 ms 23428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 290 ms 37388 KB Output is correct
2 Correct 306 ms 37324 KB Output is correct
3 Correct 378 ms 37384 KB Output is correct
4 Correct 403 ms 47156 KB Output is correct
5 Correct 353 ms 37536 KB Output is correct
6 Correct 409 ms 46864 KB Output is correct
7 Correct 414 ms 46688 KB Output is correct
8 Correct 417 ms 47024 KB Output is correct
9 Correct 359 ms 46800 KB Output is correct
10 Correct 400 ms 46776 KB Output is correct
11 Correct 398 ms 46776 KB Output is correct
12 Correct 445 ms 46692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 296 ms 37412 KB Output is correct
3 Correct 310 ms 37324 KB Output is correct
4 Correct 387 ms 37384 KB Output is correct
5 Correct 321 ms 38424 KB Output is correct
6 Correct 309 ms 39192 KB Output is correct
7 Correct 347 ms 39384 KB Output is correct
8 Correct 402 ms 47132 KB Output is correct
9 Correct 382 ms 47028 KB Output is correct
10 Correct 310 ms 37488 KB Output is correct
11 Correct 346 ms 41388 KB Output is correct
12 Correct 152 ms 35440 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 2 ms 856 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 2 ms 856 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 2 ms 860 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 34 ms 2972 KB Output is correct
32 Correct 32 ms 2624 KB Output is correct
33 Correct 35 ms 3096 KB Output is correct
34 Correct 31 ms 2900 KB Output is correct
35 Correct 30 ms 3412 KB Output is correct
36 Correct 30 ms 3416 KB Output is correct
37 Correct 32 ms 2904 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 656 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 277 ms 37488 KB Output is correct
48 Correct 256 ms 37360 KB Output is correct
49 Correct 312 ms 35064 KB Output is correct
50 Correct 294 ms 40824 KB Output is correct
51 Correct 365 ms 46788 KB Output is correct
52 Correct 399 ms 46776 KB Output is correct
53 Correct 51 ms 23428 KB Output is correct
54 Correct 290 ms 37388 KB Output is correct
55 Correct 306 ms 37324 KB Output is correct
56 Correct 378 ms 37384 KB Output is correct
57 Correct 403 ms 47156 KB Output is correct
58 Correct 353 ms 37536 KB Output is correct
59 Correct 409 ms 46864 KB Output is correct
60 Correct 414 ms 46688 KB Output is correct
61 Correct 417 ms 47024 KB Output is correct
62 Correct 359 ms 46800 KB Output is correct
63 Correct 400 ms 46776 KB Output is correct
64 Correct 398 ms 46776 KB Output is correct
65 Correct 445 ms 46692 KB Output is correct
66 Correct 0 ms 344 KB Output is correct
67 Correct 279 ms 37388 KB Output is correct
68 Correct 298 ms 37292 KB Output is correct
69 Correct 348 ms 37324 KB Output is correct
70 Correct 331 ms 38308 KB Output is correct
71 Correct 329 ms 39240 KB Output is correct
72 Correct 333 ms 39460 KB Output is correct
73 Correct 400 ms 47036 KB Output is correct
74 Correct 404 ms 47108 KB Output is correct
75 Correct 326 ms 37568 KB Output is correct
76 Correct 360 ms 41304 KB Output is correct
77 Correct 150 ms 35276 KB Output is correct
78 Correct 1 ms 344 KB Output is correct
79 Correct 1 ms 604 KB Output is correct
80 Correct 1 ms 604 KB Output is correct
81 Correct 1 ms 604 KB Output is correct
82 Correct 2 ms 604 KB Output is correct
83 Correct 2 ms 860 KB Output is correct
84 Correct 2 ms 748 KB Output is correct
85 Correct 1 ms 604 KB Output is correct
86 Correct 33 ms 2908 KB Output is correct
87 Correct 37 ms 2808 KB Output is correct
88 Correct 35 ms 3076 KB Output is correct
89 Correct 38 ms 3040 KB Output is correct
90 Correct 32 ms 3348 KB Output is correct
91 Correct 28 ms 3496 KB Output is correct
92 Correct 32 ms 2908 KB Output is correct
93 Correct 262 ms 37472 KB Output is correct
94 Correct 275 ms 37384 KB Output is correct
95 Correct 259 ms 35048 KB Output is correct
96 Correct 307 ms 40840 KB Output is correct
97 Correct 352 ms 46788 KB Output is correct
98 Correct 422 ms 46816 KB Output is correct
99 Correct 44 ms 23408 KB Output is correct
100 Correct 449 ms 46780 KB Output is correct
101 Correct 434 ms 46784 KB Output is correct
102 Correct 443 ms 46776 KB Output is correct
103 Correct 425 ms 46780 KB Output is correct
104 Correct 399 ms 46780 KB Output is correct
105 Correct 471 ms 46772 KB Output is correct
106 Correct 251 ms 33928 KB Output is correct
107 Correct 312 ms 37328 KB Output is correct
108 Correct 302 ms 35088 KB Output is correct
109 Correct 335 ms 35020 KB Output is correct
110 Correct 387 ms 46776 KB Output is correct
111 Correct 491 ms 46684 KB Output is correct