답안 #776805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
776805 2023-07-08T09:41:49 Z ymm Collapse (JOI18_collapse) C++17
100 / 100
1225 ms 23208 KB
#include "collapse.h"

#include <bits/stdc++.h>
#define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
typedef long long ll;
typedef std::pair<int,int> pii;
using namespace std;

const int N = 100'010;

namespace dsu {
	int par[N];
	int sz[N];
	vector<pii> his;
	int cnt;
	bool rec;

	void init() {
		fill(par, par+N, -1);
		fill(sz, sz+N, 1);
		his.clear();
		cnt = 0;
		rec = 0;
	}

	int rt(int v) { return par[v] == -1? v: rt(par[v]); }

	void unite(int v, int u) {
		v = rt(v);
		u = rt(u);
		if (v == u)
			return;
		if (sz[v] < sz[u])
			swap(v, u);
		par[u] = v;
		sz[v] += sz[u];
		++cnt;
		if (rec)
			his.emplace_back(v, u);
	}

	void rollback() {
		while (his.size()) {
			auto [v, u] = his.back();
			his.pop_back();
			--cnt;
			sz[v] -= sz[u];
			par[u] = -1;
		}
		rec = 0;
	}
}

vector<pair<int,pii>> A[N], T[N];

void make_graph(vector<int> t, vector<int> V, vector<int> U)
{
	set<pair<pii,int>> e;
	Loop (i,0,t.size()) {
		int v = V[i], u = U[i];
		if (v > u)
			swap(v, u);
		if (t[i]) {
			auto it = e.lower_bound({{v, u}, -1});
			int l = it->second, r = i;
			A[v].push_back({u, {l,r}});
			T[u].push_back({v, {l,r}});
			e.erase(it);
		} else {
			e.insert({{v, u}, i});
		}
	}
	for (auto [dard, l] : e) {
		auto [v, u] = dard;
		int r = t.size();
		A[v].push_back({u, {l,r}});
		T[u].push_back({v, {l,r}});
	}
}

const int S = 1000;
int ans[N];

std::vector<int> simulateCollapse(
	int n,
	std::vector<int> T,
	std::vector<int> X,
	std::vector<int> Y,
	std::vector<int> W,
	std::vector<int> P
) {
	make_graph(T, X, Y);
	vector<int> Q(W.size());
	iota(Q.begin(), Q.end(), 0);
	sort(Q.begin(), Q.end(), [&](int i, int j) { return W[i] < W[j]; });
	for (int l = 0, r = 0; l < W.size(); l = r) {
		while (r < W.size() && W[Q[r]] - W[Q[l]] <= S)
			++r;
		vector<int> Q2;
		Loop (i,l,r)
			Q2.push_back(Q[i]);
		sort(Q2.begin(), Q2.end(), [&](int i, int j) { return P[i] < P[j]; });
		Loop (phase,0,2) {
			dsu::init();
			vector<pair<pii,pii>> E;
			int p = phase == 0? 0: n-1;
			for (int q : Q2) {
				while (phase == 0? p <= P[q]: p > P[q]) {
					for (auto [u, t] : (phase == 0? ::T[p]: ::A[p])) {
						if (t.first <= W[Q[l]] && W[Q[r-1]] < t.second) {
							dsu::unite(p, u);
							continue;
						}
						if (W[Q[r-1]] < t.first || t.second <= W[Q[l]])
							continue;
						E.push_back({{p,u},t});
					}
					p += phase == 0? 1: -1;
				}
				dsu::rec = 1;
				for (auto [e, t] : E) {
					if (t.first <= W[q] && W[q] < t.second)
						dsu::unite(e.first, e.second);
				}
				ans[q] += dsu::cnt;
				dsu::rollback();
			}
			reverse(Q2.begin(), Q2.end());
		}
	}
	Loop (i,0,Q.size())
		ans[i] = n - ans[i];
	return vector<int>(ans, ans+Q.size());
}

Compilation message

collapse.cpp: In function 'void make_graph(std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:4:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
      |                                          ^
collapse.cpp:59:2: note: in expansion of macro 'Loop'
   59 |  Loop (i,0,t.size()) {
      |  ^~~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:96:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |  for (int l = 0, r = 0; l < W.size(); l = r) {
      |                         ~~^~~~~~~~~~
collapse.cpp:97:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |   while (r < W.size() && W[Q[r]] - W[Q[l]] <= S)
      |          ~~^~~~~~~~~~
collapse.cpp:4:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define Loop(x, l, r) for (ll x = (l); x < (r); ++x)
      |                                          ^
collapse.cpp:131:2: note: in expansion of macro 'Loop'
  131 |  Loop (i,0,Q.size())
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6228 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 10 ms 6172 KB Output is correct
6 Correct 20 ms 6612 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 4 ms 5916 KB Output is correct
9 Correct 14 ms 6396 KB Output is correct
10 Correct 19 ms 6372 KB Output is correct
11 Correct 33 ms 6692 KB Output is correct
12 Correct 28 ms 6680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 8996 KB Output is correct
2 Correct 26 ms 8980 KB Output is correct
3 Correct 101 ms 13904 KB Output is correct
4 Correct 37 ms 9080 KB Output is correct
5 Correct 187 ms 14280 KB Output is correct
6 Correct 174 ms 9972 KB Output is correct
7 Correct 278 ms 22032 KB Output is correct
8 Correct 196 ms 15012 KB Output is correct
9 Correct 25 ms 9360 KB Output is correct
10 Correct 31 ms 9436 KB Output is correct
11 Correct 143 ms 9668 KB Output is correct
12 Correct 253 ms 15760 KB Output is correct
13 Correct 418 ms 18660 KB Output is correct
14 Correct 512 ms 23208 KB Output is correct
15 Correct 401 ms 22960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 9036 KB Output is correct
2 Correct 28 ms 8980 KB Output is correct
3 Correct 35 ms 9028 KB Output is correct
4 Correct 40 ms 9108 KB Output is correct
5 Correct 176 ms 9228 KB Output is correct
6 Correct 258 ms 9972 KB Output is correct
7 Correct 410 ms 18568 KB Output is correct
8 Correct 679 ms 22056 KB Output is correct
9 Correct 34 ms 9356 KB Output is correct
10 Correct 356 ms 9668 KB Output is correct
11 Correct 896 ms 22136 KB Output is correct
12 Correct 1203 ms 22620 KB Output is correct
13 Correct 964 ms 22208 KB Output is correct
14 Correct 1213 ms 22612 KB Output is correct
15 Correct 877 ms 22136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6228 KB Output is correct
2 Correct 4 ms 5972 KB Output is correct
3 Correct 4 ms 5972 KB Output is correct
4 Correct 5 ms 5972 KB Output is correct
5 Correct 10 ms 6172 KB Output is correct
6 Correct 20 ms 6612 KB Output is correct
7 Correct 5 ms 5972 KB Output is correct
8 Correct 4 ms 5916 KB Output is correct
9 Correct 14 ms 6396 KB Output is correct
10 Correct 19 ms 6372 KB Output is correct
11 Correct 33 ms 6692 KB Output is correct
12 Correct 28 ms 6680 KB Output is correct
13 Correct 24 ms 8996 KB Output is correct
14 Correct 26 ms 8980 KB Output is correct
15 Correct 101 ms 13904 KB Output is correct
16 Correct 37 ms 9080 KB Output is correct
17 Correct 187 ms 14280 KB Output is correct
18 Correct 174 ms 9972 KB Output is correct
19 Correct 278 ms 22032 KB Output is correct
20 Correct 196 ms 15012 KB Output is correct
21 Correct 25 ms 9360 KB Output is correct
22 Correct 31 ms 9436 KB Output is correct
23 Correct 143 ms 9668 KB Output is correct
24 Correct 253 ms 15760 KB Output is correct
25 Correct 418 ms 18660 KB Output is correct
26 Correct 512 ms 23208 KB Output is correct
27 Correct 401 ms 22960 KB Output is correct
28 Correct 24 ms 9036 KB Output is correct
29 Correct 28 ms 8980 KB Output is correct
30 Correct 35 ms 9028 KB Output is correct
31 Correct 40 ms 9108 KB Output is correct
32 Correct 176 ms 9228 KB Output is correct
33 Correct 258 ms 9972 KB Output is correct
34 Correct 410 ms 18568 KB Output is correct
35 Correct 679 ms 22056 KB Output is correct
36 Correct 34 ms 9356 KB Output is correct
37 Correct 356 ms 9668 KB Output is correct
38 Correct 896 ms 22136 KB Output is correct
39 Correct 1203 ms 22620 KB Output is correct
40 Correct 964 ms 22208 KB Output is correct
41 Correct 1213 ms 22612 KB Output is correct
42 Correct 877 ms 22136 KB Output is correct
43 Correct 231 ms 14848 KB Output is correct
44 Correct 486 ms 21996 KB Output is correct
45 Correct 343 ms 15104 KB Output is correct
46 Correct 662 ms 22420 KB Output is correct
47 Correct 33 ms 9296 KB Output is correct
48 Correct 41 ms 9356 KB Output is correct
49 Correct 226 ms 9668 KB Output is correct
50 Correct 407 ms 11244 KB Output is correct
51 Correct 427 ms 15448 KB Output is correct
52 Correct 832 ms 17408 KB Output is correct
53 Correct 730 ms 16984 KB Output is correct
54 Correct 940 ms 19036 KB Output is correct
55 Correct 780 ms 18648 KB Output is correct
56 Correct 887 ms 20216 KB Output is correct
57 Correct 940 ms 21804 KB Output is correct
58 Correct 1185 ms 22272 KB Output is correct
59 Correct 895 ms 22804 KB Output is correct
60 Correct 1225 ms 23204 KB Output is correct