#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[timer]=u,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;
vector<vector<int>> v;
Node neutral = {inf, (int)-inf};
vector<int> Merge(vector<int> &v1, vector<int> &v2){
int i=0,j=0;
vector<int> v;
while(i<v1.size()&&j<v2.size()){
if(v1[i] <= v2[j]){
v.push_back(v1[i]);i++;
}else {
v.push_back(v2[j]);j++;
}
}for(;i<v1.size();i++)v.push_back(v1[i]);
for(;j<v2.size();j++)v.push_back(v2[j]);
return v;
}
void build(){
sz=1;
while(sz<n)sz<<=1;
t.assign(sz*2, neutral);
v.assign(sz*2, vector<int>(0));
for(int i=0;i<n;i++){
t[i+sz] = {a[i], a[i]};
v[i+sz].push_back(a[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)};
v[i] = Merge(v[i*2], v[i*2+1]);
}
}
int less_x(int l, int r, int L, int R, int node, int x){
if(L >= r || R <= l)return 0;
int ans = 0;
if(L >= l && R <= r){
ans = upper_bound(all(v[node]), x)-v[node].begin()-1;
return ans;
}int mid = (L+R)/2;
ans += less_x(l, r, L, mid, node*2, x);
ans += less_x(l, r, mid, R, node*2+1, x);
return ans;
}int less_x(int l, int r, int x){return less_x(l, r, 0, sz, 1, x);}
int f_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 sz;
if(L >= l && R <= r){
if(t[node*2].mn < x)return f_less(l, r, L, R, node*2, x);
else f_less(l, r, L, 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 0;
if(R-L == 1)
if(t[node].mx > x)return L;
else return sz;
if(L >= l && R <= r){
if(t[node*2].mx > x)return f_great(l, r, L, R, node*2, x);
else f_great(l, r, L, 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){
if(t[node*2+1].mn < x)return l_less(l, r, L, R, node*2+1, x);
else l_less(l, r, L, R, 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){
if(t[node*2+1].mx > x)return l_great(l, r, L, R, node*2+1, x);
else l_great(l, r, L, R, 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){
int ff = seg.f_less(start, end, l);
int ss = seg.l_great(start,end, r);
if(ff < ss)ans1 = 0;
else {
if(seg.less_x(start, ff, r) - seg.less_x(start, ff, l-1) == 0)ans1=0;
}
}else {
swap(start, end);
int ff = seg.l_less(start, end, l);
int ss = seg.f_great(start,end, r);
if(ff > ss)ans1 = 0;
else {
if(seg.less_x(ff, start, r) - seg.less_x(ff, start, l-1) == 0)ans1=0;
}
}ans.push_back(ans1);
}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 'std::vector<int> segtree::Merge(std::vector<int>&, std::vector<int>&)':
werewolf.cpp:28:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | while(i<v1.size()&&j<v2.size()){
| ~^~~~~~~~~~
werewolf.cpp:28:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | while(i<v1.size()&&j<v2.size()){
| ~^~~~~~~~~~
werewolf.cpp:34:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | }for(;i<v1.size();i++)v.push_back(v1[i]);
| ~^~~~~~~~~~
werewolf.cpp:35:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(;j<v2.size();j++)v.push_back(v2[j]);
| ~^~~~~~~~~~
werewolf.cpp: In member function 'int segtree::f_less(int, int, int, int, int, int)':
werewolf.cpp:65:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
65 | if(R-L == 1)
| ^
werewolf.cpp: In member function 'int segtree::f_great(int, int, int, int, int, int)':
werewolf.cpp:78:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
78 | if(R-L == 1)
| ^
werewolf.cpp: In member function 'int segtree::l_less(int, int, int, int, int, int)':
werewolf.cpp:93:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
93 | if(R-L == 1)
| ^
werewolf.cpp: In member function 'int segtree::l_great(int, int, int, int, int, int)':
werewolf.cpp:106:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
106 | if(R-L == 1)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
356 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 |
356 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 |
550 ms |
147324 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
356 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |