正文
增加 758 行删除 6 行
21 行未改动22232425262728293039 行未改动70717273747576777879808182838485868788899021 行未改动任何方阵都可以写作若干基本矩阵的乘积,每一个基本矩阵都形如单位矩阵的某一格增加一个数。我们称 $n\times n$ 单位矩阵在第 $i$ 行第 $j$ 列增加 $k$ 的基本矩阵记作 $\mathbf{P}_n(i,j,k)$(也许和更加标准的写法不同,但是这些已经够用了)。一个横向向量乘上这个矩阵就等价与第 $j$ 个数加上第 $i$ 个数的 $k$ 倍。一个向量乘一个基本矩阵都可以映射到某一位增加某一位的若干倍这个操作。因此我们知道这个算法的一个运算过程,这个过程中只包括 `a += k * b` 这样的运算就可以还原出这个算法的等价矩阵,但是暴力乘两个矩阵是三次方的,还需要做非常多步,显然太慢,后面会讲神奇的东西。考虑构造一个线性算法 $\mathcal{A}(\begin{bmatrix}w_1&&\dots&&w_q\end{bmatrix})=\begin{bmatrix}w_1&&\dots&&w_q\end{bmatrix}\begin{bmatrix}x_1\\\vdots\\x_q\end{bmatrix}$ 返回值大小为 $1\times1$,这就是上文的第二个问题。如果可以只通过若干变量包括 $w$,只使用 `a += b` 这样的操作得到答案,则我们声称可以迅速还原出 $x$。(注意这里认为树的结构、输入的 $A$、输入的 $B$、那些路径等等都是已知量,怎样循环怎样 `a += b` 什么的是外围的事情,理解这一点很重要。也就是说,只有全部有关 $w$ 的计算必须写成线性的乘基本矩阵,只有 $w$ 是第二个题的算法的输入。)如果我们还需要 $tmp$ 个临时变量(还也许可能需要一个常量全程为 $1$ 方便数加,不过这题不需要),则我们解决上文的第二个问题的算法可以写为这样的形式:$$\begin{bmatrix}w_1&&\dots&&w_q&&0&&\dots&&0\end{bmatrix}\mathbf{P}_{q+tmp}(i_1,j_1,k_1)\mathbf{P}_{q+tmp}(i_2,j_2,k_2)\dots\mathbf{P}_{q+tmp}(i_o,j_o,k_o)\begin{bmatrix}0\\\vdots\\0\\1\\0\\\vdots\\0\end{bmatrix}$$39 行未改动再次考虑链,把 $l\le u<v\le r$ 容斥出来。如果我们可以求出 $1\le u=v\le i$ 这样的 $(u,v)$ 数量,和 $1\le u\le i$ 且 $1\le v\le j$ 的 $(u,v)$ 数量。(好像这里就和别的生成函数题解的做法贴近了。)可以把 $l\le u<v\le r$ 拆成上面的两种东西加加建减,而上面的第一个东西是好求的,第二个东西仍然可以莫队。把这个思想搬到树上,把上面类似 $1\le u\le i$ 这样的限制改为 $u$ 为 $i$ 的祖先,跑莫队就好写得多。定义 $f(i,j)$ 为 $u$ 为 $i$ 的祖先且 $v$ 为 $j$ 的祖先的 $A_u+A_v$ 产生的价值之和。定义 $g(i)$ 为 $u$ 为 $i$ 的祖先的时候 $2A_u$ 产生的价值的和,定义 $fa_i$ 为 $i$ 的爹,关于根的爹的 $f$ 均为 $0$。则路径 $x\rightarrow y$ 的答案可以凑出容斥系数:$f(x,x)-2f(x,fa_{\operatorname{LCA}(x,y)})+f(fa_{\operatorname{LCA}(x,y)},fa_{\operatorname{LCA}(x,y)})+f(y,y)-2f(y,\operatorname{LCA}(x,y))+f(\operatorname{LCA}(x,y),\operatorname{LCA}(x,y))+f(x,y)-f(fa_{\operatorname{LCA}(x,y)},y)-f(x,\operatorname{LCA}(x,y))+f(fa_{\operatorname{LCA}(x,y)},\operatorname{LCA}(x,y))-g(x)-g(y)+g(\operatorname{LCA}(x,y))+g(fa_{\operatorname{LCA}(x,y)})$怎么莫队是外围的事,考虑正着做会有哪几步,首先会在树上 DFS 处理二次离线的需求(包括在桶里某些位置增加 $w$,根据桶增加),之后做前缀和求出需要的 $f$,之后把十个 $f$ 加起来求出一个原路径的价值,之后求所有原路径的价值和。倒观上面这一段,是真正的按字倒背,再翻译为简体中文,得到下面的东西:把所有路径的价值设为 $1$,把需要的十个 $f$ 的价值标记上,把排序后的 $f$ 的价值做后缀和,二次离线,DFS 处理这些需求(包括把需求加入桶,根据 $B-A$ 的桶标记对应的 $w$ 的价值)。$w$ 的价值也就是 $x$。有关 $g$ 的部分,正着做是这样的,根据 $2A_u$ 决定是否是 $0$ 或某一个 $w$,做树上前缀和得到 $g$,求出对每个原路径的贡献,求和。倒着看就是每个原路径的价值是 $1$,求出每个 $g$ 的价值,求出 $g$ 的价值的子树和,就是单点价值,根据这个价值确定是否需要更新一个 $x$。然后就做完了?---## $10$ 倍大常数代码别急正在写。21 行未改动22232425262728293039 行未改动70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184221 行未改动任何方阵都可以写作若干基本矩阵的乘积,每一个基本矩阵都形如单位矩阵的某一格增加一个数。我们称 $n\times n$ 单位矩阵在第 $i$ 行第 $j$ 列增加 $k$ 的基本矩阵记作 $\mathbf{P}_n(i,j,k)$(也许和更加标准的写法不同,但是这些已经够用了)。一个横向向量乘上这个矩阵就等价与第 $j$ 个数加上第 $i$ 个数的 $k$ 倍。一个向量乘一个基本矩阵都可以映射到某一位增加某一位的若干倍这个操作。因此我们知道这个算法的一个运算过程,这个过程中只包括 `a += k * b` 这样的运算就可以还原出这个算法的等价矩阵,但是暴力乘两个矩阵是三次方的,还需要做非常多步,显然太慢,后面会讲神奇的东西。考虑构造一个线性算法 $\mathcal{A}(\begin{bmatrix}w_1&&\dots&&w_q\end{bmatrix})=\begin{bmatrix}w_1&&\dots&&w_q\end{bmatrix}\begin{bmatrix}x_1\\\vdots\\x_q\end{bmatrix}$ 返回值大小为 $1\times1$,这就是上文的第二个问题。如果可以只通过若干变量包括 $w$,只使用 `a += k * b` 这样的操作得到答案,则我们声称可以迅速还原出 $x$。(注意这里认为树的结构、输入的 $A$、输入的 $B$、那些路径等等都是已知量,怎样循环怎样 `a += k * b` 什么的是外围的事情,理解这一点很重要。也就是说,只有全部有关 $w$ 的计算必须写成线性的乘基本矩阵,只有 $w$ 是第二个题的算法的输入。)如果我们还需要 $tmp$ 个临时变量(还也许可能需要一个常量全程为 $1$ 方便数加,不过这题不需要),则我们解决上文的第二个问题的算法可以写为这样的形式:$$\begin{bmatrix}w_1&&\dots&&w_q&&0&&\dots&&0\end{bmatrix}\mathbf{P}_{q+tmp}(i_1,j_1,k_1)\mathbf{P}_{q+tmp}(i_2,j_2,k_2)\dots\mathbf{P}_{q+tmp}(i_o,j_o,k_o)\begin{bmatrix}0\\\vdots\\0\\1\\0\\\vdots\\0\end{bmatrix}$$39 行未改动再次考虑链,把 $l\le u<v\le r$ 容斥出来。如果我们可以求出 $1\le u=v\le i$ 这样的 $(u,v)$ 数量,和 $1\le u\le i$ 且 $1\le v\le j$ 的 $(u,v)$ 数量。(好像这里就和别的生成函数题解的做法贴近了。)可以把 $l\le u<v\le r$ 拆成上面的两种东西加加建减,而上面的第一个东西是好求的,第二个东西仍然可以莫队。把这个思想搬到树上,把上面类似 $1\le u\le i$ 这样的限制改为 $u$ 为 $i$ 的祖先,跑莫队就好写得多。定义 $f(i,j)$ 为 $u$ 为 $i$ 的祖先且 $v$ 为 $j$ 的祖先的 $A_u+A_v$ 产生的价值之和。定义 $g(i)$ 为 $u$ 为 $i$ 的祖先的时候 $2A_u$ 产生的价值的和,定义 $fa_i$ 为 $i$ 的爹,关于根的爹的 $f$ 均为 $0$。则路径 $x\rightarrow y$ 的答案可以凑出容斥系数:太绕了分开写:分为 $x\leftrightarrow\operatorname{LCA}(x,y)$ 这一段和剩下的部分。$f$ 的结构很具备分配率,因此一个段和一个段的产生的价值可以用分配律拆成 $f$。前一部分的内部贡献 $\frac{f(x,x)-2f(x,fa_{\operatorname{LCA}(x,y)})+f(fa_{\operatorname{LCA}(x,y)},fa_{\operatorname{LCA}(x,y)})-g(x)+g(fa_{\operatorname{LCA}(x,y)})}2$;\第二部分贡献 $\frac{f(y,y)-2f(y,\operatorname{LCA}(x,y))+f(\operatorname{LCA}(x,y),\operatorname{LCA}(x,y))-g(y)+g(\operatorname{LCA}(x,y))}2$;\第一部分选 $u$,第二部分选 $v$ 贡献 $f(x,y)-f(fa_{\operatorname{LCA}(x,y)},y)-f(x,\operatorname{LCA}(x,y))+f(fa_{\operatorname{LCA}(x,y)},\operatorname{LCA}(x,y))$。求这个 $f$ 需要把树按 DFS 序拍平,每个点拆成进入和走出,走出产生负一倍贡献,这样就跑到序列上了怎么莫队是外围的事,考虑正着做有关 $w$ 的计算会有哪几步,首先会在树的 DFS 序上处理二次离线的需求(包括在桶里某些位置增加 $w$,根据桶增加),之后做前缀和求出需要的 $f$,之后把十个 $f$ 加起来求出一个原路径的价值,之后求所有原路径的价值和。倒观上面这一段,是真正的按字倒背,再翻译为简体中文,得到下面的东西:把所有路径的价值设为 $1$,把需要的十个 $f$ 的价值标记上,把排序后的 $f$ 的价值做后缀和,二次离线,DFS 序上处理这些需求(包括把需求加入桶,根据 $B-A$ 的桶标记对应的 $w$ 的价值)。$w$ 的价值也就是 $x$。有关 $g$ 的部分,正着做是这样的,根据 $2A_u$ 决定是否是 $0$ 或某一个 $w$,做树上前缀和得到 $g$,求出对每个原路径的贡献,求和。倒着看就是每个原路径的价值是 $1$,求出每个 $g$ 的价值,求出 $g$ 的价值的子树和,就是单点价值,根据 $A$ 确定是否需要用这个价值更新一个 $x$。然后就做完了?---## $10$ 倍大常数代码:::success[代码]```cpp#include <bits/stdc++.h>// #include <bits/extc++.h>#define multiple_cases(T) signed T; cin >> T; while (T--)#define variable_name(x) #x#define all(x) (x).begin(), (x).end()#define adebug(a, b, c) #a << "[" << (b) << "..." << (c) << "] = " << vector<typename decay<decltype(*((a) + (b)))>::type>((a) + (b), (a) + (c) + 1)#define file_io(a) (freopen(#a ".in", "r", stdin), freopen(#a ".out", "w", stdout))#ifdef assert#undef assert#endif#define assert(x) if (!(x)) { std::cerr << "Assertion failed: " << #x << ", line " << __LINE__ << std::endl; exit(1); } else;using namespace std;// using namespace __gnu_cxx;// using namespace __gnu_pbds;namespace quick_io {template<typename... Args>ostream& operator<<(ostream& os, const tuple<Args...>& t);template<typename T, typename Alloc>ostream &operator<<(ostream &A, const vector<T, Alloc> &b);template<typename T, typename Alloc>ostream &operator<<(ostream &A, const deque<T, Alloc> &b);template<typename T1, typename T2>ostream &operator<<(ostream &A, const pair<T1, T2> &b);template<typename T, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const set<T, Compare, Alloc> &b);template<typename T, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const multiset<T, Compare, Alloc> &b);template<typename T, typename T2, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const map<T, T2, Compare, Alloc> &b);template<typename T, typename T2, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const multimap<T, T2, Compare, Alloc> &b);template<typename T, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_set<T, Hash, KeyEqual, Alloc> &b);template<typename T, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_multiset<T, Hash, KeyEqual, Alloc> &b);template<typename T, typename T2, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_map<T, T2, Hash, KeyEqual, Alloc> &b);template<typename T, typename T2, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_multimap<T, T2, Hash, KeyEqual, Alloc> &b);template<typename T, typename Alloc>ostream &operator<<(ostream &A, const vector<T, Alloc> &b) {A << "[";for (typename vector<T, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "]";}template<typename T, typename Alloc>ostream &operator<<(ostream &A, const deque<T, Alloc> &b) {A << "[";for (typename deque<T, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "]";}template<typename T1, typename T2>ostream &operator<<(ostream &A, const pair<T1, T2> &b) {return A << '(' << b.first << ',' << b.second << ')';}template<typename T, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const set<T, Compare, Alloc> &b) {A << "{";for (typename set<T, Compare, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const multiset<T, Compare, Alloc> &b) {A << "{";for (typename multiset<T, Compare, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename T2, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const map<T, T2, Compare, Alloc> &b) {A << "{";for (typename map<T, T2, Compare, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename T2, typename Compare, typename Alloc>ostream &operator<<(ostream &A, const multimap<T, T2, Compare, Alloc> &b) {A << "{";for (typename multimap<T, T2, Compare, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_set<T, Hash, KeyEqual, Alloc> &b) {A << "{";for (typename unordered_set<T, Hash, KeyEqual, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_multiset<T, Hash, KeyEqual, Alloc> &b) {A << "{";for (typename unordered_multiset<T, Hash, KeyEqual, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename T2, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_map<T, T2, Hash, KeyEqual, Alloc> &b) {A << "{";for (typename unordered_map<T, T2, Hash, KeyEqual, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}template<typename T, typename T2, typename Hash, typename KeyEqual, typename Alloc>ostream &operator<<(ostream &A, const unordered_multimap<T, T2, Hash, KeyEqual, Alloc> &b) {A << "{";for (typename unordered_multimap<T, T2, Hash, KeyEqual, Alloc>::const_iterator it = b.begin(); it != b.end(); ++it) {if (it != b.begin()) A << ",";A << *it;}return A << "}";}void print_tuple(ostream&, const tuple<>&) {}template<typename T, typename... Rest>void print_tuple(ostream& os, const tuple<T, Rest...>& t) {os << get<0>(t);if (sizeof...(Rest) > 0) {os << ",";print_tuple(os, reinterpret_cast<const tuple<Rest...>&>(t));}}template<typename... Args>ostream& operator<<(ostream& os, const tuple<Args...>& t) {os << "(";print_tuple(os, t);return os << ")";}template<typename T1, typename T2>istream &operator>>(istream &A, pair<T1, T2> &b) {return A >> b.first >> b.second;}template<typename T>void print_array(T b, T e, string s = " ") {while (b != e) {cout << *b;b++;if (b != e) {cout << s;}}}template<typename T>void auto_print(T &b, size_t n, string s = " ") {for (size_t i = 1; i < n; i++) {cout << b[i] << s;}cout << b[n];}template<typename T>void auto_print(T &b, string s = " ") {for (auto i : b) {cout << i << s;}}template<typename T>void print_n(T b, size_t n, string s = " ") {if (n == 0) return;cout << *b;for (size_t i = 1; i < n; i++) {b++;cout << s << *b;}}template<typename T>void read_array(T b, T e) {while (b != e) {cin >> *b;b++;}}template<typename T>void auto_read(T &b, size_t n) {for (size_t i = 1; i <= n; i++) {cin >> b[i];}}template<typename T>void read_n(T b, size_t n) {cin >> *b;for (size_t i = 1; i < n; i++) {b++;cin >> *b;}}template <typename T>std::string to_string(const T& value) {std::ostringstream oss;oss << value;return oss.str();}std::string debug_index() {return "";}template <typename first_t, typename... rest_t>std::string debug_index(const first_t& first, const rest_t&... rest) {return "[" + to_string(first) + "]" + debug_index(rest...);}template <typename T>auto get_value(T&& arr) -> decltype(std::forward<T>(arr)) {return std::forward<T>(arr);}template <typename T, typename first_t, typename... rest_t>auto get_value(T&& arr, first_t&& first, rest_t&&... rest) {return get_value(std::forward<T>(arr)[std::forward<first_t>(first)],std::forward<rest_t>(rest)...);}#define debug(first, ...) #first << debug_index(__VA_ARGS__) << " = " << get_value(first, ##__VA_ARGS__)#define spc << ' ' <<#undef quick_io_int_length_limit#undef quick_io_int_radix_type#undef quick_io_length_type}using namespace quick_io;namespace fast_ios {// #define fast_ios_buffer_enough// #define fast_is_buffer_enough// #define fast_os_buffer_enough// #define fast_ios_jump_invisible// #define fast_is_jump_invisible// #define fast_os_jump_invisible#ifdef fast_ios_buffer_enough#define fast_is_buffer_enough#define fast_os_buffer_enough#endif#ifdef fast_ios_jump_invisible#define fast_is_jump_invisible#define fast_os_jump_invisible#endiftemplate<size_t buffer_size = 65536>struct fast_reader_t {// short, int, long, long long, __int128// unsigned short, int, long, long long, __int128// very safe if there are no overflowsize_t cursor;unsigned char c;unsigned char buffer[buffer_size];inline fast_reader_t() : cursor(buffer_size), c(EOF) {}inline unsigned char &get() {#ifndef fast_is_buffer_enoughif (cursor < buffer_size) {#endifreturn c = buffer[cursor++];#ifndef fast_is_buffer_enough}for (size_t i = fread(buffer, 1, buffer_size, stdin); i < buffer_size; i++) {buffer[i] = EOF;}return c = buffer[(cursor = 0)++];#endif}inline void unget() {cursor--;}template<typename T>typename enable_if<is_integral<T>::value&& is_unsigned<T>::value&& !is_same<T, unsigned char>::value, fast_reader_t&>::typeinline operator>>(T &x) {x = 0;while (get() < '0' || c > '9');do {x = (x << 3) + (x << 1) + (c ^ '0');} while (get() >= '0' && c <= '9');#ifndef fast_is_jump_invisibleunget();#endifreturn *this;}template<typename T>typename enable_if<is_integral<T>::value&& is_signed<T>::value&& !is_same<T, char>::value, fast_reader_t&>::typeinline operator>>(T &x) {typename make_unsigned<T>::type x_ = 0;bool sign = false;while ((get() < '0' || c > '9') && c != '-' && c != '+');if (c == '-') {sign = true;get();} else if (c == '+') {get();}do {x_ = (x_ << 3) + (x_ << 1) + (c ^ '0');} while (get() >= '0' && c <= '9');x = x_;if (sign) {x = -x;}#ifndef fast_is_jump_invisibleunget();#endifreturn *this;}inline fast_reader_t &operator>>(char &x) {while (!isgraph(get()));x = c;return *this;}inline fast_reader_t &operator>>(unsigned char &x) {while (!isgraph(get()));x = c;return *this;}inline fast_reader_t &operator>>(string &x) {x = "";while (get() <= 32);do {x += c;} while (get() > 32);#ifndef fast_is_jump_invisibleunget();#endifreturn *this;}inline fast_reader_t &operator>>(char *x) {while (get() <= 32);do {*(x++) = c;} while (get() > 32);*x = 0;#ifndef fast_is_jump_invisibleunget();#endifreturn *this;}template<typename T>typename enable_if<is_floating_point<T>::value, fast_reader_t&>::typeinline operator>>(T &x) {x = 0;bool sign = false;while ((get() < '0' || c > '9') && c != '-' && c != '+' && c != '.');if (c == '-') {sign = true;get();} else if (c == '+') {get();} else if (c == '.') {unget();}if (c != '.') do {x = x * 10 + (c ^ '0');} while (get() >= '0' && c <= '9');if (c == '.') {T f = 0.1;while (get() >= '0' && c <= '9') {x += (c ^ '0') * f;f /= 10;}}if (sign) {x = -x;}unget();return *this;}};template<size_t buffer_size = 65536>struct fast_writer_t {// short, int, long, long long, __int128// unsigned short, int, long, long long, __int128size_t cursor;unsigned char buffer[buffer_size];inline fast_writer_t() : cursor(0) {}inline ~fast_writer_t() {if (cursor) {flush();}}inline void flush() {__builtin_fwrite(buffer, 1, cursor, stdout);cursor = 0;}inline void put(unsigned char c) {buffer[cursor++] = c;#ifndef fast_os_buffer_enoughif (cursor == buffer_size) {flush();}#endif}template<typename T>typename enable_if<is_integral<T>::value&& is_unsigned<T>::value&& !is_same<unsigned char, T>::value,fast_writer_t&>::typeinline operator<<(const T &x) {if (x >= T(10)) {*this << x / T(10);}put('0' ^ x % T(10));return *this;}template<typename T>typename enable_if<is_integral<T>::value&& is_signed<T>::value&& !is_same<char, T>::value,fast_writer_t&>::typeinline operator<<(const T &x) {if (x >= T(0)) {return *this << (typename make_unsigned<T>::type)x;} else {put('-');return *this << (typename make_unsigned<T>::type)(-x);}}inline fast_writer_t &operator<<(char x) {put(x);return *this;}inline fast_writer_t &operator<<(unsigned char x) {put(x);return *this;}inline fast_writer_t &operator<<(const string &x) {for (string::const_iterator it = x.cbegin(); it != x.cend(); it++) {put(*it);}return *this;}inline fast_writer_t &operator<<(const char *x) {while (*x) {put(*(x++));}return *this;}inline fast_writer_t &operator<<(const unsigned char *x) {while (*x) {put(*(x++));}return *this;}};fast_reader_t<> ewin;fast_writer_t<> ewout;}template<typename T_key,typename T_value,typename head_container = vector<size_t>,typename nxt_container = vector<size_t>,typename data_container = vector<T_value>,typename index_type = size_t>struct basic_fs {// front star, without initializationdata_container data;nxt_container nxt;head_container head;index_type cnt;basic_fs() : cnt(0) {}struct data_t {T_key key;basic_fs* base;struct iterator {index_type pos;data_t* base;T_value &operator*() { return base->base->data[pos]; }iterator &operator++() { pos = base->base->nxt[pos]; return *this; }operator index_type&() { return pos; }};iterator end() { return {0, this}; }iterator begin() { return {base->head[key], this}; }void push_back(const T_value &value) {base->data[++base->cnt] = value;base->nxt[base->cnt] = base->head[key];base->head[key] = base->cnt;}bool empty() { return base->head[key] == 0; }size_t size() {size_t ans = 0;for (size_t i = base->head[key]; i; i = base->nxt[i]) {ans++;}return ans;}void clear() { base->head[key] = 0; }friend ostream &operator<<(ostream &os, const data_t &a) {os << "[";size_t i = a.base->head[a.key];while (i) {os << a.base->data[i];i = a.base->nxt[i];if (i) {os << ",";}}return os << "]";}};data_t operator[](const T_key &x) { return {x, this}; }};template<typename T,size_t index_range,size_t value_size = index_range>using array_fs = basic_fs<size_t,T,int[index_range],int[value_size],T[value_size],int>;#define int long long#if 1using ll = long long;using ull = unsigned long long;using i8 = int8_t;using u8 = uint8_t;using i16 = int16_t;using u16 = uint16_t;using i32 = int32_t;using u32 = uint32_t;using i64 = int64_t;using u64 = uint64_t;using i128 = __int128_t;using u128 = __uint128_t;using pii = pair<int, int>;using vi = vector<int>;using vvi = vector<vi>;using vpii = vector<pii>;using sti = set<int>;using mpii = map<int, int>;using usti = unordered_set<int>;using umpii = unordered_map<int, int>;using msti = multiset<int>;using umsti = unordered_multiset<int>;using sos = ostream;using sis = istream;using soss = ostringstream;using siss = istringstream;#endif#define min(x, y) ((x) < (y) ? (x) : (y))#define max(x, y) ((x) > (y) ? (x) : (y))#define mmin(x, y) ((x) < (y) ? (x) : (y))#define mmax(x, y) ((x) > (y) ? (x) : (y))#define Cpre(x) mint fac[(x) + 5], inv[(x) + 5]; struct Cpre##x##_t { Cpre##x##_t() { build_factorial(x, fac, inv); } } Cpre##x;#define lowbit(x) ((x) & -(x))#define abs(x) ((x) < 0 ? -(x) : (x))// #define swap(a,b) a ^= b ^= a ^= b#define INF 1e18#define bin(a,b) bitset<a>(b)#define getbit(a, b) (((a) >> (b)) & 1)#define cde(...) cout << debug(__VA_ARGS__) << endl#define dmi << '-' <<#define dpl << '+' <<#define dmu << '*' <<#define ddi << '/' <<#define dor << '|' <<#define dan << '&' <<#define dxo << '^' <<#define dno << '!' <<#define dco << ',' <<#define ddo << '.' <<using fast_ios::ewin;using fast_ios::ewout;// #define cin ewin// #define cout ewout#define MAXN 100005#define MAXM 200005#define MAXQ 2005#define K 500int n, a[MAXN], m, s[MAXM], t[MAXM];struct query_f_t {int x, y, k;query_f_t(int X = 0, int Y = 0, int _K = 0) : x(X), y(Y), k(_K) {}} query_f[MAXM * 10];int qf;int q, b[MAXQ];array_fs<int, MAXN, MAXN * 2> e;int fa[MAXN], dfn[MAXN], id[MAXN * 2], stamp, dep[MAXN];array_fs<tuple<int, int, int>, MAXN * 2, MAXM * 20> updates;int g[MAXN];int buc[MAXN];int x[MAXQ];int invb[MAXN * 2];void dfs(int u, int fa) {::fa[u] = fa;dep[u] = dep[fa] + 1;id[dfn[u] = ++stamp] = u;for (int v : e[u]) {if (v == fa) continue;dfs(v, u);}id[++stamp] = -u;}void dfs_g(int u, int fa) {for (int v : e[u]) {if (v == fa) continue;dfs_g(v, u);g[u] += g[v];}if (invb[2 * a[u]]) {x[invb[2 * a[u]]] += g[u];}}namespace LCA {int anc[MAXN][20];void build() {for (int i = 1; i <= n; i++) {anc[i][0] = fa[i];}for (int j = 1; j < 20; j++) {for (int i = 1; i <= n; i++) {anc[i][j] = anc[anc[i][j - 1]][j - 1];}}}int LCA(int x, int y) {if (dep[x] < dep[y]) swap(x, y);for (int i = 19; ~i; i--) {if (dep[anc[x][i]] >= dep[y]) {x = anc[x][i];}}if (x == y) return x;for (int i = 19; ~i; i--) {if (anc[x][i] != anc[y][i]) {x = anc[x][i];y = anc[y][i];}}return fa[x];}}signed main() {cin >> n;for (int i = 1; i <= n; i++) {cin >> a[i];}for (int i = 1, u, v; i < n; i++) {cin >> u >> v;e[u].push_back(v);e[v].push_back(u);}cin >> m;for (int i = 1; i <= m; i++) {cin >> s[i] >> t[i];}cin >> q;for (int i = 1; i <= q; i++) {cin >> b[i];invb[b[i]] = i;}dfs(1, 0);LCA::build();for (int i = 1; i <= m; i++) {int a = LCA::LCA(s[i], t[i]);int x = s[i];int y = t[i];query_f[++qf] = {x, x, 1};query_f[++qf] = {x, fa[a], -2};query_f[++qf] = {fa[a], fa[a], 1};g[x]--;g[fa[a]]++;query_f[++qf] = {y, y, 1};query_f[++qf] = {y, a, -2};query_f[++qf] = {a, a, 1};g[y]--;g[a]++;query_f[++qf] = {x, y, 2};query_f[++qf] = {fa[a], y, -2};query_f[++qf] = {x, a, -2};query_f[++qf] = {fa[a], a, 2};}dfs_g(1, 0);for (int i = 1; i <= qf; i++) {query_f[i].x = dfn[query_f[i].x];query_f[i].y = dfn[query_f[i].y];}sort(query_f + 1, query_f + qf + 1, [](const query_f_t &a, const query_f_t &b)->bool {if (a.x / K == b.x / K) {return a.y < b.y;} else {return a.x / K < b.x / K;}});for (int i = qf - 1; i; i--) {query_f[i].k += query_f[i + 1].k;}int fi = 0, fj = 0;for (int i = 1; i <= qf; i++) {// cout << query_f[i].x spc query_f[i].y spc query_f[i].k << endl;if (fi < query_f[i].x) {updates[fj].push_back({fi + 1, query_f[i].x, query_f[i].k});} else if (fi > query_f[i].x) {updates[fj].push_back({query_f[i].x + 1, fi, -query_f[i].k});}fi = query_f[i].x;if (fj < query_f[i].y) {updates[fi].push_back({fj + 1, query_f[i].y, query_f[i].k});} else if (fj > query_f[i].y) {updates[fi].push_back({query_f[i].y + 1, fj, -query_f[i].k});}fj = query_f[i].y;}for (int i = 2 * n; i; i--) {for (tuple<int, int, int> &j : updates[i]) {for (int k = get<0>(j); k <= get<1>(j); k++) {buc[a[abs(id[k])]] += get<2>(j) * (id[k] > 0 ? 1 : -1);}}for (int j = 1; j <= q; j++) {if (a[abs(id[i])] <= b[j]) {x[j] += buc[b[j] - a[abs(id[i])]] * (id[i] > 0 ? 1 : -1);}}}for (int i = 1; i <= q; i++) {cout << x[i] / 2 << endl;}return 0;}```:::感谢 GPT 2 负责部分代码编写。感谢 GPT 5.5 负责全部调试工作。