Submission #60849

# Submission time Handle Problem Language Result Execution time Memory
60849 2018-07-24T20:49:23 Z egorlifar Collapse (JOI18_collapse) C++17
100 / 100
7182 ms 107988 KB
#include "collapse.h"
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <iomanip>
#include <deque>
#include <bits/stdc++.h> 

     
using namespace std;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228                                                         
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define x first
#define y second
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } 
const int block = 1000;

 
class DSU {
private:
	vector<int> parent;
	vector<int> rank;
	bool need;
	vector<int> change;
public:
	DSU(int n, bool need_ = false) : parent(n), rank(n, 0), need(need_) {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
		}
	}
	int findset(int x) {
		if (parent[x] == x) {
			return x;
		}
		return parent[x] = findset(parent[x]);
	}
	bool setunion(int x, int y) {
		int xp = findset(x);
		int yp = findset(y);
		if (xp == yp) {
			return false;
		} else {
			if (need) {
				change.pb(xp);
				change.pb(yp);
			}
			if (rank[xp] > rank[yp]) {
				parent[yp] = xp;
			} else if (rank[xp] < rank[yp]) {
				parent[xp] = yp;
			} else {
				parent[yp] = xp;
				rank[xp]++;
			}
			return true;
		}
	}
	void reset() {
		for (auto i : change) {
			parent[i] = i;
			rank[i] = 0;
		}
		change.clear();
	}
};

 
vector<pair<int, int>> solve(
	int n,
	vector<pair<int, int> > fixed,
	vector<pair<int, pair<int, shared_ptr<vector<pair<int, int> >>>>> query
) {
	auto comp_second = [](const pair<int, int>& a, const pair<int, int>& b){ return a.second < b.second; };
	auto comp_first_only = [](const pair<int, pair<int, shared_ptr<vector<pair<int, int> >>>>& a, const pair<int, pair<int, shared_ptr<vector<pair<int, int> >>>>& b){ return a.first < b.first; };
	sort(all(fixed), comp_second);
	sort(all(query), comp_first_only);
	vector<pair<int, int> > ret;
	DSU large(n), tmp(n, true);
	int now = 0;
	int count = 0;
	for (auto i: query) {
		while (now < sz(fixed) && fixed[now].second <= i.first) {
			if (large.setunion(fixed[now].first, fixed[now].second)) {
				count++;
			}
			now++;
		}
		int c = count;
		for (auto j: *i.second.second) {
			if (j.second <= i.first && tmp.setunion(large.findset(j.first), large.findset(j.second))) {
				c++;
			}
		}
		ret.pb(make_pair(i.second.first, i.first + 1 - c));
		tmp.reset();
	}
	return ret;
}

 
pair<int, int> maxmin(int a, int b) {
	return make_pair(min(a, b), max(a, b));
}
 

pair<int, int> revp(int n, pair<int, int> p) {
	return make_pair(n - 1 - p.second, n - 1 - p.first);
}


vector<int> simulateCollapse(int N, vector<int> T, vector<int> X, vector<int> Y, vector<int> W, vector<int> P) {
	vector<pair<pair<int, int>, int>> q;
	for(int i = 0; i < sz(W); i++) {
		q.pb(make_pair(make_pair(W[i], P[i]), i));
	}
	sort(all(q));
	vector<int> ans(sz(q), 0);
	set<pair<int, int> > path;
	int now = 0;
	for(int s = 0; s < sz(T); s += block) {
		int e = min(s + block, sz(T));
		set<pair<int, int> > change;
		for (int i = s; i < e; i++) {
			change.insert(maxmin(X[i], Y[i]));
		}
		auto fixed = path;
		set<pair<int, int> > np;
		for(auto i: change) {
			if (fixed.find(i) != fixed.end()) {
				fixed.erase(i);
				np.insert(i);
			}
		}
		vector<pair<int, int> > fixed1, fixed2;
		for (auto i: fixed) {
			fixed1.pb(i);
			fixed2.pb(revp(N, i));
		}
		vector<pair<int, pair<int, shared_ptr<vector<pair<int, int> >> > > > query1, query2;
		for (int i = s; i < e; i++) {
			if (T[i] == 0) {
				path.insert(maxmin(X[i], Y[i]));
				np.insert(maxmin(X[i], Y[i]));
			} else {
				path.erase(maxmin(X[i], Y[i]));
				np.erase(maxmin(X[i], Y[i]));
			}
			shared_ptr<vector<pair<int, int> > > p1(new vector<pair<int, int> >), p2(new vector<pair<int, int> >);
			for (auto j: np) {
				p1->pb(j);
				p2->pb(revp(N, j));
			}
			while (now < sz(q) && q[now].first.first == i) {
				query1.pb(make_pair(q[now].first.second, make_pair(q[now].second, p1)));
				query2.pb(make_pair(N - 2 - q[now].first.second, make_pair(q[now].second, p2)));
				now++;
			}
		}
		for (auto x: solve(N, fixed1, query1)) {
			ans[x.first] += x.second;
		}
		for (auto x: solve(N, fixed2, query2)) {
			ans[x.first] += x.second;
		}
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 888 KB Output is correct
2 Correct 7 ms 1424 KB Output is correct
3 Correct 8 ms 1476 KB Output is correct
4 Correct 8 ms 1692 KB Output is correct
5 Correct 19 ms 1712 KB Output is correct
6 Correct 114 ms 8592 KB Output is correct
7 Correct 7 ms 8592 KB Output is correct
8 Correct 10 ms 8592 KB Output is correct
9 Correct 21 ms 8592 KB Output is correct
10 Correct 109 ms 8592 KB Output is correct
11 Correct 212 ms 9220 KB Output is correct
12 Correct 158 ms 9712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 16192 KB Output is correct
2 Correct 93 ms 16960 KB Output is correct
3 Correct 338 ms 16960 KB Output is correct
4 Correct 99 ms 18492 KB Output is correct
5 Correct 919 ms 18492 KB Output is correct
6 Correct 533 ms 23672 KB Output is correct
7 Correct 4876 ms 37436 KB Output is correct
8 Correct 921 ms 37436 KB Output is correct
9 Correct 79 ms 37436 KB Output is correct
10 Correct 92 ms 37436 KB Output is correct
11 Correct 412 ms 37436 KB Output is correct
12 Correct 1251 ms 37436 KB Output is correct
13 Correct 4244 ms 42720 KB Output is correct
14 Correct 6770 ms 51020 KB Output is correct
15 Correct 5860 ms 53548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 53548 KB Output is correct
2 Correct 75 ms 53548 KB Output is correct
3 Correct 97 ms 53548 KB Output is correct
4 Correct 95 ms 53548 KB Output is correct
5 Correct 396 ms 53548 KB Output is correct
6 Correct 619 ms 53548 KB Output is correct
7 Correct 3934 ms 53548 KB Output is correct
8 Correct 5683 ms 55176 KB Output is correct
9 Correct 83 ms 55176 KB Output is correct
10 Correct 1263 ms 59136 KB Output is correct
11 Correct 6840 ms 61172 KB Output is correct
12 Correct 6980 ms 63528 KB Output is correct
13 Correct 6118 ms 66208 KB Output is correct
14 Correct 6563 ms 68668 KB Output is correct
15 Correct 5928 ms 71076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 888 KB Output is correct
2 Correct 7 ms 1424 KB Output is correct
3 Correct 8 ms 1476 KB Output is correct
4 Correct 8 ms 1692 KB Output is correct
5 Correct 19 ms 1712 KB Output is correct
6 Correct 114 ms 8592 KB Output is correct
7 Correct 7 ms 8592 KB Output is correct
8 Correct 10 ms 8592 KB Output is correct
9 Correct 21 ms 8592 KB Output is correct
10 Correct 109 ms 8592 KB Output is correct
11 Correct 212 ms 9220 KB Output is correct
12 Correct 158 ms 9712 KB Output is correct
13 Correct 78 ms 16192 KB Output is correct
14 Correct 93 ms 16960 KB Output is correct
15 Correct 338 ms 16960 KB Output is correct
16 Correct 99 ms 18492 KB Output is correct
17 Correct 919 ms 18492 KB Output is correct
18 Correct 533 ms 23672 KB Output is correct
19 Correct 4876 ms 37436 KB Output is correct
20 Correct 921 ms 37436 KB Output is correct
21 Correct 79 ms 37436 KB Output is correct
22 Correct 92 ms 37436 KB Output is correct
23 Correct 412 ms 37436 KB Output is correct
24 Correct 1251 ms 37436 KB Output is correct
25 Correct 4244 ms 42720 KB Output is correct
26 Correct 6770 ms 51020 KB Output is correct
27 Correct 5860 ms 53548 KB Output is correct
28 Correct 66 ms 53548 KB Output is correct
29 Correct 75 ms 53548 KB Output is correct
30 Correct 97 ms 53548 KB Output is correct
31 Correct 95 ms 53548 KB Output is correct
32 Correct 396 ms 53548 KB Output is correct
33 Correct 619 ms 53548 KB Output is correct
34 Correct 3934 ms 53548 KB Output is correct
35 Correct 5683 ms 55176 KB Output is correct
36 Correct 83 ms 55176 KB Output is correct
37 Correct 1263 ms 59136 KB Output is correct
38 Correct 6840 ms 61172 KB Output is correct
39 Correct 6980 ms 63528 KB Output is correct
40 Correct 6118 ms 66208 KB Output is correct
41 Correct 6563 ms 68668 KB Output is correct
42 Correct 5928 ms 71076 KB Output is correct
43 Correct 624 ms 71076 KB Output is correct
44 Correct 5139 ms 73436 KB Output is correct
45 Correct 812 ms 73436 KB Output is correct
46 Correct 5395 ms 78312 KB Output is correct
47 Correct 82 ms 78312 KB Output is correct
48 Correct 104 ms 78312 KB Output is correct
49 Correct 689 ms 78312 KB Output is correct
50 Correct 1454 ms 78312 KB Output is correct
51 Correct 1344 ms 78312 KB Output is correct
52 Correct 3821 ms 79692 KB Output is correct
53 Correct 3570 ms 82080 KB Output is correct
54 Correct 4213 ms 86940 KB Output is correct
55 Correct 4056 ms 89832 KB Output is correct
56 Correct 5071 ms 93980 KB Output is correct
57 Correct 6208 ms 98580 KB Output is correct
58 Correct 6059 ms 101072 KB Output is correct
59 Correct 6298 ms 105852 KB Output is correct
60 Correct 7182 ms 107988 KB Output is correct