#include #ifndef ATCODER_LAZYSEGTREE_HPP #define ATCODER_LAZYSEGTREE_HPP 1 #ifndef ATCODER_INTERNAL_BITOP_HPP #define ATCODER_INTERNAL_BITOP_HPP 1 #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #endif // ATCODER_INTERNAL_BITOP_HPP namespace atcoder { template struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} explicit lazy_segtree(int n) : lazy_segtree(std::vector(n, e())) {} explicit lazy_segtree(const std::vector& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector(2 * size, e()); lz = std::vector(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector d; std::vector lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder #endif // ATCODER_LAZYSEGTREE_HPP namespace update_min { using S = int64_t; S op(S a, S b) { return std::min(a, b); } S e() { return std::numeric_limits::max(); } using F = std::optional; S mapping(F f, S x) { return f ? *f : x; } F composition(F f, F g) { return f ? f : g; } F id() { return std::nullopt; } using segtree = atcoder::lazy_segtree; } // namespace update_min namespace add_min { using S = int64_t; S op(S a, S b) { return std::min(a, b); } S e() { return std::numeric_limits::max(); } using F = int64_t; S mapping(F f, S x) { return f + x; } F composition(F f, F g) { return f + g; } F id() { return 0; } using segtree = atcoder::lazy_segtree; } // namespace add_min namespace update_sum { struct S { int64_t val; int64_t len; }; S op(S a, S b) { return {a.val + b.val, a.len + b.len}; } S e() { return {0, 0}; } using F = std::optional; S mapping(F f, S x) { if (f) x.val = *f * x.len; return x; } F composition(F f, F g) { return f ? f : g; } F id() { return std::nullopt; } class segtree : public atcoder::lazy_segtree { public: segtree() : lazy_segtree() {} explicit segtree(int n) : lazy_segtree(std::vector(n, {0, 1})) {} explicit segtree(const std::vector& v) : lazy_segtree(itos(v)) {} private: static std::vector itos(const std::vector& v) { std::vector w(v.size()); for (size_t i = 0; i < v.size(); ++i) { w[i] = {v[i], 1}; } return w; } }; } // namespace update_sum namespace add_sum { struct S { int64_t val; int64_t len; }; S op(S a, S b) { return {a.val + b.val, a.len + b.len}; } S e() { return {0, 0}; } using F = int64_t; S mapping(F f, S x) { return {x.val + f * x.len, x.len}; } F composition(F f, F g) { return f + g; } F id() { return 0; } class segtree : public atcoder::lazy_segtree { public: segtree() : lazy_segtree() {} explicit segtree(int n) : lazy_segtree(std::vector(n, {0, 1})) {} explicit segtree(const std::vector& v) : lazy_segtree(itos(v)) {} private: static std::vector itos(const std::vector& v) { std::vector w(v.size()); for (size_t i = 0; i < v.size(); ++i) { w[i] = {v[i], 1}; } return w; } }; } // namespace add_sum #ifndef GRAPH_H_ #define GRAPH_H_ template class Graph { public: struct Edge { int from, to; T weight; }; Graph(int n) : edges_(n) {} void AddEdge(int from, int to, T weight = T()) { edges_[from].push_back({from, to, weight}); } const std::vector &Edges(int from) const { return edges_[from]; } std::vector &MutableEdges(int from) { return edges_[from]; } int NumVertices() const { return edges_.size(); } bool IsTree() const { std::vector visited(NumVertices()); auto rec = [&](auto rec, int node, int parent) -> bool { if (visited[node]) return false; visited[node] = true; for (const Edge &e : Edges(node)) { if (e.to != parent && !rec(rec, e.to, node)) { return false; } } return true; }; return rec(rec, 0, -1); } private: std::vector> edges_; }; #endif template class HeavyLightDecomposition { public: HeavyLightDecomposition(const Graph& g, int root = 0) : g_(g) { #ifdef DEBUG assert(g.IsTree()); #endif int n = g.NumVertices(); attr_.resize(n); Dfs1(root, -1, 0); int index = 0; Dfs2(root, -1, root, index); } std::vector> Query(int u, int v) const { std::vector> ret; while (Begin(u) != Begin(v)) { if (Depth(Begin(u)) < Depth(Begin(v))) std::swap(u, v); ret.emplace_back(Index(Begin(u)), Index(u) + 1); u = Parent(Begin(u)); } u = Index(u), v = Index(v); if (u > v) std::swap(u, v); ret.emplace_back(u, v + 1); return ret; } int LCA(int u, int v) const { while (Begin(u) != Begin(v)) { if (Depth(Begin(u)) < Depth(Begin(v))) std::swap(u, v); u = Parent(Begin(u)); } return Depth(u) < Depth(v) ? u : v; } int32_t Index(int node) const { return attr_[node].index; } int32_t Parent(int node) const { return attr_[node].parent; } private: void Dfs1(int node, int parent, int depth) { Attr& a = attr_[node]; a.depth = depth; a.parent = parent; a.size = 1; a.heavy = -1; for (const auto& e : g_.Edges(node)) { if (e.to == parent) continue; Dfs1(e.to, node, depth + 1); a.size += Size(e.to); if (a.heavy == -1 || Size(a.heavy) < Size(e.to)) { a.heavy = e.to; } } } void Dfs2(int node, int parent, int begin, int& index) { Attr& a = attr_[node]; a.index = index++; a.begin = begin; if (a.heavy == -1) return; Dfs2(a.heavy, node, begin, index); for (const auto& e : g_.Edges(node)) { if (e.to == parent || e.to == a.heavy) continue; Dfs2(e.to, node, e.to, index); } } int32_t Begin(int node) const { return attr_[node].begin; } int32_t Depth(int node) const { return attr_[node].depth; } int32_t Heavy(int node) const { return attr_[node].heavy; } int32_t Size(int node) const { return attr_[node].size; } const Graph& g_; struct Attr { int32_t begin; int32_t depth; int32_t heavy; int32_t index; int32_t parent; int32_t size; }; std::vector attr_; }; #include template struct is_dereferenceable : std::false_type {}; template struct is_dereferenceable())>> : std::true_type {}; template struct is_iterable : std::false_type {}; template struct is_iterable())), decltype(std::end(std::declval()))>> : std::true_type {}; template struct is_applicable : std::false_type {}; template struct is_applicable::value)>> : std::true_type {}; template void debug(const T& value, const Ts&... args); template void debug(const T& v) { if constexpr (is_dereferenceable::value) { std::cerr << "{"; if (v) { debug(*v); } else { std::cerr << "nil"; } std::cerr << "}"; } else if constexpr (is_iterable::value && !std::is_same::value) { std::cerr << "{"; for (auto it = std::begin(v); it != std::end(v); ++it) { if (it != std::begin(v)) std::cerr << ", "; debug(*it); } std::cerr << "}"; } else if constexpr (is_applicable::value) { std::cerr << "{"; std::apply([](const auto&... args) { debug(args...); }, v); std::cerr << "}"; } else { std::cerr << v; } } template void debug(const T& value, const Ts&... args) { debug(value); std::cerr << ", "; debug(args...); } #if DEBUG #define dbg(...) \ do { \ cerr << #__VA_ARGS__ << ": "; \ debug(__VA_ARGS__); \ cerr << " (L" << __LINE__ << ")\n"; \ } while (0) #else #define dbg(...) #endif void read_from_cin() {} template void read_from_cin(T& value, Ts&... args) { std::cin >> value; read_from_cin(args...); } #define rd(type, ...) \ type __VA_ARGS__; \ read_from_cin(__VA_ARGS__); #define ints(...) rd(int, __VA_ARGS__); #define strings(...) rd(string, __VA_ARGS__); // Strings used for yes/no questions. Defined as variables so that it can be // adjusted for each contest site. const char *yes_str = "Yes", *no_str = "No"; template void write_to_cout(const T& value) { if constexpr (std::is_same::value) { std::cout << (value ? yes_str : no_str); } else if constexpr (is_iterable::value && !std::is_same::value) { for (auto it = std::begin(value); it != std::end(value); ++it) { if (it != std::begin(value)) std::cout << " "; std::cout << *it; } } else { std::cout << value; } } template void write_to_cout(const T& value, const Ts&... args) { write_to_cout(value); std::cout << ' '; write_to_cout(args...); } #define wt(...) \ do { \ write_to_cout(__VA_ARGS__); \ cout << '\n'; \ } while (0) #define all(x) (x).begin(), (x).end() #define eb(...) emplace_back(__VA_ARGS__) #define pb(...) push_back(__VA_ARGS__) #define dispatch(_1, _2, _3, name, ...) name #define as_i64(x) \ ( \ [] { \ static_assert( \ std::is_integral< \ typename std::remove_reference::type>::value, \ "rep macro supports std integral types only"); \ }, \ static_cast(x)) #define rep3(i, a, b) for (int64_t i = as_i64(a); i < as_i64(b); ++i) #define rep2(i, n) rep3(i, 0, n) #define rep1(n) rep2(_loop_variable_, n) #define rep(...) dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define rrep3(i, a, b) for (int64_t i = as_i64(b) - 1; i >= as_i64(a); --i) #define rrep2(i, n) rrep3(i, 0, n) #define rrep1(n) rrep2(_loop_variable_, n) #define rrep(...) dispatch(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__) #define each3(k, v, c) for (auto&& [k, v] : c) #define each2(e, c) for (auto&& e : c) #define each(...) dispatch(__VA_ARGS__, each3, each2)(__VA_ARGS__) template std::istream& operator>>(std::istream& is, std::vector& v) { for (T& vi : v) is >> vi; return is; } template std::istream& operator>>(std::istream& is, std::pair& p) { is >> p.first >> p.second; return is; } template bool chmax(T& a, U b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, U b) { if (a > b) { a = b; return true; } return false; } template auto max(T a, U b) { return a > b ? a : b; } template auto min(T a, U b) { return a < b ? a : b; } template auto max(const T& v) { return *std::max_element(v.begin(), v.end()); } template auto min(const T& v) { return *std::min_element(v.begin(), v.end()); } template int64_t sz(const T& v) { return std::size(v); } template int64_t popcount(T i) { return std::bitset::digits>(i).count(); } template bool hasbit(T s, int i) { return std::bitset::digits>(s)[i]; } template auto div_floor(T n, U d) { if (d < 0) { n = -n; d = -d; } if (n < 0) { return -((-n + d - 1) / d); } return n / d; }; template auto div_ceil(T n, U d) { if (d < 0) { n = -n; d = -d; } if (n < 0) { return -(-n / d); } return (n + d - 1) / d; } template bool even(T x) { return x % 2 == 0; } std::array, 4> adjacent(int64_t i, int64_t j) { return {{{i + 1, j}, {i, j + 1}, {i - 1, j}, {i, j - 1}}}; } bool inside(int64_t i, int64_t j, int64_t I, int64_t J) { return 0 <= i && i < I && 0 <= j && j < J; } template void sort(T& v) { return std::sort(v.begin(), v.end()); } template void sort(T& v, Compare comp) { return std::sort(v.begin(), v.end(), comp); } template void reverse(T& v) { return std::reverse(v.begin(), v.end()); } template typename T::value_type accumulate(const T& v) { return std::accumulate(v.begin(), v.end(), typename T::value_type()); } // big = 2305843009213693951 = 2^61-1 ~= 2.3*10^18 const int64_t big = std::numeric_limits::max() / 4; using i64 = int64_t; using i32 = int32_t; template using low_priority_queue = std::priority_queue, std::greater>; template using V = std::vector; template using VV = V>; void Main(); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout << std::fixed << std::setprecision(20); Main(); return 0; } const auto& Fix = boost::hana::fix; using namespace std; #define int i64 void Main() { ints(n); Graph g(n); rep(n - 1) { ints(u, v); --u, --v; g.AddEdge(u, v); g.AddEdge(v, u); } HeavyLightDecomposition hld(g); add_sum::segtree t(V(n, 1)); ints(q); int ans = 0; rep(q) { ints(a, b); --a, --b; each(l, r, hld.Query(a, b)) { ans += t.prod(l, r).val; t.apply(l, r, 1); } } wt(ans); }