Submission #126857

# Submission time Handle Problem Language Result Execution time Memory
126857 2019-07-08T14:21:15 Z sebinkim Golf (JOI17_golf) C++14
100 / 100
4295 ms 367128 KB
#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define compress(V) (sort((V).begin(), (V).end()), (V).erase(unique((V).begin(), (V).end()), (V).end()))
#define getcoor(V, x) (lb((V).begin(), (V).end(), (x)) - (V).begin())

using namespace std;

typedef pair <int, int> pii;

struct rect{
	int x1, x2, y1, y2;
	rect() {}
	rect(int x1, int x2, int y1, int y2) :
		x1(x1), x2(x2), y1(y1), y2(y2) {}
	bool operator == (rect &r)
		{ return x1 == r.x1 && x2 == r.x2 && y1 == r.y1 && y2 == r.y2; }
	void rotate() { swap(x1, y1); swap(x2, y2); }
};

struct segtree{
	set <pii> T[1101010];
	int sz = 1 << 19;
	
	void insert(int l, int r, int p, int i)
	{
		l += sz; r += sz;
		
		for(; l<=r; ){
			if(l & 1) T[l].insert(pii(p, i));
			if(~r & 1) T[r].insert(pii(p, i));
			l = l + 1 >> 1;
			r = r - 1 >> 1;
		}
	}
	
	void find(int p, int l, int r, queue <pii> &Q, int *C, int d)
	{
		for(p+=sz; p; p>>=1){
			for(; ; ){
				auto it = T[p].lower_bound(pii(l, -1));
				if(it == T[p].end() || it -> first > r) break;
				
				if(!C[it -> second]){
					C[it -> second] = 1;
					Q.push(pii(it -> second, d + 1));
				}
				T[p].erase(it);
			}
		}
	}
};

segtree _TX, _TY;
segtree *TX = &_TX, *TY = &_TY;
vector <rect> R, L;
set <int> S;
vector <pii> VI[404040], VO[404040];
vector <int> X, Y, V;
queue <pii> Q;
int C[1101010];
int n, x, y, sx, sy, ex, ey;
int vsx, vsy, vex, vey;

int main()
{
	int i, j, l, r, p, d;
	int x1, x2, y1, y2;
	
	scanf("%d%d%d%d%d", &sx, &sy, &ex, &ey, &n);
	
	X.pb(0); X.pb(2e9); Y.pb(0); Y.pb(2e9);
	X.pb(sx); X.pb(ex); Y.pb(sy); Y.pb(ey);
	
	for(i=0; i<n; i++){
		scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
		X.pb(x1); X.pb(x2); Y.pb(y1); Y.pb(y2);
		R.emplace_back(x1, x2, y1, y2);
	}
	
	compress(X); compress(Y);
	x = X.size(); y = Y.size();
	
	sx = getcoor(X, sx); sy = getcoor(Y, sy);
	ex = getcoor(X, ex); ey = getcoor(Y, ey);
	
	for(i=0; i<n; i++){
		R[i].x1 = getcoor(X, R[i].x1); R[i].x2 = getcoor(X, R[i].x2);
		R[i].y1 = getcoor(Y, R[i].y1); R[i].y2 = getcoor(Y, R[i].y2);
	}
	
	for(i=0; i<2; i++){
		S.clear();
		
		for(j=0; j<x; j++){
			VI[j].clear(); VO[j].clear();
		}
		
		for(rect &r: R){
			VI[r.x1].emplace_back(r.y1, r.y2);
			VO[r.x2].emplace_back(r.y1, r.y2);
		}
		
		S.clear();
		S.insert(0); S.insert(y - 1);
		
		for(j=0; j<x; j++){
			for(pii &t: VO[j]){
				S.erase(t.first); S.erase(t.second);
				l = *prev(S.lb(t.first)); r = *S.lb(t.second);
				L.emplace_back(j, j, l, r);
				TY -> insert(l, r, j, L.size() - 1);
			}
			
			if(sx == j){
				l = *prev(S.lb(sy)); r = *S.lb(sy);
				L.emplace_back(j, j, l, r);
				vsx = L.size() - 1;
			}
			
			if(ex == j){
				l = *prev(S.lb(ey)); r = *S.lb(ey);
				L.emplace_back(j, j, l, r);
				TY -> insert(l, r, j, L.size() - 1);
				vex = L.size() - 1;
			}
			
			for(pii &t: VI[j]){
				l = *prev(S.lb(t.first)); r = *S.lb(t.second);
				L.emplace_back(j, j, l, r);
				TY -> insert(l, r, j, L.size() - 1);
				S.insert(t.first); S.insert(t.second);
			}
		}
		
		swap(x, y); swap(X, Y); swap(TX, TY);
		swap(sx, sy); swap(ex, ey);
		swap(vsx, vsy); swap(vex, vey);
		
		for(rect &r: R) r.rotate();
		for(rect &r: L) r.rotate();
	}
	
	if(L[vsx] == L[vex] || L[vsy] == L[vey]){
		printf("1\n"); return 0;
	}
	
	Q.push(pii(vsx, 1)); Q.push(pii(vsy, 1));
	C[vsx] = 1; C[vsy] = 1;
	
	for(; !Q.empty(); ){
		tie(p, d) = Q.front(); Q.pop();
		if(p == vex || p == vey){
			printf("%d\n", d); return 0;
		}
		
		V.clear();
		
		if(L[p].x1 == L[p].x2) TX -> find(L[p].x1, L[p].y1, L[p].y2, Q, C, d);
		else TY -> find(L[p].y1, L[p].x1, L[p].x2, Q, C, d);
	}
	
	return 0;
}

Compilation message

golf.cpp: In member function 'void segtree::insert(int, int, int, int)':
golf.cpp:34:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    l = l + 1 >> 1;
        ~~^~~
golf.cpp:35:10: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
    r = r - 1 >> 1;
        ~~^~~
golf.cpp: In function 'int main()':
golf.cpp:72:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d%d", &sx, &sy, &ex, &ey, &n);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
golf.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 84 ms 122744 KB Output is correct
2 Correct 80 ms 122712 KB Output is correct
3 Correct 79 ms 122744 KB Output is correct
4 Correct 109 ms 122876 KB Output is correct
5 Correct 94 ms 123916 KB Output is correct
6 Correct 102 ms 123872 KB Output is correct
7 Correct 88 ms 123896 KB Output is correct
8 Correct 92 ms 123964 KB Output is correct
9 Correct 99 ms 123896 KB Output is correct
10 Correct 102 ms 123896 KB Output is correct
11 Correct 92 ms 123968 KB Output is correct
12 Correct 90 ms 123896 KB Output is correct
13 Correct 101 ms 123896 KB Output is correct
14 Correct 89 ms 123896 KB Output is correct
15 Correct 82 ms 123256 KB Output is correct
16 Correct 88 ms 123896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 122744 KB Output is correct
2 Correct 80 ms 122712 KB Output is correct
3 Correct 79 ms 122744 KB Output is correct
4 Correct 109 ms 122876 KB Output is correct
5 Correct 94 ms 123916 KB Output is correct
6 Correct 102 ms 123872 KB Output is correct
7 Correct 88 ms 123896 KB Output is correct
8 Correct 92 ms 123964 KB Output is correct
9 Correct 99 ms 123896 KB Output is correct
10 Correct 102 ms 123896 KB Output is correct
11 Correct 92 ms 123968 KB Output is correct
12 Correct 90 ms 123896 KB Output is correct
13 Correct 101 ms 123896 KB Output is correct
14 Correct 89 ms 123896 KB Output is correct
15 Correct 82 ms 123256 KB Output is correct
16 Correct 88 ms 123896 KB Output is correct
17 Correct 92 ms 124192 KB Output is correct
18 Correct 92 ms 124152 KB Output is correct
19 Correct 91 ms 124152 KB Output is correct
20 Correct 87 ms 124152 KB Output is correct
21 Correct 87 ms 124152 KB Output is correct
22 Correct 95 ms 124280 KB Output is correct
23 Correct 91 ms 124152 KB Output is correct
24 Correct 138 ms 124152 KB Output is correct
25 Correct 97 ms 124284 KB Output is correct
26 Correct 87 ms 124152 KB Output is correct
27 Correct 82 ms 123384 KB Output is correct
28 Correct 116 ms 124000 KB Output is correct
29 Correct 148 ms 124152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 122744 KB Output is correct
2 Correct 80 ms 122712 KB Output is correct
3 Correct 79 ms 122744 KB Output is correct
4 Correct 109 ms 122876 KB Output is correct
5 Correct 94 ms 123916 KB Output is correct
6 Correct 102 ms 123872 KB Output is correct
7 Correct 88 ms 123896 KB Output is correct
8 Correct 92 ms 123964 KB Output is correct
9 Correct 99 ms 123896 KB Output is correct
10 Correct 102 ms 123896 KB Output is correct
11 Correct 92 ms 123968 KB Output is correct
12 Correct 90 ms 123896 KB Output is correct
13 Correct 101 ms 123896 KB Output is correct
14 Correct 89 ms 123896 KB Output is correct
15 Correct 82 ms 123256 KB Output is correct
16 Correct 88 ms 123896 KB Output is correct
17 Correct 92 ms 124192 KB Output is correct
18 Correct 92 ms 124152 KB Output is correct
19 Correct 91 ms 124152 KB Output is correct
20 Correct 87 ms 124152 KB Output is correct
21 Correct 87 ms 124152 KB Output is correct
22 Correct 95 ms 124280 KB Output is correct
23 Correct 91 ms 124152 KB Output is correct
24 Correct 138 ms 124152 KB Output is correct
25 Correct 97 ms 124284 KB Output is correct
26 Correct 87 ms 124152 KB Output is correct
27 Correct 82 ms 123384 KB Output is correct
28 Correct 116 ms 124000 KB Output is correct
29 Correct 148 ms 124152 KB Output is correct
30 Correct 3250 ms 327984 KB Output is correct
31 Correct 3470 ms 328300 KB Output is correct
32 Correct 3795 ms 329476 KB Output is correct
33 Correct 4282 ms 330304 KB Output is correct
34 Correct 3580 ms 332572 KB Output is correct
35 Correct 4295 ms 332020 KB Output is correct
36 Correct 3545 ms 331532 KB Output is correct
37 Correct 3792 ms 331492 KB Output is correct
38 Correct 3545 ms 332840 KB Output is correct
39 Correct 3853 ms 330692 KB Output is correct
40 Correct 2236 ms 367128 KB Output is correct
41 Correct 1776 ms 348324 KB Output is correct
42 Correct 1384 ms 285724 KB Output is correct
43 Correct 1542 ms 293600 KB Output is correct
44 Correct 1370 ms 296032 KB Output is correct
45 Correct 1527 ms 315484 KB Output is correct
46 Correct 2334 ms 306412 KB Output is correct
47 Correct 1920 ms 323652 KB Output is correct
48 Correct 1599 ms 306092 KB Output is correct
49 Correct 1543 ms 307456 KB Output is correct
50 Correct 101 ms 124024 KB Output is correct
51 Correct 95 ms 124108 KB Output is correct
52 Correct 101 ms 124024 KB Output is correct