Submission #503716

# Submission time Handle Problem Language Result Execution time Memory
503716 2022-01-08T17:05:42 Z amunduzbaev Collapse (JOI18_collapse) C++14
35 / 100
1692 ms 26220 KB
#include "collapse.h"

#include "bits/stdc++.h"
using namespace std;

#ifndef EVAL
#include "grader.cpp"
#endif

#define ar array

namespace First{

struct node{
	int l, r, a, b;
};

struct DSU{
	vector<int> par, sz;
	int n;
	vector<ar<int, 2>> last;
	
	void init(int N){
		n = N;
		par.resize(n), sz.resize(n);
		for(int i=0;i<n;i++) par[i] = i, sz[i] = 1;
	}
	
	int f(int x){ return (par[x] == x ? x : f(par[x])); }
	
	void merge(int a, int b){
		a = f(a), b = f(b);
		if(a == b) return;
		if(sz[a] < sz[b]) swap(a, b);
		par[b] = a, sz[a] += sz[b];
		last.push_back({a, b}), n--;
	}
	
	void roll_back(int m){
		while((int)last.size() > m){
			int a = last.back()[0], b = last.back()[1]; last.pop_back();
			par[b] = b, sz[a] -= sz[b], n++;
		}
	}
};

vector<int> solve(int n, vector<int> t, vector<int> a,
vector<int> b, vector<int> in, vector<int> p){
	int P = p[0];
	int q = in.size(), m = a.size();
	map<ar<int, 2>, int> mm;
	vector<node> edges;
	for(int i=0;i<m;i++){
		if(a[i] > b[i]) swap(a[i], b[i]);
		if(a[i] <= P && P < b[i]) continue;
		if(t[i] == 0){
			mm[{a[i], b[i]}] = i;
		} else {
			edges.push_back({mm[{a[i], b[i]}], i - 1, a[i], b[i]});
			mm.erase({a[i], b[i]});
		}
	}
	
	for(auto x : mm){
		edges.push_back({x.second, m - 1, x.first[0], x.first[1]});
	}
	
	for(int i=0;i<q;i++){
		edges.push_back({in[i], in[i], -1, i});
	} vector<int> res(q);
	DSU dsu;
	dsu.init(n);
	
	function<void(int, int, vector<node>)> go = [&](int l, int r, vector<node> qq){
		int m = dsu.last.size();
		if(l == r){
			for(auto x : qq){
				if(~x.a){
					if(x.l <= l && l <= x.r) dsu.merge(x.a, x.b);
				} else {
					if(l == x.l) res[x.b] = dsu.n;
				}
			}
			
			dsu.roll_back(m);
			return;
		}
		
		vector<node> q2;
		int mid = (l + r) >> 1;
		for(auto x : qq){
			if(~x.a){
				if(x.l <= l && r <= x.r) dsu.merge(x.a, x.b);
				else if(max(x.l, l) <= min(x.r, r)) q2.push_back(x);
			} else {
				if(l <= x.l && x.l <= r) q2.push_back(x);
			}
		}
		
		go(l, mid, q2), go(mid + 1, r, q2);
		dsu.roll_back(m);
	};
	
	go(0, m - 1, edges);
	
	return res;
}

}

namespace SMOL{

vector<int> solve(int n, vector<int> t, vector<int> a,
vector<int> b, vector<int> in, vector<int> p){
	int m = (int)t.size(), q = (int)in.size();
	vector<int> v(q), rr(q);
	iota(v.begin(), v.end(), 0);
	sort(v.begin(), v.end(), [&](int i, int j){
		return (in[i] < in[j]);
	});
	set<ar<int, 2>> ss;
	int j = 0;
	
	auto check = [&](int i){
		int P = p[i];
		
		vector<vector<int>> edges(n);
		for(auto x : ss){
			if(x[0] <= P && x[1] > P) continue;
			edges[x[0]].push_back(x[1]);
			edges[x[1]].push_back(x[0]);
		}
		vector<int> used(n);
		int res = 0;
		
		function<void(int)> dfs = [&](int u){
			used[u] = 1;
			for(auto x : edges[u]){
				if(used[x]) continue;
				dfs(x);
			}
		};
		
		for(int i=0;i<n;i++){
			if(used[i]) continue;
			res++, dfs(i);
		}
		
		rr[i] = res; //.push_back(res);
	};
	
	for(int i=0;i<m;i++){
		if(a[i] > b[i]) swap(a[i], b[i]);
		if(t[i]) ss.erase({a[i], b[i]});
		else ss.insert({a[i], b[i]});
		
		while(j < q && in[v[j]] == i){
			check(v[j]);
			j++;
		}
	}
	
	return rr;
}

}

