#include <bits/stdc++.h>
#include "werewolf.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
const int nax=2e5+5;
vector<int> adj[nax];
int n;
int segtree[nax*4][2];
int tab[nax];
void build(int pos,int l,int r){
if(l==r){
segtree[pos][0]=segtree[pos][1]=tab[l];
return;
}
int mid=(r+l)/2;
build(pos*2+1,l,mid);
build(pos*2+2,mid+1,r);
segtree[pos][0]=min(segtree[pos*2+1][0],segtree[pos*2+2][0]);
segtree[pos][1]=max(segtree[pos*2+1][1],segtree[pos*2+2][1]);
return;
}
int query0(int pos,int l,int r,int left,int right){
if(l>r||l>right||r<left) return 1e9;
if(l>=left&&r<=right) return segtree[pos][0];
int mid=(r+l)/2;
return min(query0(pos*2+1,l,mid,left,right),query0(pos*2+2,mid+1,r,left,right));
}
int query1(int pos,int l,int r,int left,int right){
if(l>r||l>right||r<left) return -1e9;
if(l>=left&&r<=right) return segtree[pos][1];
int mid=(r+l)/2;
return max(query1(pos*2+1,l,mid,left,right),query1(pos*2+2,mid+1,r,left,right));
}
int bs_min_left(int x,int cur){
int l=-1;
int r=x;
while(r-l>1){
int mid=(r+l)/2;
if (query0(0,0,n-1,mid,x)>=cur)
r=mid;
else l=mid;
}
return r;
}
int bs_min_right(int x,int cur){
int l=x;
int r=n;
while(r-l>1){
int mid=(r+l)/2;
if (query0(0,0,n-1,x,mid)>=cur)
l=mid;
else r=mid;
}
return l;
}
int bs_max_left(int x,int cur){
int l=-1;
int r=x;
while(r-l>1){
int mid=(r+l)/2;
if (query1(0,0,n-1,mid,x)<=cur)
r=mid;
else l=mid;
}
return r;
}
int bs_max_right(int x,int cur){
int l=x;
int r=n;
while(r-l>1){
int mid=(r+l)/2;
if (query1(0,0,n-1,x,mid)<=cur)
l=mid;
else r=mid;
}
return l;
}
set<int> mergesort[nax*4];
void build_mergesort(int pos,int l,int r){
if(l==r){
mergesort[pos].insert(tab[l]);
return;
}
int mid=(r+l)/2;
build_mergesort(pos*2+1,l,mid);
build_mergesort(pos*2+2,mid+1,r);
for(auto u:mergesort[pos*2+1]) mergesort[pos].insert(u);
for(auto u:mergesort[pos*2+2]) mergesort[pos].insert(u);
return;
}
bool query(int pos,int l,int r,int left,int right,pair<int,int> cur){
if(l>r||l>right||r<left) return 0;
if(l>=left&&r<=right){
return *mergesort[pos].lower_bound(cur.fi)<=cur.se;
}
int mid=(r+l)/2;
return query(pos*2+1,l,mid,left,right,cur)|query(pos*2+2,mid+1,r,left,right,cur);
}
int t=0;
int conv[nax];
void dfs(int x,int p){
conv[x]=t;
tab[t++]=x;
for(auto u:adj[x]){
if(u==p) continue;
dfs(u,x);
}
return;
}
vector<int> mn(nax);
vector<int> mx(nax);
bool vis[nax];
void min_djikastra(int x){
priority_queue<int> pq;
pq.push(x);
mn[x]=x;
vis[x]=true;
while(!pq.empty()){
int node=pq.top();
pq.pop();
for(auto u:adj[node]){
if(vis[u]) continue;
vis[u]=true;
mn[u]=min(u,mn[node]);
pq.push(u);
}
}
}
void max_djikastra(int x){
priority_queue<int,vector<int>,greater<int>> pq;
pq.push(x);
mx[x]=x;
vis[x]=true;
while(!pq.empty()){
int node=pq.top();
pq.pop();
for(auto u:adj[node]){
if(vis[u]) continue;
vis[u]=true;
mx[u]=max(u,mx[node]);
pq.push(u);
}
}
}
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) {
int Q=R.size();
int M=X.size();
if(N<=3e3+5&&M<=6e3+5&&Q<=3e3+5){
n=N;
for (int i = 0; i < (int)X.size(); ++i)
{
adj[X[i]].pb(Y[i]);
adj[Y[i]].pb(X[i]);
}
vector<int> ans;
for (int i = 0; i < Q; ++i)
{
for (int j = 0; j < N; ++j)
{
mn[j]=1e9;
mx[j]=-1e9;
vis[j]=0;
}
min_djikastra(S[i]);
for (int j = 0; j < N; ++j)
{
vis[j]=0;
}
max_djikastra(E[i]);
pair<int,int> cur={L[i],R[i]};
bool test=false;
for (int j = L[i]; j < R[i]+1; ++j)
{
if(cur.fi<=mn[j]&&mx[j]<=cur.se){
test=true;
}
if(test) break;
}//cout <<endl;
ans.pb((test==true ? 1 : 0));
}
return ans;
}else{
n=N;
int q=R.size();
int v[n];
memset(v,0,sizeof v);
for (int i = 0; i < n-1; ++i)
{
adj[X[i]].pb(Y[i]);
adj[Y[i]].pb(X[i]);
v[X[i]]++;
v[Y[i]]++;
}
int lst;
for (int i = 0; i < n; ++i)
{
if(v[i]==1){
lst=i;
}
}
dfs(lst,-1);
build(0,0,n-1);
build_mergesort(0,0,n-1);
vector<int> ans;
for (int i = 0; i < q; ++i)
{
pair<int,int> cur={L[i],R[i]};
S[i]=conv[S[i]];
E[i]=conv[E[i]];
bool test=(E[i]>=S[i]);
pair<int,int> cnt;
if(test){
int right=bs_min_right(S[i],cur.fi);
int left=bs_max_left(E[i],cur.se);
cnt={right,left};
}else{
int right=bs_min_left(S[i],cur.fi);
int left=bs_max_right(E[i],cur.se);
cnt={left,right};
}
if(cnt.fi<cnt.se){
ans.pb(0);
continue;
}
ans.pb(1);
}
return ans;
}
}
/*namespace {
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
} // namespace
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int N = read_int();
int M = read_int();
int Q = read_int();
std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
for (int j = 0; j < M; ++j) {
X[j] = read_int();
Y[j] = read_int();
}
for (int i = 0; i < Q; ++i) {
S[i] = read_int();
E[i] = read_int();
L[i] = read_int();
R[i] = read_int();
}
std::vector<int> A = check_validity(N, X, Y, S, E, L, R);
for (size_t i = 0; i < A.size(); ++i) {
printf("%d\n", A[i]);
}
return 0;
}*/
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:208:12: warning: 'lst' may be used uninitialized in this function [-Wmaybe-uninitialized]
208 | dfs(lst,-1);
| ~~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
44120 KB |
Output is correct |
2 |
Correct |
16 ms |
44252 KB |
Output is correct |
3 |
Correct |
16 ms |
44124 KB |
Output is correct |
4 |
Correct |
16 ms |
44312 KB |
Output is correct |
5 |
Correct |
17 ms |
44124 KB |
Output is correct |
6 |
Correct |
18 ms |
44316 KB |
Output is correct |
7 |
Correct |
18 ms |
44120 KB |
Output is correct |
8 |
Correct |
17 ms |
44124 KB |
Output is correct |
9 |
Correct |
17 ms |
44220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
44120 KB |
Output is correct |
2 |
Correct |
16 ms |
44252 KB |
Output is correct |
3 |
Correct |
16 ms |
44124 KB |
Output is correct |
4 |
Correct |
16 ms |
44312 KB |
Output is correct |
5 |
Correct |
17 ms |
44124 KB |
Output is correct |
6 |
Correct |
18 ms |
44316 KB |
Output is correct |
7 |
Correct |
18 ms |
44120 KB |
Output is correct |
8 |
Correct |
17 ms |
44124 KB |
Output is correct |
9 |
Correct |
17 ms |
44220 KB |
Output is correct |
10 |
Correct |
1182 ms |
44680 KB |
Output is correct |
11 |
Correct |
1014 ms |
44636 KB |
Output is correct |
12 |
Correct |
309 ms |
44636 KB |
Output is correct |
13 |
Correct |
1110 ms |
44632 KB |
Output is correct |
14 |
Correct |
866 ms |
44632 KB |
Output is correct |
15 |
Correct |
1335 ms |
44632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1856 ms |
260900 KB |
Output is correct |
2 |
Correct |
1910 ms |
260848 KB |
Output is correct |
3 |
Correct |
1856 ms |
260744 KB |
Output is correct |
4 |
Correct |
1857 ms |
260812 KB |
Output is correct |
5 |
Correct |
1825 ms |
260704 KB |
Output is correct |
6 |
Correct |
1881 ms |
260832 KB |
Output is correct |
7 |
Correct |
1841 ms |
260812 KB |
Output is correct |
8 |
Correct |
1796 ms |
260908 KB |
Output is correct |
9 |
Correct |
1239 ms |
260696 KB |
Output is correct |
10 |
Correct |
1424 ms |
260812 KB |
Output is correct |
11 |
Correct |
1602 ms |
260684 KB |
Output is correct |
12 |
Correct |
1374 ms |
260940 KB |
Output is correct |
13 |
Correct |
1711 ms |
260828 KB |
Output is correct |
14 |
Correct |
1771 ms |
260824 KB |
Output is correct |
15 |
Correct |
1766 ms |
261072 KB |
Output is correct |
16 |
Correct |
1757 ms |
260848 KB |
Output is correct |
17 |
Correct |
1848 ms |
260680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
44120 KB |
Output is correct |
2 |
Correct |
16 ms |
44252 KB |
Output is correct |
3 |
Correct |
16 ms |
44124 KB |
Output is correct |
4 |
Correct |
16 ms |
44312 KB |
Output is correct |
5 |
Correct |
17 ms |
44124 KB |
Output is correct |
6 |
Correct |
18 ms |
44316 KB |
Output is correct |
7 |
Correct |
18 ms |
44120 KB |
Output is correct |
8 |
Correct |
17 ms |
44124 KB |
Output is correct |
9 |
Correct |
17 ms |
44220 KB |
Output is correct |
10 |
Correct |
1182 ms |
44680 KB |
Output is correct |
11 |
Correct |
1014 ms |
44636 KB |
Output is correct |
12 |
Correct |
309 ms |
44636 KB |
Output is correct |
13 |
Correct |
1110 ms |
44632 KB |
Output is correct |
14 |
Correct |
866 ms |
44632 KB |
Output is correct |
15 |
Correct |
1335 ms |
44632 KB |
Output is correct |
16 |
Correct |
1856 ms |
260900 KB |
Output is correct |
17 |
Correct |
1910 ms |
260848 KB |
Output is correct |
18 |
Correct |
1856 ms |
260744 KB |
Output is correct |
19 |
Correct |
1857 ms |
260812 KB |
Output is correct |
20 |
Correct |
1825 ms |
260704 KB |
Output is correct |
21 |
Correct |
1881 ms |
260832 KB |
Output is correct |
22 |
Correct |
1841 ms |
260812 KB |
Output is correct |
23 |
Correct |
1796 ms |
260908 KB |
Output is correct |
24 |
Correct |
1239 ms |
260696 KB |
Output is correct |
25 |
Correct |
1424 ms |
260812 KB |
Output is correct |
26 |
Correct |
1602 ms |
260684 KB |
Output is correct |
27 |
Correct |
1374 ms |
260940 KB |
Output is correct |
28 |
Correct |
1711 ms |
260828 KB |
Output is correct |
29 |
Correct |
1771 ms |
260824 KB |
Output is correct |
30 |
Correct |
1766 ms |
261072 KB |
Output is correct |
31 |
Correct |
1757 ms |
260848 KB |
Output is correct |
32 |
Correct |
1848 ms |
260680 KB |
Output is correct |
33 |
Incorrect |
1773 ms |
251704 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |