답안 #380909

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
380909 2021-03-23T14:29:17 Z qwerty234 Collapse (JOI18_collapse) C++14
100 / 100
7474 ms 31268 KB
#include <bits/stdc++.h>
#include "collapse.h"
#define pb push_back
#define fi first
#define se second
using namespace std;

const int blocksz = 300;

struct query {
	int plan_day, collapse_town, id;
	query(int x, int y, int z) {plan_day = x; collapse_town = y; id = z;}
};
bool cmpquery(query A, query B) {return A.plan_day < B.plan_day;}

struct operation {
	int u, v, sz_u, sz_v;
	operation(int x, int y, int z, int d) {u = x; v = y; sz_u = z; sz_v = d;}
};
vector <int> sz, p;
vector <operation> op;
int sz_comp;

void make_set(int n) {
	sz.assign(n, 1); p.assign(n, 0); op.clear();
	for (int i = 0; i < n; i++) p[i] = i;
	sz_comp = n;
}

int getp(int u) {
	if (p[u] == u) return u;
	return getp(p[u]);
}

int add(int u, int v) {
	u = getp(u); v = getp(v);
	if (u == v) return 0;
	if (sz[u] > sz[v]) swap(u, v);
	op.pb({u, v, sz[u], sz[v]});
	p[u] = v;
	sz[v] += sz[u];
	sz_comp--;
	return 1;
}

void go_back() {
	operation opp = op[op.size() - 1];
	int u = opp.u, v = opp.v, sz_u = opp.sz_u, sz_v = opp.sz_v;
	p[u] = u; p[v] = v; sz[u] = sz_u; sz[v] = sz_v;
	sz_comp++;
	op.pop_back();
}

vector <int> simulateCollapse(int n, vector <int> t, vector <int> x, vector <int> y, vector <int> w, vector <int> p) {
	int c = t.size(), q = w.size(), m = 0;
	// if (n > 5005 || c > 5005 || q > 5005)
	// 	return {};
	vector <query> qu; qu.clear();
	for (int i = 0; i < q; i++) {
		qu.pb({w[i], p[i], i});
	}
	sort(qu.begin(), qu.end(), cmpquery);
	vector <int> refer, state, vis; refer.assign(c, 0);
	vector <pair<int, int>> edges; edges.clear();
	map <pair<int, int>, int> edge_id; edge_id.clear();
	for (int i = 0; i < c; i++) {
		if (x[i] > y[i]) swap(x[i], y[i]);
		if (!edge_id.count({x[i], y[i]})) {
			edge_id[{x[i], y[i]}] = m++; edges.pb({x[i], y[i]});
		}
		refer[i] = edge_id[{x[i], y[i]}];
	}
	vector <vector<int>> min_v, max_v, qus_v; min_v.assign(n, {}); max_v.assign(n, {}); qus_v.assign(n, {});
	vector <int> ans; ans.assign(q, 0);
	int curblock = 0;
	while (true) {
		int l = curblock * blocksz, r = min(l + blocksz - 1, q - 1);
		vis.assign(m, 0); state.assign(m, 0);
		if (l >= q) break;
		for (int j = 0; j < qu[l].plan_day; j++) state[refer[j]] ^= 1;
		for (int i = 0; i < n; i++) {
			min_v[i].clear(); max_v[i].clear(); qus_v[i].clear();
		}
		for (int i = l; i <= r; i++) {
			qus_v[qu[i].collapse_town].pb(i);
		}
		for (int j = qu[l].plan_day; j <= qu[r].plan_day; j++)
			vis[refer[j]] = 1;
		for (int i = 0; i < m; i++) {
			if (state[i] && !vis[i]) {
				min_v[edges[i].fi].pb(i);
				max_v[edges[i].se].pb(i);
			}
		}
		make_set(n);
		for (int i = 0; i <= n - 2; i++) {
			for (int id : max_v[i])
				add(edges[id].fi, edges[id].se);
			for (int id : qus_v[i]) {
				int cnt_op = 0;
				for (int j = qu[l].plan_day; j <= qu[id].plan_day; j++) state[refer[j]] ^= 1;
				for (int j = qu[l].plan_day; j <= qu[r].plan_day; j++) {
					if (state[refer[j]] && edges[refer[j]].se <= i)
						cnt_op += add(edges[refer[j]].fi, edges[refer[j]].se);
				}
				for (int j = qu[l].plan_day; j <= qu[id].plan_day; j++) state[refer[j]] ^= 1;
				ans[qu[id].id] += sz_comp - (n - i - 1);
				while (cnt_op--) go_back();
			}
		}
		make_set(n);
		for (int i = n - 1; i >= 1; i--) {
			for (int id : min_v[i])
				add(edges[id].fi, edges[id].se);
			for (int id : qus_v[i - 1]) {
				int cnt_op = 0;
				for (int j = qu[l].plan_day; j <= qu[id].plan_day; j++) state[refer[j]] ^= 1;
				for (int j = qu[l].plan_day; j <= qu[r].plan_day; j++) {
					if (state[refer[j]] && i <= edges[refer[j]].fi)
						cnt_op += add(edges[refer[j]].fi, edges[refer[j]].se);
				}
				for (int j = qu[l].plan_day; j <= qu[id].plan_day; j++) state[refer[j]] ^= 1;
				ans[qu[id].id] += sz_comp - i;
				while (cnt_op--) go_back();
			}
		}
		curblock++;
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 620 KB Output is correct
2 Correct 3 ms 616 KB Output is correct
3 Correct 4 ms 616 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 34 ms 620 KB Output is correct
6 Correct 28 ms 1132 KB Output is correct
7 Correct 5 ms 1024 KB Output is correct
8 Correct 5 ms 1004 KB Output is correct
9 Correct 27 ms 1360 KB Output is correct
10 Correct 43 ms 1408 KB Output is correct
11 Correct 48 ms 1920 KB Output is correct
12 Correct 40 ms 1788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 3556 KB Output is correct
2 Correct 51 ms 3556 KB Output is correct
3 Correct 678 ms 6240 KB Output is correct
4 Correct 44 ms 3560 KB Output is correct
5 Correct 590 ms 6604 KB Output is correct
6 Correct 117 ms 4064 KB Output is correct
7 Correct 1528 ms 15712 KB Output is correct
8 Correct 723 ms 11488 KB Output is correct
9 Correct 446 ms 11232 KB Output is correct
10 Correct 487 ms 11232 KB Output is correct
11 Correct 529 ms 11488 KB Output is correct
12 Correct 1192 ms 18572 KB Output is correct
13 Correct 2856 ms 22588 KB Output is correct
14 Correct 6518 ms 26892 KB Output is correct
15 Correct 4749 ms 26516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 3556 KB Output is correct
2 Correct 47 ms 3556 KB Output is correct
3 Correct 44 ms 3560 KB Output is correct
4 Correct 47 ms 3560 KB Output is correct
5 Correct 56 ms 3560 KB Output is correct
6 Correct 132 ms 4064 KB Output is correct
7 Correct 1155 ms 12564 KB Output is correct
8 Correct 2263 ms 17012 KB Output is correct
9 Correct 492 ms 14432 KB Output is correct
10 Correct 554 ms 13536 KB Output is correct
11 Correct 4847 ms 28640 KB Output is correct
12 Correct 7091 ms 28972 KB Output is correct
13 Correct 5092 ms 28724 KB Output is correct
14 Correct 7474 ms 29000 KB Output is correct
15 Correct 5216 ms 28776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 620 KB Output is correct
2 Correct 3 ms 616 KB Output is correct
3 Correct 4 ms 616 KB Output is correct
4 Correct 3 ms 616 KB Output is correct
5 Correct 34 ms 620 KB Output is correct
6 Correct 28 ms 1132 KB Output is correct
7 Correct 5 ms 1024 KB Output is correct
8 Correct 5 ms 1004 KB Output is correct
9 Correct 27 ms 1360 KB Output is correct
10 Correct 43 ms 1408 KB Output is correct
11 Correct 48 ms 1920 KB Output is correct
12 Correct 40 ms 1788 KB Output is correct
13 Correct 40 ms 3556 KB Output is correct
14 Correct 51 ms 3556 KB Output is correct
15 Correct 678 ms 6240 KB Output is correct
16 Correct 44 ms 3560 KB Output is correct
17 Correct 590 ms 6604 KB Output is correct
18 Correct 117 ms 4064 KB Output is correct
19 Correct 1528 ms 15712 KB Output is correct
20 Correct 723 ms 11488 KB Output is correct
21 Correct 446 ms 11232 KB Output is correct
22 Correct 487 ms 11232 KB Output is correct
23 Correct 529 ms 11488 KB Output is correct
24 Correct 1192 ms 18572 KB Output is correct
25 Correct 2856 ms 22588 KB Output is correct
26 Correct 6518 ms 26892 KB Output is correct
27 Correct 4749 ms 26516 KB Output is correct
28 Correct 51 ms 3556 KB Output is correct
29 Correct 47 ms 3556 KB Output is correct
30 Correct 44 ms 3560 KB Output is correct
31 Correct 47 ms 3560 KB Output is correct
32 Correct 56 ms 3560 KB Output is correct
33 Correct 132 ms 4064 KB Output is correct
34 Correct 1155 ms 12564 KB Output is correct
35 Correct 2263 ms 17012 KB Output is correct
36 Correct 492 ms 14432 KB Output is correct
37 Correct 554 ms 13536 KB Output is correct
38 Correct 4847 ms 28640 KB Output is correct
39 Correct 7091 ms 28972 KB Output is correct
40 Correct 5092 ms 28724 KB Output is correct
41 Correct 7474 ms 29000 KB Output is correct
42 Correct 5216 ms 28776 KB Output is correct
43 Correct 806 ms 12256 KB Output is correct
44 Correct 1543 ms 17604 KB Output is correct
45 Correct 903 ms 13908 KB Output is correct
46 Correct 2293 ms 19368 KB Output is correct
47 Correct 478 ms 15328 KB Output is correct
48 Correct 492 ms 14176 KB Output is correct
49 Correct 552 ms 14560 KB Output is correct
50 Correct 952 ms 16484 KB Output is correct
51 Correct 1383 ms 23008 KB Output is correct
52 Correct 2730 ms 26156 KB Output is correct
53 Correct 2210 ms 25116 KB Output is correct
54 Correct 3802 ms 27808 KB Output is correct
55 Correct 2948 ms 26684 KB Output is correct
56 Correct 3696 ms 28000 KB Output is correct
57 Correct 4364 ms 29408 KB Output is correct
58 Correct 5954 ms 30436 KB Output is correct
59 Correct 5183 ms 31072 KB Output is correct
60 Correct 7310 ms 31268 KB Output is correct