Submission #794744

# Submission time Handle Problem Language Result Execution time Memory
794744 2023-07-26T21:21:53 Z IvanJ Werewolf (IOI18_werewolf) C++17
100 / 100
1182 ms 198500 KB
#include "werewolf.h"
#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5;

int n, m, q;
int ans[maxn];
vector<int> graph[maxn];
vector<pair<ii, int>> qL[maxn], qR[maxn];

struct Tree {
	int sz, dt;
	vector<vector<int>> adj, anc;
	vector<int> par, pos, l, r;
	vector<int> perm, vis;
	
	void init(int _sz) {
		sz = _sz;
		for(int i = 0;i < sz;i++) 
			adj.pb({}), par.pb(i), pos.pb(-1), vis.pb(0);
	}
	
	int find(int x) {return (x == par[x]) ? x : par[x] = find(par[x]);}
	
	void add(int v) {
		int x = sz++;
		adj.pb({}), par.pb(x);
		pos[v] = x, vis[v] = 1;
		vector<int> a;
		set<int> s;
		s.insert(v);
		a.pb(v);
		for(int y : graph[v]) 
			if(vis[y]) s.insert(find(y)), a.pb(y);
		for(int i : a) par[find(i)] = x;
		adj[x] = vector<int>(all(s));
	}
	
	void dfs(int x) {
		l[x] = dt;
		for(int i = 1;i < 20;i++) 
			anc[x][i] = anc[anc[x][i - 1]][i - 1];
		for(int y : adj[x]) anc[y][0] = x, dfs(y);
		r[x] = dt - 1;
		if(x < n) perm.pb(x), dt++;
	}
	
	void build() {
		dt = 0;
		adj.pb({});
		for(int i = 0;i < sz;i++) 
			if(par[i] == i) adj[sz].pb(i);
		sz++;
		vector<int> V(20, sz - 1);
		for(int i = 0;i < sz;i++) 
			l.pb(0), r.pb(0), anc.pb(V);
		dfs(sz - 1);
	}
	
	ii get_range(int x, int y) {
		for(int i = 19;i >= 0;i--) {
			if(anc[x][i] <= pos[y]) 
				x = anc[x][i];
		}
		return {l[x], r[x]};
	}
} H, W;

struct bit {
	int sz;
	vector<int> fwt;
	void init(int _sz) {
		sz = _sz + 1;
		for(int i = 0;i < sz;i++) fwt.pb(0);
	}
	
	void update(int x) {x++;for(;x < sz;x += (x & -x)) fwt[x]++;}
	
	int query(int x) {
		x++;
		int ret = 0;
		for(;x > 0;x -= (x & -x)) ret += fwt[x];
		return ret;
	}
	
	int get(int lo, int hi) {return query(hi) - query(lo - 1);}
} DS;

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
	n = N, m = (int)X.size(), q = (int)S.size();
	for(int i = 0;i < m;i++) 
		graph[X[i]].pb(Y[i]), graph[Y[i]].pb(X[i]);

	H.init(n), W.init(n);
	for(int i = 0;i < n;i++) 
		W.add(i), H.add(n - i - 1);
	
	H.build(), W.build();

	for(int i = 0;i < q;i++) {
		ii r1 = H.get_range(S[i], L[i]);
		ii r2 = W.get_range(E[i], R[i]);
		qL[r2.x].pb({r1, i}), qR[r2.y].pb({r1, i});
	}
	
	vector<int> p;
	vector<int> pos(n, 0);
	for(int i = 0;i < n;i++) pos[H.perm[i]] = i;
	for(int i = 0;i < n;i++) p.pb(pos[W.perm[i]]);

	DS.init(n);
	for(int i = 0;i < n;i++) {
		for(auto r : qL[i]) {
			int lo = r.x.x, hi = r.x.y;
			int cnt = DS.get(lo, hi);
			ans[r.y] -= cnt;
		}
		DS.update(p[i]);
		for(auto r : qR[i]) {
			int lo = r.x.x, hi = r.x.y;
			int cnt = DS.get(lo, hi);
			ans[r.y] += cnt;
		}
	}
	
	vector<int> ret;
	for(int i = 0;i < q;i++) ret.pb(ans[i] > 0);
	return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14496 KB Output is correct
2 Correct 8 ms 14396 KB Output is correct
3 Correct 7 ms 14400 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 7 ms 14492 KB Output is correct
6 Correct 7 ms 14404 KB Output is correct
7 Correct 7 ms 14424 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 9 ms 14400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14496 KB Output is correct
2 Correct 8 ms 14396 KB Output is correct
3 Correct 7 ms 14400 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 7 ms 14492 KB Output is correct
6 Correct 7 ms 14404 KB Output is correct
7 Correct 7 ms 14424 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 9 ms 14400 KB Output is correct
10 Correct 15 ms 17108 KB Output is correct
11 Correct 14 ms 17112 KB Output is correct
12 Correct 14 ms 17108 KB Output is correct
13 Correct 14 ms 17096 KB Output is correct
14 Correct 14 ms 17108 KB Output is correct
15 Correct 15 ms 17132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 988 ms 182992 KB Output is correct
2 Correct 932 ms 184436 KB Output is correct
3 Correct 796 ms 182820 KB Output is correct
4 Correct 825 ms 182220 KB Output is correct
5 Correct 792 ms 182176 KB Output is correct
6 Correct 905 ms 182560 KB Output is correct
7 Correct 1020 ms 181616 KB Output is correct
8 Correct 823 ms 184424 KB Output is correct
9 Correct 688 ms 183068 KB Output is correct
10 Correct 621 ms 181392 KB Output is correct
11 Correct 663 ms 182232 KB Output is correct
12 Correct 734 ms 181876 KB Output is correct
13 Correct 1043 ms 184420 KB Output is correct
14 Correct 1060 ms 184244 KB Output is correct
15 Correct 1046 ms 184296 KB Output is correct
16 Correct 1025 ms 184536 KB Output is correct
17 Correct 937 ms 181748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14496 KB Output is correct
2 Correct 8 ms 14396 KB Output is correct
3 Correct 7 ms 14400 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 7 ms 14492 KB Output is correct
6 Correct 7 ms 14404 KB Output is correct
7 Correct 7 ms 14424 KB Output is correct
8 Correct 9 ms 14420 KB Output is correct
9 Correct 9 ms 14400 KB Output is correct
10 Correct 15 ms 17108 KB Output is correct
11 Correct 14 ms 17112 KB Output is correct
12 Correct 14 ms 17108 KB Output is correct
13 Correct 14 ms 17096 KB Output is correct
14 Correct 14 ms 17108 KB Output is correct
15 Correct 15 ms 17132 KB Output is correct
16 Correct 988 ms 182992 KB Output is correct
17 Correct 932 ms 184436 KB Output is correct
18 Correct 796 ms 182820 KB Output is correct
19 Correct 825 ms 182220 KB Output is correct
20 Correct 792 ms 182176 KB Output is correct
21 Correct 905 ms 182560 KB Output is correct
22 Correct 1020 ms 181616 KB Output is correct
23 Correct 823 ms 184424 KB Output is correct
24 Correct 688 ms 183068 KB Output is correct
25 Correct 621 ms 181392 KB Output is correct
26 Correct 663 ms 182232 KB Output is correct
27 Correct 734 ms 181876 KB Output is correct
28 Correct 1043 ms 184420 KB Output is correct
29 Correct 1060 ms 184244 KB Output is correct
30 Correct 1046 ms 184296 KB Output is correct
31 Correct 1025 ms 184536 KB Output is correct
32 Correct 937 ms 181748 KB Output is correct
33 Correct 1120 ms 191940 KB Output is correct
34 Correct 230 ms 53884 KB Output is correct
35 Correct 1180 ms 193208 KB Output is correct
36 Correct 1054 ms 191580 KB Output is correct
37 Correct 1086 ms 192756 KB Output is correct
38 Correct 1033 ms 191868 KB Output is correct
39 Correct 1067 ms 195908 KB Output is correct
40 Correct 1159 ms 197916 KB Output is correct
41 Correct 842 ms 191148 KB Output is correct
42 Correct 794 ms 190200 KB Output is correct
43 Correct 993 ms 195884 KB Output is correct
44 Correct 945 ms 191948 KB Output is correct
45 Correct 862 ms 196448 KB Output is correct
46 Correct 984 ms 196204 KB Output is correct
47 Correct 1099 ms 192892 KB Output is correct
48 Correct 1182 ms 192812 KB Output is correct
49 Correct 1025 ms 192880 KB Output is correct
50 Correct 1004 ms 192772 KB Output is correct
51 Correct 1151 ms 198500 KB Output is correct
52 Correct 1004 ms 198424 KB Output is correct