Submission #745085

# Submission time Handle Problem Language Result Execution time Memory
745085 2023-05-19T11:35:42 Z 박상훈(#9964) Event Hopping (BOI22_events) C++17
100 / 100
253 ms 17176 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 1e9 + 100;

struct Seg{
	pair<int, int> tree[400400];
	int sz;
	void init(int n){
		sz = n;
		fill(tree, tree+sz*2, pair<int, int>(INF, 0));
	}
	void update(int p, int x, int y){
		p += sz;
		tree[p] = min(tree[p], pair<int, int>(x, y));
		for (p>>=1;p;p>>=1) tree[p] = min(tree[p<<1], tree[p<<1|1]);
	}
	pair<int, int> query(int l, int r){
		++r;
		pair<int, int> ret(INF, 0);
		for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
			if (l&1) ret = min(ret, tree[l++]);
			if (r&1) ret = min(ret, tree[--r]);
		}

		assert(ret.first < INF);
		return ret;
	}
}tree;

int n, S[100100], E[100100], sp[100100][20];

bool ok(int x, int y){
	return E[x] <= E[y];
}

void naive(int x, int y){
	if (!ok(x, y)){
		printf("impossible\n");
		return;
	}

	if (x==y){
		printf("0\n");
		return;
	}

	int l = S[y], r = E[y], ans = 1;
	while(true){
		if (l<=E[x]){
			printf("%d\n", ans);
			return;
		}

		int nl = INF;
		for (int i=1;i<=n;i++) if (l<=E[i] && E[i]<=r){
			nl = min(nl, S[i]);
		}
		if (l==nl) break;
		l = nl;
		ans++;
	}

	printf("impossible\n");
}

int calc(int x, int p){
	for (int i=19;i>=0;i--) if (p&(1<<i)){
		x = sp[x][i];
	}
	return S[x];
}

void solve(int x, int y){
	if (!ok(x, y)){
		printf("impossible\n");
		return;
	}

	if (x==y){
		printf("0\n");
		return;
	}

	int ansl = 1, ansr = n-1, ans = INF;
	while(ansl<=ansr){
		int ansm = (ansl + ansr)>>1;
		if (calc(y, ansm-1) <= E[x]) ans = ansm, ansr = ansm-1;
		else ansl = ansm+1;
	}

	if (ans==INF) printf("impossible\n");
	else printf("%d\n", ans);
}

