#include "werewolf.h"
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pii pair <int, int>
#define pb push_back
const int N = 4e5 + 5;
int par[N][2],cnt[2],tin[2],in[N][2], out[N][2], tree[4 * N];
vector <int> v[N][2];
int get_col(int a, int ty) {
if (a == par[a][ty]) return par[a][ty];
else return par[a][ty] = get_col(par[a][ty],ty);
}
void merge(int a, int b, int ty) {
a = get_col(a, ty);
b = get_col(b, ty);
if (a == b) return ;
cnt[ty]++;
par[a][ty] = cnt[ty];
par[b][ty] = cnt[ty];
par[cnt[ty]][ty] = cnt[ty];
v[cnt[ty]][ty].pb(a);
v[cnt[ty]][ty].pb(b);
}
void dfs(int a, int p, int ty) {
tin[ty]++;
in[a][ty] = tin[ty];
//cout<<a<<" "<<in[a][ty]<<"\n";
for (int to : v[a][ty]) {
if (to == p) continue;
// cout<<ty<<" --- > "<<a<<" "<<to<<"\n";
dfs(to, a, ty);
}
out[a][ty] = tin[ty];
}
void dfs1(int a, int p, int ty) {
tin[ty]++;
in[a][ty] = tin[ty];
// cout<<a<<" "<<in[a][ty]<<"\n";
for (int to : v[a][ty]) {
if (to == p) continue;
dfs1(to, a, ty);
}
out[a][ty] = tin[ty];
}
void update(int node, int le, int ri, int idx, int val) {
if (le > idx || ri < idx) return ;
if (le == ri) {
tree[node] = max(tree[node],val);
return ;
}
int mid = (le + ri) / 2;
update(2 * node, le,mid,idx,val);
update(2 * node + 1, mid + 1, ri, idx,val);
tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
int getans(int node, int le, int ri, int start, int end) {
if (le > end || ri < start) return -1;
if (le >= start && ri <= end) {
return tree[node];
}
int mid = (le + ri) / 2;
return max(getans(2 * node, le,mid,start, end), getans(2 * node + 1, mid + 1, ri, start, end));
}
vector <int> vec[N],quer[N];
vector <pii> queries[N],add[N];
pii que[N];
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();
int m = x.size();
for (int i = 0; i < m; i++) {
vec[x[i]].pb(y[i]);
vec[y[i]].pb(x[i]);
}
// First Tree
cnt[0] = n - 1; cnt[1] = n - 1;
for (int i = 0; i < q; i++) {
queries[l[i]].pb({s[i], i});
}
for (int i = 0; i < n; i++) { par[i][0] = i;}
for (int i = n - 1; i >= 0; i--) {
for (int to : vec[i]) {
if (to >= i) {
merge(to, i, 0);
}
}
for (pii sth : queries[i]) {
int strt = sth.f; int idx = sth.s;
que[idx].f = get_col(strt, 0);
}
}
// Second Tree
for (int i = 0; i < n; i++) { par[i][1] = i;}
for (int i = 0; i <= n - 1; i++) {
queries[i].clear();
}
for (int i = 0; i < q; i++) {
queries[r[i]].pb({e[i], i});
}
for (int i = 0; i <= n - 1; i++) {
for (int to : vec[i]) {
if (to <= i) {
merge(to, i, 1);
}
}
for (pii sth : queries[i]) {
int strt = sth.f; int idx = sth.s;
que[idx].s = get_col(strt, 1);
}
}
for (int i = 0; i <= cnt[0]; i++) {
if (get_col(i, 0) == i) {
v[cnt[0] + 1][0].pb(i);
}
}
dfs(cnt[0] + 1, -1, 0);
for (int i = 0; i <= cnt[1]; i++) {
if (get_col(i, 1) == i) {
v[cnt[1] + 1][1].pb(i);
}
}
dfs1(cnt[1] + 1, -1, 1);
int mx = 0;
for (int i = 0; i < q; i++) {
quer[out[que[i].f][0]].pb(i);
mx = max(mx, out[que[i].f][0] + 1);
}
for (int i = 0; i < n; i++) {
add[out[i][0]].pb({in[i][1], in[i][0]});
mx = max(mx, in[i][1]);
}
vector<int> ans(q);
for (int i = 0; i < 4 * N; i++) tree[i] = -1;
for (int i = 0; i < mx; i++) {
for (pii sth : add[i]) {
update(1,0,mx,sth.f, sth.s);
}
for (int id : quer[i]) {
if (getans(1, 0, mx, in[que[id].s][1], out[que[id].s][1]) >= in[que[id].f][0]) {
ans[id] = 1;
} else ans[id] = 0;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62968 KB |
Output is correct |
2 |
Correct |
28 ms |
62968 KB |
Output is correct |
3 |
Correct |
30 ms |
63060 KB |
Output is correct |
4 |
Correct |
31 ms |
62916 KB |
Output is correct |
5 |
Correct |
29 ms |
62948 KB |
Output is correct |
6 |
Correct |
31 ms |
62920 KB |
Output is correct |
7 |
Correct |
28 ms |
62952 KB |
Output is correct |
8 |
Correct |
30 ms |
62892 KB |
Output is correct |
9 |
Correct |
28 ms |
62948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62968 KB |
Output is correct |
2 |
Correct |
28 ms |
62968 KB |
Output is correct |
3 |
Correct |
30 ms |
63060 KB |
Output is correct |
4 |
Correct |
31 ms |
62916 KB |
Output is correct |
5 |
Correct |
29 ms |
62948 KB |
Output is correct |
6 |
Correct |
31 ms |
62920 KB |
Output is correct |
7 |
Correct |
28 ms |
62952 KB |
Output is correct |
8 |
Correct |
30 ms |
62892 KB |
Output is correct |
9 |
Correct |
28 ms |
62948 KB |
Output is correct |
10 |
Correct |
36 ms |
63988 KB |
Output is correct |
11 |
Correct |
35 ms |
63892 KB |
Output is correct |
12 |
Correct |
36 ms |
63788 KB |
Output is correct |
13 |
Correct |
34 ms |
64020 KB |
Output is correct |
14 |
Correct |
32 ms |
64016 KB |
Output is correct |
15 |
Correct |
36 ms |
64044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
698 ms |
127052 KB |
Output is correct |
2 |
Correct |
556 ms |
133304 KB |
Output is correct |
3 |
Correct |
584 ms |
128224 KB |
Output is correct |
4 |
Correct |
593 ms |
126028 KB |
Output is correct |
5 |
Correct |
581 ms |
126092 KB |
Output is correct |
6 |
Correct |
636 ms |
126772 KB |
Output is correct |
7 |
Correct |
598 ms |
124588 KB |
Output is correct |
8 |
Correct |
522 ms |
133172 KB |
Output is correct |
9 |
Correct |
599 ms |
127148 KB |
Output is correct |
10 |
Correct |
495 ms |
125292 KB |
Output is correct |
11 |
Correct |
516 ms |
125388 KB |
Output is correct |
12 |
Correct |
559 ms |
125116 KB |
Output is correct |
13 |
Correct |
586 ms |
134724 KB |
Output is correct |
14 |
Correct |
560 ms |
134884 KB |
Output is correct |
15 |
Correct |
566 ms |
134764 KB |
Output is correct |
16 |
Correct |
570 ms |
134988 KB |
Output is correct |
17 |
Correct |
587 ms |
124496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62968 KB |
Output is correct |
2 |
Correct |
28 ms |
62968 KB |
Output is correct |
3 |
Correct |
30 ms |
63060 KB |
Output is correct |
4 |
Correct |
31 ms |
62916 KB |
Output is correct |
5 |
Correct |
29 ms |
62948 KB |
Output is correct |
6 |
Correct |
31 ms |
62920 KB |
Output is correct |
7 |
Correct |
28 ms |
62952 KB |
Output is correct |
8 |
Correct |
30 ms |
62892 KB |
Output is correct |
9 |
Correct |
28 ms |
62948 KB |
Output is correct |
10 |
Correct |
36 ms |
63988 KB |
Output is correct |
11 |
Correct |
35 ms |
63892 KB |
Output is correct |
12 |
Correct |
36 ms |
63788 KB |
Output is correct |
13 |
Correct |
34 ms |
64020 KB |
Output is correct |
14 |
Correct |
32 ms |
64016 KB |
Output is correct |
15 |
Correct |
36 ms |
64044 KB |
Output is correct |
16 |
Correct |
698 ms |
127052 KB |
Output is correct |
17 |
Correct |
556 ms |
133304 KB |
Output is correct |
18 |
Correct |
584 ms |
128224 KB |
Output is correct |
19 |
Correct |
593 ms |
126028 KB |
Output is correct |
20 |
Correct |
581 ms |
126092 KB |
Output is correct |
21 |
Correct |
636 ms |
126772 KB |
Output is correct |
22 |
Correct |
598 ms |
124588 KB |
Output is correct |
23 |
Correct |
522 ms |
133172 KB |
Output is correct |
24 |
Correct |
599 ms |
127148 KB |
Output is correct |
25 |
Correct |
495 ms |
125292 KB |
Output is correct |
26 |
Correct |
516 ms |
125388 KB |
Output is correct |
27 |
Correct |
559 ms |
125116 KB |
Output is correct |
28 |
Correct |
586 ms |
134724 KB |
Output is correct |
29 |
Correct |
560 ms |
134884 KB |
Output is correct |
30 |
Correct |
566 ms |
134764 KB |
Output is correct |
31 |
Correct |
570 ms |
134988 KB |
Output is correct |
32 |
Correct |
587 ms |
124496 KB |
Output is correct |
33 |
Correct |
677 ms |
129200 KB |
Output is correct |
34 |
Correct |
286 ms |
100568 KB |
Output is correct |
35 |
Correct |
675 ms |
134504 KB |
Output is correct |
36 |
Correct |
678 ms |
128468 KB |
Output is correct |
37 |
Correct |
678 ms |
133268 KB |
Output is correct |
38 |
Correct |
692 ms |
129384 KB |
Output is correct |
39 |
Correct |
665 ms |
143244 KB |
Output is correct |
40 |
Correct |
684 ms |
135952 KB |
Output is correct |
41 |
Correct |
625 ms |
130792 KB |
Output is correct |
42 |
Correct |
570 ms |
125892 KB |
Output is correct |
43 |
Correct |
736 ms |
138324 KB |
Output is correct |
44 |
Correct |
677 ms |
131960 KB |
Output is correct |
45 |
Correct |
626 ms |
141196 KB |
Output is correct |
46 |
Correct |
612 ms |
141024 KB |
Output is correct |
47 |
Correct |
593 ms |
135008 KB |
Output is correct |
48 |
Correct |
608 ms |
134840 KB |
Output is correct |
49 |
Correct |
597 ms |
134892 KB |
Output is correct |
50 |
Correct |
585 ms |
134988 KB |
Output is correct |
51 |
Correct |
662 ms |
134940 KB |
Output is correct |
52 |
Correct |
662 ms |
134924 KB |
Output is correct |