답안 #122481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122481 2019-06-28T11:33:22 Z egorlifar Golf (JOI17_golf) C++17
100 / 100
3834 ms 312700 KB
/*
ЗАПУСКАЕМ 
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░ 
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <chrono>
 
using namespace std;

template<class T1, class T2> inline int chkmin(T1 &x, const T2 &y) {
    if (x > y) {
        x = y;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T1, class T2> inline int chkmax(T1 &x, const T2 &y) {
    if (x < y) {
        x = y;
        return 1;
    }
    else {
        return 0;
    }
}

#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define mp make_pair
#define pb push_back
//#define read(FILENAME) freopen((string(FILENAME) + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((string(FILENAME) + ".out").c_str(), "w", stdout);

template<class iterator> void output(iterator begin, iterator end, ostream &out = cerr) {
    while (begin != end) {
        out << (*begin) << " ";
        begin++;
    }
    out << endl;
}


template<class T> void output(const T &x, ostream &out = cerr) {
    output(x.begin(), x.end(), out);
}

void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0); 
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define szz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
typedef tuple<int, int, int> t3;
 
struct rect {
	rect(){}
	rect(int x1, int x2, int y1, int y2):x1(x1), y1(y1), x2(x2), y2(y2){}
	int x1, x2, y1, y2;
};
struct seg {
	seg(){}
	seg(int x1, int x2, int y, int c) : x1(x1), x2(x2), y(y), c(c) {}
	int x1, x2, y, c;
	bool operator<(const seg &rhs)const {
		if(y != rhs.y) return y < rhs.y;
		return c < rhs.c;
	}
};
pii LR[400040];
 
vector <seg> V;
vector <int> vx, vy;
rect P[100010];
int N, cs, LX;
int T[1<<19];
 
void update_t2(int rt, int l, int r, int s, int e, int val) {
	if(s <= l && r <= e) {
		T[rt] = val; return;
	}
	if(T[rt]) {
		T[rt<<1] = T[rt];
		T[rt<<1|1] = T[rt];
		T[rt] = 0;
	}
	int m = (l + r) >> 1;
	if(s <= m) update_t2(rt<<1, l, m, s, e, val);
	if(m < e) update_t2(rt<<1|1, m+1, r, s, e, val);
}
int read_t2(int rt, int l, int r, int x) {
	if(l == r) return T[rt];
	if(T[rt] != 0) return T[rt];
	int m = (l + r) >> 1;
	if(x <= m) return read_t2(rt<<1, l, m, x);
	else return read_t2(rt<<1|1, m+1, r, x);
}
 
void update_t(int l, int r, int val) {
	if(l <= r) update_t2(1, 0, LX, l, r, val);
}
 
int read_t(int x) {
	return read_t2(1, 0, LX, x);
}
 
void flip_xy() {
	swap(vx, vy); for(int i=1;i<=N;i++) swap(P[i].x1, P[i].y1), swap(P[i].x2, P[i].y2);
}
void get_line(int cmd) {
	for(int i=1;i<=N;i++) {
		V.pb(seg(P[i].x1+1, P[i].x2-1, P[i].y2, 1));
		V.pb(seg(P[i].x1, 4*i+cmd, P[i].y1, -1));
		V.pb(seg(P[i].x2, 4*i+1+cmd, P[i].y1, -1));
	}
	sort(all(V));
	for(auto &s : V) {
		if(s.c < 0) {
			LR[s.x2].Fi = read_t(s.x1);
		}
		else {
			update_t(s.x1, s.x2, s.y);
		}
	}
	memset(T, 0, sizeof T); V.clear();
	
	for(int i=1;i<=N;i++) {
		V.pb(seg(P[i].x1+1, P[i].x2-1, -P[i].y1, 1));
		V.pb(seg(P[i].x1, 4*i+cmd, -P[i].y2, -1));
		V.pb(seg(P[i].x2, 4*i+1+cmd, -P[i].y2, -1));
	}
	sort(all(V));
	for(auto &s : V) {
		if(s.c < 0) {
			int v = -read_t(s.x1);
			if(v == 0) v = LX;
			LR[s.x2].Se = v;
		}
		else {
			update_t(s.x1, s.x2, s.y);
		}
	}
	memset(T, 0, sizeof T); V.clear();
}
 
int dis[400040];
set <pii> Sx[2][1<<19]; const int ADD = 1<<18;
 
void update(int a, int x, int y1, int y2, int val) {
	y1 += ADD; y2 += ADD;
	while(y1 <= y2) {
		if(y1 & 1) Sx[a][y1++].insert(pii(x, val));
		if(!(y2 & 1)) Sx[a][y2--].insert(pii(x, val));
		y1 >>= 1, y2 >>= 1;
	}
}
 
int read(int a, int x1, int x2, int y) {
	y += ADD;
	while(y) {
		auto it = Sx[a][y].lower_bound(pii(x1, -1));
		while(1) {
			if(it == Sx[a][y].end() || it->Fi > x2) break;
			if(dis[it->Se] == -1) return it->Se;
			auto jt = it++;
			Sx[a][y].erase(jt);
		}
		y >>= 1;
	}
	return -1;
}
 
int pp[400040]; int Find(int x) { return pp[x] == x ? x : pp[x] = Find(pp[x]); }
int main() {
	int ss, tt, uu, vv; scanf("%d%d%d%d", &ss, &tt, &uu, &vv);
	scanf("%d", &N);
	P[1] = rect(ss, ss, tt, tt);
	P[2] = rect(uu, uu, vv, vv);
	for(int i=3;i<=N+2;i++) {
		int x1, x2, y1, y2;
		scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
		P[i] = rect(x1, x2, y1, y2);
	}
	N += 2;
	for(int i=1;i<=N;i++) {
		vx.pb(P[i].x1); vx.pb(P[i].x2);
		vy.pb(P[i].y1); vy.pb(P[i].y2);
	}
	sort(all(vx)); vx.resize(unique(all(vx)) - vx.begin());
	sort(all(vy)); vy.resize(unique(all(vy)) - vy.begin());
	for(int i=1;i<=N;i++) {
		P[i].x1 = (int)(lower_bound(all(vx), P[i].x1) - vx.begin() + 1);
		P[i].x2 = (int)(lower_bound(all(vx), P[i].x2) - vx.begin() + 1);
		P[i].y1 = (int)(lower_bound(all(vy), P[i].y1) - vy.begin() + 1);
		P[i].y2 = (int)(lower_bound(all(vy), P[i].y2) - vy.begin() + 1);
	}
	LX = max(szz(vx), szz(vy)) + 1;
	
	get_line(0);
	flip_xy();
	get_line(2);
	flip_xy();
	
	map <t3, int> Mx[2];
	rep(i, 4*N+5) pp[i] = i;
	for(int i=1;i<=N;i++) {
		rep(a, 4) {
			pii p = LR[4*i+a];
			t3 t;
			if(a == 0) t = t3(P[i].x1, p.Fi, p.Se);
			else if(a == 1) t = t3(P[i].x2, p.Fi, p.Se);
			else if(a == 2) t = t3(P[i].y1, p.Fi, p.Se);
			else t = t3(P[i].y2, p.Fi, p.Se);
			int ta = !!(a & 2);
			if(Mx[ta].find(t) == Mx[ta].end()) Mx[ta][t] = 4*i+a;
			else pp[Find(4*i+a)] = Find(Mx[ta][t]);
		}
	}
	
	for(int i=4;i<4*N+4;i++) if(pp[i] == i) {
		if(i&2) {
			int x = (i%4==2 ? P[i/4].y1 : P[i/4].y2);
			update(1, x, LR[i].Fi, LR[i].Se, i);
		}
		else {
			int x = (i%4==0 ? P[i/4].x1 : P[i/4].x2);
			update(0, x, LR[i].Fi, LR[i].Se, i);
		}
	}
	
	memset(dis, -1, sizeof dis);
	vector <int> q;
	for(int i=4;i<=7;i++) {
		int pi = Find(i);
		if(dis[pi] == -1) {
			dis[pi] = 1; q.pb(pi);
		}
	}
	for(int i=8;i<12;i++) if(dis[Find(i)] == 1) {
		puts("1");
		return 0;
	}
	
	rep(i, szz(q)) {
		int t = q[i];
		while(1) {
			int v = -1;
			if(t&2) {
				int x = (t%4==2 ? P[t/4].y1 : P[t/4].y2);
				v = read(0, LR[t].Fi, LR[t].Se, x);
			}
			else {
				int x = (t%4==0 ? P[t/4].x1 : P[t/4].x2);
				v = read(1, LR[t].Fi, LR[t].Se, x);
			}
			if(v == -1) break;
			q.pb(v); dis[v] = dis[t] + 1;
			
			for(int i=8;i<12;i++) if(Find(i) == v) {
				printf("%d\n", dis[v]);
				return 0;
			}
		}
	}
	int ans = 1e9;
	for(int i=8;i<12;i++) {
		int pi = Find(i);
		if(dis[pi] != -1) ans = min(ans, dis[pi]);
	}
	printf("%d\n", ans);
	
	return 0;
}

Compilation message

golf.cpp: In constructor 'rect::rect(int, int, int, int)':
golf.cpp:107:14: warning: 'rect::y1' will be initialized after [-Wreorder]
  int x1, x2, y1, y2;
              ^~
golf.cpp:107:10: warning:   'int rect::x2' [-Wreorder]
  int x1, x2, y1, y2;
          ^~
golf.cpp:106:2: warning:   when initialized here [-Wreorder]
  rect(int x1, int x2, int y1, int y2):x1(x1), y1(y1), x2(x2), y2(y2){}
  ^~~~
golf.cpp: In function 'int main()':
golf.cpp:223:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int ss, tt, uu, vv; scanf("%d%d%d%d", &ss, &tt, &uu, &vv);
                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
golf.cpp:224:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
golf.cpp:229: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);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 53248 KB Output is correct
2 Correct 35 ms 53248 KB Output is correct
3 Correct 37 ms 53240 KB Output is correct
4 Correct 46 ms 53376 KB Output is correct
5 Correct 44 ms 54392 KB Output is correct
6 Correct 69 ms 54268 KB Output is correct
7 Correct 50 ms 54264 KB Output is correct
8 Correct 45 ms 54400 KB Output is correct
9 Correct 53 ms 54392 KB Output is correct
10 Correct 46 ms 54392 KB Output is correct
11 Correct 55 ms 54368 KB Output is correct
12 Correct 46 ms 54272 KB Output is correct
13 Correct 45 ms 54312 KB Output is correct
14 Correct 43 ms 54400 KB Output is correct
15 Correct 40 ms 53632 KB Output is correct
16 Correct 43 ms 54008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 53248 KB Output is correct
2 Correct 35 ms 53248 KB Output is correct
3 Correct 37 ms 53240 KB Output is correct
4 Correct 46 ms 53376 KB Output is correct
5 Correct 44 ms 54392 KB Output is correct
6 Correct 69 ms 54268 KB Output is correct
7 Correct 50 ms 54264 KB Output is correct
8 Correct 45 ms 54400 KB Output is correct
9 Correct 53 ms 54392 KB Output is correct
10 Correct 46 ms 54392 KB Output is correct
11 Correct 55 ms 54368 KB Output is correct
12 Correct 46 ms 54272 KB Output is correct
13 Correct 45 ms 54312 KB Output is correct
14 Correct 43 ms 54400 KB Output is correct
15 Correct 40 ms 53632 KB Output is correct
16 Correct 43 ms 54008 KB Output is correct
17 Correct 48 ms 54912 KB Output is correct
18 Correct 59 ms 54872 KB Output is correct
19 Correct 48 ms 54732 KB Output is correct
20 Correct 64 ms 54904 KB Output is correct
21 Correct 49 ms 54880 KB Output is correct
22 Correct 47 ms 54904 KB Output is correct
23 Correct 52 ms 54876 KB Output is correct
24 Correct 47 ms 54912 KB Output is correct
25 Correct 46 ms 54840 KB Output is correct
26 Correct 47 ms 54784 KB Output is correct
27 Correct 38 ms 53736 KB Output is correct
28 Correct 42 ms 54136 KB Output is correct
29 Correct 42 ms 54216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 53248 KB Output is correct
2 Correct 35 ms 53248 KB Output is correct
3 Correct 37 ms 53240 KB Output is correct
4 Correct 46 ms 53376 KB Output is correct
5 Correct 44 ms 54392 KB Output is correct
6 Correct 69 ms 54268 KB Output is correct
7 Correct 50 ms 54264 KB Output is correct
8 Correct 45 ms 54400 KB Output is correct
9 Correct 53 ms 54392 KB Output is correct
10 Correct 46 ms 54392 KB Output is correct
11 Correct 55 ms 54368 KB Output is correct
12 Correct 46 ms 54272 KB Output is correct
13 Correct 45 ms 54312 KB Output is correct
14 Correct 43 ms 54400 KB Output is correct
15 Correct 40 ms 53632 KB Output is correct
16 Correct 43 ms 54008 KB Output is correct
17 Correct 48 ms 54912 KB Output is correct
18 Correct 59 ms 54872 KB Output is correct
19 Correct 48 ms 54732 KB Output is correct
20 Correct 64 ms 54904 KB Output is correct
21 Correct 49 ms 54880 KB Output is correct
22 Correct 47 ms 54904 KB Output is correct
23 Correct 52 ms 54876 KB Output is correct
24 Correct 47 ms 54912 KB Output is correct
25 Correct 46 ms 54840 KB Output is correct
26 Correct 47 ms 54784 KB Output is correct
27 Correct 38 ms 53736 KB Output is correct
28 Correct 42 ms 54136 KB Output is correct
29 Correct 42 ms 54216 KB Output is correct
30 Correct 2730 ms 275452 KB Output is correct
31 Correct 2905 ms 276236 KB Output is correct
32 Correct 3278 ms 279940 KB Output is correct
33 Correct 3567 ms 280544 KB Output is correct
34 Correct 3834 ms 281176 KB Output is correct
35 Correct 3706 ms 283468 KB Output is correct
36 Correct 2599 ms 279764 KB Output is correct
37 Correct 3168 ms 280800 KB Output is correct
38 Correct 3447 ms 281076 KB Output is correct
39 Correct 3715 ms 281288 KB Output is correct
40 Correct 2341 ms 312700 KB Output is correct
41 Correct 2713 ms 293360 KB Output is correct
42 Correct 1688 ms 239728 KB Output is correct
43 Correct 1978 ms 240952 KB Output is correct
44 Correct 2512 ms 260044 KB Output is correct
45 Correct 2699 ms 261408 KB Output is correct
46 Correct 2445 ms 261492 KB Output is correct
47 Correct 2422 ms 279484 KB Output is correct
48 Correct 1948 ms 241516 KB Output is correct
49 Correct 2563 ms 261592 KB Output is correct
50 Correct 58 ms 54136 KB Output is correct
51 Correct 44 ms 54136 KB Output is correct
52 Correct 41 ms 54136 KB Output is correct