답안 #1011333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1011333 2024-06-30T10:57:07 Z Muaath_5 늑대인간 (IOI18_werewolf) C++17
100 / 100
847 ms 236968 KB
#include "werewolf.h"
using namespace std;
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define pii pair<int, int>
using namespace std;

const int N = 1<<19, INF = 1e9, LG = 20;

class reachability {
public:
	int sz;
	int dsu[N], par[N];
	vector<int> adj[N];
	reachability(){}
	reachability(int n) {
		sz = n;
		for (int i = 1; i <= n; i++)
			dsu[i] = par[i] = i;
	}
	
	int root(int x) {
		return x == dsu[x] ? x : dsu[x] = root(dsu[x]);
	}
	bool connect(int u, int v) {
		u = root(u), v = root(v);
		if (u == v) return false;
		sz++;
		dsu[u] = dsu[v] = dsu[sz] = sz;
		par[u] = par[v] = par[sz] = sz;
		adj[sz].push_back(u);
		adj[sz].push_back(v); 
		return true;
	}

	
	int jmp[N][LG];
	int dt[N], ft[N];
	int _time;
	int block[N];
    // Maintain minimum/maximum in every subtree
    int mn[N], mx[N];
	void dfsTree() {
        fill(mx, mx+sz+1, -INF);
        fill(mn, mn+sz+1, +INF);
		_time = 1;
		dfs(sz);
	}
	void dfs(int node) {
		dt[node] = _time++;
        if (adj[node].size() == 0)
            mn[node] = mx[node] = node;
		for (int ch : adj[node]) {
			dfs(ch);
            chmin(mn[node], mn[ch]);
            chmax(mx[node], mx[ch]);
        }
		ft[node] = _time-1;
        
	}
    void buildSt() {
        for (int i = 1; i <= sz; i++)
			jmp[i][0] = par[i];
		for (int j = 1; j < LG; j++) {
			for (int i = 1; i <= sz; i++) {
				jmp[i][j] = jmp[jmp[i][j-1]][j-1];
			}
		}
    }
	
	void cutLastEdge() {
		block[sz] = 1;
		sz--;
	}
	
    int findCompLeqMx(int u, int lim) {
        for (int i = LG-1; i >= 0; i--) {
			if (mx[jmp[u][i]] <= lim) {
				u = jmp[u][i];
			}
		}
        return u;
    }

    int findCompGeqMn(int u, int lim) {
        for (int i = LG-1; i >= 0; i--) {
			if (mn[jmp[u][i]] >= lim) {
				u = jmp[u][i];
			}
		}
        return u;
    }

	int findComp(int u) {
		for (int i = LG-1; i >= 0; i--) {
			if (!block[jmp[u][i]]) {
				u = jmp[u][i];
			}
		}
		return u;
	}
};


int tree[4*N];
void update(int idx, int l=0, int r=N-1, int node=1) {
	if (l == r) {
		tree[node]++;
		return;
	}
	const int mid = (l+r)/2;
	if (idx <= mid)
		update(idx, l, mid, node*2);
	else
		update(idx, mid+1, r, node*2+1);
	tree[node] = tree[node*2] + tree[node*2+1];
}

ll query(int ql, int qr, int l=0, int r=N-1, int node=1) {
	if (ql <= l && r <= qr) return tree[node];
	if (l > qr || r < ql) return 0;
	const int mid = (l+r)/2;
	return query(ql, qr, l, mid, node*2) + query(ql, qr, mid+1, r, node*2+1);
}