int main(){
	int q;
	scanf("%d %d", &n, &q);

	vector<int> X;
	for (int i=1;i<=n;i++){
		scanf("%d %d", S+i, E+i);
		X.push_back(S[i]);
		X.push_back(E[i]);	
	} 

	sort(X.begin(), X.end());
	X.erase(unique(X.begin(), X.end()), X.end());
	tree.init(X.size() + 1);
	for (int i=1;i<=n;i++){
		S[i] = lower_bound(X.begin(), X.end(), S[i]) - X.begin() + 1;
		E[i] = lower_bound(X.begin(), X.end(), E[i]) - X.begin() + 1;

		tree.update(E[i], S[i], i);
	}

	for (int i=1;i<=n;i++){
		sp[i][0] = tree.query(S[i], E[i]).second;
	}
	for (int j=1;j<20;j++){
		for (int i=1;i<=n;i++) sp[i][j] = sp[sp[i][j-1]][j-1];
	}


	while(q--){
		int x, y;
		scanf("%d %d", &x, &y);
		solve(x, y);
	}
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
events.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |   scanf("%d %d", S+i, E+i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
events.cpp:128:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 173 ms 14456 KB Output is correct
3 Correct 214 ms 16736 KB Output is correct
4 Correct 253 ms 16556 KB Output is correct
5 Correct 205 ms 16684 KB Output is correct
6 Correct 190 ms 16704 KB Output is correct
7 Correct 189 ms 16792 KB Output is correct
8 Correct 161 ms 17128 KB Output is correct
9 Correct 156 ms 17132 KB Output is correct
10 Correct 197 ms 16964 KB Output is correct
11 Correct 169 ms 17068 KB Output is correct
12 Correct 123 ms 16320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 2 ms 3540 KB Output is correct
5 Correct 2 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 2 ms 3540 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 2 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 2 ms 3540 KB Output is correct
5 Correct 2 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 2 ms 3540 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 2 ms 3540 KB Output is correct
10 Correct 1 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3540 KB Output is correct
13 Correct 2 ms 3540 KB Output is correct
14 Correct 2 ms 3540 KB Output is correct
15 Correct 2 ms 3540 KB Output is correct
16 Correct 2 ms 3540 KB Output is correct
17 Correct 3 ms 3540 KB Output is correct
18 Correct 2 ms 3540 KB Output is correct
19 Correct 78 ms 5196 KB Output is correct
20 Correct 72 ms 5396 KB Output is correct
21 Correct 72 ms 5752 KB Output is correct
22 Correct 48 ms 5760 KB Output is correct
23 Correct 54 ms 5600 KB Output is correct
24 Correct 52 ms 5592 KB Output is correct
25 Correct 74 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
3 Correct 2 ms 3540 KB Output is correct
4 Correct 2 ms 3540 KB Output is correct
5 Correct 2 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 2 ms 3540 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 2 ms 3540 KB Output is correct
10 Correct 2 ms 3412 KB Output is correct
11 Correct 2 ms 3412 KB Output is correct
12 Correct 2 ms 3540 KB Output is correct
13 Correct 2 ms 3540 KB Output is correct
14 Correct 2 ms 3540 KB Output is correct
15 Correct 2 ms 3472 KB Output is correct
16 Correct 2 ms 3540 KB Output is correct
17 Correct 2 ms 3540 KB Output is correct
18 Correct 2 ms 3540 KB Output is correct
19 Correct 85 ms 12944 KB Output is correct
20 Correct 79 ms 14096 KB Output is correct
21 Correct 92 ms 14676 KB Output is correct
22 Correct 80 ms 14888 KB Output is correct
23 Correct 95 ms 14792 KB Output is correct
24 Correct 91 ms 14880 KB Output is correct
25 Correct 36 ms 14208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 14740 KB Output is correct
2 Correct 212 ms 16660 KB Output is correct
3 Correct 250 ms 16624 KB Output is correct
4 Correct 151 ms 17060 KB Output is correct
5 Correct 201 ms 17084 KB Output is correct
6 Correct 204 ms 16792 KB Output is correct
7 Correct 189 ms 16736 KB Output is correct
8 Correct 158 ms 16892 KB Output is correct
9 Correct 94 ms 14876 KB Output is correct
10 Correct 196 ms 16320 KB Output is correct
11 Correct 186 ms 16168 KB Output is correct
12 Correct 186 ms 16336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 173 ms 14456 KB Output is correct
3 Correct 214 ms 16736 KB Output is correct
4 Correct 253 ms 16556 KB Output is correct
5 Correct 205 ms 16684 KB Output is correct
6 Correct 190 ms 16704 KB Output is correct
7 Correct 189 ms 16792 KB Output is correct
8 Correct 161 ms 17128 KB Output is correct
9 Correct 156 ms 17132 KB Output is correct
10 Correct 197 ms 16964 KB Output is correct
11 Correct 169 ms 17068 KB Output is correct
12 Correct 123 ms 16320 KB Output is correct
13 Correct 1 ms 3412 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 2 ms 3540 KB Output is correct
16 Correct 2 ms 3540 KB Output is correct
17 Correct 2 ms 3540 KB Output is correct
18 Correct 2 ms 3540 KB Output is correct
19 Correct 2 ms 3540 KB Output is correct
20 Correct 2 ms 3540 KB Output is correct
21 Correct 2 ms 3540 KB Output is correct
22 Correct 1 ms 3412 KB Output is correct
23 Correct 2 ms 3412 KB Output is correct
24 Correct 2 ms 3540 KB Output is correct
25 Correct 2 ms 3540 KB Output is correct
26 Correct 2 ms 3540 KB Output is correct
27 Correct 2 ms 3540 KB Output is correct
28 Correct 2 ms 3540 KB Output is correct
29 Correct 3 ms 3540 KB Output is correct
30 Correct 2 ms 3540 KB Output is correct
31 Correct 78 ms 5196 KB Output is correct
32 Correct 72 ms 5396 KB Output is correct
33 Correct 72 ms 5752 KB Output is correct
34 Correct 48 ms 5760 KB Output is correct
35 Correct 54 ms 5600 KB Output is correct
36 Correct 52 ms 5592 KB Output is correct
37 Correct 74 ms 5212 KB Output is correct
38 Correct 2 ms 3412 KB Output is correct
39 Correct 2 ms 3412 KB Output is correct
40 Correct 2 ms 3540 KB Output is correct
41 Correct 2 ms 3540 KB Output is correct
42 Correct 2 ms 3540 KB Output is correct
43 Correct 2 ms 3472 KB Output is correct
44 Correct 2 ms 3540 KB Output is correct
45 Correct 2 ms 3540 KB Output is correct
46 Correct 2 ms 3540 KB Output is correct
47 Correct 85 ms 12944 KB Output is correct
48 Correct 79 ms 14096 KB Output is correct
49 Correct 92 ms 14676 KB Output is correct
50 Correct 80 ms 14888 KB Output is correct
51 Correct 95 ms 14792 KB Output is correct
52 Correct 91 ms 14880 KB Output is correct
53 Correct 36 ms 14208 KB Output is correct
54 Correct 165 ms 14740 KB Output is correct
55 Correct 212 ms 16660 KB Output is correct
56 Correct 250 ms 16624 KB Output is correct
57 Correct 151 ms 17060 KB Output is correct
58 Correct 201 ms 17084 KB Output is correct
59 Correct 204 ms 16792 KB Output is correct
60 Correct 189 ms 16736 KB Output is correct
61 Correct 158 ms 16892 KB Output is correct
62 Correct 94 ms 14876 KB Output is correct
63 Correct 196 ms 16320 KB Output is correct
64 Correct 186 ms 16168 KB Output is correct
65 Correct 186 ms 16336 KB Output is correct
66 Correct 2 ms 3412 KB Output is correct
67 Correct 168 ms 16596 KB Output is correct
68 Correct 203 ms 16608 KB Output is correct
69 Correct 247 ms 16576 KB Output is correct
70 Correct 199 ms 16652 KB Output is correct
71 Correct 193 ms 16700 KB Output is correct
72 Correct 192 ms 16640 KB Output is correct
73 Correct 150 ms 17076 KB Output is correct
74 Correct 151 ms 17176 KB Output is correct
75 Correct 201 ms 16928 KB Output is correct
76 Correct 158 ms 17068 KB Output is correct
77 Correct 126 ms 16428 KB Output is correct
78 Correct 2 ms 3444 KB Output is correct
79 Correct 2 ms 3456 KB Output is correct
80 Correct 2 ms 3540 KB Output is correct
81 Correct 3 ms 3452 KB Output is correct
82 Correct 2 ms 3540 KB Output is correct
83 Correct 3 ms 3460 KB Output is correct
84 Correct 2 ms 3492 KB Output is correct
85 Correct 2 ms 3540 KB Output is correct
86 Correct 77 ms 5380 KB Output is correct
87 Correct 77 ms 5588 KB Output is correct
88 Correct 67 ms 5636 KB Output is correct
89 Correct 49 ms 5784 KB Output is correct
90 Correct 52 ms 5652 KB Output is correct
91 Correct 47 ms 5656 KB Output is correct
92 Correct 70 ms 5224 KB Output is correct
93 Correct 80 ms 14888 KB Output is correct
94 Correct 77 ms 14144 KB Output is correct
95 Correct 91 ms 14784 KB Output is correct
96 Correct 78 ms 14888 KB Output is correct
97 Correct 100 ms 14784 KB Output is correct
98 Correct 88 ms 14872 KB Output is correct
99 Correct 35 ms 14144 KB Output is correct
100 Correct 213 ms 16760 KB Output is correct
101 Correct 216 ms 16684 KB Output is correct
102 Correct 170 ms 16900 KB Output is correct
103 Correct 192 ms 16420 KB Output is correct
104 Correct 185 ms 16268 KB Output is correct
105 Correct 196 ms 16380 KB Output is correct
106 Correct 208 ms 16088 KB Output is correct
107 Correct 245 ms 16248 KB Output is correct
108 Correct 156 ms 16748 KB Output is correct
109 Correct 161 ms 16648 KB Output is correct
110 Correct 176 ms 16576 KB Output is correct
111 Correct 171 ms 16692 KB Output is correct