Submission #45228

# Submission time Handle Problem Language Result Execution time Memory
45228 2018-04-12T01:31:48 Z gs14004 Golf (JOI17_golf) C++17
30 / 100
103 ms 2176 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int MAXN = 300005;

int n, sx, sy;
pi s, e;
struct rect{ int sx, ex, sy, ey; }a[1005];
struct seg { int x, s, e, t; };
vector<seg> segs;
int dis[400500];

bool cross(seg a, seg b){
	return a.t != b.t && b.s <= a.x && a.x <= b.e && a.s <= b.x && b.x <= a.e;
}

void shoot(int x, int y){
	int minx = 0, maxx = sx-1;
	int miny = 0, maxy = sy-1;
	for(int i=0; i<n; i++){
		if(a[i].sy < y && a[i].ey > y){
			if(x <= a[i].sx) maxx = min(maxx, a[i].sx);
			if(x >= a[i].ex) minx = max(minx, a[i].ex);
		}
		if(a[i].sx < x && a[i].ex > x){
			if(y <= a[i].sy) maxy = min(maxy, a[i].sy);
			if(y >= a[i].ey) miny = max(miny, a[i].ey);
		}
	}
	if(minx < maxx){
		segs.push_back({y, minx, maxx, 0});
	}
	if(miny < maxy){
		segs.push_back({x, miny, maxy, 1});
	}
}

int main(){
	cin >> s.first >> s.second >> e.first >> e.second >> n;
	vector<int> vx = {s.first, e.first}, vy = {s.second, e.second};
	for(int i=0; i<n; i++){
		scanf("%d %d %d %d",&a[i].sx,&a[i].ex,&a[i].sy,&a[i].ey);
		vx.push_back(a[i].sx);
		vx.push_back(a[i].ex);
		vy.push_back(a[i].sy);
		vy.push_back(a[i].ey);
	}
	sort(vx.begin(), vx.end());
	vx.resize(unique(vx.begin(), vx.end()) - vx.begin());
	sort(vy.begin(), vy.end());
	vy.resize(unique(vy.begin(), vy.end()) - vy.begin());
	auto getloc = [&](int x, vector<int> &v){
		return lower_bound(v.begin(), v.end(), x) - v.begin();
	};
	s.first = getloc(s.first, vx);
	s.second = getloc(s.second, vy);
	e.first = getloc(e.first, vx);
	e.second = getloc(e.second, vy);
	for(int i=0; i<n; i++){
		a[i].sx = getloc(a[i].sx, vx);
		a[i].ex = getloc(a[i].ex, vx);
		a[i].sy = getloc(a[i].sy, vy);
		a[i].ey = getloc(a[i].ey, vy);
	}
	sx = vx.size();
	sy = vy.size();
	shoot(s.first, s.second);
	shoot(e.first, e.second);
	for(int i=0; i<n; i++){
		shoot(a[i].sx, a[i].sy);
		shoot(a[i].ex, a[i].ey);
	}
	shoot(0, 0);
	shoot(sx-1, sy-1);
	memset(dis, 0x3f, sizeof(dis));
	queue<int> que;
	for(int i=0; i<segs.size(); i++){
		if(segs[i].t == 1 && segs[i].x == s.first && segs[i].s <= s.second && segs[i].e >= s.second){
			dis[i] = 0;
			que.push(i);
		}
		if(segs[i].t == 0 && segs[i].x == s.second && segs[i].s <= s.first && segs[i].e >= s.first){
			dis[i] = 0;
			que.push(i);
		}
	}
	while(!que.empty()){
		int x = que.front();
		que.pop();
		for(int i=0; i<segs.size(); i++){
			if(dis[i] > dis[x] + 1 && cross(segs[x], segs[i])){
				dis[i] = dis[x] + 1;
				que.push(i);
			}
		}
	}
	int ret = 1e9;
	for(int i=0; i<segs.size(); i++){
		if(segs[i].t == 1 && segs[i].x == e.first && segs[i].s <= e.second && segs[i].e >= e.second){
			ret = min(ret, dis[i]);
		}
		if(segs[i].t == 0 && segs[i].x == e.second && segs[i].s <= e.first && segs[i].e >= e.first){
			ret = min(ret, dis[i]);
		}
	}
	cout << ret + 1 << endl;
}

