CodeForces 593D Happy Tree Party(树链剖分(边权) or LCA+并查集)
来源:互联网 发布:java不是方法的特征 编辑:程序博客网 时间:2024/06/01 16:57
题意:给你一棵数,n个点n-1条边,每条边有个权值,q次询问,询问有两种:
1 a b y : a到b的路径上不断进行y = y/xi(像下取整),问最后y的值
2 p c : 将第p条边的权值改为c,c一定比原来这条边的值小
思路:
树剖:线段树记录区间的乘积,查询的时候当两者重链的top相等的时候注意去掉top点的权值。
每条边的权值都是在1e18内,所以如果线段树某个元素代表的权值之乘积大于1e18的话就直接标记成0。
并查集+LCA也能做,过段时间再补。
代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e6+5;const ll INF = 1e18;int n, m, tot, k;int fa[maxn], tree[maxn], son[maxn], deep[maxn], num[maxn], top[maxn], edge_id[maxn];ll treeFac[maxn];struct node{ int u, v; ll w;}Edge[maxn];vector<int> g[maxn];void init(){ tot = k = 0; memset(son, 0, sizeof(son)); memset(num, 0, sizeof(num)); for(int i = 0; i <= n; i++) g[i].clear();}void dfs1(int u, int pre, int d){ fa[u] = pre; deep[u] = d; num[u] = 1; for(int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if(v == pre) continue; dfs1(v, u, d+1); num[u] += num[v]; if(!son[u] || num[v] > num[son[u]]) son[u] = v; }}void dfs2(int u, int tp){ tree[u] = ++tot; top[u] = tp; if(!son[u]) return ; dfs2(son[u], tp); for(int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if(v != fa[u] && v != son[u]) dfs2(v, v); }}ll mul(ll a, ll b){ if(!a || !b) return 0; if(INF/a < b) return 0; return a*b;}void pushup(int root){ treeFac[root] = mul(treeFac[root*2], treeFac[root*2+1]);}void build(int root, int l, int r){ treeFac[root] = 1; if(l == r) return ; int mid = (l+r)/2; build(root*2, l, mid); build(root*2+1, mid+1, r);}void update(int root, int l, int r, int pos, ll val){ if(l == r) { treeFac[root] = val; return ; } int mid = (l+r)/2; if(pos <= mid) update(root*2, l, mid, pos, val); else update(root*2+1, mid+1, r, pos, val); pushup(root);}ll query(int root, int l, int r, int i, int j){ if(i <= l && j >= r) return treeFac[root]; int mid = (l+r)/2; ll res = 1; if(i <= mid) res = mul(res, query(root*2, l, mid, i, j)); if(j > mid) res = mul(res, query(root*2+1, mid+1, r, i, j)); return res;}ll cal(int u, int v){ int f1 = top[u], f2 = top[v]; ll res = 1; while(f1 != f2) { if(deep[f1] < deep[f2]) swap(f1, f2), swap(u, v); res = mul(res, query(1, 1, n, tree[f1], tree[u])); u = fa[f1]; f1 = top[u]; } if(u == v) return res; if(deep[u] > deep[v]) swap(u, v); res = mul(res, query(1, 1, n, tree[son[u]], tree[v])); return res;}int main(void){ while(cin >> n >> m) { init(); int u, v; ll w; for(int i = 1; i < n; i++) { scanf("%d%d%I64d", &u, &v, &w); g[u].push_back(v); g[v].push_back(u); Edge[i].u = u, Edge[i].v = v, Edge[i].w = w; } dfs1(1, 0, 0); dfs2(1, 1); build(1, 1, n); for(int i = 1; i < n; i++) { if(deep[Edge[i].u] > deep[Edge[i].v]) edge_id[i] = Edge[i].u; else edge_id[i] = Edge[i].v; } for(int i = 1; i < n; i++) update(1, 1, n, tree[edge_id[i]], Edge[i].w); while(m--) { int cmd; scanf("%d", &cmd); if(cmd == 1) { scanf("%d%d%I64d", &u, &v, &w); ll ans = cal(u, v); if(!ans) puts("0"); else printf("%I64d\n", w/ans); } else { scanf("%d%I64d", &u, &w); update(1, 1, n, tree[edge_id[u]], w); } } } return 0;}
阅读全文
1 0
- CodeForces 593D Happy Tree Party(树链剖分(边权) or LCA+并查集)
- codeforces 593 D. Happy Tree Party (LCA + 并查集)
- codeforces 593D Happy Tree Party Lca+并查集
- codeforces #329 D. Happy Tree Party (LCA+并查集 || 树链剖分)
- [CodeForces 593D]Happy Tree Party[LCA][路径压缩]
- LCA + 并查集 Happy Tree Party codeforces593D
- 【Codeforces Round 329 (Div 2) D】【LCA+并查集路径压缩】Happy Tree Party 除上两点间路径全部权值
- Codeforces 593D - Happy Tree Party(树链剖分)
- 【13.91%】【codeforces 593D】Happy Tree Party
- CodeForces 593D Happy Tree Party
- Codeforces 593D-Happy Tree Party
- Codeforces Round #329 D Happy Tree Party(树链剖分)
- codeforces 593D(并查集 + LCA)
- Codeforces Round #329 (Div. 2) 593D. Happy Tree Party(树链剖分)
- Summer Training day6 codeforces 593D LCA+并查集
- Codeforces Round #329 (Div. 2) D. Happy Tree Party(树链剖分)
- Codeforces Round #329 (Div. 2) D. Happy Tree Party
- 并查集 codeforces party
- ISwipeRefreshLayout
- 简单矩阵转置
- Git常用操作总结
- javase基础01
- 退出App前的一个判断
- CodeForces 593D Happy Tree Party(树链剖分(边权) or LCA+并查集)
- 素数距离问题
- Giraph调试奇怪问题记录
- 坚持到底
- PX4固定翼调试校准流程及实验相关问题记录分析
- 使用for循环输出九九乘法表
- 编译器的差别gcc和VS
- 一个弱鸡刚开通博客
- Spring-国际化信息01-基础知识