#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAX_N = 4e5 + 5;
struct Dsu {
int n;
vi loga, par, tim;
vvi dp, tree;
vi l, r;
Dsu() {}
Dsu(int n): n(n) {
par.resize(n);
iota(all(par), 0);
dp.resize(1, vi(n));
tree.resize(n);
tim.resize(n);
}
inline void Add(int t) {
par.push_back(n), dp[0].push_back(n);
tree.push_back({});
tim.push_back(t);
n++;
}
int Find(int i) {
return par[i] == i ? i : par[i] = Find(par[i]);
}
void Union(int a, int b, int t) {
int pa = Find(a), pb = Find(b);
if (pa == pb) return;
dp[0][pa] = dp[0][pb] = n;
par[pa] = par[pb] = n;
Add(t);
tree.back().push_back(pa);
tree.back().push_back(pb);
}
void PreOrder(int node, int &t) {
if (tree[node].empty()) {
l[node] = t++;
r[node] = t;
return;
}
l[node] = n, r[node] = 0;
for (int neighbor : tree[node]) {
PreOrder(neighbor, t);
chkmin(l[node], l[neighbor]), chkmax(r[node], r[neighbor]);
}
}
void Build() {
loga.resize(n + 1);
for (int i = 2; i <= n; i++) loga[i] = loga[i >> 1] + 1;
dp.resize(loga[n] + 1, vi(n));
for (int i = 1; i <= loga[n]; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
int t = 0;
l.resize(n), r.resize(n);
PreOrder(n - 1, t);
}
int Jump(int node, int t) {
for (int i = loga[n]; i >= 0; i--) {
if (tim[dp[i][node]] <= t) {
node = dp[i][node];
}
}
return node;
}
};
struct Seg {
vi seg, lp, rp;
Seg() {Add();}
inline void Add() {
seg.push_back(0), lp.push_back(0), rp.push_back(0);
}
int update(int i, int ind, int l = 0, int r = MAX_N) {
int copy = seg.size();
Add();
seg[copy] = seg[ind];
lp[copy] = lp[ind], rp[copy] = rp[ind];
if (l + 1 == r) {
seg[copy] = 1;
return copy;
}
if (i < (l + r) >> 1) {
if (!lp[ind]) {
lp[ind] = seg.size();
Add();
}
int y = update(i, lp[ind], l, (l + r) >> 1);
lp[copy] = y;
} else {
if (!rp[ind]) {
rp[ind] = seg.size();
Add();
}
int y = update(i, rp[ind],(l + r) >> 1, r);
rp[copy] = y;
}
seg[copy]++;
return copy;
}
int query(int a, int b, int ind, int l = 0, int r = MAX_N) {
if (a <= l && r <= b) return seg[ind];
if (r <= a || b <= l) return 0;
return (lp[ind] ? query(a, b, lp[ind], l, (l + r) >> 1) : 0) +
(rp[ind] ? query(a, b, rp[ind], (l + r) >> 1, r) : 0);
}
};
Seg seg = Seg();
int n;
int ind[MAX_N];
int roots[MAX_N];
inline int Sum(int x1, int y1, int x2, int y2) {
int v1 = seg.query(y1, y2, roots[x2]), v2 = seg.query(y1, y2, roots[x1]);
return v1 - v2;
}
vi check_validity(int N, vi X, vi Y, vi s, vi e, vi l, vi r) {
n = N;
int m = X.size();
iota(ind, ind + m, 0);
Dsu d_min(n), d_max(n);
sort(ind, ind + m, [&] (int i, int j) {
return max(X[i], Y[i]) < max(X[j], Y[j]);
});
for (int i = 0; i < m; i++) {
d_min.Union(X[ind[i]], Y[ind[i]], max(X[ind[i]], Y[ind[i]]));
}
sort(ind, ind + m, [&] (int i, int j) {
return min(X[i], Y[i]) > min(X[j], Y[j]);
});
for (int i = 0; i < m; i++) {
d_max.Union(X[ind[i]], Y[ind[i]], n - min(X[ind[i]], Y[ind[i]]));
}
d_min.Build(), d_max.Build();
vii points(n);
for (int i = 0; i < n; i++) {
points[i] = {d_min.l[i], d_max.l[i]};
}
sort(all(points));
for (int i = 0; i < n; i++) {
roots[i + 1] = seg.update(points[i].y, roots[i]);
}
int q = s.size();
vi ans(q);
for (int i = 0; i < q; i++) {
int t_min = r[i], t_max = n - l[i];
int v1 = d_min.Jump(e[i], t_min), v2 = d_max.Jump(s[i], t_max);
int x1 = d_min.l[v1], x2 = d_min.r[v1], y1 = d_max.l[v2], y2 = d_max.r[v2];
ans[i] = Sum(x1, y1, x2, y2) != 0;
}
return ans;
}
//6 6 3
//5 1
//1 2
//1 3
//3 4
//3 0
//5 2
//4 2 1 2
//4 2 2 2
//5 4 3 4
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
312 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 |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
312 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 |
304 KB |
Output is correct |
10 |
Correct |
10 ms |
2892 KB |
Output is correct |
11 |
Correct |
9 ms |
2904 KB |
Output is correct |
12 |
Correct |
10 ms |
2884 KB |
Output is correct |
13 |
Correct |
10 ms |
3036 KB |
Output is correct |
14 |
Correct |
9 ms |
3020 KB |
Output is correct |
15 |
Correct |
9 ms |
3016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1473 ms |
185124 KB |
Output is correct |
2 |
Correct |
1664 ms |
196352 KB |
Output is correct |
3 |
Correct |
1487 ms |
194488 KB |
Output is correct |
4 |
Correct |
1426 ms |
193732 KB |
Output is correct |
5 |
Correct |
1465 ms |
193876 KB |
Output is correct |
6 |
Correct |
1626 ms |
193556 KB |
Output is correct |
7 |
Correct |
1320 ms |
193536 KB |
Output is correct |
8 |
Correct |
1569 ms |
196596 KB |
Output is correct |
9 |
Correct |
868 ms |
194516 KB |
Output is correct |
10 |
Correct |
713 ms |
193732 KB |
Output is correct |
11 |
Correct |
705 ms |
193860 KB |
Output is correct |
12 |
Correct |
723 ms |
193752 KB |
Output is correct |
13 |
Correct |
1767 ms |
196292 KB |
Output is correct |
14 |
Correct |
1768 ms |
196324 KB |
Output is correct |
15 |
Correct |
1720 ms |
196336 KB |
Output is correct |
16 |
Correct |
1665 ms |
196312 KB |
Output is correct |
17 |
Correct |
1354 ms |
193828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
312 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 |
304 KB |
Output is correct |
10 |
Correct |
10 ms |
2892 KB |
Output is correct |
11 |
Correct |
9 ms |
2904 KB |
Output is correct |
12 |
Correct |
10 ms |
2884 KB |
Output is correct |
13 |
Correct |
10 ms |
3036 KB |
Output is correct |
14 |
Correct |
9 ms |
3020 KB |
Output is correct |
15 |
Correct |
9 ms |
3016 KB |
Output is correct |
16 |
Correct |
1473 ms |
185124 KB |
Output is correct |
17 |
Correct |
1664 ms |
196352 KB |
Output is correct |
18 |
Correct |
1487 ms |
194488 KB |
Output is correct |
19 |
Correct |
1426 ms |
193732 KB |
Output is correct |
20 |
Correct |
1465 ms |
193876 KB |
Output is correct |
21 |
Correct |
1626 ms |
193556 KB |
Output is correct |
22 |
Correct |
1320 ms |
193536 KB |
Output is correct |
23 |
Correct |
1569 ms |
196596 KB |
Output is correct |
24 |
Correct |
868 ms |
194516 KB |
Output is correct |
25 |
Correct |
713 ms |
193732 KB |
Output is correct |
26 |
Correct |
705 ms |
193860 KB |
Output is correct |
27 |
Correct |
723 ms |
193752 KB |
Output is correct |
28 |
Correct |
1767 ms |
196292 KB |
Output is correct |
29 |
Correct |
1768 ms |
196324 KB |
Output is correct |
30 |
Correct |
1720 ms |
196336 KB |
Output is correct |
31 |
Correct |
1665 ms |
196312 KB |
Output is correct |
32 |
Correct |
1354 ms |
193828 KB |
Output is correct |
33 |
Correct |
1935 ms |
194272 KB |
Output is correct |
34 |
Correct |
426 ms |
27988 KB |
Output is correct |
35 |
Correct |
2225 ms |
196596 KB |
Output is correct |
36 |
Correct |
1846 ms |
194184 KB |
Output is correct |
37 |
Correct |
2217 ms |
195912 KB |
Output is correct |
38 |
Correct |
1926 ms |
194664 KB |
Output is correct |
39 |
Correct |
2064 ms |
199388 KB |
Output is correct |
40 |
Correct |
1365 ms |
202020 KB |
Output is correct |
41 |
Correct |
1420 ms |
195456 KB |
Output is correct |
42 |
Correct |
856 ms |
194340 KB |
Output is correct |
43 |
Correct |
2183 ms |
200036 KB |
Output is correct |
44 |
Correct |
1919 ms |
195852 KB |
Output is correct |
45 |
Correct |
1484 ms |
199468 KB |
Output is correct |
46 |
Correct |
1947 ms |
199188 KB |
Output is correct |
47 |
Correct |
1841 ms |
196492 KB |
Output is correct |
48 |
Correct |
1856 ms |
196304 KB |
Output is correct |
49 |
Correct |
1889 ms |
196468 KB |
Output is correct |
50 |
Correct |
1909 ms |
196392 KB |
Output is correct |
51 |
Correct |
1314 ms |
202164 KB |
Output is correct |
52 |
Correct |
1357 ms |
202212 KB |
Output is correct |