답안 #55203

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
55203 2018-07-06T10:24:09 Z 윤교준(#1528) 절취선 (JOI14_ho_t5) C++11
100 / 100
676 ms 130740 KB
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[sz(V)-2])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmax(a,b) (a)=max((a),(b))
#define upmin(a,b) (a)=min((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;

const int MAXN = 100005;
const int MAXX = 200055;

struct SEG {
	SEG() { init(); }

	vector<pii> UFV;
	int d[MAXX*4], dc[MAXX*4], dp[MAXX*4];

	void init() { fill(d, d+MAXX*4, -1); fill(dp, dp+MAXX*4, -1); }
	void prop(int i, int s, int e) {
		if(dp[i] < 0) return;
		if(s == e) {
			if(0 <= d[i]) UFV.eb(d[i], dp[i]);
			dp[i] = -1;
			return;
		}
		if(dc[i*2] && 0 <= dp[i*2]) UFV.eb(dp[i], dp[i*2]);
		if(dc[i*2+1] && 0 <= dp[i*2+1]) UFV.eb(dp[i], dp[i*2+1]);
		dp[i*2] = dp[i*2+1] = dp[i];
		dp[i] = -1;
	}
	void push(int i, int s, int e, int x, int r) {
		prop(i, s, e); if(x < s || e < x) return;
		if(s == e) {
			d[i] = r;
			dc[i] = (r < 0 ? 0 : 1);
			return;
		}
		int m = (s+e)/2;
		push(i*2, s, m, x, r);
		push(i*2+1, m+1, e, x, r);
		dc[i] = dc[i*2] + dc[i*2+1];
	}
	void push(int x, int r) { push(1, 0, MAXX-1, x, r); }
	void upd(int i, int s, int e, int p, int q, int r) {
		prop(i, s, e); if(q < p || e < p || q < s) return;
		if(p <= s && e <= q) {
			dp[i] = r;
			prop(i, s, e);
			return;
		}
		int m = (s+e)/2;
		upd(i*2, s, m, p, q, r);
		upd(i*2+1, m+1, e, p, q, r);
	}
	void upd(int s, int e, int x) { upd(1, 0, MAXX-1, s, e, x); }
	void fin(int i, int s, int e) {
		prop(i, s, e);
		if(s == e) return;
		int m = (s+e)/2;
		fin(i*2, s, m);
		fin(i*2+1, m+1, e);
	}
	void fin() { fin(1, 0, MAXX-1); }
} seg;

struct BIT {
	int d[MAXX];
	void upd(int x, int r) {
		for(x += 5; x < MAXX; x += rb(x))
			d[x] += r;
	}
	int get(int x) {
		int r = 0; for(x += 5; x; x -= rb(x))
			r += d[x];
		return r;
	}
} bit;

struct EVT {
	EVT(int type, int y, int idx)
		: type(type), y(y), idx(idx) {}
	int type, y, idx;

	bool operator < (const EVT &t) const {
		if(y != t.y) return y < t.y;
		return type < t.type;
	}
};

vector<EVT> EV;

int ud[MAXX];

int SX[MAXN], EX[MAXN], SY[MAXN], EY[MAXN];

ll Ans;
int N, H, W;

int uf(int i) { return i == ud[i] ? i : (ud[i] = uf(ud[i])); }
void uf(int a, int b) { ud[uf(b)] = uf(a); }

void pushf(int x, int c) {
	bit.upd(x, 1);
	seg.push(x, c);
}

void popf(int x) {
	bit.upd(x, -1);
	seg.push(x, -1);
}

void linef(int sx, int ex, int c) {
	//printf("linef %d %d %d\n", sx, ex, c);
	Ans += bit.get(ex) - bit.get(sx-1);
	seg.upd(sx, ex, c);
}

int main() {
    //freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);

	iota(ud, ud+MAXX, 0);

	cin >> W >> H >> N;
	EY[0] = EY[1] = H; SX[1] = EX[1] = W;
	SY[3] = EY[3] = H; EX[2] = EX[3] = W;
	for(int i = 0; i < N; i++)
		cin >> SX[i+4] >> SY[i+4] >> EX[i+4] >> EY[i+4];
	N += 4;

	{
		vector<int> V;
		for(int i = 0; i < N; i++) {
			V.pb(SX[i]); V.pb(EX[i]);
		}

		sorv(V); univ(V);

		for(int i = 0; i < N; i++) {
			SX[i] = (int)(lower_bound(allv(V), SX[i]) - V.begin());
			EX[i] = (int)(lower_bound(allv(V), EX[i]) - V.begin());
		}
	}
	{
		vector<int> V;
		for(int i = 0; i < N; i++) {
			V.pb(SY[i]); V.pb(EY[i]);
		}

		sorv(V); univ(V);

		for(int i = 0; i < N; i++) {
			SY[i] = (int)(lower_bound(allv(V), SY[i]) - V.begin());
			EY[i] = (int)(lower_bound(allv(V), EY[i]) - V.begin());
		}
	}

	for(int i = 0; i < N; i++) {
		if(SX[i] == EX[i]) {
			EV.eb(1, SY[i], i);
			EV.eb(3, EY[i], i);
		} else {
			EV.eb(2, SY[i], i);
		}
	}

	sorv(EV);

	for(auto &v : EV) {
		if(1 == v.type) {
			int idx = v.idx, x = SX[idx];
			pushf(x, idx);
		} else if(3 == v.type) {
			int idx = v.idx, x = SX[idx];
			popf(x);
		} else {
			int idx = v.idx, sx = SX[idx], ex = EX[idx];
			linef(sx, ex, idx);
		}
	}

	//printf("CNT %lld\n", Ans);

	Ans -= N;

	//printf("N %d\n", N);

	seg.fin();

	for(auto &e : seg.UFV) {
		int a, b; tie(a, b) = e;
		if(a < 0 || b < 0) continue;
		uf(a, b);
	}

	{
		vector<int> V;
		for(int i = 0; i < N; i++)
			V.pb(uf(i));
		sorv(V); univ(V);

		Ans += sz(V);

		/*
		printf("C %d\n", sz(V));
		for(int i = 0; i < N; i++)
            printf("%d ; %d\n", i, uf(i));
        */
	}

	cout << Ans << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7652 KB Output is correct
3 Correct 9 ms 7652 KB Output is correct
4 Correct 12 ms 7652 KB Output is correct
5 Correct 10 ms 7688 KB Output is correct
6 Correct 12 ms 7688 KB Output is correct
7 Correct 11 ms 7840 KB Output is correct
8 Correct 11 ms 7996 KB Output is correct
9 Correct 10 ms 7996 KB Output is correct
10 Correct 11 ms 7996 KB Output is correct
11 Correct 13 ms 8144 KB Output is correct
12 Correct 13 ms 8144 KB Output is correct
13 Correct 11 ms 8144 KB Output is correct
14 Correct 11 ms 8144 KB Output is correct
15 Correct 15 ms 8144 KB Output is correct
16 Correct 8 ms 8144 KB Output is correct
17 Correct 9 ms 8144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7652 KB Output is correct
3 Correct 9 ms 7652 KB Output is correct
4 Correct 12 ms 7652 KB Output is correct
5 Correct 10 ms 7688 KB Output is correct
6 Correct 12 ms 7688 KB Output is correct
7 Correct 11 ms 7840 KB Output is correct
8 Correct 11 ms 7996 KB Output is correct
9 Correct 10 ms 7996 KB Output is correct
10 Correct 11 ms 7996 KB Output is correct
11 Correct 13 ms 8144 KB Output is correct
12 Correct 13 ms 8144 KB Output is correct
13 Correct 11 ms 8144 KB Output is correct
14 Correct 11 ms 8144 KB Output is correct
15 Correct 15 ms 8144 KB Output is correct
16 Correct 8 ms 8144 KB Output is correct
17 Correct 9 ms 8144 KB Output is correct
18 Correct 8 ms 8144 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 12 ms 8144 KB Output is correct
21 Correct 12 ms 8144 KB Output is correct
22 Correct 11 ms 8144 KB Output is correct
23 Correct 12 ms 8144 KB Output is correct
24 Correct 12 ms 8144 KB Output is correct
25 Correct 13 ms 8144 KB Output is correct
26 Correct 12 ms 8144 KB Output is correct
27 Correct 12 ms 8144 KB Output is correct
28 Correct 14 ms 8144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 8144 KB Output is correct
2 Correct 11 ms 8144 KB Output is correct
3 Correct 9 ms 8144 KB Output is correct
4 Correct 11 ms 8144 KB Output is correct
5 Correct 39 ms 8972 KB Output is correct
6 Correct 157 ms 11524 KB Output is correct
7 Correct 396 ms 14868 KB Output is correct
8 Correct 354 ms 14948 KB Output is correct
9 Correct 340 ms 14948 KB Output is correct
10 Correct 285 ms 14948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14948 KB Output is correct
2 Correct 13 ms 14948 KB Output is correct
3 Correct 64 ms 14948 KB Output is correct
4 Correct 11 ms 14948 KB Output is correct
5 Correct 13 ms 14948 KB Output is correct
6 Correct 603 ms 81000 KB Output is correct
7 Correct 24 ms 81000 KB Output is correct
8 Correct 190 ms 81000 KB Output is correct
9 Correct 254 ms 81000 KB Output is correct
10 Correct 286 ms 81000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 9 ms 7652 KB Output is correct
3 Correct 9 ms 7652 KB Output is correct
4 Correct 12 ms 7652 KB Output is correct
5 Correct 10 ms 7688 KB Output is correct
6 Correct 12 ms 7688 KB Output is correct
7 Correct 11 ms 7840 KB Output is correct
8 Correct 11 ms 7996 KB Output is correct
9 Correct 10 ms 7996 KB Output is correct
10 Correct 11 ms 7996 KB Output is correct
11 Correct 13 ms 8144 KB Output is correct
12 Correct 13 ms 8144 KB Output is correct
13 Correct 11 ms 8144 KB Output is correct
14 Correct 11 ms 8144 KB Output is correct
15 Correct 15 ms 8144 KB Output is correct
16 Correct 8 ms 8144 KB Output is correct
17 Correct 9 ms 8144 KB Output is correct
18 Correct 8 ms 8144 KB Output is correct
19 Correct 9 ms 8144 KB Output is correct
20 Correct 12 ms 8144 KB Output is correct
21 Correct 12 ms 8144 KB Output is correct
22 Correct 11 ms 8144 KB Output is correct
23 Correct 12 ms 8144 KB Output is correct
24 Correct 12 ms 8144 KB Output is correct
25 Correct 13 ms 8144 KB Output is correct
26 Correct 12 ms 8144 KB Output is correct
27 Correct 12 ms 8144 KB Output is correct
28 Correct 14 ms 8144 KB Output is correct
29 Correct 14 ms 8144 KB Output is correct
30 Correct 11 ms 8144 KB Output is correct
31 Correct 9 ms 8144 KB Output is correct
32 Correct 11 ms 8144 KB Output is correct
33 Correct 39 ms 8972 KB Output is correct
34 Correct 157 ms 11524 KB Output is correct
35 Correct 396 ms 14868 KB Output is correct
36 Correct 354 ms 14948 KB Output is correct
37 Correct 340 ms 14948 KB Output is correct
38 Correct 285 ms 14948 KB Output is correct
39 Correct 10 ms 14948 KB Output is correct
40 Correct 13 ms 14948 KB Output is correct
41 Correct 64 ms 14948 KB Output is correct
42 Correct 11 ms 14948 KB Output is correct
43 Correct 13 ms 14948 KB Output is correct
44 Correct 603 ms 81000 KB Output is correct
45 Correct 24 ms 81000 KB Output is correct
46 Correct 190 ms 81000 KB Output is correct
47 Correct 254 ms 81000 KB Output is correct
48 Correct 286 ms 81000 KB Output is correct
49 Correct 11 ms 81000 KB Output is correct
50 Correct 12 ms 81000 KB Output is correct
51 Correct 11 ms 81000 KB Output is correct
52 Correct 229 ms 81000 KB Output is correct
53 Correct 145 ms 81000 KB Output is correct
54 Correct 269 ms 81000 KB Output is correct
55 Correct 509 ms 81000 KB Output is correct
56 Correct 290 ms 81000 KB Output is correct
57 Correct 595 ms 98560 KB Output is correct
58 Correct 590 ms 98560 KB Output is correct
59 Correct 277 ms 98560 KB Output is correct
60 Correct 249 ms 98560 KB Output is correct
61 Correct 280 ms 98560 KB Output is correct
62 Correct 277 ms 98560 KB Output is correct
63 Correct 676 ms 119264 KB Output is correct
64 Correct 320 ms 119264 KB Output is correct
65 Correct 642 ms 126916 KB Output is correct
66 Correct 654 ms 130740 KB Output is correct
67 Correct 568 ms 130740 KB Output is correct
68 Correct 527 ms 130740 KB Output is correct
69 Correct 578 ms 130740 KB Output is correct
70 Correct 613 ms 130740 KB Output is correct
71 Correct 315 ms 130740 KB Output is correct
72 Correct 265 ms 130740 KB Output is correct