/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
//#include "werewolf.h"
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
const int M_MAX = 400000;
const int Q_MAX = 200000;
int N, M, Q;
vector <vector <int>> adj;
struct DSU {
vector <int> ancestor;
vector <vector <int>> sons;
int cntNodes;
DSU () {
cntNodes = N;
ancestor = vector <int> (N, -1);
sons = vector <vector <int>> (N);
}
int findRoot (int u) {
return (ancestor[u] == -1 ? u : ancestor[u] = findRoot(ancestor[u]));
}
void join (int u, int v) {
u = findRoot(u);
v = findRoot(v);
if (u != v) {
int w = cntNodes++;
sons.push_back({});
ancestor.push_back(-1);
sons[w].push_back(u);
sons[w].push_back(v);
ancestor[u] = w;
ancestor[v] = w;
}
}
vector <pair <int, int>> range;
vector <int> order;
int currID;
void dfs (int u) {
if (sons[u].empty()) {
order.push_back(u);
range[u] = make_pair(currID, currID);
currID++;
} else {
range[u] = make_pair(INT_MAX, INT_MIN);
for (int v : sons[u]) {
dfs(v);
range[u].first = min(range[u].first, range[v].first);
range[u].second = max(range[u].second, range[v].second);
}
}
}
void linearize () {
range = vector <pair <int, int>> (cntNodes);
currID = 0;
dfs(cntNodes - 1);
}
};
vector <vector <int>> SGT;
void build (int node, int l, int r, vector <int> &arr) {
SGT[node].resize(r - l + 1);
if (l == r) {
SGT[node][0] = arr[l];
return;
}
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
build(lSon, l, mid, arr);
build(rSon, mid + 1, r, arr);
merge(SGT[lSon].begin(), SGT[lSon].end(), SGT[rSon].begin(), SGT[rSon].end(), SGT[node].begin());
}
void build (vector <int> &arr) {
SGT = vector <vector <int>> (N * 4);
build(1, 0, N - 1, arr);
}
bool query (int node, int l, int r, int ql, int qr, int mn, int mx) {
if (ql <= l && r <= qr) {
int lo = 0, hi = (int) SGT[node].size();
while (lo < hi) {
int avg = (lo + hi) / 2;
if (SGT[node][avg] < mn) {
lo = avg + 1;
} else {
hi = avg;
}
}
return (lo < (int) SGT[node].size() && SGT[node][lo] <= mx);
}
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
if (ql <= mid) {
if (query(lSon, l, mid, ql, qr, mn, mx) == true) {
return true;
}
}
if (mid + 1 <= qr) {
if (query(rSon, mid + 1, r, ql, qr, mn, mx) == true) {
return true;
}
}
return false;
}
bool query (int ql, int qr, int mn, int mx) {
return query(1, 0, N - 1, ql, qr, mn, mx);
}
vector <int> check_validity (int _N, vector <int> X, vector <int> Y,
vector <int> S, vector <int> E,
vector <int> L, vector <int> R) {
N = _N; M = (int) X.size(); Q = (int) S.size();
adj = vector <vector <int>> (N);
for (int i = 0; i < M; i++) {
adj[X[i]].push_back(Y[i]);
adj[Y[i]].push_back(X[i]);
}
vector <vector <int>> withL(N), withR(N);
for (int i = 0; i < Q; i++) {
withL[L[i]].push_back(i);
withR[R[i]].push_back(i);
}
vector <int> Lnode(Q), Rnode(Q);
DSU DSULarge;
for (int u = N - 1; u >= 0; u--) {
for (int v : adj[u]) {
if (u < v) {
DSULarge.join(u, v);
}
}
for (int i : withL[u]) {
Lnode[i] = DSULarge.findRoot(S[i]);
}
}
DSU DSUSmall;
for (int u = 0; u < N; u++) {
for (int v : adj[u]) {
if (v < u) {
DSUSmall.join(u, v);
}
}
for (int i : withR[u]) {
Rnode[i] = DSUSmall.findRoot(E[i]);
}
}
DSULarge.linearize();
DSUSmall.linearize();
vector <pair <int, int>> Lrange(Q), Rrange(Q);
for (int i = 0; i < Q; i++) {
Lrange[i] = DSULarge.range[Lnode[i]];
Rrange[i] = DSUSmall.range[Rnode[i]];
}
vector <int> arr(N);
for (int u = 0; u < N; u++) {
arr[DSULarge.range[u].first] = DSUSmall.range[u].first;
}
build(arr);
vector <int> answer(Q);
for (int i = 0; i < Q; i++) {
answer[i] = query(Lrange[i].first, Lrange[i].second, Rrange[i].first, Rrange[i].second);
}
return answer;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
296 KB |
Output is correct |
10 |
Correct |
7 ms |
2328 KB |
Output is correct |
11 |
Correct |
7 ms |
2260 KB |
Output is correct |
12 |
Correct |
8 ms |
2260 KB |
Output is correct |
13 |
Correct |
8 ms |
2328 KB |
Output is correct |
14 |
Correct |
6 ms |
2260 KB |
Output is correct |
15 |
Correct |
7 ms |
2352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
677 ms |
135812 KB |
Output is correct |
2 |
Correct |
529 ms |
139216 KB |
Output is correct |
3 |
Correct |
529 ms |
136988 KB |
Output is correct |
4 |
Correct |
543 ms |
136036 KB |
Output is correct |
5 |
Correct |
594 ms |
135964 KB |
Output is correct |
6 |
Correct |
646 ms |
135828 KB |
Output is correct |
7 |
Correct |
582 ms |
133520 KB |
Output is correct |
8 |
Correct |
521 ms |
139228 KB |
Output is correct |
9 |
Correct |
505 ms |
135960 KB |
Output is correct |
10 |
Correct |
502 ms |
134756 KB |
Output is correct |
11 |
Correct |
532 ms |
134816 KB |
Output is correct |
12 |
Correct |
532 ms |
135580 KB |
Output is correct |
13 |
Correct |
665 ms |
139280 KB |
Output is correct |
14 |
Correct |
653 ms |
139364 KB |
Output is correct |
15 |
Correct |
609 ms |
139412 KB |
Output is correct |
16 |
Correct |
629 ms |
139364 KB |
Output is correct |
17 |
Correct |
598 ms |
133692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
296 KB |
Output is correct |
10 |
Correct |
7 ms |
2328 KB |
Output is correct |
11 |
Correct |
7 ms |
2260 KB |
Output is correct |
12 |
Correct |
8 ms |
2260 KB |
Output is correct |
13 |
Correct |
8 ms |
2328 KB |
Output is correct |
14 |
Correct |
6 ms |
2260 KB |
Output is correct |
15 |
Correct |
7 ms |
2352 KB |
Output is correct |
16 |
Correct |
677 ms |
135812 KB |
Output is correct |
17 |
Correct |
529 ms |
139216 KB |
Output is correct |
18 |
Correct |
529 ms |
136988 KB |
Output is correct |
19 |
Correct |
543 ms |
136036 KB |
Output is correct |
20 |
Correct |
594 ms |
135964 KB |
Output is correct |
21 |
Correct |
646 ms |
135828 KB |
Output is correct |
22 |
Correct |
582 ms |
133520 KB |
Output is correct |
23 |
Correct |
521 ms |
139228 KB |
Output is correct |
24 |
Correct |
505 ms |
135960 KB |
Output is correct |
25 |
Correct |
502 ms |
134756 KB |
Output is correct |
26 |
Correct |
532 ms |
134816 KB |
Output is correct |
27 |
Correct |
532 ms |
135580 KB |
Output is correct |
28 |
Correct |
665 ms |
139280 KB |
Output is correct |
29 |
Correct |
653 ms |
139364 KB |
Output is correct |
30 |
Correct |
609 ms |
139412 KB |
Output is correct |
31 |
Correct |
629 ms |
139364 KB |
Output is correct |
32 |
Correct |
598 ms |
133692 KB |
Output is correct |
33 |
Correct |
773 ms |
137028 KB |
Output is correct |
34 |
Correct |
263 ms |
38604 KB |
Output is correct |
35 |
Correct |
704 ms |
139604 KB |
Output is correct |
36 |
Correct |
729 ms |
136516 KB |
Output is correct |
37 |
Correct |
725 ms |
138852 KB |
Output is correct |
38 |
Correct |
731 ms |
137016 KB |
Output is correct |
39 |
Correct |
627 ms |
143132 KB |
Output is correct |
40 |
Correct |
753 ms |
142688 KB |
Output is correct |
41 |
Correct |
622 ms |
138340 KB |
Output is correct |
42 |
Correct |
600 ms |
135152 KB |
Output is correct |
43 |
Correct |
659 ms |
142916 KB |
Output is correct |
44 |
Correct |
704 ms |
138820 KB |
Output is correct |
45 |
Correct |
531 ms |
142176 KB |
Output is correct |
46 |
Correct |
525 ms |
142056 KB |
Output is correct |
47 |
Correct |
660 ms |
139428 KB |
Output is correct |
48 |
Correct |
613 ms |
139340 KB |
Output is correct |
49 |
Correct |
656 ms |
139516 KB |
Output is correct |
50 |
Correct |
642 ms |
139492 KB |
Output is correct |
51 |
Correct |
736 ms |
141504 KB |
Output is correct |
52 |
Correct |
691 ms |
141540 KB |
Output is correct |