Submission #293959

# Submission time Handle Problem Language Result Execution time Memory
293959 2020-09-08T14:08:08 Z shayan_p Werewolf (IOI18_werewolf) C++17
100 / 100
1535 ms 151768 KB
#include<bits/stdc++.h>
#include "werewolf.h"

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

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

const int maxn = 2e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;

vector<int> tmpv[maxn];
int pr[maxn];
bool seen[maxn];

int fnd(int u){
    return pr[u] < 0 ? u : pr[u] = fnd(pr[u]);
}
vector<pii> build_tree(vector<int> vec, vector<pii> ed){ // TLE?
    memset(pr, -1, sizeof pr);
    memset(seen, 0, sizeof seen);
    for(int u : vec){
	tmpv[u].clear();
    }
    for(pii p : ed){
	tmpv[p.F].PB(p.S);
	tmpv[p.S].PB(p.F);
    }
    vector<pii> ans;
    for(int u : vec){
	seen[u] = 1;
	for(int y : tmpv[u]){
	    if(!seen[y])
		continue;
	    y = fnd(y);
	    if(y == u)
		continue;
	    ans.PB({u, y});
	    pr[y] = u;
	}
    }
    assert(sz(ans) + 1 == sz(vec));
    return ans;
}

vector<int> v[maxn];
int segl[maxn], segr[maxn];

void prep(int u, int par = -1){
    static int C = 0;
    segl[u] = C++;
    for(int y : v[u])
	if(y != par)
	    prep(y, u);
    segr[u] = C;
}

int sp[20][maxn];

void prep2(int u, int par = -1){
    sp[0][u] = par;
    for(int i = 1; i < 20; i++)
	sp[i][u] = (sp[i-1][u] == -1 ? -1 : sp[i-1][sp[i-1][u]]);
    for(int y : v[u])
	if(y != par)
	    prep2(y, u);
}

set<int> st[maxn];
vector< pair<pii, int> > qur[maxn];
int ans[maxn];

void dfs(int u, int par = -1){
    st[u].insert(u);
    for(int y : v[u]){
	if(y != par){
	    dfs(y, u);
	    if(sz(st[u]) < sz(st[y]))
		swap(st[u], st[y]);
	    for(int x : st[y])
		st[u].insert(x);
	}
    }
    for(auto [seg, id] : qur[u]){
	auto it = st[u].lower_bound(seg.F);
	ans[id] = it != st[u].end() && (*it) < seg.S;
    }
}

