#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> g[200000];
int e[200000];
int par1[200000][20], par2[200000][20];
int find(int u)
{
return e[u] == u ? u : e[u] = find(e[u]);
}
bool vis1[200000] = {false}, vis2[200000] = {false};
int euler1[200000], euler2[200000];
int in1[200000], out1[200000], t1 = 0;
int in2[200000], out2[200000], t2 = 0;
vector<int> c1[200000], c2[200000];
void dfs1(int u)
{
vis1[u] = true;
in1[u] = t1++;
for (int v : c1[u])
if (!vis1[v])
dfs1(v);
out1[u] = t1;
}
void dfs2(int u)
{
vis2[u] = true;
in2[u] = t2++;
for (int v : c2[u])
if (!vis2[v])
dfs2(v);
out2[u] = t2;
}
const int BS = 450;
struct Query
{
int l1, r1, l2, r2, t;
};
bool operator<(const Query &A, const Query &B)
{
if (A.l1 / BS < B.l1 / BS) return true;
if (A.l1 / BS > B.l1 / BS) return false;
if ((A.l1 / BS) % 2) return A.r1 < B.r1;
return A.r1 > B.r1;
}
int t[200000] = {0};
void update(int N, int idx, int val)
{
for (; idx < N; idx |= (idx + 1))
t[idx] += val;
}
int quer(int r)
{
int res = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
res += t[r];
return res;
}
int query(int l, int r)
{
return quer(r) - quer(l - 1);
}
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 Q = S.size(), M = X.size();
vector<int> A(Q, 0);
for (int i = 0; i < N; i++)
par1[i][0] = par2[i][0] = i;
for (int i = 0; i < M; i++)
{
g[X[i]].push_back(Y[i]);
g[Y[i]].push_back(X[i]);
}
for (int i = 0; i < N; i++)
e[i] = i;
for (int u = 0; u < N; u++)
for (int v : g[u])
if (v < u)
{
v = find(v);
e[v] = u;
par1[v][0] = u;
}
for (int i = 0; i < N; i++)
e[i] = i;
for (int u = N - 1; u >= 0; u--)
for (int v : g[u])
if (v > u)
{
v = find(v);
e[v] = u;
par2[v][0] = u;
}
for (int k = 1; k < 20; k++)
for (int u = 0; u < N; u++)
{
par1[u][k] = par1[par1[u][k-1]][k-1];
par2[u][k] = par2[par2[u][k-1]][k-1];
}
for (int i = 0; i < N; i++)
{
c1[par1[i][0]].push_back(i);
c2[par2[i][0]].push_back(i);
}
for (int i = N - 1; i >= 0; i--)
if (!vis1[i])
dfs1(i);
for (int i = 0; i < N; i++)
if (!vis2[i])
dfs2(i);
/*
for (int i = 0; i < N; i++)
cout << par1[i][0] << ' ';
cout << '\n';
for (int i = 0; i < N; i++)
cout << par2[i][0] << ' ';
cout << '\n';
for (int i = 0; i < N; i++)
cout << in1[i] << ' ';
cout << '\n';
for (int i = 0; i < N; i++)
cout << in2[i] << ' ';
cout << '\n';
*/
vector<Query> queries(Q);
for (int i = 0; i < Q; i++)
{
for (int k = 19; k >= 0; k--)
{
if (par1[E[i]][k] <= R[i])
E[i] = par1[E[i]][k];
if (par2[S[i]][k] >= L[i])
S[i] = par2[S[i]][k];
}
queries[i] = {in1[E[i]], out1[E[i]]-1, in2[S[i]], out2[S[i]]-1, i};
}
for (int i = 0; i < N; i++)
{
euler1[in1[i]] = i;
euler2[in2[i]] = i;
}
sort(queries.begin(), queries.end());
int l = 0, r = -1;
for (auto [l1, r1, l2, r2, t] : queries)
{
while (r < r1)
update(N, in2[euler1[++r]], 1);
while (l > l1)
update(N, in2[euler1[--l]], 1);
while (r > r1)
update(N, in2[euler1[r--]], -1);
while (l < l1)
update(N, in2[euler1[l++]], -1);
A[t] = (query(l2, r2) > 0);
}
return A;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14456 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14456 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
12 ms |
15488 KB |
Output is correct |
11 |
Correct |
12 ms |
15444 KB |
Output is correct |
12 |
Correct |
13 ms |
15372 KB |
Output is correct |
13 |
Correct |
16 ms |
15548 KB |
Output is correct |
14 |
Correct |
13 ms |
15572 KB |
Output is correct |
15 |
Correct |
12 ms |
15576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
81176 KB |
Output is correct |
2 |
Correct |
477 ms |
84556 KB |
Output is correct |
3 |
Correct |
671 ms |
82124 KB |
Output is correct |
4 |
Correct |
727 ms |
81296 KB |
Output is correct |
5 |
Correct |
721 ms |
81332 KB |
Output is correct |
6 |
Correct |
693 ms |
81132 KB |
Output is correct |
7 |
Correct |
418 ms |
81080 KB |
Output is correct |
8 |
Correct |
446 ms |
84520 KB |
Output is correct |
9 |
Correct |
344 ms |
82236 KB |
Output is correct |
10 |
Correct |
352 ms |
81228 KB |
Output is correct |
11 |
Correct |
398 ms |
81228 KB |
Output is correct |
12 |
Correct |
371 ms |
81100 KB |
Output is correct |
13 |
Correct |
627 ms |
90020 KB |
Output is correct |
14 |
Correct |
693 ms |
89916 KB |
Output is correct |
15 |
Correct |
665 ms |
89912 KB |
Output is correct |
16 |
Correct |
620 ms |
89908 KB |
Output is correct |
17 |
Correct |
422 ms |
81128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14456 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14404 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
6 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14420 KB |
Output is correct |
10 |
Correct |
12 ms |
15488 KB |
Output is correct |
11 |
Correct |
12 ms |
15444 KB |
Output is correct |
12 |
Correct |
13 ms |
15372 KB |
Output is correct |
13 |
Correct |
16 ms |
15548 KB |
Output is correct |
14 |
Correct |
13 ms |
15572 KB |
Output is correct |
15 |
Correct |
12 ms |
15576 KB |
Output is correct |
16 |
Correct |
446 ms |
81176 KB |
Output is correct |
17 |
Correct |
477 ms |
84556 KB |
Output is correct |
18 |
Correct |
671 ms |
82124 KB |
Output is correct |
19 |
Correct |
727 ms |
81296 KB |
Output is correct |
20 |
Correct |
721 ms |
81332 KB |
Output is correct |
21 |
Correct |
693 ms |
81132 KB |
Output is correct |
22 |
Correct |
418 ms |
81080 KB |
Output is correct |
23 |
Correct |
446 ms |
84520 KB |
Output is correct |
24 |
Correct |
344 ms |
82236 KB |
Output is correct |
25 |
Correct |
352 ms |
81228 KB |
Output is correct |
26 |
Correct |
398 ms |
81228 KB |
Output is correct |
27 |
Correct |
371 ms |
81100 KB |
Output is correct |
28 |
Correct |
627 ms |
90020 KB |
Output is correct |
29 |
Correct |
693 ms |
89916 KB |
Output is correct |
30 |
Correct |
665 ms |
89912 KB |
Output is correct |
31 |
Correct |
620 ms |
89908 KB |
Output is correct |
32 |
Correct |
422 ms |
81128 KB |
Output is correct |
33 |
Correct |
1045 ms |
81548 KB |
Output is correct |
34 |
Correct |
350 ms |
49932 KB |
Output is correct |
35 |
Correct |
1602 ms |
92808 KB |
Output is correct |
36 |
Correct |
1271 ms |
90192 KB |
Output is correct |
37 |
Correct |
1540 ms |
91848 KB |
Output is correct |
38 |
Correct |
1420 ms |
90820 KB |
Output is correct |
39 |
Correct |
941 ms |
100288 KB |
Output is correct |
40 |
Correct |
843 ms |
100372 KB |
Output is correct |
41 |
Correct |
589 ms |
91264 KB |
Output is correct |
42 |
Correct |
480 ms |
90180 KB |
Output is correct |
43 |
Correct |
855 ms |
97940 KB |
Output is correct |
44 |
Correct |
1248 ms |
91832 KB |
Output is correct |
45 |
Correct |
869 ms |
100860 KB |
Output is correct |
46 |
Correct |
867 ms |
100260 KB |
Output is correct |
47 |
Correct |
627 ms |
98532 KB |
Output is correct |
48 |
Correct |
643 ms |
98392 KB |
Output is correct |
49 |
Correct |
649 ms |
98504 KB |
Output is correct |
50 |
Correct |
660 ms |
98376 KB |
Output is correct |
51 |
Correct |
583 ms |
100156 KB |
Output is correct |
52 |
Correct |
607 ms |
100144 KB |
Output is correct |