Submission #67427

# Submission time Handle Problem Language Result Execution time Memory
67427 2018-08-14T09:14:37 Z aome Collapse (JOI18_collapse) C++17
100 / 100
5469 ms 79620 KB
#include "collapse.h"

#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

struct Event {
	int T, type, x, y;

	bool operator < (const Event& rhs) const {
		return (T == rhs.T) ? type < rhs.type : T < rhs.T;
	}
};

struct Edge {
	int id, u, v;

	bool operator < (const Edge& rhs) const {
		return u < rhs.u;
	}
};

int n;
int par[N];
int res[N];
int f[2][N];
int pos[N];
bool state[N];
bool iedge[N];
bool inode[N];
vector<Event> vec, buf;
vector< pair<int, int> > dsu[2][500];
map< pair<int, int>, int > label;
vector<Edge> E1, E2;

int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); }

bool join(int u, int v) {
	u = find(u), v = find(v);
	if (inode[u]) swap(u, v); // root prefer important node
	par[u] = v; return u != v; 
}

void solve() {
	vector<int> vnode;
	vector<Edge> vedge;

	int cnt = 0;
	for (auto i : buf) {
		if (i.type < 0) {
			int id = label[{i.x, i.y}];
			if (!iedge[id]) {
				iedge[id] = 1;
				vedge.push_back({id, i.x, i.y});
			}
			if (!inode[i.x]) {
				inode[i.x] = 1, vnode.push_back(i.x);
			}
			if (!inode[i.y]) {
				inode[i.y] = 1, vnode.push_back(i.y);
			}
		}
		else pos[i.type] = cnt++;
	}

	{	
		vector< pair<int, int> > vec1;
		for (auto i : buf) {
			if (i.type >= 0) vec1.push_back({i.x, i.type});
		}
		sort(vec1.begin(), vec1.end());
		for (int i = 0; i < n; ++i) par[i] = i;
		int ptr = 0, cur = 0, cnt = 0;
		for (auto i : vec1) {
			while (cur < i.first) ++cur, ++cnt;
			while (ptr < E1.size() && E1[ptr].u <= i.first) {
				if (!iedge[E1[ptr].id] && state[E1[ptr].id]) {
					cnt -= join(E1[ptr].u, E1[ptr].v);
				}
				++ptr;
			}
			f[0][i.second] = cnt;
			for (auto j : vnode) {
				dsu[0][pos[i.second]].push_back({j, find(j)});
			}
		}
	}

	{	
		vector< pair<int, int> > vec2;
		for (auto i : buf) {
			if (i.type >= 0) vec2.push_back({i.x + 1, i.type});
		}
		sort(vec2.begin(), vec2.end());
		reverse(vec2.begin(), vec2.end());
		for (int i = 0; i < n; ++i) par[i] = i;
		int ptr = 0, cur = 0, cnt = 0;
		for (auto i : vec2) {
			while (cur < n - i.first + 1) ++cur, ++cnt;
			while (ptr < E2.size() && E2[ptr].u >= i.first) {
				if (!iedge[E2[ptr].id] && state[E2[ptr].id]) {
					cnt -= join(E2[ptr].u, E2[ptr].v);
				}
				++ptr;
			}
			f[1][i.second] = cnt;
			for (auto j : vnode) {
				dsu[1][pos[i.second]].push_back({j, find(j)});
			}
		}
	}

	for (auto i : buf) {
		if (i.type < 0) {
			int id = label[{i.x, i.y}];
			state[id] ^= 1; continue;
		}
		int id = i.type;
		for (int j = 0; j < 2; ++j) {
			for (auto k : dsu[j][pos[id]]) par[k.first] = k.second;
			for (auto k : vnode) {
				if (j == 0 && k > i.x || j == 1 && k <= i.x) continue; 
				f[j][id] -= par[k] == k;
			}
			for (auto k : vedge) {
				// k.u < k.v
				if (state[k.id] && (k.v <= i.x || k.u > i.x)) join(k.u, k.v);
			}
			for (auto k : vnode) {
				if (j == 0 && k > i.x || j == 1 && k <= i.x) continue;
				f[j][id] += par[k] == k;
			}
		}
		res[id] = f[0][id] + f[1][id];
	}

	for (int i = 0; i < cnt; ++i) dsu[0][i].clear(), dsu[1][i].clear();
	for (auto i : buf) {
		if (i.type < 0) {
			iedge[label[{i.x, i.y}]] = inode[i.x] = inode[i.y] = 0;
		}
	}
	buf.clear();
}

