#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5, inf = 1e9;
int n, sz, m, timer=0;
int depth[N];
int a[N];
vector<int> edges[N];
void dfs(int u, int p, int d = 0){
a[u]=timer,timer++;
depth[u] = d;
for(auto v : edges[u]){
if(v == p)continue;
dfs(v, u, d+1);
}
}
struct segtree {
struct Node{
int mn, mx;
};
vector<Node> t;
Node neutral = {inf, 0};
void build(){
sz=1;
while(sz<n)sz<<=1;
t.assign(sz*2, neutral);
for(int i=0;i<n;i++){
t[a[i]+sz] = {i, i};
}for(int i=sz-1;i>0;i--){
t[i] = {min(t[i*2].mn, t[i*2+1].mn), max(t[i*2].mx, t[i*2+1].mx)};
}
}
int f_less(int l, int r, int L, int R, int node, int x){
if(L >= r || R <= l)return sz;
if(R-L == 1){
if(L >= l && t[node].mn < x)return L;
else return sz;
}int mid = (L+R)/2;
if(L >= l && R <= r){
if(t[node*2].mn < x)return f_less(l, r, L, mid, node*2, x);
else return f_less(l, r, mid, R, node*2+1, x);
}return min(f_less(l, r, L, mid, node*2, x),
f_less(l, r, mid, R, node*2+1, x));
}int f_less(int l, int x){return f_less(l, sz, 0, sz, 1, x);}
int f_great(int l, int r, int L, int R, int node, int x){
if(L >= r || R <= l)return sz;
if(R-L == 1){
if(L >= l && t[node].mx > x)return L;
else return sz;
}int mid = (L+R)/2;
if(L >= l && R <= r){
if(t[node*2].mx > x)return f_great(l, r, L, mid, node*2, x);
else return f_great(l, r, mid, R, node*2+1, x);
}return min(f_great(l, r, L, mid, node*2, x),
f_great(l, r, mid, R, node*2+1, x));
}int f_great(int l, int x){return f_great(l, sz, 0, sz, 1, x);}
//903475834u1ify98pw75ruehruiae7893yu524hfuiayw78ryhweajkrasfasfasdfsddasf4589i7889he6ghwerg
int l_less(int l, int r, int L, int R, int node, int x){
if(L >= r || R <= l)return 0;
if(R-L == 1){
if(R <= r && t[node].mn < x)return L;
else return 0;
}int mid = (L+R)/2;
if(L >= l && R <= r){
if(t[node*2+1].mn < x)return l_less(l, r, mid, R, node*2+1, x);
else return l_less(l, r, L, mid, node*2, x);
}return max(l_less(l, r, L, mid, node*2, x),
l_less(l, r, mid, R, node*2+1, x));
}int l_less(int r, int x){return l_less(0, r, 0, sz, 1, x);}
int l_great(int l, int r, int L, int R, int node, int x){
if(L >= r || R <= l)return 0;
if(R-L == 1){
if(R <= r && t[node].mx > x)return L;
else return 0;
}int mid = (L+R)/2;
if(L >= l && R <= r){
if(t[node*2+1].mx > x)return l_great(l, r, mid, R, node*2+1, x);
else return l_great(l, r, L, mid, node*2, x);
}return max(l_great(l, r, L, mid, node*2, x),
l_great(l, r, mid, R, node*2+1, x));
}int l_great(int r, int x){return l_great(0, r, 0, sz, 1, x);}
}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) {
n = N;
m = X.size();
for(int i=0;i<m;i++){
edges[X[i]].push_back(Y[i]);
edges[Y[i]].push_back(X[i]);
}
dfs(0, 0);
int mxdep = 0, inddep = 0;
for(int i=0;i<n;i++){
if(depth[i] > mxdep)mxdep=depth[i], inddep = i;
}timer=0;
dfs(inddep, inddep);
seg.build();
int Q = S.size();
// for(int i=0;i<n;i++)cout << a[i] << ' ';cout << '\n';
vector<int> ans;
for(int i=0;i<Q;i++){
int start = a[S[i]], end = a[E[i]];
int l = L[i], r = R[i];
int ans1 = 1;
if(start <= end){
// cout << "f";
int ff = seg.f_less(start, l);
int ss = seg.l_great(end, r);
if(ff < ss || (abs(start-end) <= 1 && !(S[i] <= r&&S[i]>=l||E[i] <= r&&E[i]>=l)))ans1 = 0;
}else {
// cout << "s";
int ff = seg.l_less(start, l);
int ss = seg.f_great(end, r);
// cout << S[i] << " : " << E[i] << ";;;; " << start << " : " << end << "; " << ff << ' ' << ss << "\n\n";
if(ff > ss || (abs(start-end) <= 1 && !(S[i] <= r&&S[i]>=l||E[i] <= r&&E[i]>=l)))ans1 = 0;
}ans.push_back(ans1);
// cout << i << ' ' << ans1 << '\n';
}return ans;
}
/*
6 6 3
5 1
1 2
1 3
3 4
3 0
5 2
4 2 1 2
4 2 2 2
5 4 3 4
*/
/*
5 4 5
0 1
1 3
0 4
3 2
1 4 2 3
1 4 1 3
1 4 0 3
1 4 0 4
1 2 2 3
*/
Compilation message
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:116:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
116 | if(ff < ss || (abs(start-end) <= 1 && !(S[i] <= r&&S[i]>=l||E[i] <= r&&E[i]>=l)))ans1 = 0;
werewolf.cpp:122:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
122 | if(ff > ss || (abs(start-end) <= 1 && !(S[i] <= r&&S[i]>=l||E[i] <= r&&E[i]>=l)))ans1 = 0;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
347 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
347 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
752 ms |
36976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
347 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |