답안 #592880

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
592880 2022-07-09T17:29:21 Z Temmie 늑대인간 (IOI18_werewolf) C++17
100 / 100
1263 ms 120488 KB
#include "werewolf.h"
#include <bits/stdc++.h>

struct Dsu {
	
	std::vector <int> p;
	std::vector <std::vector <int>> g;
	std::vector <int> tour;
	std::vector <int> l, r;
	
	inline Dsu(int s) {
		p.resize(s);
		std::iota(p.begin(), p.end(), 0);
		g.resize(s);
		l.resize(s);
		r.resize(s);
	}
	
	inline int find(int v) {
		return v == p[v] ? v : (p[v] = find(p[v]));
	}
	
	inline void unite(int u, int v) {
		if ((u = find(u)) != (v = find(v))) {
			g[u].push_back(v);
			p[v] = u;
		}
	}
	
	void make(int node) {
		l[node] = tour.size();
		tour.push_back(node);
		for (int x : g[node]) {
			make(x);
		}
		r[node] = tour.size();
	}
	
};

struct Rag {
	
	int size;
	std::vector <std::vector <int>> v;
	
	Rag(const std::vector <int>& a) {
		size = 1;
		while (size < (int) a.size()) {
			size <<= 1;
		}
		v.resize(size << 1);
		make(a, 0, 0, size = a.size());
	}
	
	void make(const std::vector <int>& a, int now, int l, int r) {
		if (!(r - l - 1)) {
			v[now].push_back(a[l]);
			return;
		}
		int mid = (l + r) >> 1;
		make(a, now * 2 + 1, l, mid);
		make(a, now * 2 + 2, mid, r);
		for (int lp = 0, rp = 0;
		lp < (int) v[now * 2 + 1].size() || rp < (int) v[now * 2 + 2].size(); ) {
			if (lp == (int) v[now * 2 + 1].size()) v[now].push_back(v[now * 2 + 2][rp++]);
			else if (rp == (int) v[now * 2 + 2].size()) v[now].push_back(v[now * 2 + 1][lp++]);
			else if (v[now * 2 + 1][lp] < v[now * 2 + 2][rp]) v[now].push_back(v[now * 2 + 1][lp++]);
			else v[now].push_back(v[now * 2 + 2][rp++]);
		}
	}
	
	int query(int tl, int tr, int le, int ri) {
		return query(tl, tr, le, ri, 0, 0, size);
	}
	
	int query(int tl, int tr, int le, int ri, int now, int l, int r) {
		if (l >= tr || r <= tl) {
			return 0;
		}
		if (l >= tl && r <= tr) {
			return
			std::lower_bound(v[now].begin(), v[now].end(), ri) -
			std::lower_bound(v[now].begin(), v[now].end(), le);
		}
		int mid = (l + r) >> 1;
		return query(tl, tr, le, ri, now * 2 + 1, l, mid) + query(tl, tr, le, ri, now * 2 + 2, mid, r);
	}
	
};

