蓝桥杯第 3 场 算法季度赛第八题 升级电缆题解
题目链接:升级电缆
视频讲解链接:b站
貌似大部分人一开始想偏了,想些多
显然
先讲讲比较无脑的一些做法,考虑链信息使用树剖维护,那么我们可以任意查询一条链上的信息,这个查询的复杂度是双
观察到答案显然是跟二分性有关,那么常见的这种树上的思路,并且是多次查询的有两个方向:
-
二分 +
。 -
在树上进行二分答案。
后者比较苛刻,对于树类问题,常常需要可能某个轴需要维护的是可差分的信息。
说说前者,比较暴力一点,观察到这个题涉及到的一些信息需要维护:
-
关于
所在的值域限制轴。 -
关于
开销总和的信息统计。 -
关于树上链
的限制。
那么我们发现,有两对偏序限制
考虑特殊的树套树,单
基于第一种做法,显然没啥好说的,既然是使用主席树维护信息,那么观察到
码量上应该还是不算小的,还需要写一个边化点的树链剖分,不会的可以做做
考虑下第二种做法:
需要支持树上二分答案,那么信息要求是比较苛刻的,外轴显然是需要一个用于可差分的信息偏序限制,而维护的信息则为可差分信息,内轴即为真正的二分答案轴。
这其实是一个基础板子,不会的可以去学学树上主席树:Count on a tree
那么问题就很简单了,对外轴维护关于
而二分的轴即为内轴,设置信息的轴,即为
而维护的信息必须为可差分信息,那么显然为前两者作用下的
这样一来就可以进行正确的主席树上的二分了,然后有些人想要找二分的上界
考虑下:当存在
当左侧出现
参照代码
#include <bits/stdc++.h> // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") #define isPbdsFile #ifdef isPbdsFile #include <bits/extc++.h> #else #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/rope> #endif using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tii; typedef tuple<ll, ll, ll> tll; typedef unsigned int ui; typedef unsigned long long ull; #define hash1 unordered_map #define hash2 gp_hash_table #define hash3 cc_hash_table #define stdHeap std::priority_queue #define pbdsHeap __gnu_pbds::priority_queue #define sortArr(a, n) sort(a+1,a+n+1) #define all(v) v.begin(),v.end() #define yes cout<<"YES" #define no cout<<"NO" #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout); #define forn(i, a, b) for(int i = a; i <= b; i++) #define forv(i, a, b) for(int i=a;i>=b;i--) #define ls(x) (x<<1) #define rs(x) (x<<1|1) #define endl '\n' //用于Miller-Rabin [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T> int disc(T* a, int n) { return unique(a + 1, a + n + 1) - (a + 1); } template <typename T> T lowBit(T x) { return x & -x; } template <typename T> T Rand(T l, T r) { static mt19937 Rand(time(nullptr)); uniform_int_distribution<T> dis(l, r); return dis(Rand); } template <typename T1, typename T2> T1 modt(T1 a, T2 b) { return (a % b + b) % b; } template <typename T1, typename T2, typename T3> T1 qPow(T1 a, T2 b, T3 c) { a %= c; T1 ans = 1; for (; b; b >>= 1, (a *= a) %= c) if (b & 1) (ans *= a) %= c; return modt(ans, c); } template <typename T> void read(T& x) { x = 0; T sign = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') sign = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sign; } template <typename T, typename... U> void read(T& x, U&... y) { read(x); read(y...); } template <typename T> void write(T x) { if (typeid(x) == typeid(char)) return; if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 ^ 48); } template <typename C, typename T, typename... U> void write(C c, T x, U... y) { write(x), putchar(c); write(c, y...); } template <typename T11, typename T22, typename T33> struct T3 { T11 one; T22 tow; T33 three; bool operator<(const T3 other) const { if (one == other.one) { if (tow == other.tow) return three < other.three; return tow < other.tow; } return one < other.one; } T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three) { } }; template <typename T1, typename T2> void uMax(T1& x, T2 y) { if (x < y) x = y; } template <typename T1, typename T2> void uMin(T1& x, T2 y) { if (x > y) x = y; } constexpr int N = 1e5 + 10; constexpr int MX = 1e9; constexpr ll INF = 1e18; constexpr int T = log2(N) + 1; typedef __int128 i128; struct Node { int left, right; i128 sum; } node[N << 6]; #define left(x) node[x].left #define right(x) node[x].right #define sum(x) node[x].sum int cnt; int fa[N][T + 1], deep[N]; inline void add(const int pre, int& curr, const int pos, const ll val, const int l = 1, const int r = MX) { node[curr = ++cnt] = node[pre]; sum(curr) += val; if (l == r) return; const int mid = l + r >> 1; if (pos <= mid) add(left(pre),left(curr), pos, val, l, mid); else add(right(pre),right(curr), pos, val, mid + 1, r); } inline int query(const int rtU, const int rtV, const int rtLCA, const ll sumV, const int l = 1, const int r = MX) { if (l == r) return l; const int mid = l + r >> 1; const i128 leftSum = sum(left(rtU)) + sum(left(rtV)) - 2 * sum(left(rtLCA)); if (leftSum > sumV) return query(left(rtU),left(rtV),left(rtLCA), sumV, l, mid); return query(right(rtU),right(rtV),right(rtLCA), sumV - leftSum, mid + 1, r); } typedef tuple<int, int, int, int> t4; vector<t4> child[N]; int n, q; int root[N]; inline void dfs(const int curr, const int pa) { deep[curr] = deep[fa[curr][0] = pa] + 1; forn(i, 1, T) fa[curr][i] = fa[fa[curr][i - 1]][i - 1]; for (const auto [nxt,v,c,s] : child[curr]) { if (nxt == pa) continue; root[nxt] = root[curr]; add(root[nxt], root[nxt], v, c); add(root[nxt], root[nxt], s, INF); dfs(nxt, curr); } } inline int LCA(int x, int y) { if (deep[x] < deep[y]) swap(x, y); forv(i, T, 0) if (deep[fa[x][i]] >= deep[y]) x = fa[x][i]; if (x == y) return x; forv(i, T, 0) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return fa[x][0]; } inline void solve() { cin >> n; forn(i, 1, n-1) { int x, y, v, c, s; cin >> x >> y >> v >> c >> s; child[x].emplace_back(y, v, c, s); child[y].emplace_back(x, v, c, s); } dfs(1, 0); cin >> q; while (q--) { int u, v; ll sum; cin >> u >> v >> sum; const int lca = LCA(u, v); cout << query(root[u], root[v], root[lca], sum) << endl; } } signed int main() { // MyFile Spider //------------------------------------------------------ // clock_t start = clock(); int test = 1; // read(test); // cin >> test; forn(i, 1, test) solve(); // while (cin >> n, n)solve(); // while (cin >> test)solve(); // clock_t end = clock(); // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步