1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| const int MAXN = 200000 + 5; int n, m, Q, head[MAXN], nxt[MAXN << 2], ver[MAXN << 2], rt[MAXN << 6]; void add(int u, int v) { nxt[++head[0]] = head[u]; ver[head[u] = head[0]] = v; } struct Edge { int f[MAXN], fa[MAXN][21], dfn[MAXN], sz[MAXN], rnk[MAXN]; bool mark; std::vector<int> e[MAXN]; int find(int x) {return x == f[x] ? x : f[x] = find(f[x]);} void build() { for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= n && mark; i++) for (int j = head[i]; j; j = nxt[j]) { if (ver[j] > i) continue; int u = find(i), v = find(ver[j]); if (u == v) continue; f[v] = u; e[u].push_back(v); } for (int i = n; i && !mark; i--) for (int j = head[i]; j; j = nxt[j]) { if (ver[j] < i) continue; int u = find(i), v = find(ver[j]); if (u == v) continue; f[v] = u; e[u].push_back(v); } } void dfs(int u, int _fa) { rnk[dfn[u] = ++dfn[0]] = u; sz[u] = 1; fa[u][0] = _fa; for (int i = 1; i <= 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (auto v : e[u]) { if (_fa == v) continue; dfs(v, u); sz[u] += sz[v]; } } int calc(int x, int k) { for (int i = 20; ~i && mark; i--) if (fa[x][i] && fa[x][i] <= k) x = fa[x][i]; for (int i = 20; ~i && !mark; i--) if (fa[x][i] && fa[x][i] >= k) x = fa[x][i]; return x; } }T1, T2; struct Persistent_Segment_Tree { int val[MAXN << 6], tol, ls[MAXN << 6], rs[MAXN << 6]; #define mid ((l + r) >> 1) void insert(int &x, int l, int r, int p, int &pre) { x = ++tol; val[x] = val[pre] + 1; if (l == r) return; if (p <= mid) rs[x] = rs[pre], insert(ls[x], l, mid, p, ls[pre]); else ls[x] = ls[pre], insert(rs[x], mid + 1, r, p, rs[pre]); } int query(int L, int R, int l, int r, int ll, int rr) { if (r < ll || l > rr) return 0; if (ll <= l && r <= rr) return val[R] - val[L]; return query(ls[L], ls[R], l, mid, ll, rr) + query(rs[L], rs[R], mid + 1, r, ll, rr); } } st; int main() { io >> n >> m >> Q; for (int i = 1, u, v; i <= m; i++) { io >> u >> v; u++; v++; add(u, v); add(v, u); } T1.mark = false; T2.mark = true; T1.build(); T2.build(); T1.dfs(1, 0); T2.dfs(n, 0); for (int i = 1; i <= n; i++) st.insert(rt[i], 1, n, T2.dfn[T1.rnk[i]], rt[i - 1]); for (int s, t, l, r; Q--;) { io >> s >> t >> l >> r; s++; t++; l++; r++; int x = T1.calc(s, l), y = T2.calc(t, r); io << (st.query(rt[T1.dfn[x] - 1], rt[T1.dfn[x] + T1.sz[x] - 1], 1, n, T2.dfn[y], T2.dfn[y] + T2.sz[y] - 1) > 0) << '\n'; } return 0; }
|