#include<bits/stdc++.h>
#include "werewolf.h"
using namespace std;
const int maxn=200000+5;
vector<int>adj[maxn];
struct dsu{
int par[maxn],sz[maxn],lca[maxn][20],fpar[maxn];
vector<int>child[maxn];
pair<int,int>stf[maxn];
int timea;
dsu(){
for(int i=0;i<maxn;i++){
for(int j=0;j<20;j++){
lca[i][j]=-1;
}
}
timea=0;
for(int i=0;i<maxn;i++){
par[i]=i;
fpar[i]=i;
}
}
int root(int c){
if(fpar[c]==c){
return c;
}
return fpar[c]=root(fpar[c]);
}
void con(int u,int v,int vv){
int rootu=root(u),rootv=root(v);
if(rootu!=rootv){
if(vv==0){
if(rootu<rootv){
swap(rootu,rootv);
}
}
else{
if(rootu>rootv){
swap(rootu,rootv);
}
}
par[rootv]=rootu;
child[rootu].push_back(rootv);
fpar[rootv]=rootu;
}
}
void dfs(int u){
timea++;
stf[u].first=timea;
for(auto x:child[u]){
lca[x][0]=u;
// cout<<u<<" "<<x<<" "<<timea<<endl;
dfs(x);
}
stf[u].second=timea;
}
void build(){
int rishe=root(1);
dfs(rishe);
for(int i=1;i<20;i++){
for(int j=0;j<maxn;j++){
if(lca[j][i-1]==-1){
continue;
}
lca[j][i]=lca[lca[j][i-1]][i-1];
}
}
}
int ww(int u,int had,int vv){
for(int i=19;i>=0;i--){
if(lca[u][i]!=-1){
// cout<<u<<" "<<vv<<" "<<lca[u][i]<<endl;
if(vv==0){
if(lca[u][i]<=had){
u=lca[u][i];
}
}
else{
if(lca[u][i]>=had){
u=lca[u][i];
}
}
}
}
return u;
}
};
int kaf=(1<<18);
struct segment{
struct node{
vector<int>all;
};
node seg[(1<<19)];
vector<int>merge(vector<int>a,vector<int>b){
vector<int>ret;
int i=0,j=0;
while(i<(int)a.size()&&j<(int)b.size()){
if(a[i]<b[j]){
ret.push_back(a[i]);
i++;
}
else{
ret.push_back(b[j]);
j++;
}
}
while(i<(int)a.size()){
ret.push_back(a[i]);
i++;
}
while(j<(int)b.size()){
ret.push_back(b[j]);
j++;
}
return ret;
}
void build(int i){
if((i<<1)>=(1<<19)){
return ;
}
build((i<<1));
build((i<<1)^1);
seg[i].all=merge(seg[(i<<1)].all,seg[(i<<1)^1].all);
}
int pors(int i,int l,int r,int tl,int tr,int hl,int hr){
if(l>tr||r<tl){
return 0;
}
if(l>=tl&&r<=tr){
int p=lower_bound(seg[i].all.begin(),seg[i].all.end(),hl)-seg[i].all.begin();
// cout<<l<<" dsa "<<r<<" "<<tl<<" "<<tr<<" "<<hl<<" "<<hr<<endl;
// for(auto x:seg[i].all){
// cout<<x<<" ";
// }
// cout<<"by"<<endl;
if(p==(int)seg[i].all.size()){
return 0;
}
if(seg[i].all[p]<=hr){
return 1;
}
return 0;
}
int m=(l+r)>>1;
int d=pors((i<<1),l,m,tl,tr,hl,hr);
d|=pors((i<<1)^1,m+1,r,tl,tr,hl,hr);
return d;
}
};
dsu a,b;
segment seg;
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 m=x.size();
for(int i=0;i<m;i++){
adj[x[i]].push_back(y[i]);
adj[y[i]].push_back(x[i]);
}
for(int i=0;i<=n-1;i++){
for(auto x:adj[i]){
if(x<=i){
a.con(x,i,0);
}
}
}
for(int i=n-1;i>=0;i--){
for(auto x:adj[i]){
if(x>=i){
b.con(x,i,1);
}
}
}
a.build();
b.build();
for(int i=0;i<n;i++){
seg.seg[kaf+a.stf[i].first].all.push_back(b.stf[i].first);
// cout<<i<<" asd "<<a.stf[i].first<<" "<<b.stf[i].first<<endl;
}
seg.build(1);
int q=s.size();
vector<int>res(q);
for(int i=0;i<q;i++){
int u=s[i],v=e[i],tl=l[i],tr=r[i];
if(u<tl||v>tr){
res[i]=0;
continue;
}
int bu=b.ww(u,tl,1),bv=a.ww(v,tr,0);
int d=seg.pors(1,0,kaf-1,a.stf[bv].first,a.stf[bv].second,b.stf[bu].first,b.stf[bu].second);
res[i]=d;
// cout<<bu<<" "<<bv<<" "<<u<<" "<<v<<" "<<tl<<" "<<tr<<endl;
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
64284 KB |
Output is correct |
2 |
Correct |
67 ms |
64272 KB |
Output is correct |
3 |
Correct |
57 ms |
64212 KB |
Output is correct |
4 |
Correct |
59 ms |
64208 KB |
Output is correct |
5 |
Correct |
64 ms |
64304 KB |
Output is correct |
6 |
Correct |
64 ms |
64356 KB |
Output is correct |
7 |
Correct |
61 ms |
64284 KB |
Output is correct |
8 |
Correct |
57 ms |
64296 KB |
Output is correct |
9 |
Correct |
58 ms |
64288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
64284 KB |
Output is correct |
2 |
Correct |
67 ms |
64272 KB |
Output is correct |
3 |
Correct |
57 ms |
64212 KB |
Output is correct |
4 |
Correct |
59 ms |
64208 KB |
Output is correct |
5 |
Correct |
64 ms |
64304 KB |
Output is correct |
6 |
Correct |
64 ms |
64356 KB |
Output is correct |
7 |
Correct |
61 ms |
64284 KB |
Output is correct |
8 |
Correct |
57 ms |
64296 KB |
Output is correct |
9 |
Correct |
58 ms |
64288 KB |
Output is correct |
10 |
Correct |
67 ms |
65188 KB |
Output is correct |
11 |
Correct |
70 ms |
65136 KB |
Output is correct |
12 |
Correct |
65 ms |
65112 KB |
Output is correct |
13 |
Correct |
74 ms |
65240 KB |
Output is correct |
14 |
Correct |
68 ms |
65288 KB |
Output is correct |
15 |
Correct |
74 ms |
65252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
677 ms |
122992 KB |
Output is correct |
2 |
Correct |
888 ms |
124908 KB |
Output is correct |
3 |
Correct |
855 ms |
123364 KB |
Output is correct |
4 |
Correct |
765 ms |
122836 KB |
Output is correct |
5 |
Correct |
730 ms |
122808 KB |
Output is correct |
6 |
Correct |
699 ms |
122680 KB |
Output is correct |
7 |
Correct |
607 ms |
122572 KB |
Output is correct |
8 |
Correct |
859 ms |
124984 KB |
Output is correct |
9 |
Correct |
704 ms |
123448 KB |
Output is correct |
10 |
Correct |
505 ms |
122772 KB |
Output is correct |
11 |
Correct |
534 ms |
122812 KB |
Output is correct |
12 |
Correct |
613 ms |
122760 KB |
Output is correct |
13 |
Correct |
859 ms |
129896 KB |
Output is correct |
14 |
Correct |
875 ms |
129964 KB |
Output is correct |
15 |
Correct |
857 ms |
130024 KB |
Output is correct |
16 |
Correct |
887 ms |
129892 KB |
Output is correct |
17 |
Correct |
646 ms |
122664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
64284 KB |
Output is correct |
2 |
Correct |
67 ms |
64272 KB |
Output is correct |
3 |
Correct |
57 ms |
64212 KB |
Output is correct |
4 |
Correct |
59 ms |
64208 KB |
Output is correct |
5 |
Correct |
64 ms |
64304 KB |
Output is correct |
6 |
Correct |
64 ms |
64356 KB |
Output is correct |
7 |
Correct |
61 ms |
64284 KB |
Output is correct |
8 |
Correct |
57 ms |
64296 KB |
Output is correct |
9 |
Correct |
58 ms |
64288 KB |
Output is correct |
10 |
Correct |
67 ms |
65188 KB |
Output is correct |
11 |
Correct |
70 ms |
65136 KB |
Output is correct |
12 |
Correct |
65 ms |
65112 KB |
Output is correct |
13 |
Correct |
74 ms |
65240 KB |
Output is correct |
14 |
Correct |
68 ms |
65288 KB |
Output is correct |
15 |
Correct |
74 ms |
65252 KB |
Output is correct |
16 |
Correct |
677 ms |
122992 KB |
Output is correct |
17 |
Correct |
888 ms |
124908 KB |
Output is correct |
18 |
Correct |
855 ms |
123364 KB |
Output is correct |
19 |
Correct |
765 ms |
122836 KB |
Output is correct |
20 |
Correct |
730 ms |
122808 KB |
Output is correct |
21 |
Correct |
699 ms |
122680 KB |
Output is correct |
22 |
Correct |
607 ms |
122572 KB |
Output is correct |
23 |
Correct |
859 ms |
124984 KB |
Output is correct |
24 |
Correct |
704 ms |
123448 KB |
Output is correct |
25 |
Correct |
505 ms |
122772 KB |
Output is correct |
26 |
Correct |
534 ms |
122812 KB |
Output is correct |
27 |
Correct |
613 ms |
122760 KB |
Output is correct |
28 |
Correct |
859 ms |
129896 KB |
Output is correct |
29 |
Correct |
875 ms |
129964 KB |
Output is correct |
30 |
Correct |
857 ms |
130024 KB |
Output is correct |
31 |
Correct |
887 ms |
129892 KB |
Output is correct |
32 |
Correct |
646 ms |
122664 KB |
Output is correct |
33 |
Correct |
927 ms |
122872 KB |
Output is correct |
34 |
Correct |
386 ms |
95644 KB |
Output is correct |
35 |
Correct |
1063 ms |
125056 KB |
Output is correct |
36 |
Correct |
787 ms |
123272 KB |
Output is correct |
37 |
Correct |
1016 ms |
124344 KB |
Output is correct |
38 |
Correct |
863 ms |
123812 KB |
Output is correct |
39 |
Correct |
972 ms |
130252 KB |
Output is correct |
40 |
Correct |
900 ms |
133012 KB |
Output is correct |
41 |
Correct |
793 ms |
123728 KB |
Output is correct |
42 |
Correct |
519 ms |
123148 KB |
Output is correct |
43 |
Correct |
1143 ms |
129732 KB |
Output is correct |
44 |
Correct |
973 ms |
124300 KB |
Output is correct |
45 |
Correct |
822 ms |
130704 KB |
Output is correct |
46 |
Correct |
844 ms |
130408 KB |
Output is correct |
47 |
Correct |
849 ms |
130064 KB |
Output is correct |
48 |
Correct |
849 ms |
130036 KB |
Output is correct |
49 |
Correct |
867 ms |
130132 KB |
Output is correct |
50 |
Correct |
844 ms |
130060 KB |
Output is correct |
51 |
Correct |
735 ms |
132660 KB |
Output is correct |
52 |
Correct |
763 ms |
132812 KB |
Output is correct |