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 "werewolf.h"
#include <bits/stdc++.h>
#define MAX 1000000000
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
int l,r;
bitset<200010> vh,vw;
vector<vi> G;
void dfs_human(int u){
if(u<l) return;
vh[u]=1;
for(auto &v:G[u]){
if(!vh[v]){
dfs_human(v);
}
}
}
void dfs_wolf(int u){
if(u>r) return;
vw[u]=1;
for(auto &v:G[u]){
if(!vw[v]){
dfs_wolf(v);
}
}
}
map<int,int> m,mtr;
vector<int> path;
int vis[800010];
ii tree[800010];
void dfs(int u){
path.push_back(u);
vis[u]=1;
for(auto &v: G[u]){
if(!vis[v]){
dfs(v);
}
}
}
int le[200010];
void init(int node,int a,int b){
if(a>b) return;
if(a==b){
mtr[a]=node;
le[node]=a;
tree[node]=ii(path[a],path[a]);
return;
}
int mid=(a+b)/2;
init(2*node+1,a,mid);
init(2*node+2,mid+1,b);
tree[node].first=min(tree[2*node+1].first,tree[2*node+2].first);
tree[node].second=max(tree[2*node+1].second,tree[2*node+2].second);
}
int find_left(int node,int l,int r){
//si node simepre se puede
while(node!=0){
if(tree[node].first>=l and tree[node].second<=r) {
if(node%2==0){
node/=2;
node--;
}
else node/=2;
}
else break;
}
if(node==0 and tree[node].first>=l and tree[node].second<=r) return 0;
node=2*node+1;
//cout<<l<<" "<<r<<endl;
while(true){
if(le[node]!=-1) break;
//cout<<node<<" ";
bool hl,hr;
hl=(tree[2*node+1].first>=l and tree[2*node+1].second<=r);
hr=(tree[2*node+2].first>=l and tree[2*node+2].second<=r);
if(hl==0 and hr==0) node=2*node+2;
else if(hl==1 and hr==0) node=2*node+2;
else if(hl==0 or hl==1) node=2*node+1;
/* if(hl==1) node=2*node+1;
else node=2*node+2;*/
}
// cout<<node<<endl;
if(tree[node].first<l or tree[node].second>r) return le[node]+1;
return le[node];
}
int M;
int find_right(int node,int l,int r){
while(node!=0){
if(tree[node].first>=l and tree[node].second<=r){
if(node%2==0){
node/=2;
node--;
}
else node/=2;
}
else break;
}
//if(node==0 and tree[node].second<=r) return M;
node=2*node+2;
while(true){
if(le[node]!=-1) break;
bool hl,hr,ha;
hl=(tree[2*node+1].first>=l and tree[2*node+1].second<=r);
hr=(tree[2*node+2].first>=l and tree[2*node+2].second<=r);
ha=(tree[node].first>=l and tree[2*node+2].second<=r);
if(hr==0 and hl==0) node=2*node+1;
else if(hr==0 and hl==1) node=2*node+1;
else node=2*node+2;
}
if(tree[node].first<l or tree[node].second>r) return le[node]-1;
return le[node];
}
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
M=N-1;
G.resize(N+1);
for(int i=0;i<X.size();i++){
int u=X[i],v=Y[i];
G[u].push_back(v);
G[v].push_back(u);
}
bool sw=0;
int id;
for(int i=0;i<N;i++){
if(G[i].size()>2){
sw=1;
}
if(G[i].size()==1) id=i;
}
vector<int> res;
/* if(sw==1){
for(int i=0;i<S.size();i++){
l=L[i];
r=R[i];
vh.reset();
vw.reset();
dfs_human(S[i]);
dfs_wolf(E[i]);
bool sw=0;
for(int i=0;i<N;i++){
if(vh[i]==1 and vw[i]==1){
sw=1;
break;
}
}
res.push_back(sw);
}
}*/
// else{
dfs(id);
for(int i=0;i<path.size();i++){
m[path[i]]=i;
}
memset(le,-1,sizeof le);
init(0,0,N-1);
int q=S.size();
for(int i=0;i<q;i++){
int ns=mtr[m[S[i]]];
int ne=mtr[m[E[i]]];
int la=find_left(ns,L[i],N-1);
int ra=find_right(ns,L[i],N-1);
int lb=find_left(ne,0,R[i]);
int rb=find_right(ne,0,R[i]);
//cout<<la<<" "<<ra<<" "<<lb<<" "<<rb<<" "<<ns<<" "<<ne<<endl;
if((la<=lb and ra>=lb) or (la<=rb and ra>=rb)) res.push_back(1);
else res.push_back(0);
}
// }
return res;
}
Compilation message (stderr)
werewolf.cpp: In function 'int find_right(int, int, int)':
werewolf.cpp:104:16: warning: variable 'ha' set but not used [-Wunused-but-set-variable]
104 | bool hl,hr,ha;
| ^~
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:120:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(int i=0;i<X.size();i++){
| ~^~~~~~~~~
werewolf.cpp:154:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for(int i=0;i<path.size();i++){
| ~^~~~~~~~~~~~
werewolf.cpp:125:8: warning: variable 'sw' set but not used [-Wunused-but-set-variable]
125 | bool sw=0;
| ^~
werewolf.cpp:153:8: warning: 'id' may be used uninitialized in this function [-Wmaybe-uninitialized]
153 | dfs(id);
| ~~~^~~~
# | 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... |