Submission #399634

#TimeUsernameProblemLanguageResultExecution timeMemory
399634Jarif_RahmanWerewolf (IOI18_werewolf)C++17
7 / 100
4080 ms165496 KiB
#include "werewolf.h" #include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; struct reachability_tree{ int n; vector<int> p; vector<vector<int>> v; vector<int> w; reachability_tree(int _n, int _w){ n = _n; p.resize(n); v.resize(n); w.resize(n, _w); for(int i = 0; i < n; i++) p[i] = i; } int get(int x){ if(p[x] != x) p[x] = get(p[x]); return p[x]; } void unite(int a, int b, int _w){ a = get(a), b = get(b); p[a] = n, p[b] = n; p.pb(n); v.pb({}); v[n].pb(a); if(a != b) v[n].pb(b); w.pb(_w); n++; } }; int n, m, q; reachability_tree rtl(0, 0), rtr(0, 0); vector<int> szl, szr, al, ar, posl, posr; vector<vector<int>> ancl, ancr; void dfsl(int nd, int ss){ posl[nd] = al.size(); al.pb(nd); for(int x: rtl.v[nd]) if(x != ss) dfsl(x, nd); for(int x: rtl.v[nd]) szl[nd]+=szl[x]; ancl[nd][1] = ss; } void dfsr(int nd, int ss){ posr[nd] = ar.size(); ar.pb(nd); for(int x: rtr.v[nd]) if(x != ss) dfsr(x, nd); for(int x: rtr.v[nd]) szr[nd]+=szr[x]; ancr[nd][1] = ss; } int get_anc_l(int nd, int ss){ for(int i = 1; i < 21; i++){ if(ss%2 == 1) nd = ancl[nd][i]; if(nd == -1) return -1; ss/=2; } return nd; } int get_anc_r(int nd, int ss){ for(int i = 1; i < 21; i++){ if(ss%2 == 1) nd = ancr[nd][i]; if(nd == -1) return -1; ss/=2; } return nd; } int bsl(int nd, int vl){ int a = 0, b = 8e5; while(a < b){ int md = (a+b+1)/2; int x = get_anc_l(nd, md); if(x == -1) x = 1e9; else x = rtl.w[x]; if(x > vl) b = md-1; else a = md; } return get_anc_l(nd, a); } int bsr(int nd, int vl){ int a = 0, b = 8e5; while(a < b){ int md = (a+b+1)/2; int x = get_anc_r(nd, md); if(x == -1) x = -1; else x = rtr.w[x]; if(x < vl) b = md-1; else a = md; } return get_anc_r(nd, a); } vector<int> check_validity(int n, vector<int> U, vector<int> V, vector<int> ss, vector<int> ee, vector<int> L, vector<int> R){ ::n = n, m = U.size(), q = ss.size(); vector<pair<int, int>> edgel(m), edger(m); for(int i = 0; i < m; i++) edgel[i] = {U[i], V[i]}, edger[i] = {U[i], V[i]}; sort(edgel.begin(), edgel.end(), [&](pair<int, int> a, pair<int, int> b){ return max(a.f, a.sc) < max(b.f, b.sc); }); sort(edger.begin(), edger.end(), [&](pair<int, int> a, pair<int, int> b){ return min(a.f, a.sc) > min(b.f, b.sc); }); rtl = reachability_tree(n, 0), rtr = reachability_tree(n, n+1); for(int i = 0; i < m; i++) rtl.unite(edgel[i].f, edgel[i].sc, max(edgel[i].f, edgel[i].sc)), rtr.unite(edger[i].f, edger[i].sc, min(edger[i].f, edger[i].sc)); ancl.resize(rtl.n, vector<int>(21, -1)); ancr.resize(rtr.n, vector<int>(21, -1)); szl.resize(rtl.n, 1); szr.resize(rtr.n, 1); posl.resize(rtl.n); posr.resize(rtr.n); dfsl(rtl.n-1, -1); dfsr(rtr.n-1, -1); for(int i = 0; i < rtl.n; i++) ancl[i][0] = i; for(int i = 0; i < rtr.n; i++) ancr[i][0] = i; for(int i = 2; i < 21; i++) for(int j = 0; j < rtl.n; j++){ if(ancl[j][i-1] == -1) continue; ancl[j][i] = ancl[ancl[j][i-1]][i-1]; } for(int i = 2; i < 21; i++) for(int j = 0; j < rtr.n; j++){ if(ancr[j][i-1] == -1) continue; ancr[j][i] = ancr[ancr[j][i-1]][i-1]; } vector<int> ans(q); for(int qq = 0; qq < q; qq++){ int ndl = bsl(ee[qq], R[qq]), ndr = bsr(ss[qq], L[qq]); cerr << ndl << " " << ndr << " d\n"; for(int i = posl[ndl]; i < posl[ndl]+szl[ndl]; i++) for(int j = posr[ndr]; j < posr[ndr]+szr[ndr]; j++) if(al[i] == ar[j] && al[i] < n) ans[qq] = 1; } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...