Submission #229980

# Submission time Handle Problem Language Result Execution time Memory
229980 2020-05-07T12:53:21 Z dennisstar None (JOI14_ho_t5) C++17
100 / 100
2073 ms 166248 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;
		assert((r1==0||r1==1)&&(r3==0||r3==1));
	}

	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)); cx.resize(unique(all(cx))-cx.begin());
	sort(all(cy)); cy.resize(unique(all(cy))-cy.begin());
	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:120: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);
          ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 39 ms 60480 KB Output is correct
2 Correct 42 ms 60408 KB Output is correct
3 Correct 39 ms 60416 KB Output is correct
4 Correct 39 ms 60408 KB Output is correct
5 Correct 40 ms 60536 KB Output is correct
6 Correct 41 ms 60408 KB Output is correct
7 Correct 40 ms 60672 KB Output is correct
8 Correct 46 ms 60920 KB Output is correct
9 Correct 43 ms 60672 KB Output is correct
10 Correct 45 ms 60968 KB Output is correct
11 Correct 49 ms 61076 KB Output is correct
12 Correct 49 ms 60668 KB Output is correct
13 Correct 45 ms 61048 KB Output is correct
14 Correct 44 ms 60792 KB Output is correct
15 Correct 43 ms 60820 KB Output is correct
16 Correct 41 ms 60408 KB Output is correct
17 Correct 41 ms 60416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 60480 KB Output is correct
2 Correct 42 ms 60408 KB Output is correct
3 Correct 39 ms 60416 KB Output is correct
4 Correct 39 ms 60408 KB Output is correct
5 Correct 40 ms 60536 KB Output is correct
6 Correct 41 ms 60408 KB Output is correct
7 Correct 40 ms 60672 KB Output is correct
8 Correct 46 ms 60920 KB Output is correct
9 Correct 43 ms 60672 KB Output is correct
10 Correct 45 ms 60968 KB Output is correct
11 Correct 49 ms 61076 KB Output is correct
12 Correct 49 ms 60668 KB Output is correct
13 Correct 45 ms 61048 KB Output is correct
14 Correct 44 ms 60792 KB Output is correct
15 Correct 43 ms 60820 KB Output is correct
16 Correct 41 ms 60408 KB Output is correct
17 Correct 41 ms 60416 KB Output is correct
18 Correct 40 ms 60536 KB Output is correct
19 Correct 40 ms 60540 KB Output is correct
20 Correct 46 ms 60408 KB Output is correct
21 Correct 44 ms 60928 KB Output is correct
22 Correct 47 ms 60792 KB Output is correct
23 Correct 42 ms 60920 KB Output is correct
24 Correct 47 ms 61048 KB Output is correct
25 Correct 46 ms 60928 KB Output is correct
26 Correct 45 ms 60672 KB Output is correct
27 Correct 44 ms 61048 KB Output is correct
28 Correct 46 ms 61052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 60800 KB Output is correct
2 Correct 41 ms 60792 KB Output is correct
3 Correct 42 ms 60672 KB Output is correct
4 Correct 43 ms 60928 KB Output is correct
5 Correct 98 ms 66296 KB Output is correct
6 Correct 468 ms 75504 KB Output is correct
7 Correct 1028 ms 99944 KB Output is correct
8 Correct 1008 ms 101096 KB Output is correct
9 Correct 616 ms 84712 KB Output is correct
10 Correct 325 ms 73664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 60536 KB Output is correct
2 Correct 44 ms 61056 KB Output is correct
3 Correct 114 ms 62752 KB Output is correct
4 Correct 38 ms 60408 KB Output is correct
5 Correct 42 ms 61048 KB Output is correct
6 Correct 1943 ms 138332 KB Output is correct
7 Correct 71 ms 64760 KB Output is correct
8 Correct 479 ms 97512 KB Output is correct
9 Correct 1133 ms 161660 KB Output is correct
10 Correct 1122 ms 163560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 60480 KB Output is correct
2 Correct 42 ms 60408 KB Output is correct
3 Correct 39 ms 60416 KB Output is correct
4 Correct 39 ms 60408 KB Output is correct
5 Correct 40 ms 60536 KB Output is correct
6 Correct 41 ms 60408 KB Output is correct
7 Correct 40 ms 60672 KB Output is correct
8 Correct 46 ms 60920 KB Output is correct
9 Correct 43 ms 60672 KB Output is correct
10 Correct 45 ms 60968 KB Output is correct
11 Correct 49 ms 61076 KB Output is correct
12 Correct 49 ms 60668 KB Output is correct
13 Correct 45 ms 61048 KB Output is correct
14 Correct 44 ms 60792 KB Output is correct
15 Correct 43 ms 60820 KB Output is correct
16 Correct 41 ms 60408 KB Output is correct
17 Correct 41 ms 60416 KB Output is correct
18 Correct 40 ms 60536 KB Output is correct
19 Correct 40 ms 60540 KB Output is correct
20 Correct 46 ms 60408 KB Output is correct
21 Correct 44 ms 60928 KB Output is correct
22 Correct 47 ms 60792 KB Output is correct
23 Correct 42 ms 60920 KB Output is correct
24 Correct 47 ms 61048 KB Output is correct
25 Correct 46 ms 60928 KB Output is correct
26 Correct 45 ms 60672 KB Output is correct
27 Correct 44 ms 61048 KB Output is correct
28 Correct 46 ms 61052 KB Output is correct
29 Correct 40 ms 60800 KB Output is correct
30 Correct 41 ms 60792 KB Output is correct
31 Correct 42 ms 60672 KB Output is correct
32 Correct 43 ms 60928 KB Output is correct
33 Correct 98 ms 66296 KB Output is correct
34 Correct 468 ms 75504 KB Output is correct
35 Correct 1028 ms 99944 KB Output is correct
36 Correct 1008 ms 101096 KB Output is correct
37 Correct 616 ms 84712 KB Output is correct
38 Correct 325 ms 73664 KB Output is correct
39 Correct 39 ms 60536 KB Output is correct
40 Correct 44 ms 61056 KB Output is correct
41 Correct 114 ms 62752 KB Output is correct
42 Correct 38 ms 60408 KB Output is correct
43 Correct 42 ms 61048 KB Output is correct
44 Correct 1943 ms 138332 KB Output is correct
45 Correct 71 ms 64760 KB Output is correct
46 Correct 479 ms 97512 KB Output is correct
47 Correct 1133 ms 161660 KB Output is correct
48 Correct 1122 ms 163560 KB Output is correct
49 Correct 44 ms 60920 KB Output is correct
50 Correct 43 ms 60800 KB Output is correct
51 Correct 44 ms 60792 KB Output is correct
52 Correct 740 ms 90992 KB Output is correct
53 Correct 426 ms 72560 KB Output is correct
54 Correct 810 ms 94520 KB Output is correct
55 Correct 1774 ms 136308 KB Output is correct
56 Correct 977 ms 99944 KB Output is correct
57 Correct 1889 ms 143756 KB Output is correct
58 Correct 1733 ms 136428 KB Output is correct
59 Correct 887 ms 148456 KB Output is correct
60 Correct 1090 ms 166248 KB Output is correct
61 Correct 1428 ms 158056 KB Output is correct
62 Correct 1751 ms 152312 KB Output is correct
63 Correct 1873 ms 141800 KB Output is correct
64 Correct 616 ms 84840 KB Output is correct
65 Correct 2016 ms 142772 KB Output is correct
66 Correct 2073 ms 142272 KB Output is correct
67 Correct 1344 ms 112360 KB Output is correct
68 Correct 1240 ms 111508 KB Output is correct
69 Correct 1370 ms 119372 KB Output is correct
70 Correct 1271 ms 119356 KB Output is correct
71 Correct 652 ms 78312 KB Output is correct
72 Correct 627 ms 78312 KB Output is correct