namespace Second{

vector<int> solve(int n, vector<int> t, vector<int> a,
vector<int> b, vector<int> in, vector<int> p){
	assert(0);
}

}

vector<int> simulateCollapse(
int n, vector<int> t, vector<int> a,
vector<int> b, vector<int> in, vector<int> p) {
	int q = in.size(), m = t.size();
	if(n <= 5000 && q <= 5000 && m <= 5000) return SMOL::solve(n, t, a, b, in, p);
	//~ assert(0);
	if(*max_element(p.begin(), p.end()) == *min_element(p.begin(), p.end())){
		return First::solve(n, t, a, b, in, p);
	} if(*max_element(t.begin(), t.end()) == 0){
		return Second::solve(n, t, a, b, in, p);
	} else assert(0);
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 588 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 9 ms 460 KB Output is correct
5 Correct 8 ms 588 KB Output is correct
6 Correct 430 ms 916 KB Output is correct
7 Correct 255 ms 556 KB Output is correct
8 Correct 249 ms 588 KB Output is correct
9 Correct 260 ms 764 KB Output is correct
10 Correct 480 ms 856 KB Output is correct
11 Correct 1692 ms 1412 KB Output is correct
12 Correct 1224 ms 1192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 6204 KB Output is correct
2 Correct 34 ms 12956 KB Output is correct
3 Correct 122 ms 18148 KB Output is correct
4 Correct 36 ms 12616 KB Output is correct
5 Correct 116 ms 18488 KB Output is correct
6 Correct 53 ms 13392 KB Output is correct
7 Correct 139 ms 25256 KB Output is correct
8 Correct 119 ms 18588 KB Output is correct
9 Correct 27 ms 6956 KB Output is correct
10 Correct 37 ms 13488 KB Output is correct
11 Correct 49 ms 13812 KB Output is correct
12 Correct 129 ms 20324 KB Output is correct
13 Correct 146 ms 23140 KB Output is correct
14 Correct 153 ms 26220 KB Output is correct
15 Correct 153 ms 26068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 6068 KB Output is correct
2 Runtime error 15 ms 5180 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 588 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 9 ms 460 KB Output is correct
5 Correct 8 ms 588 KB Output is correct
6 Correct 430 ms 916 KB Output is correct
7 Correct 255 ms 556 KB Output is correct
8 Correct 249 ms 588 KB Output is correct
9 Correct 260 ms 764 KB Output is correct
10 Correct 480 ms 856 KB Output is correct
11 Correct 1692 ms 1412 KB Output is correct
12 Correct 1224 ms 1192 KB Output is correct
13 Correct 26 ms 6204 KB Output is correct
14 Correct 34 ms 12956 KB Output is correct
15 Correct 122 ms 18148 KB Output is correct
16 Correct 36 ms 12616 KB Output is correct
17 Correct 116 ms 18488 KB Output is correct
18 Correct 53 ms 13392 KB Output is correct
19 Correct 139 ms 25256 KB Output is correct
20 Correct 119 ms 18588 KB Output is correct
21 Correct 27 ms 6956 KB Output is correct
22 Correct 37 ms 13488 KB Output is correct
23 Correct 49 ms 13812 KB Output is correct
24 Correct 129 ms 20324 KB Output is correct
25 Correct 146 ms 23140 KB Output is correct
26 Correct 153 ms 26220 KB Output is correct
27 Correct 153 ms 26068 KB Output is correct
28 Correct 24 ms 6068 KB Output is correct
29 Runtime error 15 ms 5180 KB Execution killed with signal 6
30 Halted 0 ms 0 KB -