Submission #45230

# Submission time Handle Problem Language Result Execution time Memory
45230 2018-04-12T02:50:10 Z gs14004 Golf (JOI17_golf) C++17
30 / 100
10000 ms 28044 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int pool = 500000;

int n, sx, sy, V;
pi s, e;
struct rect{ int sx, ex, sy, ey; }a[100005];
struct seg { int x, s, e, idx; };
vector<seg> segx, segy;
vector<int> gph0[pool];
vector<int> gph1[pool];
int dis[pool];
bool vis[pool];

void solve(vector<seg> &l, vector<seg> &r){
	for(auto &i : l){
		for(auto &j : r){
			if(i.s <= j.x && j.x <= i.e && j.s <= i.x && i.x <= j.e){
				gph1[i.idx].push_back(j.idx);
			}
		}
	}
}

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){
		segx.push_back({y, minx, maxx, V++});
	}
	if(miny < maxy){
		segy.push_back({x, miny, maxy, V++});
	}
}

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);
	sort(segx.begin(), segx.end(), [&](const seg &a, const seg &b){
		return a.x < b.x;
	});
	sort(segy.begin(), segy.end(), [&](const seg &a, const seg &b){
		return a.x < b.x;
	});
	solve(segx, segy);
	solve(segy, segx);
	deque<int> que;
	memset(dis, 0x3f, sizeof(dis));
	for(auto &i : segx){
		if(i.x == s.second && i.s <= s.first && s.first <= i.e){
			dis[i.idx] = 0;
			que.push_back(i.idx);
		}
	}
	for(auto &i : segy){
		if(i.x == s.first && i.s <= s.second && s.second <= i.e){
			dis[i.idx] = 0;
			que.push_back(i.idx);
		}
	}
	while(!que.empty()){
		int x = que.front();
		que.pop_front();
		if(vis[x]) continue;
		vis[x] = 1;
		for(auto &i : gph0[x]){
			if(dis[i] > dis[x]){
				dis[i] = dis[x];
				que.push_front(i);
			}
		}
		for(auto &i : gph1[x]){
			if(dis[i] > dis[x] + 1){
				dis[i] = dis[x] + 1;
				que.push_back(i);
			}
		}
	}
	int ret = 1e9;
	for(auto &i : segx){
		if(i.x == e.second && i.s <= e.first && e.first <= i.e){
			ret = min(ret, dis[i.idx]);
		}
	}
	for(auto &i : segy){
		if(i.x == e.first && i.s <= e.second && e.second <= i.e){
			ret = min(ret, dis[i.idx]);
		}
	}
	cout << ret + 1 << endl;
}

Compilation message

golf.cpp: In function 'int main()':
golf.cpp:53: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 21 ms 25856 KB Output is correct
2 Correct 21 ms 25856 KB Output is correct
3 Correct 19 ms 25856 KB Output is correct
4 Correct 22 ms 25856 KB Output is correct
5 Correct 50 ms 26360 KB Output is correct
6 Correct 45 ms 26360 KB Output is correct
7 Correct 44 ms 26360 KB Output is correct
8 Correct 51 ms 26360 KB Output is correct
9 Correct 49 ms 26360 KB Output is correct
10 Correct 45 ms 26364 KB Output is correct
11 Correct 45 ms 26360 KB Output is correct
12 Correct 48 ms 26360 KB Output is correct
13 Correct 59 ms 26360 KB Output is correct
14 Correct 46 ms 26360 KB Output is correct
15 Correct 23 ms 25984 KB Output is correct
16 Correct 31 ms 26104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 25856 KB Output is correct
2 Correct 21 ms 25856 KB Output is correct
3 Correct 19 ms 25856 KB Output is correct
4 Correct 22 ms 25856 KB Output is correct
5 Correct 50 ms 26360 KB Output is correct
6 Correct 45 ms 26360 KB Output is correct
7 Correct 44 ms 26360 KB Output is correct
8 Correct 51 ms 26360 KB Output is correct
9 Correct 49 ms 26360 KB Output is correct
10 Correct 45 ms 26364 KB Output is correct
11 Correct 45 ms 26360 KB Output is correct
12 Correct 48 ms 26360 KB Output is correct
13 Correct 59 ms 26360 KB Output is correct
14 Correct 46 ms 26360 KB Output is correct
15 Correct 23 ms 25984 KB Output is correct
16 Correct 31 ms 26104 KB Output is correct
17 Correct 48 ms 26232 KB Output is correct
18 Correct 45 ms 26232 KB Output is correct
19 Correct 59 ms 26232 KB Output is correct
20 Correct 45 ms 26284 KB Output is correct
21 Correct 45 ms 26232 KB Output is correct
22 Correct 61 ms 26232 KB Output is correct
23 Correct 61 ms 26232 KB Output is correct
24 Correct 45 ms 26232 KB Output is correct
25 Correct 46 ms 26360 KB Output is correct
26 Correct 48 ms 26232 KB Output is correct
27 Correct 24 ms 25984 KB Output is correct
28 Correct 32 ms 26112 KB Output is correct
29 Correct 32 ms 26104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 25856 KB Output is correct
2 Correct 21 ms 25856 KB Output is correct
3 Correct 19 ms 25856 KB Output is correct
4 Correct 22 ms 25856 KB Output is correct
5 Correct 50 ms 26360 KB Output is correct
6 Correct 45 ms 26360 KB Output is correct
7 Correct 44 ms 26360 KB Output is correct
8 Correct 51 ms 26360 KB Output is correct
9 Correct 49 ms 26360 KB Output is correct
10 Correct 45 ms 26364 KB Output is correct
11 Correct 45 ms 26360 KB Output is correct
12 Correct 48 ms 26360 KB Output is correct
13 Correct 59 ms 26360 KB Output is correct
14 Correct 46 ms 26360 KB Output is correct
15 Correct 23 ms 25984 KB Output is correct
16 Correct 31 ms 26104 KB Output is correct
17 Correct 48 ms 26232 KB Output is correct
18 Correct 45 ms 26232 KB Output is correct
19 Correct 59 ms 26232 KB Output is correct
20 Correct 45 ms 26284 KB Output is correct
21 Correct 45 ms 26232 KB Output is correct
22 Correct 61 ms 26232 KB Output is correct
23 Correct 61 ms 26232 KB Output is correct
24 Correct 45 ms 26232 KB Output is correct
25 Correct 46 ms 26360 KB Output is correct
26 Correct 48 ms 26232 KB Output is correct
27 Correct 24 ms 25984 KB Output is correct
28 Correct 32 ms 26112 KB Output is correct
29 Correct 32 ms 26104 KB Output is correct
30 Execution timed out 10075 ms 28044 KB Time limit exceeded
31 Halted 0 ms 0 KB -