#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
const int MAXQ = 2e5;
int N, M, Q;
vector<int> S, E, L, R, ans;
struct UF
{
int par[MAXN+10];
void init() { for(int i=0; i<N; i++) par[i]=i; }
int Find(int x) { return (x==par[x])?x:(par[x]=Find(par[x])); }
}uf1, uf2;
vector<int> adj[MAXN+10], adj1[MAXN+10], adj2[MAXN+10];
int s1[MAXN+10], e1[MAXN+10], s2[MAXN+10], e2[MAXN+10], cnt;
int par1[MAXN+10][25], par2[MAXN+10][25];
void dfs(int now, int *s, int *e, vector<int> *adj)
{
s[now]=cnt++; e[now]=s[now];
for(int nxt : adj[now])
{
dfs(nxt, s, e, adj);
e[now]=max(e[now], e[nxt]);
}
}
int A[MAXN+10];
vector<int> tree[4*MAXN+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node].push_back(A[tl]);
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node].resize(tree[node*2].size()+tree[node*2+1].size());
merge(tree[node*2].begin(), tree[node*2].end(), tree[node*2+1].begin(), tree[node*2+1].end(), tree[node].begin());
}
int query(int node, int tl, int tr, int xl, int xr, int yl, int yr)
{
if(tr<xl || xr<tl) return 0;
if(xl<=tl && tr<=xr) return upper_bound(tree[node].begin(), tree[node].end(), yr)-lower_bound(tree[node].begin(), tree[node].end(), yl);
int mid=tl+tr>>1;
return query(node*2, tl, mid, xl, xr, yl, yr)+query(node*2+1, mid+1, tr, xl, xr, yl, yr);
}
vector<int> check_validity(int _N, vector<int> X, vector<int> Y, vector<int> _S, vector<int> _E, vector<int> _L, vector<int> _R)
{
int i, j;
N=_N; S=_S; E=_E; L=_L; R=_R; M=X.size(), Q=S.size(); ans.resize(Q);
for(i=0; i<M; i++)
{
adj[X[i]].push_back(Y[i]);
adj[Y[i]].push_back(X[i]);
}
uf1.init(); par1[0][0]=0;
for(i=N-1; i>=0; i--)
{
int now=i;
for(int nxt : adj[now])
{
if(nxt<now) continue;
nxt=uf1.Find(nxt);
if(now==nxt) continue;
uf1.par[nxt]=now; par1[nxt][0]=now;
adj1[now].push_back(nxt);
}
}
uf2.init(); par2[N-1][0]=N-1;
for(i=0; i<N; i++)
{
int now=i;
for(int nxt : adj[now])
{
if(nxt>now) continue;
nxt=uf2.Find(nxt);
if(now==nxt) continue;
uf2.par[nxt]=now; par2[nxt][0]=now;
adj2[now].push_back(nxt);
}
}
cnt=0; dfs(0, s1, e1, adj1);
cnt=0; dfs(N-1, s2, e2, adj2);
for(i=0; i<N; i++) A[s1[i]]=s2[i];
init(1, 0, N-1);
for(j=1; j<=20; j++) for(i=0; i<N; i++) par1[i][j]=par1[par1[i][j-1]][j-1];
for(j=1; j<=20; j++) for(i=0; i<N; i++) par2[i][j]=par2[par2[i][j-1]][j-1];
for(i=0; i<Q; i++)
{
int s=S[i], e=E[i];
if(S[i]<L[i] || E[i]>R[i]) continue;
for(j=20; j>=0; j--) if(par1[s][j]>=L[i]) s=par1[s][j];
for(j=20; j>=0; j--) if(par2[e][j]<=R[i]) e=par2[e][j];
if(query(1, 0, N-1, s1[s], e1[s], s2[e], e2[e])) ans[i]=1;
}
return ans;
}
Compilation message
werewolf.cpp: In function 'void init(int, int, int)':
werewolf.cpp:45:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
werewolf.cpp: In function 'int query(int, int, int, int, int, int, int)':
werewolf.cpp:56:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
33316 KB |
Output is correct |
2 |
Correct |
32 ms |
33272 KB |
Output is correct |
3 |
Correct |
33 ms |
33272 KB |
Output is correct |
4 |
Correct |
32 ms |
33272 KB |
Output is correct |
5 |
Correct |
33 ms |
33272 KB |
Output is correct |
6 |
Correct |
33 ms |
33272 KB |
Output is correct |
7 |
Correct |
35 ms |
33268 KB |
Output is correct |
8 |
Correct |
33 ms |
33272 KB |
Output is correct |
9 |
Correct |
33 ms |
33244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
33316 KB |
Output is correct |
2 |
Correct |
32 ms |
33272 KB |
Output is correct |
3 |
Correct |
33 ms |
33272 KB |
Output is correct |
4 |
Correct |
32 ms |
33272 KB |
Output is correct |
5 |
Correct |
33 ms |
33272 KB |
Output is correct |
6 |
Correct |
33 ms |
33272 KB |
Output is correct |
7 |
Correct |
35 ms |
33268 KB |
Output is correct |
8 |
Correct |
33 ms |
33272 KB |
Output is correct |
9 |
Correct |
33 ms |
33244 KB |
Output is correct |
10 |
Correct |
43 ms |
34808 KB |
Output is correct |
11 |
Correct |
42 ms |
34808 KB |
Output is correct |
12 |
Correct |
41 ms |
34700 KB |
Output is correct |
13 |
Correct |
42 ms |
35064 KB |
Output is correct |
14 |
Correct |
43 ms |
35064 KB |
Output is correct |
15 |
Correct |
42 ms |
34936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
978 ms |
130376 KB |
Output is correct |
2 |
Correct |
1541 ms |
144544 KB |
Output is correct |
3 |
Correct |
1279 ms |
140892 KB |
Output is correct |
4 |
Correct |
1154 ms |
139124 KB |
Output is correct |
5 |
Correct |
1065 ms |
138968 KB |
Output is correct |
6 |
Correct |
997 ms |
138964 KB |
Output is correct |
7 |
Correct |
990 ms |
138744 KB |
Output is correct |
8 |
Correct |
1358 ms |
144544 KB |
Output is correct |
9 |
Correct |
876 ms |
140728 KB |
Output is correct |
10 |
Correct |
831 ms |
139072 KB |
Output is correct |
11 |
Correct |
913 ms |
139080 KB |
Output is correct |
12 |
Correct |
726 ms |
138952 KB |
Output is correct |
13 |
Correct |
1390 ms |
150776 KB |
Output is correct |
14 |
Correct |
1293 ms |
150756 KB |
Output is correct |
15 |
Correct |
1494 ms |
150772 KB |
Output is correct |
16 |
Correct |
1295 ms |
150776 KB |
Output is correct |
17 |
Correct |
942 ms |
138756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
33316 KB |
Output is correct |
2 |
Correct |
32 ms |
33272 KB |
Output is correct |
3 |
Correct |
33 ms |
33272 KB |
Output is correct |
4 |
Correct |
32 ms |
33272 KB |
Output is correct |
5 |
Correct |
33 ms |
33272 KB |
Output is correct |
6 |
Correct |
33 ms |
33272 KB |
Output is correct |
7 |
Correct |
35 ms |
33268 KB |
Output is correct |
8 |
Correct |
33 ms |
33272 KB |
Output is correct |
9 |
Correct |
33 ms |
33244 KB |
Output is correct |
10 |
Correct |
43 ms |
34808 KB |
Output is correct |
11 |
Correct |
42 ms |
34808 KB |
Output is correct |
12 |
Correct |
41 ms |
34700 KB |
Output is correct |
13 |
Correct |
42 ms |
35064 KB |
Output is correct |
14 |
Correct |
43 ms |
35064 KB |
Output is correct |
15 |
Correct |
42 ms |
34936 KB |
Output is correct |
16 |
Correct |
978 ms |
130376 KB |
Output is correct |
17 |
Correct |
1541 ms |
144544 KB |
Output is correct |
18 |
Correct |
1279 ms |
140892 KB |
Output is correct |
19 |
Correct |
1154 ms |
139124 KB |
Output is correct |
20 |
Correct |
1065 ms |
138968 KB |
Output is correct |
21 |
Correct |
997 ms |
138964 KB |
Output is correct |
22 |
Correct |
990 ms |
138744 KB |
Output is correct |
23 |
Correct |
1358 ms |
144544 KB |
Output is correct |
24 |
Correct |
876 ms |
140728 KB |
Output is correct |
25 |
Correct |
831 ms |
139072 KB |
Output is correct |
26 |
Correct |
913 ms |
139080 KB |
Output is correct |
27 |
Correct |
726 ms |
138952 KB |
Output is correct |
28 |
Correct |
1390 ms |
150776 KB |
Output is correct |
29 |
Correct |
1293 ms |
150756 KB |
Output is correct |
30 |
Correct |
1494 ms |
150772 KB |
Output is correct |
31 |
Correct |
1295 ms |
150776 KB |
Output is correct |
32 |
Correct |
942 ms |
138756 KB |
Output is correct |
33 |
Correct |
1300 ms |
139764 KB |
Output is correct |
34 |
Correct |
434 ms |
68900 KB |
Output is correct |
35 |
Correct |
1626 ms |
143824 KB |
Output is correct |
36 |
Correct |
1248 ms |
139808 KB |
Output is correct |
37 |
Correct |
1539 ms |
142616 KB |
Output is correct |
38 |
Correct |
1298 ms |
140776 KB |
Output is correct |
39 |
Correct |
1068 ms |
155928 KB |
Output is correct |
40 |
Correct |
1254 ms |
150984 KB |
Output is correct |
41 |
Correct |
1295 ms |
141788 KB |
Output is correct |
42 |
Correct |
1191 ms |
139892 KB |
Output is correct |
43 |
Correct |
2091 ms |
150048 KB |
Output is correct |
44 |
Correct |
1550 ms |
142692 KB |
Output is correct |
45 |
Correct |
1176 ms |
156208 KB |
Output is correct |
46 |
Correct |
1508 ms |
155860 KB |
Output is correct |
47 |
Correct |
1306 ms |
150904 KB |
Output is correct |
48 |
Correct |
1179 ms |
150788 KB |
Output is correct |
49 |
Correct |
1262 ms |
150924 KB |
Output is correct |
50 |
Correct |
1186 ms |
150876 KB |
Output is correct |
51 |
Correct |
1127 ms |
151036 KB |
Output is correct |
52 |
Correct |
1130 ms |
150956 KB |
Output is correct |