답안 #860558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
860558 2023-10-13T09:42:44 Z PoPularPlusPlus Event Hopping (BOI22_events) C++17
100 / 100
120 ms 17856 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define vf first 
#define vs second
const int mod = 998244353;
 
const int MX = 1000000005;
const int L = 20;
 
struct Seg {
	int siz;
	vector<pair<int,int>> v;
 
	void init(int n){
		siz = 1;
		while(siz < n)siz *= 2;
		v.assign(siz*2,mp(MX,-1));
	}
 
	pair<int,int> merge(pair<int,int> x , pair<int,int> y){
		pair<int,int> res;
		res.vf = min(x.vf , y.vf);
		if(x.vf == y.vf)res.vs = min(x.vs , y.vs);
		else if(res.vf == x.vf)res.vs = x.vs;
		else res.vs = y.vs;
		return res;
	}
 
	void set(int i , pair<int,int> p , int x , int lx , int rx){
		if(rx - lx == 1){
			v[x] = merge(p,v[x]);
			return;
		}
		int m = (lx + rx)/2;
		if(i < m)
			set(i , p , 2 * x + 1 , lx , m);
		else 
			set(i , p , 2 * x + 2 , m , rx);
		v[x] = merge(v[2 * x + 1] , v[2 * x + 2]);
	}
 
	void set(int i , pair<int,int> p){
		set(i , p , 0 , 0 , siz);
	}
 
	pair<int,int> range_mx(int l , int r , int x , int lx , int rx){
		if(l >= rx || lx >= r)return mp(MX,-1);
		if(lx >= l && rx <= r)return v[x];
		int m = (lx + rx)/2;
		return merge(range_mx(l,r,2*x+1,lx,m),range_mx(l,r,2*x+2,m,rx));
	}
 
	pair<int,int> range_mx(int l , int r){
		return range_mx(l , r , 0 , 0 , siz);
	}
};
 
void solve(){
	int n , q;
	cin >> n >> q;
	vector<pair<int,int>> v(n);
	for(int i = 0; i < n; i++){
		cin >> v[i].vf >> v[i].vs;
	}
 
	// positions acocrding to sorted e
 
	vector<pair<int,pair<int,int>>> e_sort;
	//vector<pair<int,pair<int,int>>> rev;
 
	for(int i = 0; i < n; i++){
		e_sort.pb(mp(v[i].vs , mp(v[i].vf,i)));
	}
	sort(all(e_sort));
	//rev = e_sort;
	//reverse(all(rev));
	int pos[n],rev[n];
	for(int i = 0; i < n; i++){
		pos[e_sort[i].vs.vs] = i;
		rev[i] = e_sort[i].vs.vs;
	}
 
	// segtree 
 
	Seg st;
	st.init(n + 1);
	for(int i = n - 1; i >= 0; i--){
		st.set(i,mp(e_sort[i].vs.vf , i));
	}
 
	// binary lifting
 
	int up[n][L];
	int p[n];
	for(int i = 0; i < n; i++){
		pair<int,pair<int,int>> tp = mp(e_sort[i].vs.vf , mp(-1 , -1));
		int left = (lower_bound(all(e_sort),tp) - e_sort.begin());
		if(left == i)
			up[i][0] = p[i] = i;
		else {
			p[i] = st.range_mx(left,i).vs;
			//cout << left << ' ' << st.range_mx(left,i).vf << ' ' << p[i] << '\n';
			up[i][0] = p[i];
		}
	}
	for(int j = 1; j < L; j++){
		for(int i = 0; i < n; i++){
			if(j == 1)
				up[i][j] = up[p[i]][j-1];
			else 
				up[i][j] = up[up[i][j-1]][j-1];
		}
	}
 
	// answering the queries 
 
	while(q--){
		int s , e;
		cin >> s >> e;
		s--;
		e--;
		if(s == e){
			cout << 0 << '\n';
			continue;
		}
		int ogs = pos[s]  , oge = pos[e];
		if(ogs > oge){
			if(v[s].vs >= v[e].vf && v[s].vs <= v[e].vs){
				cout << 1 << '\n';
				continue;
			}
			else {
				cout << "impossible\n";
				continue;
			}
		}
		int i = oge;
		int mn = v[s].vs;
		int ans = 0;
		for(int j = L-1; j >= 0; j--){
			if(v[rev[up[i][j]]].vf > mn){
				ans += (1<<j);
				i = up[i][j];
			}
		}
		if(v[rev[i]].vf > mn){
			i = up[i][0];
			ans++;
		}
		ans++;
		i = up[i][0];
		if(v[rev[i]].vf > mn)
			cout << "impossible\n";
		else 
			cout << ans << "\n";
	}
}
 
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//int t;cin >> t;
	//while(t--)
		solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 78 ms 17256 KB Output is correct
3 Correct 80 ms 17096 KB Output is correct
4 Correct 91 ms 17088 KB Output is correct
5 Correct 82 ms 17208 KB Output is correct
6 Correct 80 ms 17520 KB Output is correct
7 Correct 80 ms 17348 KB Output is correct
8 Correct 67 ms 17772 KB Output is correct
9 Correct 74 ms 17708 KB Output is correct
10 Correct 95 ms 17776 KB Output is correct
11 Correct 96 ms 17584 KB Output is correct
12 Correct 63 ms 16840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 856 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 452 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 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 500 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 27 ms 2484 KB Output is correct
20 Correct 25 ms 2652 KB Output is correct
21 Correct 27 ms 2964 KB Output is correct
22 Correct 26 ms 2904 KB Output is correct
23 Correct 26 ms 2904 KB Output is correct
24 Correct 19 ms 2912 KB Output is correct
25 Correct 23 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 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 600 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 58 ms 15548 KB Output is correct
20 Correct 53 ms 14720 KB Output is correct
21 Correct 62 ms 15296 KB Output is correct
22 Correct 53 ms 15304 KB Output is correct
23 Correct 50 ms 15488 KB Output is correct
24 Correct 64 ms 15280 KB Output is correct
25 Correct 43 ms 14844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 17288 KB Output is correct
2 Correct 79 ms 17216 KB Output is correct
3 Correct 87 ms 17260 KB Output is correct
4 Correct 69 ms 17608 KB Output is correct
5 Correct 88 ms 17608 KB Output is correct
6 Correct 98 ms 17280 KB Output is correct
7 Correct 120 ms 17188 KB Output is correct
8 Correct 86 ms 17348 KB Output is correct
9 Correct 52 ms 15304 KB Output is correct
10 Correct 84 ms 17000 KB Output is correct
11 Correct 95 ms 17028 KB Output is correct
12 Correct 85 ms 17088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 78 ms 17256 KB Output is correct
3 Correct 80 ms 17096 KB Output is correct
4 Correct 91 ms 17088 KB Output is correct
5 Correct 82 ms 17208 KB Output is correct
6 Correct 80 ms 17520 KB Output is correct
7 Correct 80 ms 17348 KB Output is correct
8 Correct 67 ms 17772 KB Output is correct
9 Correct 74 ms 17708 KB Output is correct
10 Correct 95 ms 17776 KB Output is correct
11 Correct 96 ms 17584 KB Output is correct
12 Correct 63 ms 16840 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 856 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 452 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 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 500 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 27 ms 2484 KB Output is correct
32 Correct 25 ms 2652 KB Output is correct
33 Correct 27 ms 2964 KB Output is correct
34 Correct 26 ms 2904 KB Output is correct
35 Correct 26 ms 2904 KB Output is correct
36 Correct 19 ms 2912 KB Output is correct
37 Correct 23 ms 2392 KB Output is correct
38 Correct 0 ms 348 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 604 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 58 ms 15548 KB Output is correct
48 Correct 53 ms 14720 KB Output is correct
49 Correct 62 ms 15296 KB Output is correct
50 Correct 53 ms 15304 KB Output is correct
51 Correct 50 ms 15488 KB Output is correct
52 Correct 64 ms 15280 KB Output is correct
53 Correct 43 ms 14844 KB Output is correct
54 Correct 80 ms 17288 KB Output is correct
55 Correct 79 ms 17216 KB Output is correct
56 Correct 87 ms 17260 KB Output is correct
57 Correct 69 ms 17608 KB Output is correct
58 Correct 88 ms 17608 KB Output is correct
59 Correct 98 ms 17280 KB Output is correct
60 Correct 120 ms 17188 KB Output is correct
61 Correct 86 ms 17348 KB Output is correct
62 Correct 52 ms 15304 KB Output is correct
63 Correct 84 ms 17000 KB Output is correct
64 Correct 95 ms 17028 KB Output is correct
65 Correct 85 ms 17088 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 78 ms 17252 KB Output is correct
68 Correct 78 ms 17208 KB Output is correct
69 Correct 87 ms 17076 KB Output is correct
70 Correct 80 ms 17284 KB Output is correct
71 Correct 80 ms 17332 KB Output is correct
72 Correct 78 ms 17352 KB Output is correct
73 Correct 69 ms 17856 KB Output is correct
74 Correct 68 ms 17544 KB Output is correct
75 Correct 90 ms 17604 KB Output is correct
76 Correct 83 ms 17612 KB Output is correct
77 Correct 67 ms 16792 KB Output is correct
78 Correct 0 ms 348 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 464 KB Output is correct
82 Correct 1 ms 604 KB Output is correct
83 Correct 1 ms 604 KB Output is correct
84 Correct 1 ms 604 KB Output is correct
85 Correct 1 ms 604 KB Output is correct
86 Correct 27 ms 2696 KB Output is correct
87 Correct 26 ms 2904 KB Output is correct
88 Correct 29 ms 2896 KB Output is correct
89 Correct 21 ms 2908 KB Output is correct
90 Correct 20 ms 2896 KB Output is correct
91 Correct 20 ms 2908 KB Output is correct
92 Correct 22 ms 2548 KB Output is correct
93 Correct 57 ms 15300 KB Output is correct
94 Correct 54 ms 14540 KB Output is correct
95 Correct 61 ms 15232 KB Output is correct
96 Correct 54 ms 15408 KB Output is correct
97 Correct 50 ms 15456 KB Output is correct
98 Correct 62 ms 15304 KB Output is correct
99 Correct 44 ms 14800 KB Output is correct
100 Correct 99 ms 17352 KB Output is correct
101 Correct 97 ms 17324 KB Output is correct
102 Correct 94 ms 17444 KB Output is correct
103 Correct 84 ms 16832 KB Output is correct
104 Correct 81 ms 16824 KB Output is correct
105 Correct 87 ms 17000 KB Output is correct
106 Correct 105 ms 16808 KB Output is correct
107 Correct 95 ms 16744 KB Output is correct
108 Correct 87 ms 17352 KB Output is correct
109 Correct 84 ms 17416 KB Output is correct
110 Correct 112 ms 17280 KB Output is correct
111 Correct 100 ms 17284 KB Output is correct