#include <bits/stdc++.h>
#include "werewolf.h"
using namespace std;
#define pi pair<int, int>
#define f first
#define s second
#define mp make_pair
#define pb push_back
vector<int> adj[200000];
vector<int> adj2[200000];
int smallIdx[200000];
int smallStart[200000];
int smallEnd[200000];
int largeIdx[200000];
int largeStart[200000];
int largeEnd[200000];
int pa[200000];
int parSmall[200000][20];
int parLarge[200000][20];
int sz[200000];
int smallest[200000];
int largest[200000];
int numPoints[200000];
int ctr = 0;
void dfs(int u, int *idx, int *start, int *end) {
start[u] = ctr;
for (int v : adj2[u]) {
dfs(v, idx, start, end);
}
end[u] = ctr;
idx[u] = ctr++;
}
int get(int x) { return x == pa[x] ? x : pa[x] = get(pa[x]); }
void unite(int a, int b) {
a = get(a), b = get(b);
if (a == b) return;
if (sz[a] > sz[b]) swap(a, b);
pa[a] = b;
sz[b] += sz[a];
if (smallest[b] > smallest[a]) {
// put b as child of a
adj2[smallest[a]].pb(smallest[b]);
parSmall[smallest[b]][0] = smallest[a];
} else {
adj2[smallest[b]].pb(smallest[a]);
parSmall[smallest[a]][0] = smallest[b];
}
smallest[b] = min(smallest[b], smallest[a]);
}
void unite2(int a, int b) {
a = get(a), b = get(b);
if (a == b) return;
if (sz[a] > sz[b]) swap(a, b);
pa[a] = b;
sz[b] += sz[a];
if (largest[b] < largest[a]) {
// put b as child of a
adj2[largest[a]].pb(largest[b]);
parLarge[largest[b]][0] = largest[a];
} else {
adj2[largest[b]].pb(largest[a]);
parLarge[largest[a]][0] = largest[b];
}
largest[b] = max(largest[b], largest[a]);
}
int ft[200001];
int qry(int x) {
x++;
int s = 0;
while (x) {
s += ft[x];
x -= x & -x;
}
return s;
}
void upd(int k, int v) {
k++;
while (k <= 200000) {
ft[k] += v;
k += k&-k;
}
}
// first two are [y1, y2]. second.f = index. second.s = multiplier.
vector<pair<pair<int, int>, pair<int, int>>> queries[200000];
vector<int> points[200000];
vector<int> check_validity(int n, vector<int> X, vector<int> Y,
vector<int> S, vector<int> E,
vector<int> L, vector<int> R) {
for (int i = 0; i < (int)X.size(); i++) {
adj[X[i]].push_back(Y[i]);
adj[Y[i]].push_back(X[i]);
}
for (int i = 0; i < n; i++) {
pa[i] = i;
sz[i] = 1;
smallest[i] = i;
}
for (int i = n-1; ~i; i--) {
for (int v : adj[i]) {
if (v > i) {
unite(v, i);
}
}
}
ctr = 0;
dfs(0, smallIdx, smallStart, smallEnd);
parSmall[0][0] = 0;
for (int i = 1; i < 20; i++) {
for (int j = 0; j < n; j++) {
parSmall[j][i] = parSmall[parSmall[j][i-1]][i-1];
}
}
for (int i = 0; i < n; i++) {
pa[i] = i;
sz[i] = 1;
largest[i] = i;
adj2[i].clear();
}
for (int i = 0; i < n; i++) {
for (int v : adj[i]) {
if (v < i) {
unite2(v, i);
}
}
}
ctr = 0;
dfs(n-1, largeIdx, largeStart, largeEnd);
parLarge[n-1][0] = n-1;
for (int i = 1; i < 20; i++) {
for (int j = 0; j < n; j++) {
parLarge[j][i] = parLarge[parLarge[j][i-1]][i-1];
}
}
vector<int> A(S.size(), 0);
for (int i = 0; i < n; i++) {
points[smallIdx[i]].pb(i);
}
for (int i = 0; i < (int)S.size(); i++) {
int u = S[i];
for (int j = 19; ~j; j--) {
if (parSmall[u][j] >= L[i]) {
u = parSmall[u][j];
}
}
int smallLeft = smallStart[u], smallRight = smallEnd[u];
u = E[i];
for (int j = 19; ~j; j--) {
if (parLarge[u][j] <= R[i]) {
u = parLarge[u][j];
}
}
int largeLeft = largeStart[u], largeRight = largeEnd[u];
queries[smallRight].pb(mp(mp(largeLeft, largeRight), mp(i, 1)));
if (smallLeft - 1 >= 0)
queries[smallLeft - 1].pb(mp(mp(largeLeft, largeRight), mp(i, -1)));
/*
for (int j = 0; j < n; j++) {
if (largeLeft <= largeIdx[j] && largeIdx[j] <= largeRight) {
if (smallLeft <= smallIdx[j] && smallIdx[j] <= smallRight) {
A[i] = 1;
}
}
}
*/
}
for (int i = 0; i < n; i++) {
for (int p : points[i]) {
upd(largeIdx[p], 1);
}
for (auto q : queries[i]) {
numPoints[q.s.f] += q.s.s*(qry(q.f.s)-qry(q.f.f-1));
}
}
for (int i = 0; i < (int)S.size(); i++) {
A[i] = numPoints[i] > 0;
}
return A;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19308 KB |
Output is correct |
2 |
Correct |
15 ms |
19308 KB |
Output is correct |
3 |
Correct |
15 ms |
19180 KB |
Output is correct |
4 |
Correct |
15 ms |
19180 KB |
Output is correct |
5 |
Correct |
13 ms |
19308 KB |
Output is correct |
6 |
Correct |
13 ms |
19308 KB |
Output is correct |
7 |
Correct |
13 ms |
19308 KB |
Output is correct |
8 |
Correct |
13 ms |
19308 KB |
Output is correct |
9 |
Correct |
14 ms |
19308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19308 KB |
Output is correct |
2 |
Correct |
15 ms |
19308 KB |
Output is correct |
3 |
Correct |
15 ms |
19180 KB |
Output is correct |
4 |
Correct |
15 ms |
19180 KB |
Output is correct |
5 |
Correct |
13 ms |
19308 KB |
Output is correct |
6 |
Correct |
13 ms |
19308 KB |
Output is correct |
7 |
Correct |
13 ms |
19308 KB |
Output is correct |
8 |
Correct |
13 ms |
19308 KB |
Output is correct |
9 |
Correct |
14 ms |
19308 KB |
Output is correct |
10 |
Correct |
19 ms |
20460 KB |
Output is correct |
11 |
Correct |
19 ms |
20460 KB |
Output is correct |
12 |
Correct |
22 ms |
20460 KB |
Output is correct |
13 |
Correct |
19 ms |
20588 KB |
Output is correct |
14 |
Correct |
19 ms |
20608 KB |
Output is correct |
15 |
Correct |
20 ms |
20460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
801 ms |
98500 KB |
Output is correct |
2 |
Correct |
782 ms |
111188 KB |
Output is correct |
3 |
Correct |
671 ms |
107500 KB |
Output is correct |
4 |
Correct |
669 ms |
105560 KB |
Output is correct |
5 |
Correct |
688 ms |
105692 KB |
Output is correct |
6 |
Correct |
741 ms |
106868 KB |
Output is correct |
7 |
Correct |
760 ms |
104656 KB |
Output is correct |
8 |
Correct |
771 ms |
111172 KB |
Output is correct |
9 |
Correct |
669 ms |
104088 KB |
Output is correct |
10 |
Correct |
546 ms |
105324 KB |
Output is correct |
11 |
Correct |
571 ms |
104904 KB |
Output is correct |
12 |
Correct |
597 ms |
104912 KB |
Output is correct |
13 |
Correct |
880 ms |
110172 KB |
Output is correct |
14 |
Correct |
844 ms |
110300 KB |
Output is correct |
15 |
Correct |
843 ms |
110172 KB |
Output is correct |
16 |
Correct |
844 ms |
110112 KB |
Output is correct |
17 |
Correct |
808 ms |
104372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19308 KB |
Output is correct |
2 |
Correct |
15 ms |
19308 KB |
Output is correct |
3 |
Correct |
15 ms |
19180 KB |
Output is correct |
4 |
Correct |
15 ms |
19180 KB |
Output is correct |
5 |
Correct |
13 ms |
19308 KB |
Output is correct |
6 |
Correct |
13 ms |
19308 KB |
Output is correct |
7 |
Correct |
13 ms |
19308 KB |
Output is correct |
8 |
Correct |
13 ms |
19308 KB |
Output is correct |
9 |
Correct |
14 ms |
19308 KB |
Output is correct |
10 |
Correct |
19 ms |
20460 KB |
Output is correct |
11 |
Correct |
19 ms |
20460 KB |
Output is correct |
12 |
Correct |
22 ms |
20460 KB |
Output is correct |
13 |
Correct |
19 ms |
20588 KB |
Output is correct |
14 |
Correct |
19 ms |
20608 KB |
Output is correct |
15 |
Correct |
20 ms |
20460 KB |
Output is correct |
16 |
Correct |
801 ms |
98500 KB |
Output is correct |
17 |
Correct |
782 ms |
111188 KB |
Output is correct |
18 |
Correct |
671 ms |
107500 KB |
Output is correct |
19 |
Correct |
669 ms |
105560 KB |
Output is correct |
20 |
Correct |
688 ms |
105692 KB |
Output is correct |
21 |
Correct |
741 ms |
106868 KB |
Output is correct |
22 |
Correct |
760 ms |
104656 KB |
Output is correct |
23 |
Correct |
771 ms |
111172 KB |
Output is correct |
24 |
Correct |
669 ms |
104088 KB |
Output is correct |
25 |
Correct |
546 ms |
105324 KB |
Output is correct |
26 |
Correct |
571 ms |
104904 KB |
Output is correct |
27 |
Correct |
597 ms |
104912 KB |
Output is correct |
28 |
Correct |
880 ms |
110172 KB |
Output is correct |
29 |
Correct |
844 ms |
110300 KB |
Output is correct |
30 |
Correct |
843 ms |
110172 KB |
Output is correct |
31 |
Correct |
844 ms |
110112 KB |
Output is correct |
32 |
Correct |
808 ms |
104372 KB |
Output is correct |
33 |
Correct |
872 ms |
108484 KB |
Output is correct |
34 |
Correct |
325 ms |
55916 KB |
Output is correct |
35 |
Correct |
959 ms |
111580 KB |
Output is correct |
36 |
Correct |
874 ms |
107800 KB |
Output is correct |
37 |
Correct |
910 ms |
110700 KB |
Output is correct |
38 |
Correct |
891 ms |
108524 KB |
Output is correct |
39 |
Correct |
864 ms |
119916 KB |
Output is correct |
40 |
Correct |
899 ms |
113996 KB |
Output is correct |
41 |
Correct |
743 ms |
108752 KB |
Output is correct |
42 |
Correct |
624 ms |
102100 KB |
Output is correct |
43 |
Correct |
920 ms |
115564 KB |
Output is correct |
44 |
Correct |
843 ms |
110228 KB |
Output is correct |
45 |
Correct |
767 ms |
118376 KB |
Output is correct |
46 |
Correct |
761 ms |
118304 KB |
Output is correct |
47 |
Correct |
886 ms |
110300 KB |
Output is correct |
48 |
Correct |
890 ms |
110220 KB |
Output is correct |
49 |
Correct |
884 ms |
110340 KB |
Output is correct |
50 |
Correct |
862 ms |
111692 KB |
Output is correct |
51 |
Correct |
879 ms |
115700 KB |
Output is correct |
52 |
Correct |
799 ms |
115820 KB |
Output is correct |