Compilation message

golf.cpp: In function 'int main()':
golf.cpp:79:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<segs.size(); i++){
               ~^~~~~~~~~~~~
golf.cpp:92:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0; i<segs.size(); i++){
                ~^~~~~~~~~~~~
golf.cpp:100:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<segs.size(); i++){
               ~^~~~~~~~~~~~
golf.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d %d",&a[i].sx,&a[i].ex,&a[i].sy,&a[i].ey);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 6 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 6 ms 1920 KB Output is correct
5 Correct 89 ms 2048 KB Output is correct
6 Correct 95 ms 2048 KB Output is correct
7 Correct 90 ms 2168 KB Output is correct
8 Correct 94 ms 2048 KB Output is correct
9 Correct 94 ms 2092 KB Output is correct
10 Correct 93 ms 2084 KB Output is correct
11 Correct 94 ms 2048 KB Output is correct
12 Correct 96 ms 2096 KB Output is correct
13 Correct 97 ms 2048 KB Output is correct
14 Correct 98 ms 2040 KB Output is correct
15 Correct 16 ms 2048 KB Output is correct
16 Correct 52 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 6 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 6 ms 1920 KB Output is correct
5 Correct 89 ms 2048 KB Output is correct
6 Correct 95 ms 2048 KB Output is correct
7 Correct 90 ms 2168 KB Output is correct
8 Correct 94 ms 2048 KB Output is correct
9 Correct 94 ms 2092 KB Output is correct
10 Correct 93 ms 2084 KB Output is correct
11 Correct 94 ms 2048 KB Output is correct
12 Correct 96 ms 2096 KB Output is correct
13 Correct 97 ms 2048 KB Output is correct
14 Correct 98 ms 2040 KB Output is correct
15 Correct 16 ms 2048 KB Output is correct
16 Correct 52 ms 2176 KB Output is correct
17 Correct 93 ms 2048 KB Output is correct
18 Correct 93 ms 2092 KB Output is correct
19 Correct 93 ms 2048 KB Output is correct
20 Correct 99 ms 2048 KB Output is correct
21 Correct 102 ms 2048 KB Output is correct
22 Correct 95 ms 2048 KB Output is correct
23 Correct 103 ms 2040 KB Output is correct
24 Correct 94 ms 2048 KB Output is correct
25 Correct 95 ms 2048 KB Output is correct
26 Correct 99 ms 2040 KB Output is correct
27 Correct 25 ms 2048 KB Output is correct
28 Correct 55 ms 2048 KB Output is correct
29 Correct 61 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1920 KB Output is correct
2 Correct 6 ms 1920 KB Output is correct
3 Correct 5 ms 1920 KB Output is correct
4 Correct 6 ms 1920 KB Output is correct
5 Correct 89 ms 2048 KB Output is correct
6 Correct 95 ms 2048 KB Output is correct
7 Correct 90 ms 2168 KB Output is correct
8 Correct 94 ms 2048 KB Output is correct
9 Correct 94 ms 2092 KB Output is correct
10 Correct 93 ms 2084 KB Output is correct
11 Correct 94 ms 2048 KB Output is correct
12 Correct 96 ms 2096 KB Output is correct
13 Correct 97 ms 2048 KB Output is correct
14 Correct 98 ms 2040 KB Output is correct
15 Correct 16 ms 2048 KB Output is correct
16 Correct 52 ms 2176 KB Output is correct
17 Correct 93 ms 2048 KB Output is correct
18 Correct 93 ms 2092 KB Output is correct
19 Correct 93 ms 2048 KB Output is correct
20 Correct 99 ms 2048 KB Output is correct
21 Correct 102 ms 2048 KB Output is correct
22 Correct 95 ms 2048 KB Output is correct
23 Correct 103 ms 2040 KB Output is correct
24 Correct 94 ms 2048 KB Output is correct
25 Correct 95 ms 2048 KB Output is correct
26 Correct 99 ms 2040 KB Output is correct
27 Correct 25 ms 2048 KB Output is correct
28 Correct 55 ms 2048 KB Output is correct
29 Correct 61 ms 2048 KB Output is correct
30 Execution timed out 6 ms 384 KB Time limit exceeded (wall clock)
31 Halted 0 ms 0 KB -