/**
* Author : boatinw99
* Date : 3.5.2019 15:42
*/
#include "werewolf.h"
#include<bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define fi first
#define se second
const int N = 2e5+9 ,inf = 1e9 ;
struct Q
{
int st,ed,l,r,idx ;
}query[N];
unordered_map<int,int> mp[N];
vector<int> memb[N];
pii edge[N<<1] ;
int n,m,q,p1[N],p2[N],sz[N],up[N];
int find(int u)
{
while(p1[u])u=p1[u];
return u ;
}
void Union(int u,int v)
{
int mn = min(u,v);
u=find(u),v=find(v);
if(u==v)return ;
if(sz[u]>sz[v])swap(u,v);
p1[u]=v;
sz[v]+=sz[u];
up[u]=mn;
}
void constructdsu()
{
for(int i=1;i<=n;i++)
{
p2[i]=i;
sz[i]=1;
memb[i].push_back(i);
}
sort(edge+1,edge+1+m,[&](const pii a,const pii b){
return min(a.fi,a.se)>min(b.fi,b.se);});
for(int i=1;i<=m;i++)Union(edge[i].fi,edge[i].se);
for(int i=1;i<=n;i++)
{
int node = i , dist = inf ;
while(node)
{
mp[node][i]=dist;
dist=min(dist,up[node]);
node=p1[node];
}
}
}
void magic_Union(int u,int v)
{
u=p2[u],v=p2[v];
if(u==v)return ;
if(memb[u].size()>memb[v].size())swap(u,v);
for(auto it:memb[u])
{
p2[it]=v;
int node = it;
while(node)
{
auto its = mp[node].find(u);
if(its==mp[node].end())break ;
mp[node][v]=max(mp[node][v],its->se);
mp[node].erase(its);
node=p1[node];
}
memb[v].push_back(it);
}
}
vector<int> check_validity(int SZ,vector<int> X,vector<int> Y,
vector<int> S,vector<int> E,
vector<int> L,vector<int> R)
{
n=SZ;
m=X.size();
q=S.size();
vector<int> ans(q);
for(int i=1;i<=m;i++)edge[i]={X[i-1]+1,Y[i-1]+1};
for(int i=1;i<=q;i++)query[i]={S[i-1]+1,E[i-1]+1,L[i-1]+1,R[i-1]+1,i};
constructdsu();
///Done preprocess
sort(query+1,query+1+q,[&](const Q a,const Q b){
return a.r<b.r;});
sort(edge+1,edge+1+m,[&](const pii a,const pii b){
return max(a.fi,a.se)<max(b.fi,b.se);});
for(int i=1,j=1;i<=q;i++)
{
while(j<=m&&max(edge[j].fi,edge[j].se)<=query[i].r)
{
magic_Union(edge[j].fi,edge[j].se);
j++;
}
int st = query[i].st , ed = query[i].ed , mnup = inf , dist = 0;
while(st)
{
dist=max(dist,min(mnup,mp[st][p2[ed]]));
mnup=min(mnup,up[st]);
st=p1[st];
}
ans[query[i].idx-1]=(p2[query[i].st]==p2[query[i].ed]||dist>=query[i].l);
}
return ans ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16000 KB |
Output is correct |
2 |
Correct |
17 ms |
16000 KB |
Output is correct |
3 |
Correct |
17 ms |
16072 KB |
Output is correct |
4 |
Correct |
17 ms |
16000 KB |
Output is correct |
5 |
Correct |
18 ms |
16092 KB |
Output is correct |
6 |
Correct |
19 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
22 ms |
15992 KB |
Output is correct |
9 |
Correct |
21 ms |
16000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16000 KB |
Output is correct |
2 |
Correct |
17 ms |
16000 KB |
Output is correct |
3 |
Correct |
17 ms |
16072 KB |
Output is correct |
4 |
Correct |
17 ms |
16000 KB |
Output is correct |
5 |
Correct |
18 ms |
16092 KB |
Output is correct |
6 |
Correct |
19 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
22 ms |
15992 KB |
Output is correct |
9 |
Correct |
21 ms |
16000 KB |
Output is correct |
10 |
Correct |
30 ms |
16996 KB |
Output is correct |
11 |
Correct |
26 ms |
17024 KB |
Output is correct |
12 |
Correct |
35 ms |
17276 KB |
Output is correct |
13 |
Correct |
28 ms |
17016 KB |
Output is correct |
14 |
Correct |
28 ms |
17152 KB |
Output is correct |
15 |
Correct |
33 ms |
17116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4070 ms |
112176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16000 KB |
Output is correct |
2 |
Correct |
17 ms |
16000 KB |
Output is correct |
3 |
Correct |
17 ms |
16072 KB |
Output is correct |
4 |
Correct |
17 ms |
16000 KB |
Output is correct |
5 |
Correct |
18 ms |
16092 KB |
Output is correct |
6 |
Correct |
19 ms |
16000 KB |
Output is correct |
7 |
Correct |
20 ms |
16000 KB |
Output is correct |
8 |
Correct |
22 ms |
15992 KB |
Output is correct |
9 |
Correct |
21 ms |
16000 KB |
Output is correct |
10 |
Correct |
30 ms |
16996 KB |
Output is correct |
11 |
Correct |
26 ms |
17024 KB |
Output is correct |
12 |
Correct |
35 ms |
17276 KB |
Output is correct |
13 |
Correct |
28 ms |
17016 KB |
Output is correct |
14 |
Correct |
28 ms |
17152 KB |
Output is correct |
15 |
Correct |
33 ms |
17116 KB |
Output is correct |
16 |
Execution timed out |
4070 ms |
112176 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |