#include <bits/stdc++.h>
using namespace std;
const int LOG = 18;
struct unionfind{
vector<int> p;
unionfind(int N): p(N, -1){
}
int root(int x){
if (p[x] == -1){
return x;
} else {
p[x] = root(p[x]);
return p[x];
}
}
bool same(int x, int y){
return root(x) == root(y);
}
void unite(int x, int y){
x = root(x);
y = root(y);
if (x != y){
p[x] = y;
}
}
};
void dfs(vector<vector<int>> &c, vector<int> &L, vector<int> &R, int &t, int v){
L[v] = t;
t++;
for (int w : c[v]){
dfs(c, L, R, t, w);
}
R[v] = t;
}
struct binary_indexed_tree{
int N;
vector<int> BIT;
binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
}
void add(int i){
i++;
while (i <= N){
BIT[i]++;
i += i & -i;
}
}
int sum(int i){
int ans = 0;
while (i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
int sum(int L, int R){
return sum(R) - sum(L);
}
};
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 M = X.size();
int Q = S.size();
vector<vector<int>> G(N);
for (int i = 0; i < M; i++){
G[X[i]].push_back(Y[i]);
G[Y[i]].push_back(X[i]);
}
unionfind UF1(N);
vector<int> p1(N, -1);
vector<vector<int>> c1(N);
for (int i = N - 1; i >= 0; i--){
for (int j : G[i]){
if (j > i){
int x = UF1.root(j);
if (x != i){
p1[x] = i;
c1[i].push_back(x);
UF1.unite(x, i);
}
}
}
}
unionfind UF2(N);
vector<int> p2(N, -1);
vector<vector<int>> c2(N);
for (int i = 0; i < N; i++){
for (int j : G[i]){
if (j < i){
int x = UF2.root(j);
if (x != i){
p2[x] = i;
c2[i].push_back(x);
UF2.unite(x, i);
}
}
}
}
vector<int> L1(N), R1(N);
int t1 = 0;
dfs(c1, L1, R1, t1, 0);
vector<int> L2(N), R2(N);
int t2 = 0;
dfs(c2, L2, R2, t2, N - 1);
vector<vector<int>> pp1(LOG, vector<int>(N, -1));
pp1[0] = p1;
for (int i = 0; i < LOG - 1; i++){
for (int j = 0; j < N; j++){
if (pp1[i][j] != -1){
pp1[i + 1][j] = pp1[i][pp1[i][j]];
}
}
}
vector<vector<int>> pp2(LOG, vector<int>(N, -1));
pp2[0] = p2;
for (int i = 0; i < LOG - 1; i++){
for (int j = 0; j < N; j++){
if (pp2[i][j] != -1){
pp2[i + 1][j] = pp2[i][pp2[i][j]];
}
}
}
vector<int> X1(Q), X2(Q), Y1(Q), Y2(Q);
for (int i = 0; i < Q; i++){
int v1 = S[i];
for (int j = LOG - 1; j >= 0; j--){
if (pp1[j][v1] >= L[i]){
v1 = pp1[j][v1];
}
}
X1[i] = L1[v1];
X2[i] = R1[v1];
int v2 = E[i];
for (int j = LOG - 1; j >= 0; j--){
if (pp2[j][v2] != -1 && pp2[j][v2] <= R[i]){
v2 = pp2[j][v2];
}
}
Y1[i] = L2[v2];
Y2[i] = R2[v2];
}
vector<vector<int>> query_add(N + 1), query_sub(N + 1);
for (int i = 0; i < Q; i++){
query_sub[X1[i]].push_back(i);
query_add[X2[i]].push_back(i);
}
vector<vector<int>> upd(N + 1);
for (int i = 0; i < N; i++){
upd[L1[i]].push_back(L2[i]);
}
binary_indexed_tree BIT(N);
vector<int> ans(Q, 0);
for (int i = 0; i <= N; i++){
for (int j : query_add[i]){
ans[j] += BIT.sum(Y1[j], Y2[j]);
}
for (int j : query_sub[i]){
ans[j] -= BIT.sum(Y1[j], Y2[j]);
}
for (int j : upd[i]){
BIT.add(j);
}
}
for (int i = 0; i < Q; i++){
ans[i] = min(ans[i], 1);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
1876 KB |
Output is correct |
11 |
Correct |
7 ms |
1776 KB |
Output is correct |
12 |
Correct |
7 ms |
1816 KB |
Output is correct |
13 |
Correct |
5 ms |
2004 KB |
Output is correct |
14 |
Correct |
6 ms |
2004 KB |
Output is correct |
15 |
Correct |
6 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
578 ms |
102908 KB |
Output is correct |
2 |
Correct |
635 ms |
114720 KB |
Output is correct |
3 |
Correct |
604 ms |
110728 KB |
Output is correct |
4 |
Correct |
561 ms |
109424 KB |
Output is correct |
5 |
Correct |
533 ms |
109628 KB |
Output is correct |
6 |
Correct |
591 ms |
110652 KB |
Output is correct |
7 |
Correct |
539 ms |
109692 KB |
Output is correct |
8 |
Correct |
619 ms |
114720 KB |
Output is correct |
9 |
Correct |
407 ms |
110140 KB |
Output is correct |
10 |
Correct |
394 ms |
108652 KB |
Output is correct |
11 |
Correct |
440 ms |
108656 KB |
Output is correct |
12 |
Correct |
402 ms |
108316 KB |
Output is correct |
13 |
Correct |
576 ms |
122184 KB |
Output is correct |
14 |
Correct |
627 ms |
122248 KB |
Output is correct |
15 |
Correct |
642 ms |
122108 KB |
Output is correct |
16 |
Correct |
628 ms |
122132 KB |
Output is correct |
17 |
Correct |
545 ms |
109756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
6 ms |
1876 KB |
Output is correct |
11 |
Correct |
7 ms |
1776 KB |
Output is correct |
12 |
Correct |
7 ms |
1816 KB |
Output is correct |
13 |
Correct |
5 ms |
2004 KB |
Output is correct |
14 |
Correct |
6 ms |
2004 KB |
Output is correct |
15 |
Correct |
6 ms |
1876 KB |
Output is correct |
16 |
Correct |
578 ms |
102908 KB |
Output is correct |
17 |
Correct |
635 ms |
114720 KB |
Output is correct |
18 |
Correct |
604 ms |
110728 KB |
Output is correct |
19 |
Correct |
561 ms |
109424 KB |
Output is correct |
20 |
Correct |
533 ms |
109628 KB |
Output is correct |
21 |
Correct |
591 ms |
110652 KB |
Output is correct |
22 |
Correct |
539 ms |
109692 KB |
Output is correct |
23 |
Correct |
619 ms |
114720 KB |
Output is correct |
24 |
Correct |
407 ms |
110140 KB |
Output is correct |
25 |
Correct |
394 ms |
108652 KB |
Output is correct |
26 |
Correct |
440 ms |
108656 KB |
Output is correct |
27 |
Correct |
402 ms |
108316 KB |
Output is correct |
28 |
Correct |
576 ms |
122184 KB |
Output is correct |
29 |
Correct |
627 ms |
122248 KB |
Output is correct |
30 |
Correct |
642 ms |
122108 KB |
Output is correct |
31 |
Correct |
628 ms |
122132 KB |
Output is correct |
32 |
Correct |
545 ms |
109756 KB |
Output is correct |
33 |
Correct |
597 ms |
112364 KB |
Output is correct |
34 |
Correct |
259 ms |
36672 KB |
Output is correct |
35 |
Correct |
677 ms |
116504 KB |
Output is correct |
36 |
Correct |
630 ms |
112280 KB |
Output is correct |
37 |
Correct |
676 ms |
115240 KB |
Output is correct |
38 |
Correct |
605 ms |
113224 KB |
Output is correct |
39 |
Correct |
754 ms |
129036 KB |
Output is correct |
40 |
Correct |
670 ms |
121568 KB |
Output is correct |
41 |
Correct |
470 ms |
112260 KB |
Output is correct |
42 |
Correct |
443 ms |
109516 KB |
Output is correct |
43 |
Correct |
677 ms |
121372 KB |
Output is correct |
44 |
Correct |
612 ms |
113672 KB |
Output is correct |
45 |
Correct |
566 ms |
125884 KB |
Output is correct |
46 |
Correct |
528 ms |
125604 KB |
Output is correct |
47 |
Correct |
627 ms |
122272 KB |
Output is correct |
48 |
Correct |
657 ms |
122044 KB |
Output is correct |
49 |
Correct |
663 ms |
122416 KB |
Output is correct |
50 |
Correct |
711 ms |
122124 KB |
Output is correct |
51 |
Correct |
620 ms |
120496 KB |
Output is correct |
52 |
Correct |
653 ms |
120552 KB |
Output is correct |