vector<int> simulateCollapse(
	int _n,
	vector<int> T,
	vector<int> X,
	vector<int> Y,
	vector<int> W,
	vector<int> P
) {
	n = _n;
	int c = T.size();
	int q = W.size();

	for (int i = 0; i < c; ++i) {
		if (X[i] > Y[i]) swap(X[i], Y[i]);
		vec.push_back({i, -(T[i] + 1), X[i], Y[i]});
	}
	for (int i = 0; i < q; ++i) {
		vec.push_back({W[i], i, P[i], 0});
	}
	sort(vec.begin(), vec.end());

	int cnt = 0;
	for (int i = 0; i < c; ++i) {
		if (label[{X[i], Y[i]}]) continue;
		label[{X[i], Y[i]}] = ++cnt; 
		E1.push_back({cnt, Y[i], X[i]});
		E2.push_back({cnt, X[i], Y[i]});
	}

	sort(E1.begin(), E1.end());
	sort(E2.begin(), E2.end());
	reverse(E2.begin(), E2.end());

	for (int i = 0; i < vec.size(); ++i) {
		buf.push_back(vec[i]);
		if (buf.size() == 365) solve();
	}
	if (buf.size()) solve();

	vector<int> vres;
	for (int i = 0; i < q; ++i) vres.push_back(res[i]);
	return vres;
}

Compilation message

collapse.cpp: In function 'void solve()':
collapse.cpp:78:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while (ptr < E1.size() && E1[ptr].u <= i.first) {
           ~~~~^~~~~~~~~~~
collapse.cpp:102:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while (ptr < E2.size() && E2[ptr].u >= i.first) {
           ~~~~^~~~~~~~~~~
collapse.cpp:124:16: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     if (j == 0 && k > i.x || j == 1 && k <= i.x) continue; 
         ~~~~~~~^~~~~~~~~~
collapse.cpp:132:16: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     if (j == 0 && k > i.x || j == 1 && k <= i.x) continue;
         ~~~~~~~^~~~~~~~~~
collapse.cpp: In function 'std::vector<int> simulateCollapse(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
collapse.cpp:181:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < vec.size(); ++i) {
                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1012 KB Output is correct
2 Correct 7 ms 1012 KB Output is correct
3 Correct 8 ms 1012 KB Output is correct
4 Correct 10 ms 1012 KB Output is correct
5 Correct 15 ms 1288 KB Output is correct
6 Correct 59 ms 2168 KB Output is correct
7 Correct 7 ms 2168 KB Output is correct
8 Correct 8 ms 2168 KB Output is correct
9 Correct 65 ms 2596 KB Output is correct
10 Correct 86 ms 2740 KB Output is correct
11 Correct 165 ms 3872 KB Output is correct
12 Correct 85 ms 3872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 7040 KB Output is correct
2 Correct 58 ms 7040 KB Output is correct
3 Correct 260 ms 10984 KB Output is correct
4 Correct 66 ms 10984 KB Output is correct
5 Correct 706 ms 12064 KB Output is correct
6 Correct 231 ms 12064 KB Output is correct
7 Correct 2463 ms 21400 KB Output is correct
8 Correct 1689 ms 21400 KB Output is correct
9 Correct 83 ms 21400 KB Output is correct
10 Correct 97 ms 21400 KB Output is correct
11 Correct 183 ms 21400 KB Output is correct
12 Correct 1598 ms 21400 KB Output is correct
13 Correct 1737 ms 21400 KB Output is correct
14 Correct 2827 ms 21944 KB Output is correct
15 Correct 2205 ms 22096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 22096 KB Output is correct
2 Correct 60 ms 22096 KB Output is correct
3 Correct 68 ms 22096 KB Output is correct
4 Correct 74 ms 22096 KB Output is correct
5 Correct 122 ms 22096 KB Output is correct
6 Correct 330 ms 22096 KB Output is correct
7 Correct 3181 ms 22652 KB Output is correct
8 Correct 4283 ms 27984 KB Output is correct
9 Correct 83 ms 27984 KB Output is correct
10 Correct 158 ms 27984 KB Output is correct
11 Correct 4069 ms 32720 KB Output is correct
12 Correct 5119 ms 35080 KB Output is correct
13 Correct 4179 ms 37592 KB Output is correct
14 Correct 5347 ms 40104 KB Output is correct
15 Correct 4210 ms 42492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1012 KB Output is correct
2 Correct 7 ms 1012 KB Output is correct
3 Correct 8 ms 1012 KB Output is correct
4 Correct 10 ms 1012 KB Output is correct
5 Correct 15 ms 1288 KB Output is correct
6 Correct 59 ms 2168 KB Output is correct
7 Correct 7 ms 2168 KB Output is correct
8 Correct 8 ms 2168 KB Output is correct
9 Correct 65 ms 2596 KB Output is correct
10 Correct 86 ms 2740 KB Output is correct
11 Correct 165 ms 3872 KB Output is correct
12 Correct 85 ms 3872 KB Output is correct
13 Correct 42 ms 7040 KB Output is correct
14 Correct 58 ms 7040 KB Output is correct
15 Correct 260 ms 10984 KB Output is correct
16 Correct 66 ms 10984 KB Output is correct
17 Correct 706 ms 12064 KB Output is correct
18 Correct 231 ms 12064 KB Output is correct
19 Correct 2463 ms 21400 KB Output is correct
20 Correct 1689 ms 21400 KB Output is correct
21 Correct 83 ms 21400 KB Output is correct
22 Correct 97 ms 21400 KB Output is correct
23 Correct 183 ms 21400 KB Output is correct
24 Correct 1598 ms 21400 KB Output is correct
25 Correct 1737 ms 21400 KB Output is correct
26 Correct 2827 ms 21944 KB Output is correct
27 Correct 2205 ms 22096 KB Output is correct
28 Correct 45 ms 22096 KB Output is correct
29 Correct 60 ms 22096 KB Output is correct
30 Correct 68 ms 22096 KB Output is correct
31 Correct 74 ms 22096 KB Output is correct
32 Correct 122 ms 22096 KB Output is correct
33 Correct 330 ms 22096 KB Output is correct
34 Correct 3181 ms 22652 KB Output is correct
35 Correct 4283 ms 27984 KB Output is correct
36 Correct 83 ms 27984 KB Output is correct
37 Correct 158 ms 27984 KB Output is correct
38 Correct 4069 ms 32720 KB Output is correct
39 Correct 5119 ms 35080 KB Output is correct
40 Correct 4179 ms 37592 KB Output is correct
41 Correct 5347 ms 40104 KB Output is correct
42 Correct 4210 ms 42492 KB Output is correct
43 Correct 1680 ms 42492 KB Output is correct
44 Correct 4075 ms 45636 KB Output is correct
45 Correct 1941 ms 45636 KB Output is correct
46 Correct 4783 ms 50112 KB Output is correct
47 Correct 107 ms 50112 KB Output is correct
48 Correct 135 ms 50112 KB Output is correct
49 Correct 195 ms 50112 KB Output is correct
50 Correct 671 ms 50112 KB Output is correct
51 Correct 1710 ms 51752 KB Output is correct
52 Correct 3160 ms 56248 KB Output is correct
53 Correct 2410 ms 57252 KB Output is correct
54 Correct 3796 ms 62016 KB Output is correct
55 Correct 3117 ms 64064 KB Output is correct
56 Correct 3516 ms 67616 KB Output is correct
57 Correct 4085 ms 71140 KB Output is correct
58 Correct 4912 ms 73732 KB Output is correct
59 Correct 3888 ms 76292 KB Output is correct
60 Correct 5469 ms 79620 KB Output is correct