Submission #829181

# Submission time Handle Problem Language Result Execution time Memory
829181 2023-08-18T06:06:52 Z tranxuanbach Werewolf (IOI18_werewolf) C++17
100 / 100
853 ms 169740 KB
#include "werewolf.h"

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

#define isz(a) ((signed)a.size())

constexpr int N = 2e5 + 5, M = 4e5 + 5, Q = 2e5 + 5;
constexpr int NODE = 4e5 + 5;
constexpr int inf = 1e9 + 7;

struct merge_sort_tree{
	vector <int> seg[1 << 20];

	void build(int id, int l, int r, int a[]){
		if (l == r){
			seg[id].emplace_back(a[l]);
			return;
		}
		int mid = (l + r) >> 1;
		build(id * 2, l, mid, a);
		build(id * 2 + 1, mid + 1, r, a);
		merge(seg[id * 2].begin(), seg[id * 2].end(), seg[id * 2 + 1].begin(), seg[id * 2 + 1].end(), back_inserter(seg[id]));
	}

	int query(int id, int l, int r, int u, int v, int val){
		if (v < l or r < u){
			return inf;
		}
		if (u <= l and r <= v){
			auto itr = lower_bound(seg[id].begin(), seg[id].end(), val);
			return itr == seg[id].end() ? inf : *itr;
		}
		int mid = (l + r) >> 1;
		return min(query(id * 2, l, mid, u, v, val), query(id * 2 + 1, mid + 1, r, u, v, val));
	}
} seg;

struct Disjoint_set{
	int par[N];

	void reset(int sz = N){
		fill(par, par + sz, -1);
	}

	Disjoint_set(){
		reset();
	}

	int root(int x){
		return par[x] < 0 ? x : (par[x] = root(par[x]));
	}

	bool share(int x, int y){
		return root(x) == root(y);
	}

	bool merge(int x, int y){
		if ((x = root(x)) == (y = root(y))){
			return false;
		}
		if (par[x] > par[y]){
			swap(x, y);
		}
		par[x] += par[y];
		par[y] = x;
		return true;
	}
};

struct Query{
	int s, t, l, r;
};

int n, m, q;
vector <int> adj[N];
Query query[Q];

vector <int> adj_werewolf[NODE], adj_human[NODE];
int root_werewolf, root_human;
int node_werewolf[Q], node_human[Q];

int ctrtour, tour[NODE], tin[NODE], tout[NODE];

void dfs_tour(vector <int> adj[], int u){
	tour[ctrtour] = u;
	tin[u] = ctrtour;
	ctrtour++;
	for (auto v: adj[u]){
		dfs_tour(adj, v);
	}
	tout[u] = ctrtour;
}

pair <int, int> tour_range_werewolf[Q], tour_range_human[Q];
int pos_human[N], pos_werewolf[N];
int pos[NODE];
int ans[Q];

vector <int> check_validity(int _n, vector <int> _u, vector <int> _v, vector <int> _s, vector <int> _e, vector <int> _l, vector <int> _r){
	n = _n;
	m = isz(_u);
	for (int i = 0; i < m; i++){
		int u = _u[i], v = _v[i];
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}
	q = isz(_s);
	for (int i = 0; i < q; i++){
		int s = _s[i], t = _e[i], l = _l[i], r = _r[i];
		query[i] = Query{s, t, l, r};
	}

	{ // Human
		vector <pair <int, int>> query_human;
		for (int i = 0; i < q; i++){
			query_human.emplace_back(query[i].l, i);
		}
		sort(query_human.begin(), query_human.end());

		Disjoint_set dsu;
		vector <int> current_node(n);
		iota(current_node.begin(), current_node.end(), 0);
		root_human = n;

		for (int u = n - 1; u >= 0; u--){
			for (auto v: adj[u]){
				if (v < u){
					continue;
				}
				if (not dsu.share(u, v)){
					int x = current_node[dsu.root(u)], y = current_node[dsu.root(v)];
					dsu.merge(u, v);
					adj_human[root_human].emplace_back(x);
					adj_human[root_human].emplace_back(y);
					current_node[dsu.root(u)] = root_human;
					root_human++;
				}
			}
			while (not query_human.empty() and query_human.back().first == u){
				int i = query_human.back().second;
				node_human[i] = current_node[dsu.root(query[i].s)];
				query_human.pop_back();
			}
		}
		ctrtour = 0;
		dfs_tour(adj_human, root_human - 1);
		for (int i = 0; i < q; i++){
			tour_range_human[i] = pair{tin[node_human[i]], tout[node_human[i]]};
		}
		for (int u = 0; u < n; u++){
			pos_human[u] = tin[u];
		}
	}
	{ // Werewolf
		vector <pair <int, int>> query_werewolf;
		for (int i = 0; i < q; i++){
			query_werewolf.emplace_back(query[i].r, i);
		}
		sort(query_werewolf.begin(), query_werewolf.end(), greater <>());

		Disjoint_set dsu;
		vector <int> current_node(n);
		iota(current_node.begin(), current_node.end(), 0);
		root_werewolf = n;

		for (int u = 0; u < n; u++){
			for (auto v: adj[u]){
				if (v > u){
					continue;
				}
				if (not dsu.share(u, v)){
					int x = current_node[dsu.root(u)], y = current_node[dsu.root(v)];
					dsu.merge(u, v);
					adj_werewolf[root_werewolf].emplace_back(x);
					adj_werewolf[root_werewolf].emplace_back(y);
					current_node[dsu.root(u)] = root_werewolf;
					root_werewolf++;
				}
			}
			while (not query_werewolf.empty() and query_werewolf.back().first == u){
				int i = query_werewolf.back().second;
				node_werewolf[i] = current_node[dsu.root(query[i].t)];
				query_werewolf.pop_back();
			}
		}
		ctrtour = 0;
		dfs_tour(adj_werewolf, root_werewolf - 1);
		for (int i = 0; i < q; i++){
			tour_range_werewolf[i] = pair{tin[node_werewolf[i]], tout[node_werewolf[i]]};
		}
		for (int u = 0; u < n; u++){
			pos_werewolf[u] = tin[u];
		}
	}

	fill(pos, pos + root_human, inf);
	for (int u = 0; u < n; u++){
		pos[pos_human[u]] = pos_werewolf[u];
	}
	seg.build(1, 0, root_human - 1, pos);
	for (int i = 0; i < q; i++){
		if (seg.query(1, 0, root_human - 1, tour_range_human[i].first, tour_range_human[i].second - 1, tour_range_werewolf[i].first) < tour_range_werewolf[i].second){
			ans[i] = 1;
		}
		else{
			ans[i] = 0;
		}
	}
	return vector <int>(ans, ans + q);
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 49236 KB Output is correct
2 Correct 20 ms 49216 KB Output is correct
3 Correct 21 ms 49188 KB Output is correct
4 Correct 21 ms 49236 KB Output is correct
5 Correct 21 ms 49236 KB Output is correct
6 Correct 24 ms 49236 KB Output is correct
7 Correct 21 ms 49248 KB Output is correct
8 Correct 21 ms 49236 KB Output is correct
9 Correct 27 ms 49236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 49236 KB Output is correct
2 Correct 20 ms 49216 KB Output is correct
3 Correct 21 ms 49188 KB Output is correct
4 Correct 21 ms 49236 KB Output is correct
5 Correct 21 ms 49236 KB Output is correct
6 Correct 24 ms 49236 KB Output is correct
7 Correct 21 ms 49248 KB Output is correct
8 Correct 21 ms 49236 KB Output is correct
9 Correct 27 ms 49236 KB Output is correct
10 Correct 28 ms 50688 KB Output is correct
11 Correct 27 ms 50708 KB Output is correct
12 Correct 35 ms 50624 KB Output is correct
13 Correct 27 ms 50712 KB Output is correct
14 Correct 28 ms 50784 KB Output is correct
15 Correct 26 ms 50704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 618 ms 150828 KB Output is correct
2 Correct 620 ms 163888 KB Output is correct
3 Correct 651 ms 160876 KB Output is correct
4 Correct 569 ms 159568 KB Output is correct
5 Correct 621 ms 159380 KB Output is correct
6 Correct 582 ms 159240 KB Output is correct
7 Correct 546 ms 159164 KB Output is correct
8 Correct 725 ms 163928 KB Output is correct
9 Correct 497 ms 160976 KB Output is correct
10 Correct 541 ms 159476 KB Output is correct
11 Correct 532 ms 159456 KB Output is correct
12 Correct 475 ms 159340 KB Output is correct
13 Correct 612 ms 163872 KB Output is correct
14 Correct 550 ms 163920 KB Output is correct
15 Correct 561 ms 163904 KB Output is correct
16 Correct 563 ms 163888 KB Output is correct
17 Correct 537 ms 159384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 49236 KB Output is correct
2 Correct 20 ms 49216 KB Output is correct
3 Correct 21 ms 49188 KB Output is correct
4 Correct 21 ms 49236 KB Output is correct
5 Correct 21 ms 49236 KB Output is correct
6 Correct 24 ms 49236 KB Output is correct
7 Correct 21 ms 49248 KB Output is correct
8 Correct 21 ms 49236 KB Output is correct
9 Correct 27 ms 49236 KB Output is correct
10 Correct 28 ms 50688 KB Output is correct
11 Correct 27 ms 50708 KB Output is correct
12 Correct 35 ms 50624 KB Output is correct
13 Correct 27 ms 50712 KB Output is correct
14 Correct 28 ms 50784 KB Output is correct
15 Correct 26 ms 50704 KB Output is correct
16 Correct 618 ms 150828 KB Output is correct
17 Correct 620 ms 163888 KB Output is correct
18 Correct 651 ms 160876 KB Output is correct
19 Correct 569 ms 159568 KB Output is correct
20 Correct 621 ms 159380 KB Output is correct
21 Correct 582 ms 159240 KB Output is correct
22 Correct 546 ms 159164 KB Output is correct
23 Correct 725 ms 163928 KB Output is correct
24 Correct 497 ms 160976 KB Output is correct
25 Correct 541 ms 159476 KB Output is correct
26 Correct 532 ms 159456 KB Output is correct
27 Correct 475 ms 159340 KB Output is correct
28 Correct 612 ms 163872 KB Output is correct
29 Correct 550 ms 163920 KB Output is correct
30 Correct 561 ms 163904 KB Output is correct
31 Correct 563 ms 163888 KB Output is correct
32 Correct 537 ms 159384 KB Output is correct
33 Correct 699 ms 160668 KB Output is correct
34 Correct 237 ms 91120 KB Output is correct
35 Correct 769 ms 164068 KB Output is correct
36 Correct 662 ms 160004 KB Output is correct
37 Correct 777 ms 163284 KB Output is correct
38 Correct 692 ms 160760 KB Output is correct
39 Correct 553 ms 168896 KB Output is correct
40 Correct 617 ms 168928 KB Output is correct
41 Correct 682 ms 162424 KB Output is correct
42 Correct 544 ms 160112 KB Output is correct
43 Correct 853 ms 167580 KB Output is correct
44 Correct 722 ms 163144 KB Output is correct
45 Correct 621 ms 169308 KB Output is correct
46 Correct 768 ms 168976 KB Output is correct
47 Correct 545 ms 164216 KB Output is correct
48 Correct 548 ms 163952 KB Output is correct
49 Correct 572 ms 164056 KB Output is correct
50 Correct 531 ms 163868 KB Output is correct
51 Correct 604 ms 169740 KB Output is correct
52 Correct 557 ms 169704 KB Output is correct