답안 #332325

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
332325 2020-12-02T03:43:54 Z arnold518 절취선 (JOI14_ho_t5) C++14
10 / 100
1318 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;
const int MAXV = 2e6;

struct Line
{
	int x1, y1, x2, y2, p;
};

int W, H, N, M, SX, SY;
Line A[MAXN+10], B[MAXN+10];

vector<int> xcomp, ycomp;

ll V, E, C=1;

struct BIT
{
	int tree[MAXN+10];
	void update(int i, int x) { for(; i<=SY; i+=(i&-i)) tree[i]+=x; }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
	int query(int l, int r) { return query(r)-query(l-1); }
}bit;

struct Node
{
	int lc, rc;
	Node() : lc(-1), rc(-1) {}
};
Node nodes[MAXV+10];

int ncnt;
vector<int> adj[MAXV+10];
vector<int> radj[MAXV+10];

void addEdge(int u, int v)
{
	//printf("%d %d\n", u, v);
	adj[u].push_back(v);
	radj[v].push_back(u);
}

void makeTree(int node, int tl, int tr)
{
	if(tl==tr) return;
	int mid=tl+tr>>1;
	nodes[node].lc=++ncnt;
	nodes[node].rc=++ncnt;
	addEdge(nodes[node].lc, node);
	addEdge(nodes[node].rc, node);
	makeTree(nodes[node].lc, tl, mid);
	makeTree(nodes[node].rc, mid+1, tr);
}

int update(int node, int tl, int tr, int p, int k)
{
	if(p<tl || tr<p) return node;
	int ret=++ncnt;
	if(tl==tr)
	{
		if(k>0) addEdge(k, ret);
		return ret;
	}
	int mid=tl+tr>>1;
	nodes[ret].lc=update(nodes[node].lc, tl, mid, p, k);
	nodes[ret].rc=update(nodes[node].rc, mid+1, tr, p, k);
	addEdge(nodes[ret].lc, ret);
	addEdge(nodes[ret].rc, ret);
	return ret;
}

void query(int node, int tl, int tr, int l, int r, int k)
{
	if(r<tl || tr<l) return;
	if(l<=tl && tr<=r)
	{
		addEdge(node, k);
		return;
	}
	int mid=tl+tr>>1;
	query(nodes[node].lc, tl, mid, l, r, k);
	query(nodes[node].rc, mid+1, tr, l, r, k);
}

bool vis[MAXV+10];
vector<int> S;
void dfs(int now)
{
	vis[now]=true;
	for(int nxt : adj[now])
	{
		if(vis[nxt]) continue;
		dfs(nxt);
	}
	S.push_back(now);
}

int scc[MAXV+10], scnt;
void dfs2(int now)
{
	scc[now]=scnt;
	for(int nxt : radj[now])
	{
		if(scc[nxt]) continue;
		dfs2(nxt);
	}
}

int main()
{
	int t;
	scanf("%d%d%d", &W, &H, &t);
	for(int i=1; i<=t; i++)
	{
		Line p;
		scanf("%d%d%d%d", &p.x1, &p.y1, &p.x2, &p.y2);
		xcomp.push_back(p.x1);
		xcomp.push_back(p.x2);
		ycomp.push_back(p.y1);
		ycomp.push_back(p.y2);
		if(p.x1==p.x2) B[++M]=p;
		else A[++N]=p;
	}

	xcomp.push_back(0);
	ycomp.push_back(0);
	xcomp.push_back(W);
	ycomp.push_back(H);

	A[++N]={0, 0, W, 0};
	A[++N]={0, H, W, H};
	B[++M]={0, 0, 0, H};
	B[++M]={W, 0, W, H};

	sort(xcomp.begin(), xcomp.end());
	xcomp.erase(unique(xcomp.begin(), xcomp.end()), xcomp.end());
	sort(ycomp.begin(), ycomp.end());
	ycomp.erase(unique(ycomp.begin(), ycomp.end()), ycomp.end());

	SX=xcomp.size(); SY=ycomp.size();

	for(int i=1; i<=N; i++)
	{
		A[i].x1=lower_bound(xcomp.begin(), xcomp.end(), A[i].x1)-xcomp.begin()+1;
		A[i].x2=lower_bound(xcomp.begin(), xcomp.end(), A[i].x2)-xcomp.begin()+1;
		A[i].y1=lower_bound(ycomp.begin(), ycomp.end(), A[i].y1)-ycomp.begin()+1;
		A[i].y2=lower_bound(ycomp.begin(), ycomp.end(), A[i].y2)-ycomp.begin()+1;
		A[i].p=++ncnt;
	}
	for(int i=1; i<=M; i++)
	{
		B[i].x1=lower_bound(xcomp.begin(), xcomp.end(), B[i].x1)-xcomp.begin()+1;
		B[i].x2=lower_bound(xcomp.begin(), xcomp.end(), B[i].x2)-xcomp.begin()+1;
		B[i].y1=lower_bound(ycomp.begin(), ycomp.end(), B[i].y1)-ycomp.begin()+1;
		B[i].y2=lower_bound(ycomp.begin(), ycomp.end(), B[i].y2)-ycomp.begin()+1;
		B[i].p=++ncnt;
	}

	for(int i=1; i<=N; i++)
	{
		E+=A[i].x2-A[i].x1;
		V+=A[i].x2-A[i].x1+1;
	}
	for(int i=1; i<=M; i++)
	{
		E+=B[i].y2-B[i].y1;
		V+=B[i].y2-B[i].y1+1;
	}

	sort(B+1, B+M+1, [&](const Line &p, const Line &q)
	{
		return p.x1<q.x1;
	});

	vector<pair<pll, int>> VV;
	for(int i=1; i<=N; i++)
	{
		VV.push_back({{A[i].x1, A[i].y1}, 1});
		VV.push_back({{A[i].x2+1, A[i].y1}, -1});
	}
	sort(VV.begin(), VV.end());

	for(int i=1, j=0; i<=M; i++)
	{
		for(; j<VV.size() && VV[j].first.first<=B[i].x1; j++)
		{
			bit.update(VV[j].first.second, VV[j].second);
		}
		V-=bit.query(B[i].y1, B[i].y2);
	}

	int root;

	VV.clear();
	for(int i=1; i<=N; i++)
	{
		VV.push_back({{A[i].x1, A[i].y1}, A[i].p});
		VV.push_back({{A[i].x2+1, A[i].y1}, 0});
	}
	sort(VV.begin(), VV.end());

	root=++ncnt;
	makeTree(root, 1, SY);
	for(int i=1, j=0; i<=M; i++)
	{
		for(; j<VV.size() && VV[j].first.first<=B[i].x1; j++)
		{
			root=update(root, 1, SY, VV[j].first.second, VV[j].second);
			//printf("!!%d\n", VV[j].second);
		}
		query(root, 1, SY, B[i].y1, B[i].y2, B[i].p);
		//printf("??%d\n", B[i].p);
	}

	VV.clear();
	for(int i=1; i<=M; i++)
	{
		VV.push_back({{B[i].y1, B[i].x1}, B[i].p});
		VV.push_back({{B[i].y2+1, B[i].x1}, 0});
	}
	sort(VV.begin(), VV.end());
	sort(A+1, A+N+1, [&](const Line &p, const Line &q)
	{
		return p.y1<q.y1;
	});

	root=++ncnt;
	makeTree(root, 1, SX);
	for(int i=1, j=0; i<=N; i++)
	{
		for(; j<VV.size() && VV[j].first.first<=A[i].y1; j++)
		{
			root=update(root, 1, SX, VV[j].first.second, VV[j].second);
			//printf("!!%d\n", VV[j].second);
		}
		query(root, 1, SX, A[i].x1, A[i].x2, A[i].p);
		//printf("??%d\n", A[i].p);
	}

	for(int i=1; i<=ncnt; i++)
	{
		if(vis[i]) continue;
		dfs(i);
	}
	reverse(S.begin(), S.end());
	for(auto it : S)
	{
		if(scc[it]) continue;
		++scnt;
		dfs2(it);
	}

	//for(int i=1; i<=ncnt; i++) printf("%d : %d\n", i, scc[i]);

	set<int> SS;
	for(int i=1; i<=N+M; i++) SS.insert(scc[i]);
	C=SS.size();

	//printf("%lld %lld %lld\n", C, V, E);

	ll ans=C-V+E;
	printf("%lld\n", ans);
}

Compilation message

2014_ho_t5.cpp: In function 'void makeTree(int, int, int)':
2014_ho_t5.cpp:52:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |  int mid=tl+tr>>1;
      |          ~~^~~
2014_ho_t5.cpp: In function 'int update(int, int, int, int, int)':
2014_ho_t5.cpp:70:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |  int mid=tl+tr>>1;
      |          ~~^~~
2014_ho_t5.cpp: In function 'void query(int, int, int, int, int, int)':
2014_ho_t5.cpp:86:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |  int mid=tl+tr>>1;
      |          ~~^~~
2014_ho_t5.cpp: In function 'int main()':
2014_ho_t5.cpp:191:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  191 |   for(; j<VV.size() && VV[j].first.first<=B[i].x1; j++)
      |         ~^~~~~~~~~~
2014_ho_t5.cpp:212:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  212 |   for(; j<VV.size() && VV[j].first.first<=B[i].x1; j++)
      |         ~^~~~~~~~~~
2014_ho_t5.cpp:237:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  237 |   for(; j<VV.size() && VV[j].first.first<=A[i].y1; j++)
      |         ~^~~~~~~~~~
2014_ho_t5.cpp:118:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  118 |  scanf("%d%d%d", &W, &H, &t);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.cpp:122:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  122 |   scanf("%d%d%d%d", &p.x1, &p.y1, &p.x2, &p.y2);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 109932 KB Output is correct
2 Correct 71 ms 109932 KB Output is correct
3 Correct 71 ms 109932 KB Output is correct
4 Correct 71 ms 110060 KB Output is correct
5 Correct 72 ms 110188 KB Output is correct
6 Correct 73 ms 110220 KB Output is correct
7 Correct 76 ms 111084 KB Output is correct
8 Correct 83 ms 112108 KB Output is correct
9 Correct 84 ms 111852 KB Output is correct
10 Correct 82 ms 112108 KB Output is correct
11 Correct 81 ms 112108 KB Output is correct
12 Correct 76 ms 110956 KB Output is correct
13 Correct 82 ms 112236 KB Output is correct
14 Correct 82 ms 111852 KB Output is correct
15 Correct 80 ms 111888 KB Output is correct
16 Correct 72 ms 110060 KB Output is correct
17 Correct 72 ms 110060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 109932 KB Output is correct
2 Correct 71 ms 109932 KB Output is correct
3 Correct 71 ms 109932 KB Output is correct
4 Correct 71 ms 110060 KB Output is correct
5 Correct 72 ms 110188 KB Output is correct
6 Correct 73 ms 110220 KB Output is correct
7 Correct 76 ms 111084 KB Output is correct
8 Correct 83 ms 112108 KB Output is correct
9 Correct 84 ms 111852 KB Output is correct
10 Correct 82 ms 112108 KB Output is correct
11 Correct 81 ms 112108 KB Output is correct
12 Correct 76 ms 110956 KB Output is correct
13 Correct 82 ms 112236 KB Output is correct
14 Correct 82 ms 111852 KB Output is correct
15 Correct 80 ms 111888 KB Output is correct
16 Correct 72 ms 110060 KB Output is correct
17 Correct 72 ms 110060 KB Output is correct
18 Correct 71 ms 110060 KB Output is correct
19 Correct 75 ms 110060 KB Output is correct
20 Correct 81 ms 110136 KB Output is correct
21 Correct 86 ms 112364 KB Output is correct
22 Correct 84 ms 112236 KB Output is correct
23 Correct 86 ms 112488 KB Output is correct
24 Correct 85 ms 112492 KB Output is correct
25 Correct 87 ms 112492 KB Output is correct
26 Correct 83 ms 112236 KB Output is correct
27 Correct 85 ms 112492 KB Output is correct
28 Correct 85 ms 112492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 111212 KB Output is correct
2 Correct 77 ms 111340 KB Output is correct
3 Correct 82 ms 111084 KB Output is correct
4 Correct 82 ms 112492 KB Output is correct
5 Correct 317 ms 139356 KB Output is correct
6 Runtime error 1175 ms 262144 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 110188 KB Output is correct
2 Correct 84 ms 112492 KB Output is correct
3 Correct 341 ms 140764 KB Output is correct
4 Correct 72 ms 109932 KB Output is correct
5 Correct 84 ms 112492 KB Output is correct
6 Runtime error 1318 ms 262144 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 109932 KB Output is correct
2 Correct 71 ms 109932 KB Output is correct
3 Correct 71 ms 109932 KB Output is correct
4 Correct 71 ms 110060 KB Output is correct
5 Correct 72 ms 110188 KB Output is correct
6 Correct 73 ms 110220 KB Output is correct
7 Correct 76 ms 111084 KB Output is correct
8 Correct 83 ms 112108 KB Output is correct
9 Correct 84 ms 111852 KB Output is correct
10 Correct 82 ms 112108 KB Output is correct
11 Correct 81 ms 112108 KB Output is correct
12 Correct 76 ms 110956 KB Output is correct
13 Correct 82 ms 112236 KB Output is correct
14 Correct 82 ms 111852 KB Output is correct
15 Correct 80 ms 111888 KB Output is correct
16 Correct 72 ms 110060 KB Output is correct
17 Correct 72 ms 110060 KB Output is correct
18 Correct 71 ms 110060 KB Output is correct
19 Correct 75 ms 110060 KB Output is correct
20 Correct 81 ms 110136 KB Output is correct
21 Correct 86 ms 112364 KB Output is correct
22 Correct 84 ms 112236 KB Output is correct
23 Correct 86 ms 112488 KB Output is correct
24 Correct 85 ms 112492 KB Output is correct
25 Correct 87 ms 112492 KB Output is correct
26 Correct 83 ms 112236 KB Output is correct
27 Correct 85 ms 112492 KB Output is correct
28 Correct 85 ms 112492 KB Output is correct
29 Correct 81 ms 111212 KB Output is correct
30 Correct 77 ms 111340 KB Output is correct
31 Correct 82 ms 111084 KB Output is correct
32 Correct 82 ms 112492 KB Output is correct
33 Correct 317 ms 139356 KB Output is correct
34 Runtime error 1175 ms 262144 KB Execution killed with signal 11 (could be triggered by violating memory limits)
35 Halted 0 ms 0 KB -