#include "werewolf.h"
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
const int N = 2e5+5, inf = 1e9;
int n, sz, m, timer=0, l,r;
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, (int)-inf};
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(t[node].mn < x)return L;
else return sz;
if(L >= l && R <= r){
int mid = (L+R)/2;
if(t[node*2].mn < x)return f_less(l, r, L, mid, node*2, x);
else f_less(l, r, mid, R, node*2+1, x);
}int mid = (L+R)/2;
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 r, int x){return f_less(l, r, 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(t[node].mx > x)return L;
else return sz;
if(L >= l && R <= r){
int mid = (L+R)/2;
if(t[node*2].mx > x)return f_great(l, r, L, mid, node*2, x);
else f_great(l, r, mid, R, node*2+1, x);
}int mid = (L+R)/2;
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 r, int x){return f_great(l, r, 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(t[node].mn < x)return L;
else return 0;
if(L >= l && R <= r){
int mid = (L+R)/2;
if(t[node*2+1].mn < x)return l_less(l, r, mid, R, node*2+1, x);
else l_less(l, r, L, mid, node*2, x);
}int mid = (L+R)/2;
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 l, int r, int x){return l_less(l, 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(t[node].mx > x)return L;
else return 0;
if(L >= l && R <= r){
int mid = (L+R)/2;
if(t[node*2+1].mx > x)return l_great(l, r, mid, R, node*2+1, x);
else l_great(l, r, L, mid, node*2, x);
}int mid = (L+R)/2;
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 l, int r, int x){return l_great(l, 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();
vector<int> ans;
for(int i=0;i<Q;i++){
int start = a[S[i]], end = a[E[i]];
l = L[i], r = R[i];
int ans1 = 1;
if(start <= end){
// cout << "f";
int ff = seg.f_less(start, sz, l);
int ss = seg.l_great(0,end, r);
if(ff < ss)ans1 = 0;
}else {
// cout << "s";
swap(start, end);
int ff = seg.l_less(0, end, l);
int ss = seg.f_great(start,sz, r);
if(ff > ss)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 member function 'int segtree::f_less(int, int, int, int, int, int)':
werewolf.cpp:37:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
37 | if(R-L == 1)
| ^
werewolf.cpp: In member function 'int segtree::f_great(int, int, int, int, int, int)':
werewolf.cpp:51:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
51 | if(R-L == 1)
| ^
werewolf.cpp: In member function 'int segtree::l_less(int, int, int, int, int, int)':
werewolf.cpp:67:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
67 | if(R-L == 1)
| ^
werewolf.cpp: In member function 'int segtree::l_great(int, int, int, int, int, int)':
werewolf.cpp:81:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
81 | if(R-L == 1)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
344 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 |
344 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 |
Execution timed out |
4069 ms |
35760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
344 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |