Submission #102099

# Submission time Handle Problem Language Result Execution time Memory
102099 2019-03-22T09:57:54 Z gs14004 Collapse (JOI18_collapse) C++17
100 / 100
3009 ms 32952 KB
#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
const int MAXN = 100050;

struct disj{
	int pa[MAXN], rk[MAXN];
	void init(int n){ iota(pa, pa + n, 0); fill(rk, rk + n, 0); }
	int find(int x){
		return pa[x] == x ? x : find(pa[x]);
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		if(rk[p] < rk[q]) swap(p, q);
		pa[q] = p;
		if(rk[p] == rk[q]) rk[p]++;
		return 1;
	}
	bool uni(int p, int q, vector<pi> &r){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		if(rk[p] < rk[q]) swap(p, q);
		pa[q] = p;
		r.push_back(pi(1, q));
		if(rk[p] == rk[q]){
			rk[p]++;
			r.push_back(pi(2, p));
		}
		return 1;
	}
	void revert(vector<pi> &r){
		for(int i=(int)r.size()-1; i>=0; i--){
			if(r[i].first == 2) rk[r[i].second]--;
			else pa[r[i].second] = r[i].second;
		}
		r.clear();
	}
}disj;

struct edg{
	int s, e, x;
	bool operator<(const edg &e)const{
		return x < e.x;
	}
}E[MAXN];

struct bit{
	int tree[MAXN];
	void clear(){ memset(tree, 0, sizeof(tree)); }
	void add(int x, int v){
		x++;
		while(x < MAXN){
			tree[x] += v;
			x += x & -x;
		}
	}
	int query(int x){
		x++;
		int ret= 0;
		while(x){
			ret += tree[x];
			x -= x & -x;
		}
		return ret;
	}
}bit;

vector<pi> query[MAXN];
int ans[MAXN];
pi qry[MAXN];
int chk[MAXN];

void solve(int s, int e, vector<int> active_edges){
	if(s == e){
		E[qry[s].first].x = qry[s].second; 
		vector<int> nxt_active_edges = active_edges;
		nxt_active_edges.push_back(qry[s].first);
		sort(nxt_active_edges.begin(), nxt_active_edges.end(), [&](const int &a, const int &b){
			return E[a].x < E[b].x;
		});
		vector<pi> tmp;
		vector<int> sex;
		for(auto &i : nxt_active_edges){
			if(disj.uni(E[i].s, E[i].e, tmp)){
				sex.push_back(E[i].x);
		//		printf("MST add %d\n", i);
				bit.add(E[i].x, 1);
			}
		}
		for(auto &i : query[s]){
			ans[i.second] += bit.query(i.first);
		}
		disj.revert(tmp);
	//	printf("state of query %d\n", s);
		for(auto &i : sex) bit.add(i, -1);
		return;
	}
	int m = (s+e)/2;
	/*** CONTRACTION 1 ***/
	{
		vector<int> nxt_active_edges = active_edges;
		for(int i=m+1; i<=e; i++){
			chk[qry[i].first]--;
			if(chk[qry[i].first] == 0) nxt_active_edges.push_back(qry[i].first);
		}
		sort(nxt_active_edges.begin(), nxt_active_edges.end(), [&](const int &a, const int &b){
			return E[a].x < E[b].x;
		});
		vector<int> maybe, must, cand;
		{
			vector<pi> tmp;
			for(auto &i : nxt_active_edges){
				if(disj.uni(E[i].s, E[i].e, tmp)) maybe.push_back(i);
			}
			disj.revert(tmp);
		}
		{
			vector<pi> tmp;
			for(int i=s; i<=m; i++) disj.uni(E[qry[i].first].s, E[qry[i].first].e, tmp);
			for(auto &i : maybe){
				if(disj.uni(E[i].s, E[i].e, tmp)) must.push_back(i);
				else cand.push_back(i);
			}
			disj.revert(tmp);
		}
		vector<pi> tmp;
		vector<int> sex;
		for(auto &i : must){
	//		printf("MST add %d\n", i);
			disj.uni(E[i].s, E[i].e, tmp);
			bit.add(E[i].x, 1);
			sex.push_back(E[i].x);
		}
		solve(s, m, cand);
		disj.revert(tmp);
		for(int i=m+1; i<=e; i++){
			chk[qry[i].first]++;
		}
		for(auto &i : sex) bit.add(i, -1);
	}
	/*** CONTRACTION 2 ***/
	{
		vector<int> nxt_active_edges = active_edges;
		for(int i=s; i<=m; i++){
			chk[qry[i].first]--;
			if(chk[qry[i].first] == 0) nxt_active_edges.push_back(qry[i].first);
		}
		sort(nxt_active_edges.begin(), nxt_active_edges.end(), [&](const int &a, const int &b){
			return E[a].x < E[b].x;
		});
		vector<int> maybe, must, cand;
		{
			vector<pi> tmp;
			for(auto &i : nxt_active_edges){
				if(disj.uni(E[i].s, E[i].e, tmp)) maybe.push_back(i);
			}
			disj.revert(tmp);
		}
		{
			vector<pi> tmp;
			for(int i=m+1; i<=e; i++) disj.uni(E[qry[i].first].s, E[qry[i].first].e, tmp);
			for(auto &i : maybe){
				if(disj.uni(E[i].s, E[i].e, tmp)) must.push_back(i);
				else cand.push_back(i);
			}
			disj.revert(tmp);
		}
		vector<pi> tmp;
		vector<int> sex;
		for(auto &i : must){
	//		printf("MST add %d\n", i);
			disj.uni(E[i].s, E[i].e, tmp);
			bit.add(E[i].x, 1);
			sex.push_back(E[i].x);
		}
		solve(m+1, e, cand);
		disj.revert(tmp);
		for(int i=s; i<=m; i++){
			chk[qry[i].first]++;
		}
		for(auto &i : sex) bit.add(i, -1);
	}
}

void solve_dmst(int Q, int VN, int EN){
	disj.init(VN);
	vector<int> v;
	memset(chk, 0, sizeof(chk));
	for(int i=0; i<Q; i++) chk[qry[i].first]++;
	for(int i=0; i<EN; i++){
		if(chk[i] == 0) v.push_back(i);
	}
	solve(0, Q-1, v);
}

std::vector<int> simulateCollapse(int N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P){
	int Q = W.size();
	int C = T.size();
	for(int i=0; i<Q; i++){
		query[W[i]].push_back(pi(P[i], i)); // count edges with weight leq than
	}
	int cnt = 0;
	map<pi, int> mp;
	for(int i=0; i<C; i++){
		if(T[i] == 0){
			E[cnt++] = (edg){X[i], Y[i], N + 3};
			mp[pi(X[i], Y[i])] = mp[pi(Y[i], X[i])] = cnt - 1;
			qry[i] = pi(cnt - 1, max(X[i], Y[i]));
		}
		else{
			qry[i] = pi(mp[pi(X[i], Y[i])], N + 3);
		}
	}
	solve_dmst(C, N, cnt);
	for(int i=0; i<C; i++){
		for(auto &j : query[i]){
			j.first = N - 2 - j.first;
		}
	}
	for(int i=0; i<cnt; i++) E[i].x = N + 3;
	for(int i=0; i<C; i++){
		if(T[i] == 0){
			qry[i].second = N - 1 - min(E[qry[i].first].s, E[qry[i].first].e);
		}
	}
	solve_dmst(C, N, cnt);
	vector<int> ansr(Q);
	for(int i=0; i<Q; i++) ansr[i] = N - ans[i];
	return ansr;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3712 KB Output is correct
2 Correct 8 ms 3328 KB Output is correct
3 Correct 7 ms 3328 KB Output is correct
4 Correct 9 ms 3328 KB Output is correct
5 Correct 49 ms 3636 KB Output is correct
6 Correct 81 ms 4312 KB Output is correct
7 Correct 12 ms 3456 KB Output is correct
8 Correct 9 ms 3456 KB Output is correct
9 Correct 59 ms 4096 KB Output is correct
10 Correct 64 ms 4224 KB Output is correct
11 Correct 80 ms 4644 KB Output is correct
12 Correct 77 ms 4536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 6636 KB Output is correct
2 Correct 42 ms 7116 KB Output is correct
3 Correct 1120 ms 14092 KB Output is correct
4 Correct 36 ms 7292 KB Output is correct
5 Correct 1529 ms 14864 KB Output is correct
6 Correct 96 ms 8696 KB Output is correct
7 Correct 1944 ms 27856 KB Output is correct
8 Correct 2024 ms 22100 KB Output is correct
9 Correct 60 ms 7916 KB Output is correct
10 Correct 37 ms 8028 KB Output is correct
11 Correct 74 ms 9208 KB Output is correct
12 Correct 1495 ms 24260 KB Output is correct
13 Correct 2307 ms 27364 KB Output is correct
14 Correct 3009 ms 32952 KB Output is correct
15 Correct 2037 ms 32676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 6644 KB Output is correct
2 Correct 33 ms 6904 KB Output is correct
3 Correct 43 ms 7288 KB Output is correct
4 Correct 32 ms 7160 KB Output is correct
5 Correct 43 ms 7288 KB Output is correct
6 Correct 124 ms 8400 KB Output is correct
7 Correct 1471 ms 22008 KB Output is correct
8 Correct 2390 ms 28256 KB Output is correct
9 Correct 42 ms 7796 KB Output is correct
10 Correct 70 ms 8904 KB Output is correct
11 Correct 2071 ms 31536 KB Output is correct
12 Correct 2664 ms 31876 KB Output is correct
13 Correct 2053 ms 31212 KB Output is correct
14 Correct 2850 ms 31840 KB Output is correct
15 Correct 2391 ms 31248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3712 KB Output is correct
2 Correct 8 ms 3328 KB Output is correct
3 Correct 7 ms 3328 KB Output is correct
4 Correct 9 ms 3328 KB Output is correct
5 Correct 49 ms 3636 KB Output is correct
6 Correct 81 ms 4312 KB Output is correct
7 Correct 12 ms 3456 KB Output is correct
8 Correct 9 ms 3456 KB Output is correct
9 Correct 59 ms 4096 KB Output is correct
10 Correct 64 ms 4224 KB Output is correct
11 Correct 80 ms 4644 KB Output is correct
12 Correct 77 ms 4536 KB Output is correct
13 Correct 35 ms 6636 KB Output is correct
14 Correct 42 ms 7116 KB Output is correct
15 Correct 1120 ms 14092 KB Output is correct
16 Correct 36 ms 7292 KB Output is correct
17 Correct 1529 ms 14864 KB Output is correct
18 Correct 96 ms 8696 KB Output is correct
19 Correct 1944 ms 27856 KB Output is correct
20 Correct 2024 ms 22100 KB Output is correct
21 Correct 60 ms 7916 KB Output is correct
22 Correct 37 ms 8028 KB Output is correct
23 Correct 74 ms 9208 KB Output is correct
24 Correct 1495 ms 24260 KB Output is correct
25 Correct 2307 ms 27364 KB Output is correct
26 Correct 3009 ms 32952 KB Output is correct
27 Correct 2037 ms 32676 KB Output is correct
28 Correct 34 ms 6644 KB Output is correct
29 Correct 33 ms 6904 KB Output is correct
30 Correct 43 ms 7288 KB Output is correct
31 Correct 32 ms 7160 KB Output is correct
32 Correct 43 ms 7288 KB Output is correct
33 Correct 124 ms 8400 KB Output is correct
34 Correct 1471 ms 22008 KB Output is correct
35 Correct 2390 ms 28256 KB Output is correct
36 Correct 42 ms 7796 KB Output is correct
37 Correct 70 ms 8904 KB Output is correct
38 Correct 2071 ms 31536 KB Output is correct
39 Correct 2664 ms 31876 KB Output is correct
40 Correct 2053 ms 31212 KB Output is correct
41 Correct 2850 ms 31840 KB Output is correct
42 Correct 2391 ms 31248 KB Output is correct
43 Correct 1657 ms 19408 KB Output is correct
44 Correct 1749 ms 26576 KB Output is correct
45 Correct 2008 ms 20900 KB Output is correct
46 Correct 2411 ms 28140 KB Output is correct
47 Correct 42 ms 7848 KB Output is correct
48 Correct 46 ms 8180 KB Output is correct
49 Correct 77 ms 9168 KB Output is correct
50 Correct 301 ms 11244 KB Output is correct
51 Correct 1729 ms 22724 KB Output is correct
52 Correct 2116 ms 24792 KB Output is correct
53 Correct 1897 ms 23784 KB Output is correct
54 Correct 2080 ms 26928 KB Output is correct
55 Correct 2073 ms 25948 KB Output is correct
56 Correct 1987 ms 28016 KB Output is correct
57 Correct 2280 ms 29604 KB Output is correct
58 Correct 2741 ms 29996 KB Output is correct
59 Correct 2510 ms 31476 KB Output is correct
60 Correct 2934 ms 31880 KB Output is correct