vector<int> adj[N];

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R)
{
    const int M = X.size(), Q = S.size();
    for (int i = 0; i < M; i++)
        X[i]++, Y[i]++;
    for (int i = 0; i < Q; i++)
        S[i]++, E[i]++, L[i]++, R[i]++;

    for (int i = 0; i < M; i++)
        adj[X[i]].push_back(Y[i]),
        adj[Y[i]].push_back(X[i]);

    reachability human(N), werewolf(N);    
    for (int i = 1; i <= N; i++)
        for (int ch : adj[i])
            if (ch <= i)
                werewolf.connect(i, ch);

    for (int i = N; i >= 1; i--)
        for (int ch : adj[i])
            if (ch >= i)
                human.connect(i, ch);

    human.dfsTree();human.buildSt();
    werewolf.dfsTree();werewolf.buildSt();
    
    // [type, yl, yr, idx]
	const int MX = 1e6;
    vector<array<int, 4>> evts[MX];
	for (int i = 1; i <= N; i++)
        evts[human.dt[i]].push_back({0, werewolf.dt[i], 0, 0});
    for (int i = 0; i < Q; i++) {
        int u = human.findCompGeqMn(S[i], L[i]); // [L...N]
        int v = werewolf.findCompLeqMx(E[i], R[i]); // [1...R]
        evts[human.dt[u]-1].push_back({-1, werewolf.dt[v], werewolf.ft[v], i});
        evts[human.ft[u]].push_back({1, werewolf.dt[v], werewolf.ft[v], i});
    }


    vector<int> sol(Q);
    for (int i = 0; i < MX; i++) {
		for (auto [type,a,b,idx] : evts[i]) {
			if (type == 0) {
				update(a);
			} else {
				//cerr << i << ' ' << a << ' ' << b << endl;
				sol[idx] += type * query(a, b);
			}
		}
	}
    for (int i = 0; i < Q; i++)
		sol[i] = !!sol[i];
    return sol;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 171516 KB Output is correct
2 Correct 81 ms 171600 KB Output is correct
3 Correct 83 ms 171712 KB Output is correct
4 Correct 84 ms 171604 KB Output is correct
5 Correct 83 ms 171580 KB Output is correct
6 Correct 87 ms 171600 KB Output is correct
7 Correct 82 ms 171600 KB Output is correct
8 Correct 80 ms 171612 KB Output is correct
9 Correct 79 ms 171628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 171516 KB Output is correct
2 Correct 81 ms 171600 KB Output is correct
3 Correct 83 ms 171712 KB Output is correct
4 Correct 84 ms 171604 KB Output is correct
5 Correct 83 ms 171580 KB Output is correct
6 Correct 87 ms 171600 KB Output is correct
7 Correct 82 ms 171600 KB Output is correct
8 Correct 80 ms 171612 KB Output is correct
9 Correct 79 ms 171628 KB Output is correct
10 Correct 91 ms 172372 KB Output is correct
11 Correct 80 ms 172372 KB Output is correct
12 Correct 89 ms 172368 KB Output is correct
13 Correct 89 ms 172660 KB Output is correct
14 Correct 91 ms 172628 KB Output is correct
15 Correct 86 ms 172628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 673 ms 228264 KB Output is correct
2 Correct 619 ms 232276 KB Output is correct
3 Correct 635 ms 228676 KB Output is correct
4 Correct 623 ms 226940 KB Output is correct
5 Correct 679 ms 226992 KB Output is correct
6 Correct 659 ms 228432 KB Output is correct
7 Correct 580 ms 226940 KB Output is correct
8 Correct 667 ms 232120 KB Output is correct
9 Correct 565 ms 227600 KB Output is correct
10 Correct 455 ms 226700 KB Output is correct
11 Correct 501 ms 226168 KB Output is correct
12 Correct 523 ms 225648 KB Output is correct
13 Correct 738 ms 230816 KB Output is correct
14 Correct 733 ms 231080 KB Output is correct
15 Correct 749 ms 230904 KB Output is correct
16 Correct 719 ms 230960 KB Output is correct
17 Correct 734 ms 226872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 171516 KB Output is correct
2 Correct 81 ms 171600 KB Output is correct
3 Correct 83 ms 171712 KB Output is correct
4 Correct 84 ms 171604 KB Output is correct
5 Correct 83 ms 171580 KB Output is correct
6 Correct 87 ms 171600 KB Output is correct
7 Correct 82 ms 171600 KB Output is correct
8 Correct 80 ms 171612 KB Output is correct
9 Correct 79 ms 171628 KB Output is correct
10 Correct 91 ms 172372 KB Output is correct
11 Correct 80 ms 172372 KB Output is correct
12 Correct 89 ms 172368 KB Output is correct
13 Correct 89 ms 172660 KB Output is correct
14 Correct 91 ms 172628 KB Output is correct
15 Correct 86 ms 172628 KB Output is correct
16 Correct 673 ms 228264 KB Output is correct
17 Correct 619 ms 232276 KB Output is correct
18 Correct 635 ms 228676 KB Output is correct
19 Correct 623 ms 226940 KB Output is correct
20 Correct 679 ms 226992 KB Output is correct
21 Correct 659 ms 228432 KB Output is correct
22 Correct 580 ms 226940 KB Output is correct
23 Correct 667 ms 232120 KB Output is correct
24 Correct 565 ms 227600 KB Output is correct
25 Correct 455 ms 226700 KB Output is correct
26 Correct 501 ms 226168 KB Output is correct
27 Correct 523 ms 225648 KB Output is correct
28 Correct 738 ms 230816 KB Output is correct
29 Correct 733 ms 231080 KB Output is correct
30 Correct 749 ms 230904 KB Output is correct
31 Correct 719 ms 230960 KB Output is correct
32 Correct 734 ms 226872 KB Output is correct
33 Correct 825 ms 230008 KB Output is correct
34 Correct 285 ms 210392 KB Output is correct
35 Correct 814 ms 232656 KB Output is correct
36 Correct 744 ms 229336 KB Output is correct
37 Correct 847 ms 231868 KB Output is correct
38 Correct 778 ms 229804 KB Output is correct
39 Correct 779 ms 236968 KB Output is correct
40 Correct 718 ms 235896 KB Output is correct
41 Correct 618 ms 230352 KB Output is correct
42 Correct 531 ms 226488 KB Output is correct
43 Correct 740 ms 235916 KB Output is correct
44 Correct 725 ms 232016 KB Output is correct
45 Correct 692 ms 234900 KB Output is correct
46 Correct 580 ms 235468 KB Output is correct
47 Correct 828 ms 230944 KB Output is correct
48 Correct 721 ms 230928 KB Output is correct
49 Correct 737 ms 231112 KB Output is correct
50 Correct 716 ms 230920 KB Output is correct
51 Correct 635 ms 236472 KB Output is correct
52 Correct 652 ms 236360 KB Output is correct