제출 #1139642

#제출 시각아이디문제언어결과실행 시간메모리
1139642vuavisaoMin-max tree (BOI18_minmaxtree)C++20
0 / 100
104 ms15196 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") using namespace std; template<typename Lhs, typename Rhs> bool Min_self(Lhs& lhs, Rhs rhs) { if(rhs < lhs) { lhs = rhs; return true; } return false; } template<typename Lhs, typename Rhs> bool Max_self(Lhs& lhs, Rhs rhs) { if(rhs > lhs) { lhs = rhs; return true; } return false; } using ll = long long; const int N = 70'000 + 10; const int INF = 1'000'000'000; struct LazySegTree { struct Node { int val_min = -INF; int val_max = INF; int lazy_min = -2 * INF; int lazy_max = 2 * INF; Node() {}; Node(int _val_min, int _val_max, int _lazy_min, int _lazy_max) : val_min(_val_min), val_max(_val_max), lazy_min(_lazy_min), lazy_max(_lazy_max) {}; Node operator + (const Node& other) const { Node res; res.val_min = max(this->val_min, other.val_min); res.val_max = min(this->val_max, other.val_max); return res; } }; int n_node = 0; vector<Node> tree = {}; void resize(int _n) { n_node = _n; tree.resize((n_node << 2) + 10); }; LazySegTree() {}; LazySegTree(int _n) { this->resize(_n); }; void down(int node, int l, int r) { Max_self(tree[node].val_min, tree[node].lazy_min); Min_self(tree[node].val_max, tree[node].lazy_max); if (l < r) { Max_self(tree[node << 1].lazy_min, tree[node].lazy_min); Max_self(tree[node << 1 | 1].lazy_min, tree[node].lazy_min); Min_self(tree[node << 1].lazy_max, tree[node].lazy_max); Min_self(tree[node << 1 | 1].lazy_max, tree[node].lazy_max); } tree[node].lazy_min = -2 * INF; tree[node].lazy_max = 2 * INF; } void update(int node, int l, int r, int L, int R, int val_min, int val_max) { down(node, l, r); if (l > r || L > R || l > R || L > r) return; if (L <= l && r <= R) { Max_self(tree[node].lazy_min, val_min); Min_self(tree[node].lazy_max, val_max); down(node, l, r); return; } int mid = (l + r) >> 1; update(node << 1, l, mid, L, R, val_min, val_max); update(node << 1 | 1, mid + 1, r, L, R, val_min, val_max); tree[node] = tree[node << 1] + tree[node << 1 | 1]; } void Update(int l, int r, int val_min, int val_max) { return update(1, 1, n_node, l, r, val_min, val_max); } Node query(int node, int l, int r, int L, int R) { down(node, l, r); if (l > r || L > R || l > R || L > r) return Node(); if (L <= l && r <= R) return tree[node]; int mid = (l + r) >> 1; return query(node << 1, l, mid, L, R) + query(node << 1 | 1, mid + 1, r, L, R); } Node Query(int l, int r) { return query(1, 1, n_node, l, r); } }; int n, q; pair<int, int> edges[N]; vector<int> g[N]; int dist[N], parent[N], child[N]; int cnt_chain, cnt_pos; int chain_head[N], chain_of[N], inHld[N]; LazySegTree st; void dfs(int u); void hld(int u); void makeHld(); void updateHld(int u, int v, int val_min, int val_max) { while(true) { if(chain_of[u] == chain_of[v]) { int l = inHld[u], r = inHld[v]; if(l > r) swap(l, r); ++l; st.Update(l, r, val_min, val_max); break; } if(dist[chain_head[chain_of[u]]] < dist[chain_head[chain_of[v]]]) swap(u, v); int l = inHld[chain_head[chain_of[u]]]; int r = inHld[u]; st.Update(l, r, val_min, val_max); u = parent[chain_head[chain_of[u]]]; } } LazySegTree::Node queryHld(int u, int v) { LazySegTree::Node res; while(true) { if(chain_of[u] == chain_of[v]) { int l = inHld[u], r = inHld[v]; if(l > r) swap(l, r); ++l; res = res + st.Query(l, r); break; } if(dist[chain_head[chain_of[u]]] < dist[chain_head[chain_of[v]]]) swap(u, v); int l = inHld[chain_head[chain_of[u]]]; int r = inHld[u]; res = res + st.Query(l, r); u = parent[chain_head[chain_of[u]]]; } return res; } void dfs(int u) { child[u] = 1; for(const auto& v : g[u]) { if(v == parent[u]) continue; parent[v] = u; dist[v] = dist[u] + 1; dfs(v); child[u] += child[v]; } } void hld(int u) { if(chain_head[cnt_chain] == 0) chain_head[cnt_chain] = u; chain_of[u] = cnt_chain; inHld[u] = ++ cnt_pos; int big_child = - 1; for(const auto& v : g[u]) { if(v == parent[u]) continue; if(big_child == - 1 || child[v] > child[big_child]) big_child = v; } if(big_child > - 1) { hld(big_child); } for(const auto& v : g[u]) { if(v == parent[u] || v == big_child) continue; ++ cnt_chain; hld(v); } } void makeHld() { dist[1] = 1; dfs(1); cnt_chain = 1; hld(1); } void solve() { cin >> n; for (int i = 2; i <= n; ++i) { auto& [u, v] = edges[i]; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } makeHld(); cin >> q; st.resize(n); while (q-- > 0) { char type; cin >> type; int u, v; int val_min = -2 * INF, val_max = 2 * INF; cin >> u >> v; if (type == 'M') { cin >> val_max; } else { cin >> val_min; } updateHld(u, v, val_min, val_max); } for (int i = 2; i <= n; ++i) { const auto& [u, v] = edges[i]; auto [val_min, val_max, lazy_min, lazy_max] = queryHld(u, v); assert(val_min <= val_max); cout << u << ' ' << v << ' ' << val_min << '\n'; } } int32_t main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); // int t; cin >> t; int t = 1; while (t-- > 0) { solve(); cout << '\n'; } return (0 ^ 0); } /// Code by vuavisao
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...