#if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <algorithm> #include <iostream> #include <vector> using namespace std; namespace { struct Node; struct NodePtr { int i; NodePtr(nullptr_t): i{-1} { } explicit NodePtr(int i_): i{i_} { } Node& operator*() const; Node* operator->() const; explicit operator bool() const { return i >= 0; } }; struct Node { vector<NodePtr> children; NodePtr extra = nullptr; int extra_count = 0; bool over_a = false; bool over_b = false; static NodePtr make(); NodePtr clone() const { NodePtr res = Node::make(); res->extra_count = extra_count; res->extra = extra; res->children = children; return res; } NodePtr specialise() { NodePtr res = extra->clone(); --extra_count; children.emplace_back(res); return res; } }; vector<Node> nodes; NodePtr Node::make() { nodes.emplace_back(); return NodePtr(nodes.size() - 1); } Node& NodePtr::operator*() const { return nodes[i]; } Node* NodePtr::operator->() const { return &nodes[i]; } NodePtr make_tree(vector<int> const& degrees) { NodePtr root = Node::make(); NodePtr last = root; for (int d: degrees) { NodePtr cur = Node::make(); last->extra = cur; last->extra_count = d; last = cur; } return root; } int mul(int a, int b, int m) { return static_cast<long long>(a) * b % m; } int half(int a, int m) { if (a % 2 == 0) return a / 2; return (a + m) / 2; } constexpr auto mod = 1'000'000'007; template <typename F> void enumerate(NodePtr n, int ad, int bd, F&& fun, int cm = 1, int c2 = 1) { if (!n) return; if (n->over_a) --ad; else ++ad; if (n->over_b) --bd; else ++bd; fun(n, cm, c2, ad, bd); for (auto const& c: n->children) enumerate(c, ad, bd, fun, cm, c2); enumerate(n->extra, ad, bd, fun, mul(cm, n->extra_count, mod), mul(c2, n->extra_count, 2)); } int solve(vector<int> const& degrees, int al, int bl, int abl) { NodePtr root = make_tree(degrees); NodePtr last = root; for (int l = 1; l < abl; ++l) { last = last->specialise(); last->over_a = true; last->over_b = true; } NodePtr a = last; for (int l = abl; l < al; ++l) { a = a->specialise(); a->over_a = true; } NodePtr b = last; for (int l = abl; l < bl; ++l) { b = b->specialise(); b->over_b = true; } vector<int> points; int res = 0; enumerate(root, al - 2, bl - 2, [&](NodePtr n, int cm, int c2, int ad, int bd) { if (c2 == 1) { points.emplace_back(ad + bd); res += mod - bd; res %= mod; } res += mul((ad + mod - bd) % mod, half((cm + mod - c2) % mod, mod), mod); res %= mod; }); sort(points.begin(), points.end()); reverse(points.begin(), points.end()); for (int i = 0; i < int(points.size()); i += 2) { res += points[i]; res %= mod; res += mod; res %= mod; } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector<int> degrees(n - 1); for (int i = 0; i < n - 1; ++i) { cin >> degrees[i]; } for (int i = 0; i < q; ++i) { int a, b, lca; cin >> a >> b >> lca; nodes.reserve(5 * n); cout << solve(degrees, a, b, lca) << endl; nodes.clear(); } return 0; }
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | #if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <algorithm> #include <iostream> #include <vector> using namespace std; namespace { struct Node; struct NodePtr { int i; NodePtr(nullptr_t): i{-1} { } explicit NodePtr(int i_): i{i_} { } Node& operator*() const; Node* operator->() const; explicit operator bool() const { return i >= 0; } }; struct Node { vector<NodePtr> children; NodePtr extra = nullptr; int extra_count = 0; bool over_a = false; bool over_b = false; static NodePtr make(); NodePtr clone() const { NodePtr res = Node::make(); res->extra_count = extra_count; res->extra = extra; res->children = children; return res; } NodePtr specialise() { NodePtr res = extra->clone(); --extra_count; children.emplace_back(res); return res; } }; vector<Node> nodes; NodePtr Node::make() { nodes.emplace_back(); return NodePtr(nodes.size() - 1); } Node& NodePtr::operator*() const { return nodes[i]; } Node* NodePtr::operator->() const { return &nodes[i]; } NodePtr make_tree(vector<int> const& degrees) { NodePtr root = Node::make(); NodePtr last = root; for (int d: degrees) { NodePtr cur = Node::make(); last->extra = cur; last->extra_count = d; last = cur; } return root; } int mul(int a, int b, int m) { return static_cast<long long>(a) * b % m; } int half(int a, int m) { if (a % 2 == 0) return a / 2; return (a + m) / 2; } constexpr auto mod = 1'000'000'007; template <typename F> void enumerate(NodePtr n, int ad, int bd, F&& fun, int cm = 1, int c2 = 1) { if (!n) return; if (n->over_a) --ad; else ++ad; if (n->over_b) --bd; else ++bd; fun(n, cm, c2, ad, bd); for (auto const& c: n->children) enumerate(c, ad, bd, fun, cm, c2); enumerate(n->extra, ad, bd, fun, mul(cm, n->extra_count, mod), mul(c2, n->extra_count, 2)); } int solve(vector<int> const& degrees, int al, int bl, int abl) { NodePtr root = make_tree(degrees); NodePtr last = root; for (int l = 1; l < abl; ++l) { last = last->specialise(); last->over_a = true; last->over_b = true; } NodePtr a = last; for (int l = abl; l < al; ++l) { a = a->specialise(); a->over_a = true; } NodePtr b = last; for (int l = abl; l < bl; ++l) { b = b->specialise(); b->over_b = true; } vector<int> points; int res = 0; enumerate(root, al - 2, bl - 2, [&](NodePtr n, int cm, int c2, int ad, int bd) { if (c2 == 1) { points.emplace_back(ad + bd); res += mod - bd; res %= mod; } res += mul((ad + mod - bd) % mod, half((cm + mod - c2) % mod, mod), mod); res %= mod; }); sort(points.begin(), points.end()); reverse(points.begin(), points.end()); for (int i = 0; i < int(points.size()); i += 2) { res += points[i]; res %= mod; res += mod; res %= mod; } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector<int> degrees(n - 1); for (int i = 0; i < n - 1; ++i) { cin >> degrees[i]; } for (int i = 0; i < q; ++i) { int a, b, lca; cin >> a >> b >> lca; nodes.reserve(5 * n); cout << solve(degrees, a, b, lca) << endl; nodes.clear(); } return 0; } |