Submission #503711

# Submission time Handle Problem Language Result Execution time Memory
503711 2022-01-08T16:58:07 Z amunduzbaev Collapse (JOI18_collapse) C++14
30 / 100
815 ms 524292 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<set<ar<int, 2>>> ee(m);
	set<ar<int, 2>> ss;
	for(int i=0;i<m;i++){
		if(a[i] > b[i]) swap(a[i], b[i]);
		if(t[i]) assert(ss.count({a[i], b[i]})), ss.erase({a[i], b[i]});
		else assert(!ss.count({a[i], b[i]})), ss.insert({a[i], b[i]});
		ee[i] = ss;
	}
	
	vector<int> rr;
	for(int i=0;i<q;i++){
		int P = p[i];
		
		vector<vector<int>> edges(n);
		for(auto x : ee[in[i]]){
			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.push_back(res);
	} 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);
	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 4 ms 944 KB Output is correct
2 Correct 3 ms 420 KB Output is correct
3 Correct 7 ms 460 KB Output is correct
4 Correct 9 ms 516 KB Output is correct
5 Correct 20 ms 3788 KB Output is correct
6 Runtime error 815 ms 524292 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 6076 KB Output is correct
2 Correct 40 ms 12960 KB Output is correct
3 Correct 133 ms 18220 KB Output is correct
4 Correct 44 ms 12712 KB Output is correct
5 Correct 118 ms 18488 KB Output is correct
6 Correct 54 ms 13404 KB Output is correct
7 Correct 158 ms 25124 KB Output is correct
8 Correct 132 ms 18708 KB Output is correct
9 Correct 29 ms 6836 KB Output is correct
10 Correct 40 ms 13488 KB Output is correct
11 Correct 49 ms 13732 KB Output is correct
12 Correct 141 ms 20380 KB Output is correct
13 Correct 145 ms 23168 KB Output is correct
14 Correct 167 ms 26232 KB Output is correct
15 Correct 150 ms 26016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 6188 KB Output is correct
2 Runtime error 17 ms 5196 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 944 KB Output is correct
2 Correct 3 ms 420 KB Output is correct
3 Correct 7 ms 460 KB Output is correct
4 Correct 9 ms 516 KB Output is correct
5 Correct 20 ms 3788 KB Output is correct
6 Runtime error 815 ms 524292 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -