This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include "werewolf.h"
typedef vector<int> vi;
#define vii(Z) iota(begin(Z), end(Z), 0)
const int NL = 2e5, ML = 6e5;
int N, M, Q, k, dfsTimer;
int e[NL], r[NL], lca[2][NL], st[2*NL], tL[2][ML], tR[2][ML];
array<int, 2> g[ML];
int find(int u){
return e[u] < 0 ? u : e[u] = find(e[u]);
}
void dfs(int u){
if(u < N){
tL[k][u] = ++dfsTimer;
}else{
tL[k][u] = 1+dfsTimer;
auto &[vL, vR] = g[u-N];
dfs(vL);
if(vL != vR) dfs(vR);
}
tR[k][u] = dfsTimer;
}
vi check_validity(int __N, vi X, vi Y, vi S, vi E, vi L, vi R){
N = __N, Q = size(S), M = size(X);
for(int i=0; i<M; ++i)
if(X[i] > Y[i]) swap(X[i], Y[i]);
vi h(M); vii(h);
vi byE(Q); vii(byE);
vi byS(Q); vii(byS);
sort(begin(byS), end(byS), [&](int u, int v){ return L[u] < L[v]; });
sort(begin(byE), end(byE), [&](int u, int v){ return R[u] > R[v]; });
for(; k<2; ++k){
sort(begin(h), end(h), [&](int &u, int &v){
return k ? X[u] > X[v] : Y[u] < Y[v];
});
iota(r, r+N, 0);
fill(e, e+N,-1);
for(int &i : h){
int u = find(X[i]), v = find(Y[i]);
if(k) while(!empty(byS) && L[byS.back()] > X[i]){
lca[k][byS.back()] = r[find(S[byS.back()])];
byS.pop_back();
}else while(!empty(byE) && R[byE.back()] < Y[i]){
lca[k][byE.back()] = r[find(E[byE.back()])];
byE.pop_back();
}
if(e[u] > e[v]) swap(u, v);
g[i] = {r[u], r[v]};
if(u != v){
e[u] += e[v];
e[v] = u;
}
r[u] = i+N;
}
if(k) for(int &i : byS) lca[k][i] = h.back() + N;
else for(int &i : byE) lca[k][i] = h.back() + N;
dfsTimer = -1;
dfs(h.back()+N);
}
vector<array<int, 3>> q(Q);
for(int i=0; i<Q; ++i)
q[i] = {tL[0][lca[0][i]], tR[0][lca[0][i]], i};
vi byL(N), res(Q); int pt = N-1;
vii(byL);
sort(begin(byL), end(byL), [&](int u, int v){
return tL[0][u] < tL[0][v];
});
sort(rbegin(q), rend(q));
fill(st, st+N+N, ML);
for(auto &[lx, rx, id] : q){
while(pt>=0 && tL[0][byL[pt]] >= lx){
int i = tL[1][byL[pt]];
for(st[i+=N]=tL[0][byL[pt]]; i/=2; )
st[i] = min(st[2*i], st[2*i+1]);
--pt;
}
int qL = tL[1][lca[1][id]], qR = tR[1][lca[1][id]], x = N+N;
for(qL+=N, qR+=N+1; qL<qR; qL/=2, qR/=2){
if(qL & 1) x = min(x, st[qL++]);
if(qR & 1) x = min(x, st[--qR]);
}
res[id] = x <= rx;
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |