Submission #577739

# Submission time Handle Problem Language Result Execution time Memory
577739 2022-06-15T08:29:48 Z vanic Event Hopping (BOI22_events) C++14
10 / 100
1500 ms 37948 KB
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <queue>
 
using namespace std;
 
const int maxn=1e5+5, Log=17, pot=(1<<Log);
 
vector < int > svi;
 
void query(int L, int D, int x, int l, int d){
	if(L>=l && D<=d){
		svi.push_back(x);
		return;
	}
	if((L+D)/2>=l){
		query(L, (L+D)/2, x*2, l, d);
	}
	if((L+D)/2+1<=d){
		query((L+D)/2+1, D, x*2+1, l, d);
	}
}
 
 
pair < int, int > a[maxn];
set < int > s;
map < int, int > m;
int dist[pot*2];
int d[maxn];
vector < pair < int, int > >  ms[pot*2];
 
void precompute(){
	for(int i=2; i<pot*2; i++){
		ms[i].push_back({i/2, 0});
	}
}
 
void bfs(int x){
	memset(dist, -1, sizeof(dist));
	memset(d, -1, sizeof(d));
	deque < int > q;
	q.push_front(a[x].second+pot);
	dist[a[x].second+pot]=0;
	d[x]=0;
	while(!q.empty()){
		x=q.front();
		q.pop_front();
		for(int i=0; i<(int)ms[x].size(); i++){
			if(ms[x][i].second){
				if(dist[ms[x][i].first]==-1){
					dist[ms[x][i].first]=dist[x]+1;
					q.push_back(ms[x][i].first);
				}
				if(d[ms[x][i].second]==-1){
					d[ms[x][i].second]=dist[x]+1;
				}
			}
			else{
				if(dist[ms[x][i].first]==-1){
					dist[ms[x][i].first]=dist[x];
					q.push_front(ms[x][i].first);
				}
			}
		}
	}
}
 
int main(){
	precompute();
	int n, q;
	scanf("%d%d", &n, &q);
	for(int i=1; i<=n; i++){
		scanf("%d%d", &a[i].first, &a[i].second);
		s.insert(a[i].second);
	}
	int br=0;
	
	for(auto it=s.begin(); it!=s.end(); it++){
		m[*it]=br;
		br++;
	}
	set < int >::iterator it;
	for(int i=1; i<=n; i++){
		it=s.lower_bound(a[i].first);
		a[i]={m[*it], m[a[i].second]};
//		cout << a[i].first << ' ' << a[i].second << endl;
		query(0, pot-1, 1, a[i].first, a[i].second);
		while(!svi.empty()){
//			cout << "moji " << svi.back() << endl;
			ms[svi.back()].push_back({a[i].second+pot, i});
			svi.pop_back();
		}
	}
	int x, y;
	for(int i=0; i<q; i++){
		scanf("%d%d", &x, &y);
		bfs(x);
		if(d[y]==-1){
			printf("impossible\n");
		}
		else{
			printf("%d\n", d[y]);
		}
	}
	return 0;
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |  scanf("%d%d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~
events.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%d%d", &a[i].first, &a[i].second);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d%d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 16124 KB Output is correct
2 Execution timed out 1587 ms 26148 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15956 KB Output is correct
2 Correct 19 ms 16052 KB Output is correct
3 Correct 28 ms 16084 KB Output is correct
4 Correct 22 ms 16084 KB Output is correct
5 Correct 22 ms 16096 KB Output is correct
6 Correct 22 ms 16160 KB Output is correct
7 Correct 26 ms 16152 KB Output is correct
8 Correct 34 ms 16212 KB Output is correct
9 Correct 24 ms 16068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15956 KB Output is correct
2 Correct 19 ms 16052 KB Output is correct
3 Correct 28 ms 16084 KB Output is correct
4 Correct 22 ms 16084 KB Output is correct
5 Correct 22 ms 16096 KB Output is correct
6 Correct 22 ms 16160 KB Output is correct
7 Correct 26 ms 16152 KB Output is correct
8 Correct 34 ms 16212 KB Output is correct
9 Correct 24 ms 16068 KB Output is correct
10 Correct 18 ms 16000 KB Output is correct
11 Correct 26 ms 15948 KB Output is correct
12 Correct 25 ms 16172 KB Output is correct
13 Correct 23 ms 16084 KB Output is correct
14 Correct 23 ms 16084 KB Output is correct
15 Correct 22 ms 16084 KB Output is correct
16 Correct 27 ms 16180 KB Output is correct
17 Correct 28 ms 16236 KB Output is correct
18 Correct 22 ms 16092 KB Output is correct
19 Execution timed out 1580 ms 17340 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 15956 KB Output is correct
2 Correct 19 ms 16052 KB Output is correct
3 Correct 28 ms 16084 KB Output is correct
4 Correct 22 ms 16084 KB Output is correct
5 Correct 22 ms 16096 KB Output is correct
6 Correct 22 ms 16160 KB Output is correct
7 Correct 26 ms 16152 KB Output is correct
8 Correct 34 ms 16212 KB Output is correct
9 Correct 24 ms 16068 KB Output is correct
10 Correct 16 ms 15956 KB Output is correct
11 Correct 25 ms 15980 KB Output is correct
12 Correct 28 ms 16164 KB Output is correct
13 Correct 24 ms 16052 KB Output is correct
14 Correct 33 ms 16152 KB Output is correct
15 Correct 22 ms 16084 KB Output is correct
16 Correct 31 ms 16212 KB Output is correct
17 Correct 31 ms 16164 KB Output is correct
18 Correct 25 ms 16096 KB Output is correct
19 Correct 1438 ms 26228 KB Output is correct
20 Correct 923 ms 26232 KB Output is correct
21 Correct 742 ms 26724 KB Output is correct
22 Correct 370 ms 26224 KB Output is correct
23 Correct 1480 ms 37948 KB Output is correct
24 Execution timed out 1582 ms 37476 KB Time limit exceeded
25 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1578 ms 26224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 16124 KB Output is correct
2 Execution timed out 1587 ms 26148 KB Time limit exceeded
3 Halted 0 ms 0 KB -