vector<int> check_validity(int n, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
    int q = sz(S), m = sz(X);
    vector<int> vec(n);
    iota(vec.begin(), vec.end(), 0);
    vector<pii> ed;
    for(int i = 0; i < m; i++)
	ed.PB({X[i], Y[i]});
    
    reverse(vec.begin(), vec.end());
    vector<pii> T1 = build_tree(vec, ed);
    reverse(vec.begin(), vec.end());
    vector<pii> T2 = build_tree(vec, ed);
    
    for(int i = 0; i < n; i++){
	v[i].clear();
    }
    for(int i = 0; i < n-1; i++){
	v[T2[i].F].PB(T2[i].S);
	v[T2[i].S].PB(T2[i].F);
    }
    prep(n-1);
    prep2(n-1);
    for(int i = 0; i < q; i++){
	int &u = E[i];
	for(int j = 19; j >= 0; j--){
	    if(sp[j][u] != -1 && sp[j][u] <= R[i])
		u = sp[j][u];
	}	
    }


    for(int i = 0; i < n; i++){
	v[i].clear();
    }
    for(int i = 0; i < n-1; i++){
	v[T1[i].F].PB(T1[i].S);
	v[T1[i].S].PB(T1[i].F);
    }
    prep2(0);
    for(int i = 0; i < q; i++){
	int &u = S[i];
	for(int j = 19; j >= 0; j--){
	    if(sp[j][u] != -1 && sp[j][u] >= L[i])
		u = sp[j][u];
	}
    }


    for(int i = 0; i < n; i++){
	v[i].clear();
    }
    for(int i = 0; i < n-1; i++){
	v[segl[ T1[i].F ]].PB(segl[ T1[i].S ]);
	v[segl[ T1[i].S ]].PB(segl[ T1[i].F ]);
    }
   
    for(int i = 0; i < q; i++){
	qur[ segl[S[i]] ].PB({{segl[E[i]], segr[E[i]]}, i});
    }
    dfs(segl[0]);

    vector<int> ret;
    for(int i = 0; i < q; i++){
	ret.PB(ans[i]);
    }
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24960 KB Output is correct
2 Correct 18 ms 24960 KB Output is correct
3 Correct 18 ms 24960 KB Output is correct
4 Correct 17 ms 24884 KB Output is correct
5 Correct 16 ms 24960 KB Output is correct
6 Correct 17 ms 24960 KB Output is correct
7 Correct 17 ms 24960 KB Output is correct
8 Correct 18 ms 24960 KB Output is correct
9 Correct 19 ms 24992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24960 KB Output is correct
2 Correct 18 ms 24960 KB Output is correct
3 Correct 18 ms 24960 KB Output is correct
4 Correct 17 ms 24884 KB Output is correct
5 Correct 16 ms 24960 KB Output is correct
6 Correct 17 ms 24960 KB Output is correct
7 Correct 17 ms 24960 KB Output is correct
8 Correct 18 ms 24960 KB Output is correct
9 Correct 19 ms 24992 KB Output is correct
10 Correct 30 ms 26240 KB Output is correct
11 Correct 29 ms 26360 KB Output is correct
12 Correct 25 ms 26496 KB Output is correct
13 Correct 25 ms 26496 KB Output is correct
14 Correct 28 ms 26620 KB Output is correct
15 Correct 31 ms 26444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1287 ms 149984 KB Output is correct
2 Correct 1198 ms 118056 KB Output is correct
3 Correct 1116 ms 126756 KB Output is correct
4 Correct 1104 ms 140720 KB Output is correct
5 Correct 1088 ms 143860 KB Output is correct
6 Correct 1203 ms 149128 KB Output is correct
7 Correct 1256 ms 151768 KB Output is correct
8 Correct 1072 ms 118016 KB Output is correct
9 Correct 919 ms 126040 KB Output is correct
10 Correct 870 ms 140436 KB Output is correct
11 Correct 965 ms 141860 KB Output is correct
12 Correct 1006 ms 147876 KB Output is correct
13 Correct 998 ms 117416 KB Output is correct
14 Correct 1112 ms 117668 KB Output is correct
15 Correct 1010 ms 117412 KB Output is correct
16 Correct 997 ms 117548 KB Output is correct
17 Correct 1175 ms 149980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24960 KB Output is correct
2 Correct 18 ms 24960 KB Output is correct
3 Correct 18 ms 24960 KB Output is correct
4 Correct 17 ms 24884 KB Output is correct
5 Correct 16 ms 24960 KB Output is correct
6 Correct 17 ms 24960 KB Output is correct
7 Correct 17 ms 24960 KB Output is correct
8 Correct 18 ms 24960 KB Output is correct
9 Correct 19 ms 24992 KB Output is correct
10 Correct 30 ms 26240 KB Output is correct
11 Correct 29 ms 26360 KB Output is correct
12 Correct 25 ms 26496 KB Output is correct
13 Correct 25 ms 26496 KB Output is correct
14 Correct 28 ms 26620 KB Output is correct
15 Correct 31 ms 26444 KB Output is correct
16 Correct 1287 ms 149984 KB Output is correct
17 Correct 1198 ms 118056 KB Output is correct
18 Correct 1116 ms 126756 KB Output is correct
19 Correct 1104 ms 140720 KB Output is correct
20 Correct 1088 ms 143860 KB Output is correct
21 Correct 1203 ms 149128 KB Output is correct
22 Correct 1256 ms 151768 KB Output is correct
23 Correct 1072 ms 118016 KB Output is correct
24 Correct 919 ms 126040 KB Output is correct
25 Correct 870 ms 140436 KB Output is correct
26 Correct 965 ms 141860 KB Output is correct
27 Correct 1006 ms 147876 KB Output is correct
28 Correct 998 ms 117416 KB Output is correct
29 Correct 1112 ms 117668 KB Output is correct
30 Correct 1010 ms 117412 KB Output is correct
31 Correct 997 ms 117548 KB Output is correct
32 Correct 1175 ms 149980 KB Output is correct
33 Correct 1252 ms 121676 KB Output is correct
34 Correct 474 ms 60244 KB Output is correct
35 Correct 1376 ms 117856 KB Output is correct
36 Correct 1286 ms 123544 KB Output is correct
37 Correct 1440 ms 117292 KB Output is correct
38 Correct 1389 ms 121848 KB Output is correct
39 Correct 1427 ms 137660 KB Output is correct
40 Correct 1509 ms 121004 KB Output is correct
41 Correct 1351 ms 117036 KB Output is correct
42 Correct 1094 ms 122640 KB Output is correct
43 Correct 1535 ms 119760 KB Output is correct
44 Correct 1223 ms 117108 KB Output is correct
45 Correct 1160 ms 128772 KB Output is correct
46 Correct 1303 ms 138164 KB Output is correct
47 Correct 1139 ms 117672 KB Output is correct
48 Correct 1109 ms 117404 KB Output is correct
49 Correct 1138 ms 117596 KB Output is correct
50 Correct 1259 ms 117464 KB Output is correct
51 Correct 1441 ms 123836 KB Output is correct
52 Correct 1406 ms 123040 KB Output is correct