/**
* 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)
{
auto its = mp[st].find(p2[ed]);
if(its!=mp[st].end())dist=max(dist,min(mnup,mp[st][p2[ed]]));
///printf("aa %d %d | %d\n",dist,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 |
35 ms |
15972 KB |
Output is correct |
2 |
Correct |
18 ms |
16000 KB |
Output is correct |
3 |
Correct |
18 ms |
16076 KB |
Output is correct |
4 |
Correct |
18 ms |
16000 KB |
Output is correct |
5 |
Correct |
18 ms |
15960 KB |
Output is correct |
6 |
Correct |
18 ms |
16100 KB |
Output is correct |
7 |
Correct |
19 ms |
16000 KB |
Output is correct |
8 |
Correct |
18 ms |
16100 KB |
Output is correct |
9 |
Correct |
25 ms |
16052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15972 KB |
Output is correct |
2 |
Correct |
18 ms |
16000 KB |
Output is correct |
3 |
Correct |
18 ms |
16076 KB |
Output is correct |
4 |
Correct |
18 ms |
16000 KB |
Output is correct |
5 |
Correct |
18 ms |
15960 KB |
Output is correct |
6 |
Correct |
18 ms |
16100 KB |
Output is correct |
7 |
Correct |
19 ms |
16000 KB |
Output is correct |
8 |
Correct |
18 ms |
16100 KB |
Output is correct |
9 |
Correct |
25 ms |
16052 KB |
Output is correct |
10 |
Correct |
30 ms |
16984 KB |
Output is correct |
11 |
Correct |
27 ms |
16888 KB |
Output is correct |
12 |
Correct |
29 ms |
17144 KB |
Output is correct |
13 |
Correct |
41 ms |
17040 KB |
Output is correct |
14 |
Correct |
31 ms |
17116 KB |
Output is correct |
15 |
Correct |
37 ms |
17132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3550 ms |
110492 KB |
Output is correct |
2 |
Correct |
784 ms |
82468 KB |
Output is correct |
3 |
Correct |
1026 ms |
92676 KB |
Output is correct |
4 |
Correct |
1696 ms |
106628 KB |
Output is correct |
5 |
Correct |
1962 ms |
109356 KB |
Output is correct |
6 |
Correct |
2575 ms |
114472 KB |
Output is correct |
7 |
Correct |
3392 ms |
121588 KB |
Output is correct |
8 |
Correct |
707 ms |
82580 KB |
Output is correct |
9 |
Correct |
723 ms |
92704 KB |
Output is correct |
10 |
Correct |
1253 ms |
106548 KB |
Output is correct |
11 |
Correct |
1545 ms |
107928 KB |
Output is correct |
12 |
Correct |
2684 ms |
114444 KB |
Output is correct |
13 |
Correct |
677 ms |
76744 KB |
Output is correct |
14 |
Correct |
636 ms |
76584 KB |
Output is correct |
15 |
Correct |
727 ms |
76636 KB |
Output is correct |
16 |
Correct |
647 ms |
76532 KB |
Output is correct |
17 |
Correct |
3329 ms |
119192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
15972 KB |
Output is correct |
2 |
Correct |
18 ms |
16000 KB |
Output is correct |
3 |
Correct |
18 ms |
16076 KB |
Output is correct |
4 |
Correct |
18 ms |
16000 KB |
Output is correct |
5 |
Correct |
18 ms |
15960 KB |
Output is correct |
6 |
Correct |
18 ms |
16100 KB |
Output is correct |
7 |
Correct |
19 ms |
16000 KB |
Output is correct |
8 |
Correct |
18 ms |
16100 KB |
Output is correct |
9 |
Correct |
25 ms |
16052 KB |
Output is correct |
10 |
Correct |
30 ms |
16984 KB |
Output is correct |
11 |
Correct |
27 ms |
16888 KB |
Output is correct |
12 |
Correct |
29 ms |
17144 KB |
Output is correct |
13 |
Correct |
41 ms |
17040 KB |
Output is correct |
14 |
Correct |
31 ms |
17116 KB |
Output is correct |
15 |
Correct |
37 ms |
17132 KB |
Output is correct |
16 |
Correct |
3550 ms |
110492 KB |
Output is correct |
17 |
Correct |
784 ms |
82468 KB |
Output is correct |
18 |
Correct |
1026 ms |
92676 KB |
Output is correct |
19 |
Correct |
1696 ms |
106628 KB |
Output is correct |
20 |
Correct |
1962 ms |
109356 KB |
Output is correct |
21 |
Correct |
2575 ms |
114472 KB |
Output is correct |
22 |
Correct |
3392 ms |
121588 KB |
Output is correct |
23 |
Correct |
707 ms |
82580 KB |
Output is correct |
24 |
Correct |
723 ms |
92704 KB |
Output is correct |
25 |
Correct |
1253 ms |
106548 KB |
Output is correct |
26 |
Correct |
1545 ms |
107928 KB |
Output is correct |
27 |
Correct |
2684 ms |
114444 KB |
Output is correct |
28 |
Correct |
677 ms |
76744 KB |
Output is correct |
29 |
Correct |
636 ms |
76584 KB |
Output is correct |
30 |
Correct |
727 ms |
76636 KB |
Output is correct |
31 |
Correct |
647 ms |
76532 KB |
Output is correct |
32 |
Correct |
3329 ms |
119192 KB |
Output is correct |
33 |
Correct |
1955 ms |
88312 KB |
Output is correct |
34 |
Correct |
457 ms |
48140 KB |
Output is correct |
35 |
Correct |
1405 ms |
79964 KB |
Output is correct |
36 |
Correct |
2149 ms |
90436 KB |
Output is correct |
37 |
Correct |
1397 ms |
80592 KB |
Output is correct |
38 |
Correct |
2138 ms |
87756 KB |
Output is correct |
39 |
Correct |
1715 ms |
93740 KB |
Output is correct |
40 |
Correct |
1180 ms |
89780 KB |
Output is correct |
41 |
Correct |
1256 ms |
81336 KB |
Output is correct |
42 |
Correct |
2058 ms |
90272 KB |
Output is correct |
43 |
Correct |
1179 ms |
81744 KB |
Output is correct |
44 |
Correct |
1284 ms |
80552 KB |
Output is correct |
45 |
Correct |
1317 ms |
86740 KB |
Output is correct |
46 |
Correct |
1373 ms |
94852 KB |
Output is correct |
47 |
Correct |
633 ms |
76876 KB |
Output is correct |
48 |
Correct |
721 ms |
76888 KB |
Output is correct |
49 |
Correct |
736 ms |
76908 KB |
Output is correct |
50 |
Correct |
706 ms |
76756 KB |
Output is correct |
51 |
Correct |
1241 ms |
89176 KB |
Output is correct |
52 |
Correct |
1317 ms |
89012 KB |
Output is correct |