版本比较
题解:P15943 [JOI Final 2026] JOI 之旅 2 / JOI Tour 2
正文
增加 745 行删除 1 行
93 行未改动9495969798989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184293 行未改动---
## $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 #endif template<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 overflow size_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_enough if (cursor < buffer_size) { #endif return 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&>::type inline 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_invisible unget(); #endif return *this; } template<typename T> typename enable_if<is_integral<T>::value && is_signed<T>::value && !is_same<T, char>::value , fast_reader_t&>::type inline 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_invisible unget(); #endif return *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_invisible unget(); #endif return *this; } inline fast_reader_t &operator>>(char *x) { while (get() <= 32); do { *(x++) = c; } while (get() > 32); *x = 0; #ifndef fast_is_jump_invisible unget(); #endif return *this; } template<typename T> typename enable_if<is_floating_point<T>::value, fast_reader_t&>::type inline 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, __int128 size_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_enough if (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&>::type inline 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&>::type inline 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 initialization data_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 负责全部调试工作。