답안 #229982

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
229982 2020-05-07T12:55:42 Z dennisstar 절취선 (JOI14_ho_t5) C++17
100 / 100
1988 ms 148284 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define all(V) (V).begin(), (V).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int MX = 7<<15;

struct line {
	int x1, x2, y, t;
	line (int A, int B, int C, int D) {
		if (B==D) x1=A, x2=C, y=B, t=0;
		else x1=B, x2=D, y=A, t=1;
	}
};

int W, H, N;
vector<line> L;

struct seg {
	set<pii> st[1<<19];
	int l, r, y, x1;
	pii v;
	void U(int i=1, int s=0, int e=MX) {
		if (e<l||r<s) return ;
		if (l<=s&&e<=r) { st[i].em(v); return ; }
		int m=(s+e)/2;
		U(i*2, s, m); U(i*2+1, m+1, e);
	}
	void upd(int L, int R, pii V) {
		l=L, r=R, v=V;
		if (l>r) swap(l, r);
		U();
	}
	pii G(int i=1, int s=0, int e=MX) {
		auto k=st[i].lower_bound(pii(x1, -1));
		pii r=(k==st[i].end()?pii(MX+5, -1):*k);
		if (s==e) return r;
		int m=(s+e)/2;
		if (y<=m) return min(r, G(i*2, s, m));
		return min(r, G(i*2+1, m+1, e));
	}
	pii get(int Y, int X1) {
		y=Y, x1=X1;
		return G();
	}
	void E(int i=1, int s=0, int e=MX) {
		if (e<l||r<s) return ;
		if (l<=s&&e<=r) { st[i].erase(v); return ; }
		int m=(s+e)/2;
		E(i*2, s, m); E(i*2+1, m+1, e);
	}
	void er(int L, int R, pii V) {
		l=L, r=R, v=V;
		if (l>r) swap(l, r);
		E();
	}
}S[2];

int Find(vector<int> &V, int c) { return lower_bound(all(V), c)-V.begin(); }

vector<int> C1;
void dfs(int n) {
	S[L[n].t].er(L[n].x1, L[n].x2, pii(L[n].y, n));
	C1[n]=1;
	while (1) {
		auto k=S[L[n].t^1].get(L[n].y, L[n].x1);
		if (k.fi<=L[n].x2) dfs(k.se);
		else break;
	}
}

ll Component() {
	C1.resize(N);
	for (int i=0; i<N; i++) S[L[i].t].upd(L[i].x1, L[i].x2, pii(L[i].y, i));
	ll r=0;
	for (int i=0; i<N; i++) if (!C1[i]) r++, dfs(i);
	return r;
}

struct FW {
	int F[1<<18];
	void upd(int t, int v) { t++; while (t<(1<<18)) F[t]+=v, t+=t&-t; }
	int get(int t) { int r=0; while (t) r+=F[t], t-=t&-t; return r; }
	int get(int l, int r) {
		if (l>r) return 0;
		return get(r+1)-get(l);
	}
}T[2];

ll VE() {
	ll V=0, VV=0, E=0;
	vector<vector<int>> V1(MX), V2(MX);
	vector<line> G[2];
	for (auto &i:L) G[i.t].eb(i);
	for (auto &i:G[1]) V1[i.x2].eb(i.y);
	for (auto &i:G[0]) V2[i.x2].eb(i.y);

	int s=0, e=0;
	sort(all(G[0]), [](line &l1, line &l2) { return l1.y<l2.y; });
	sort(all(G[1]), [](line &l1, line &l2) { return l1.x1<l2.x1; });
	for (auto &i:G[0]) {
		for (; s<G[1].size()&&G[1][s].x1<=i.y; s++) T[0].upd(G[1][s].y, 1);
		for (; e<i.y; e++) for (auto &j:V1[e]) T[0].upd(j, -1);
		int r1=T[0].get(i.x1, i.x1), r2=T[0].get(i.x1+1, i.x2-1), r3=T[0].get(i.x2, i.x2);
		E+=r2+1;
		VV+=r1+r2+r3;
		V+=2-r1-r3;
	}

	s=0, e=0;
	sort(all(G[1]), [](line &l1, line &l2) { return l1.y<l2.y; });
	sort(all(G[0]), [](line &l1, line &l2) { return l1.x1<l2.x1; });
	for (auto &i:G[1]) {
		for (; s<G[0].size()&&G[0][s].x1<=i.y; s++) T[1].upd(G[0][s].y, 1);
		for (; e<i.y; e++) for (auto &j:V2[e]) T[1].upd(j, -1);
		int r1=T[1].get(i.x1, i.x1), r2=T[1].get(i.x1+1, i.x2-1), r3=T[1].get(i.x2, i.x2);
		E+=r2+1;
		VV+=r1+r2+r3;
		V+=2-r1-r3;
	}

	return VV/2+V-E;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	cin>>H>>W>>N;

	vector<int> cx, cy;
	cx.eb(0), cx.eb(H);
	cy.eb(0), cy.eb(W);
	L.eb(0, 0, 0, W);
	L.eb(H, 0, H, W);
	L.eb(0, 0, H, 0);
	L.eb(0, W, H, W);

	for (int i=0, a, b, c, d; i<N; i++)
		cin>>a>>b>>c>>d,
		L.eb(a, b, c, d),
		cx.eb(a), cx.eb(c),
		cy.eb(b), cy.eb(d);

	sort(all(cx)); sort(all(cy));
	H=Find(cx, H); W=Find(cy, W);
	for (auto &i:L) {
		if (i.t==0) i.x1=Find(cx, i.x1), i.x2=Find(cx, i.x2), i.y=Find(cy, i.y);
		if (i.t==1) i.x1=Find(cy, i.x1), i.x2=Find(cy, i.x2), i.y=Find(cx, i.y);
	}

	N+=4;
	cout<<Component()-VE()<<'\n';
	return 0;
}