std::vector <int> check_validity(int n, std::vector <int> _u, std::vector <int> _v,
std::vector <int> s, std::vector <int> e, std::vector <int> l, std::vector <int> r) {
	int m = _u.size();
	int q = s.size();
	std::vector <std::vector <int>> g(n), ql(n), qr(n);
	std::vector <int> pl(q), pr(q), tord(n), initial(n), ans(q);
	Dsu dsul(n), dsur(n);
	for (int i = 0; i < m; i++) {
		g[_u[i]].push_back(_v[i]);
		g[_v[i]].push_back(_u[i]);
	}
	for (int i = 0; i < q; i++) {
		ql[l[i]].push_back(i);
		qr[r[i]].push_back(i);
	}
	for (int i = 0; i < n; i++) {
		for (int x : g[n - i - 1]) {
			if (x > n - i - 1) {
				dsul.unite(n - i - 1, x);
			}
		}
		for (int x : ql[n - i - 1]) {
			pl[x] = dsul.find(s[x]);
		}
		for (int x : g[i]) {
			if (x < i) {
				dsur.unite(i, x);
			}
		}
		for (int x : qr[i]) {
			pr[x] = dsur.find(e[x]);
		}
	}
	dsul.make(dsul.find(0));
	dsur.make(dsur.find(0));
	for (int i = 0; i < n; i++) {
		tord[dsul.tour[i]] = i;
	}
	for (int i = 0; i < n; i++) {
		initial[i] = tord[dsur.tour[i]];
	}
	Rag rag(initial);
	for (int i = 0; i < q; i++) {
		ans[i] = !!rag.query(dsur.l[pr[i]], dsur.r[pr[i]], dsul.l[pl[i]], dsul.r[pl[i]]);
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 8 ms 1876 KB Output is correct
11 Correct 8 ms 1876 KB Output is correct
12 Correct 6 ms 1844 KB Output is correct
13 Correct 7 ms 2004 KB Output is correct
14 Correct 7 ms 2004 KB Output is correct
15 Correct 8 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 696 ms 109688 KB Output is correct
2 Correct 884 ms 114832 KB Output is correct
3 Correct 961 ms 113436 KB Output is correct
4 Correct 906 ms 112928 KB Output is correct
5 Correct 919 ms 112880 KB Output is correct
6 Correct 781 ms 112864 KB Output is correct
7 Correct 641 ms 110460 KB Output is correct
8 Correct 824 ms 114800 KB Output is correct
9 Correct 754 ms 112324 KB Output is correct
10 Correct 519 ms 111556 KB Output is correct
11 Correct 582 ms 111680 KB Output is correct
12 Correct 695 ms 112556 KB Output is correct
13 Correct 804 ms 119600 KB Output is correct
14 Correct 730 ms 119676 KB Output is correct
15 Correct 749 ms 119700 KB Output is correct
16 Correct 737 ms 119768 KB Output is correct
17 Correct 606 ms 110592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 8 ms 1876 KB Output is correct
11 Correct 8 ms 1876 KB Output is correct
12 Correct 6 ms 1844 KB Output is correct
13 Correct 7 ms 2004 KB Output is correct
14 Correct 7 ms 2004 KB Output is correct
15 Correct 8 ms 2004 KB Output is correct
16 Correct 696 ms 109688 KB Output is correct
17 Correct 884 ms 114832 KB Output is correct
18 Correct 961 ms 113436 KB Output is correct
19 Correct 906 ms 112928 KB Output is correct
20 Correct 919 ms 112880 KB Output is correct
21 Correct 781 ms 112864 KB Output is correct
22 Correct 641 ms 110460 KB Output is correct
23 Correct 824 ms 114800 KB Output is correct
24 Correct 754 ms 112324 KB Output is correct
25 Correct 519 ms 111556 KB Output is correct
26 Correct 582 ms 111680 KB Output is correct
27 Correct 695 ms 112556 KB Output is correct
28 Correct 804 ms 119600 KB Output is correct
29 Correct 730 ms 119676 KB Output is correct
30 Correct 749 ms 119700 KB Output is correct
31 Correct 737 ms 119768 KB Output is correct
32 Correct 606 ms 110592 KB Output is correct
33 Correct 883 ms 112960 KB Output is correct
34 Correct 298 ms 35412 KB Output is correct
35 Correct 1139 ms 114884 KB Output is correct
36 Correct 859 ms 113244 KB Output is correct
37 Correct 1018 ms 114240 KB Output is correct
38 Correct 918 ms 113692 KB Output is correct
39 Correct 937 ms 119772 KB Output is correct
40 Correct 767 ms 120488 KB Output is correct
41 Correct 912 ms 113800 KB Output is correct
42 Correct 542 ms 112036 KB Output is correct
43 Correct 1263 ms 119460 KB Output is correct
44 Correct 1024 ms 114288 KB Output is correct
45 Correct 745 ms 118904 KB Output is correct
46 Correct 829 ms 118648 KB Output is correct
47 Correct 724 ms 119844 KB Output is correct
48 Correct 752 ms 119668 KB Output is correct
49 Correct 702 ms 119884 KB Output is correct
50 Correct 696 ms 119684 KB Output is correct
51 Correct 733 ms 118424 KB Output is correct
52 Correct 663 ms 118424 KB Output is correct