#define SHINLENA2
#ifndef SHINLENA
#include "werewolf.h"
#endif
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cstddef>
#include <cassert>
using namespace std;
#define NN 200000
struct kruskal_reconstruction_tree
{
vector<int> p, tin, tout;
int l, n0;
vector<vector<int>> g;
vector<pair<int, int>> ch;
kruskal_reconstruction_tree(int n, int n0) : p(n), tin(n), tout(n), l(n0), n0(n0), g(n), ch(n, {-1,-1})
{
iota(p.begin(), p.end(), 0);
}
int find(int x)
{
return x == p[x] ? x : p[x] = find(p[x]);
}
int unite(int x, int y)
{
x = find(x); y = find(y);
if (x == y) return 0;
ch[p[x] = p[y] = ++l] = {x, y};
return 1;
}
int timer = 0;
void dfs(int u)
{
tin[u] = timer++;
for (auto v : {ch[u].first, ch[u].second}) if (v != -1) dfs(v);
tout[u] = timer - 1;
}
void tour()
{
for (int i = l +1; i--;) if (!tin[i]) dfs(i);
}
};
vector<int> t[NN<<3];
void add(int v, int l, int r, int p, int k)
{
t[v].push_back(k);
if (l == r) return;
if (p <= (l+r)/2) add(2*v+1, l, (l+r)/2, p, k);
else add(2*v+2, (l+r)/2+1, r, p, k);
}
int qry(int v, int l, int r, int x, int y, int k)
{
if (r < x || y < l) return 1e9;
if (x <= l && r <= y)
{
auto it = lower_bound(t[v].begin(), t[v].end(), k);
return it == t[v].end() ? 1e9 : *it;
}
return min(qry(2*v+1, l, (l+r)/2, x, y, k), qry(2*v+2, (l+r)/2+1, r, x, y, k));
}
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();
kruskal_reconstruction_tree k[2] {kruskal_reconstruction_tree(N+M, N-1), kruskal_reconstruction_tree(N+M, N-1)};
int Q = S.size();
vector<int> A(Q);
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]);
/* build krt */
vector<int> rtl(Q), rtr(Q);
{
vector<vector<int>> byl(N), byr(N);
for (int i = 0; i < Q; ++i) byl[L[i]].push_back(i), byr[R[i]].push_back(i);
for (int i = N; i--;)
{
for (auto v : g[i]) if (v >= i) k[0].unite(v, i);
for (auto j : byl[i]) rtl[j] = k[0].find(S[j]);
}
for (int i = 0; i < N; ++i)
{
for (auto v : g[i]) if (v <= i) k[1].unite(v, i);
for (auto j : byr[i]) rtr[j] = k[1].find(E[j]);
}
}
for (int i : {0, 1}) k[i].tour();
int LL = k[0].timer - 1;
/* build merge sort tree */
{
vector<pair<int, int>> ins;
for (int i = 0; i < N; ++i)
ins.emplace_back(k[1].tin[i], k[0].tin[i]);
sort(ins.begin(), ins.end());
for (auto [x, y] : ins) add(0, 0, LL, y, x);
}
for (int i = 0; i < Q; ++i)
{
A[i] = qry(0, 0, LL, k[0].tin[rtl[i]], k[0].tout[rtl[i]], k[1].tin[rtr[i]]) <= k[1].tout[rtr[i]];
}
return A;
}
#ifdef SHINLENA
int main()
{
int n,m,q;
cin>>n>>m>>q;
vector<int>x,y,s,e,l,r;
for (int u,v,i=0;i<m;++i)cin>>u>>v, x.push_back(u), y.push_back(v);
for (int a,b,c,d,i=0;i<q;++i)
{
cin>>a>>b>>c>>d;
s.push_back(a);
e.push_back(b);
l.push_back(c);
r.push_back(d);
}
auto A = check_validity(n,x,y,s,e,l,r);
for (auto x : A) cout << x << endl;
return 0;
}
#endif
/*
6 6 3
5 1
1 3
1 3
3 4
3 0
5 2
4 2 1 2
4 2 2 2
5 4 3 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
37980 KB |
Output is correct |
2 |
Correct |
9 ms |
37980 KB |
Output is correct |
3 |
Correct |
9 ms |
37980 KB |
Output is correct |
4 |
Correct |
9 ms |
37996 KB |
Output is correct |
5 |
Correct |
9 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
37980 KB |
Output is correct |
7 |
Correct |
9 ms |
37976 KB |
Output is correct |
8 |
Correct |
8 ms |
37976 KB |
Output is correct |
9 |
Correct |
9 ms |
37980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
37980 KB |
Output is correct |
2 |
Correct |
9 ms |
37980 KB |
Output is correct |
3 |
Correct |
9 ms |
37980 KB |
Output is correct |
4 |
Correct |
9 ms |
37996 KB |
Output is correct |
5 |
Correct |
9 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
37980 KB |
Output is correct |
7 |
Correct |
9 ms |
37976 KB |
Output is correct |
8 |
Correct |
8 ms |
37976 KB |
Output is correct |
9 |
Correct |
9 ms |
37980 KB |
Output is correct |
10 |
Correct |
14 ms |
39420 KB |
Output is correct |
11 |
Correct |
17 ms |
39256 KB |
Output is correct |
12 |
Correct |
13 ms |
39256 KB |
Output is correct |
13 |
Correct |
14 ms |
39516 KB |
Output is correct |
14 |
Correct |
16 ms |
39516 KB |
Output is correct |
15 |
Correct |
14 ms |
39740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
529 ms |
127404 KB |
Output is correct |
2 |
Correct |
608 ms |
139904 KB |
Output is correct |
3 |
Correct |
559 ms |
137132 KB |
Output is correct |
4 |
Correct |
509 ms |
136108 KB |
Output is correct |
5 |
Correct |
505 ms |
136108 KB |
Output is correct |
6 |
Correct |
521 ms |
136104 KB |
Output is correct |
7 |
Correct |
449 ms |
135752 KB |
Output is correct |
8 |
Correct |
581 ms |
139288 KB |
Output is correct |
9 |
Correct |
439 ms |
137136 KB |
Output is correct |
10 |
Correct |
442 ms |
136364 KB |
Output is correct |
11 |
Correct |
446 ms |
135964 KB |
Output is correct |
12 |
Correct |
433 ms |
136296 KB |
Output is correct |
13 |
Correct |
490 ms |
136464 KB |
Output is correct |
14 |
Correct |
481 ms |
136456 KB |
Output is correct |
15 |
Correct |
483 ms |
136264 KB |
Output is correct |
16 |
Correct |
491 ms |
136500 KB |
Output is correct |
17 |
Correct |
443 ms |
136052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
37980 KB |
Output is correct |
2 |
Correct |
9 ms |
37980 KB |
Output is correct |
3 |
Correct |
9 ms |
37980 KB |
Output is correct |
4 |
Correct |
9 ms |
37996 KB |
Output is correct |
5 |
Correct |
9 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
37980 KB |
Output is correct |
7 |
Correct |
9 ms |
37976 KB |
Output is correct |
8 |
Correct |
8 ms |
37976 KB |
Output is correct |
9 |
Correct |
9 ms |
37980 KB |
Output is correct |
10 |
Correct |
14 ms |
39420 KB |
Output is correct |
11 |
Correct |
17 ms |
39256 KB |
Output is correct |
12 |
Correct |
13 ms |
39256 KB |
Output is correct |
13 |
Correct |
14 ms |
39516 KB |
Output is correct |
14 |
Correct |
16 ms |
39516 KB |
Output is correct |
15 |
Correct |
14 ms |
39740 KB |
Output is correct |
16 |
Correct |
529 ms |
127404 KB |
Output is correct |
17 |
Correct |
608 ms |
139904 KB |
Output is correct |
18 |
Correct |
559 ms |
137132 KB |
Output is correct |
19 |
Correct |
509 ms |
136108 KB |
Output is correct |
20 |
Correct |
505 ms |
136108 KB |
Output is correct |
21 |
Correct |
521 ms |
136104 KB |
Output is correct |
22 |
Correct |
449 ms |
135752 KB |
Output is correct |
23 |
Correct |
581 ms |
139288 KB |
Output is correct |
24 |
Correct |
439 ms |
137136 KB |
Output is correct |
25 |
Correct |
442 ms |
136364 KB |
Output is correct |
26 |
Correct |
446 ms |
135964 KB |
Output is correct |
27 |
Correct |
433 ms |
136296 KB |
Output is correct |
28 |
Correct |
490 ms |
136464 KB |
Output is correct |
29 |
Correct |
481 ms |
136456 KB |
Output is correct |
30 |
Correct |
483 ms |
136264 KB |
Output is correct |
31 |
Correct |
491 ms |
136500 KB |
Output is correct |
32 |
Correct |
443 ms |
136052 KB |
Output is correct |
33 |
Correct |
698 ms |
137624 KB |
Output is correct |
34 |
Correct |
231 ms |
107748 KB |
Output is correct |
35 |
Correct |
818 ms |
142668 KB |
Output is correct |
36 |
Correct |
635 ms |
137112 KB |
Output is correct |
37 |
Correct |
782 ms |
141148 KB |
Output is correct |
38 |
Correct |
689 ms |
138156 KB |
Output is correct |
39 |
Correct |
611 ms |
144036 KB |
Output is correct |
40 |
Correct |
601 ms |
162192 KB |
Output is correct |
41 |
Correct |
682 ms |
139948 KB |
Output is correct |
42 |
Correct |
540 ms |
137104 KB |
Output is correct |
43 |
Correct |
839 ms |
152016 KB |
Output is correct |
44 |
Correct |
754 ms |
140964 KB |
Output is correct |
45 |
Correct |
551 ms |
145068 KB |
Output is correct |
46 |
Correct |
574 ms |
144876 KB |
Output is correct |
47 |
Correct |
497 ms |
137044 KB |
Output is correct |
48 |
Correct |
498 ms |
136588 KB |
Output is correct |
49 |
Correct |
538 ms |
137220 KB |
Output is correct |
50 |
Correct |
506 ms |
136740 KB |
Output is correct |
51 |
Correct |
534 ms |
163420 KB |
Output is correct |
52 |
Correct |
521 ms |
163528 KB |
Output is correct |