Compilation message

2014_ho_t5.cpp: In function 'll VE()':
2014_ho_t5.cpp:107:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (; s<G[1].size()&&G[1][s].x1<=i.y; s++) T[0].upd(G[1][s].y, 1);
          ~^~~~~~~~~~~~
2014_ho_t5.cpp:119:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (; s<G[0].size()&&G[0][s].x1<=i.y; s++) T[1].upd(G[0][s].y, 1);
          ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 60416 KB Output is correct
2 Correct 39 ms 60408 KB Output is correct
3 Correct 45 ms 60400 KB Output is correct
4 Correct 39 ms 60536 KB Output is correct
5 Correct 40 ms 60536 KB Output is correct
6 Correct 38 ms 60412 KB Output is correct
7 Correct 40 ms 60672 KB Output is correct
8 Correct 44 ms 61056 KB Output is correct
9 Correct 41 ms 60792 KB Output is correct
10 Correct 42 ms 61048 KB Output is correct
11 Correct 45 ms 61032 KB Output is correct
12 Correct 40 ms 60664 KB Output is correct
13 Correct 45 ms 61056 KB Output is correct
14 Correct 42 ms 60920 KB Output is correct
15 Correct 43 ms 60920 KB Output is correct
16 Correct 39 ms 60408 KB Output is correct
17 Correct 41 ms 60408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 60416 KB Output is correct
2 Correct 39 ms 60408 KB Output is correct
3 Correct 45 ms 60400 KB Output is correct
4 Correct 39 ms 60536 KB Output is correct
5 Correct 40 ms 60536 KB Output is correct
6 Correct 38 ms 60412 KB Output is correct
7 Correct 40 ms 60672 KB Output is correct
8 Correct 44 ms 61056 KB Output is correct
9 Correct 41 ms 60792 KB Output is correct
10 Correct 42 ms 61048 KB Output is correct
11 Correct 45 ms 61032 KB Output is correct
12 Correct 40 ms 60664 KB Output is correct
13 Correct 45 ms 61056 KB Output is correct
14 Correct 42 ms 60920 KB Output is correct
15 Correct 43 ms 60920 KB Output is correct
16 Correct 39 ms 60408 KB Output is correct
17 Correct 41 ms 60408 KB Output is correct
18 Correct 39 ms 60544 KB Output is correct
19 Correct 41 ms 60536 KB Output is correct
20 Correct 39 ms 60536 KB Output is correct
21 Correct 42 ms 61056 KB Output is correct
22 Correct 42 ms 60800 KB Output is correct
23 Correct 42 ms 61056 KB Output is correct
24 Correct 43 ms 61176 KB Output is correct
25 Correct 46 ms 61048 KB Output is correct
26 Correct 41 ms 60664 KB Output is correct
27 Correct 43 ms 61048 KB Output is correct
28 Correct 44 ms 61056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 60800 KB Output is correct
2 Correct 43 ms 60792 KB Output is correct
3 Correct 41 ms 60672 KB Output is correct
4 Correct 44 ms 61176 KB Output is correct
5 Correct 99 ms 66556 KB Output is correct
6 Correct 454 ms 75576 KB Output is correct
7 Correct 1036 ms 99280 KB Output is correct
8 Correct 1018 ms 100560 KB Output is correct
9 Correct 611 ms 84200 KB Output is correct
10 Correct 329 ms 71528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 60544 KB Output is correct
2 Correct 42 ms 61048 KB Output is correct
3 Correct 122 ms 62968 KB Output is correct
4 Correct 40 ms 60416 KB Output is correct
5 Correct 43 ms 61048 KB Output is correct
6 Correct 1945 ms 143080 KB Output is correct
7 Correct 67 ms 65016 KB Output is correct
8 Correct 538 ms 108520 KB Output is correct
9 Correct 981 ms 144364 KB Output is correct
10 Correct 932 ms 135872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 60416 KB Output is correct
2 Correct 39 ms 60408 KB Output is correct
3 Correct 45 ms 60400 KB Output is correct
4 Correct 39 ms 60536 KB Output is correct
5 Correct 40 ms 60536 KB Output is correct
6 Correct 38 ms 60412 KB Output is correct
7 Correct 40 ms 60672 KB Output is correct
8 Correct 44 ms 61056 KB Output is correct
9 Correct 41 ms 60792 KB Output is correct
10 Correct 42 ms 61048 KB Output is correct
11 Correct 45 ms 61032 KB Output is correct
12 Correct 40 ms 60664 KB Output is correct
13 Correct 45 ms 61056 KB Output is correct
14 Correct 42 ms 60920 KB Output is correct
15 Correct 43 ms 60920 KB Output is correct
16 Correct 39 ms 60408 KB Output is correct
17 Correct 41 ms 60408 KB Output is correct
18 Correct 39 ms 60544 KB Output is correct
19 Correct 41 ms 60536 KB Output is correct
20 Correct 39 ms 60536 KB Output is correct
21 Correct 42 ms 61056 KB Output is correct
22 Correct 42 ms 60800 KB Output is correct
23 Correct 42 ms 61056 KB Output is correct
24 Correct 43 ms 61176 KB Output is correct
25 Correct 46 ms 61048 KB Output is correct
26 Correct 41 ms 60664 KB Output is correct
27 Correct 43 ms 61048 KB Output is correct
28 Correct 44 ms 61056 KB Output is correct
29 Correct 40 ms 60800 KB Output is correct
30 Correct 43 ms 60792 KB Output is correct
31 Correct 41 ms 60672 KB Output is correct
32 Correct 44 ms 61176 KB Output is correct
33 Correct 99 ms 66556 KB Output is correct
34 Correct 454 ms 75576 KB Output is correct
35 Correct 1036 ms 99280 KB Output is correct
36 Correct 1018 ms 100560 KB Output is correct
37 Correct 611 ms 84200 KB Output is correct
38 Correct 329 ms 71528 KB Output is correct
39 Correct 39 ms 60544 KB Output is correct
40 Correct 42 ms 61048 KB Output is correct
41 Correct 122 ms 62968 KB Output is correct
42 Correct 40 ms 60416 KB Output is correct
43 Correct 43 ms 61048 KB Output is correct
44 Correct 1945 ms 143080 KB Output is correct
45 Correct 67 ms 65016 KB Output is correct
46 Correct 538 ms 108520 KB Output is correct
47 Correct 981 ms 144364 KB Output is correct
48 Correct 932 ms 135872 KB Output is correct
49 Correct 46 ms 61056 KB Output is correct
50 Correct 43 ms 60928 KB Output is correct
51 Correct 43 ms 60920 KB Output is correct
52 Correct 752 ms 91244 KB Output is correct
53 Correct 421 ms 72944 KB Output is correct
54 Correct 794 ms 95060 KB Output is correct
55 Correct 1817 ms 136300 KB Output is correct
56 Correct 1042 ms 99304 KB Output is correct
57 Correct 1956 ms 143592 KB Output is correct
58 Correct 1761 ms 135916 KB Output is correct
59 Correct 739 ms 119404 KB Output is correct
60 Correct 1006 ms 137684 KB Output is correct
61 Correct 1562 ms 148072 KB Output is correct
62 Correct 1907 ms 148284 KB Output is correct
63 Correct 1988 ms 142476 KB Output is correct
64 Correct 622 ms 84056 KB Output is correct
65 Correct 1904 ms 141160 KB Output is correct
66 Correct 1954 ms 140776 KB Output is correct
67 Correct 1247 ms 109932 KB Output is correct
68 Correct 1136 ms 109036 KB Output is correct
69 Correct 1354 ms 118624 KB Output is correct
70 Correct 1357 ms 118632 KB Output is correct
71 Correct 1331 ms 126568 KB Output is correct
72 Correct 1279 ms 126568 KB Output is correct