Submission #745083

# Submission time Handle Problem Language Result Execution time Memory
745083 2023-05-19T11:34:23 Z 박상훈(#9964) Event Hopping (BOI22_events) C++17
10 / 100
1500 ms 12936 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);
		naive(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 1 ms 3412 KB Output is correct
2 Execution timed out 1577 ms 12936 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 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 191 ms 3540 KB Output is correct
4 Correct 24 ms 3540 KB Output is correct
5 Correct 40 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 3 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 191 ms 3540 KB Output is correct
4 Correct 24 ms 3540 KB Output is correct
5 Correct 40 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 3 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 1 ms 3412 KB Output is correct
12 Correct 197 ms 3540 KB Output is correct
13 Correct 23 ms 3540 KB Output is correct
14 Correct 39 ms 3540 KB Output is correct
15 Correct 3 ms 3540 KB Output is correct
16 Correct 3 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 Execution timed out 1589 ms 3924 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 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 191 ms 3540 KB Output is correct
4 Correct 24 ms 3540 KB Output is correct
5 Correct 40 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 3 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 193 ms 3540 KB Output is correct
13 Correct 24 ms 3540 KB Output is correct
14 Correct 40 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 2 ms 3540 KB Output is correct
18 Correct 2 ms 3540 KB Output is correct
19 Execution timed out 1600 ms 12868 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1563 ms 12860 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3412 KB Output is correct
2 Execution timed out 1577 ms 12936 KB Time limit exceeded
3 Halted 0 ms 0 KB -