BZOJ 4515|SDOI 2016|游戏|树链剖分
2016-04-28 17:20
417 查看
未完待续
游戏在一棵有 n 个点的树上进行。最初,每个点上都只有一个数字,那个数字是 123456789123456789。
有时,Alice 会选择一条从 s 到 t 的路径,在这条路径上的每一个点上都添加一个数字。对于路径上的一个点 r,
若 r 与 s 的距离是 dis,那么 Alice 在点 r 上添加的数字是 a×dis+b。有时,Bob 会选择一条从 s 到 t 的路径。
他需要先从这条路径上选择一个点,再从那个点上选择一个数字。
Bob 选择的数字越小越好,但大量的数字让 Bob 眼花缭乱。Bob 需要你帮他找出他能够选择的最小的数字。
接下来 n−1 行,每行三个数字 u、v、w,表示树上有一条连接 u、v 的边,长度是 w。
接下来 m 行。每行第一个数字是 1 或 2。
若第一个数是 1,表示 Alice 进行操作,接下来四个数字 s、t、a、b。
若第一个数是 2,表示 Bob 进行操作,接下来四个数字 s、t。
#include <cstdio> #include <algorithm> #include <cstring> typedef long long ll; using namespace std; const int N = 100005, M = N * 2, S = M * 2; int h , p[M], v[M], sz , fa , cnt, ts; int dep , pos , son , id , top , n; ll dis , w[M], mn[S], tagA[S], tagB[S]; bool tag[N * 4]; void add(int a, int b, ll c) { p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt; } void dfs(int x) { sz[x] = 1; for (int i = h[x]; i; i = p[i]) if (v[i] != fa[x]) { dis[v[i]] = dis[x] + w[i]; dep[v[i]] = dep[x] + 1; fa[v[i]] = x; dfs(v[i]); if (sz[son[x]] < sz[v[i]]) son[x] = v[i]; sz[x] += sz[v[i]]; } } void dfs2(int x, int t) { pos[x] = ++ts; id[ts] = x; top[x] = t; if (son[x]) dfs2(son[x], t); for (int i = h[x]; i; i = p[i]) if (v[i] != son[x] && v[i] != fa[x]) dfs2(v[i], v[i]); } int lca(int a, int b) { while (top[a] != top[b]) { if (dep[top[a]] < dep[top[b]]) swap(a, b); a = fa[top[a]]; } return dep[a] < dep[b] ? a : b; } void update(int t, int l, int r) { if (tag[t]) mn[t] = min(tagA[t] * dis[id[l]], tagA[t] * dis[id[r]]) + tagB[t]; if (l != r) mn[t] = min(mn[t], min(mn[t * 2], mn[t * 2 + 1])); } void push(int t, int l, int r, ll a, ll b) { if (!tag[t]) { tag[t] = 1; tagA[t] = a; tagB[t] = b; update(t, l, r); return; } int mid = l + r >> 1; ll x1 = tagA[t] * dis[id[l]] + tagB[t], y1 = tagA[t] * dis[id[r]] + tagB[t], x2 = a * dis[id[l]] + b, y2 = a * dis[id[r]] + b; if (x2 <= x1 && y2 <= y1) { tagA[t] = a; tagB[t] = b; } else if (x2 >= x1 && y2 >= y1) return; else if (a < tagA[t]) { ll k = (b - tagB[t]) / (tagA[t] - a) + 1; if (k <= dis[id[mid]]) { swap(a, tagA[t]); swap(b, tagB[t]); push(t * 2, l, mid, a, b); } else push(t * 2 + 1, mid + 1, r, a, b); } else { ll k = (b - tagB[t] - 1) / (tagA[t] - a); if (k > dis[id[mid]]) { swap(a, tagA[t]); swap(b, tagB[t]); push(t * 2 + 1, mid + 1, r, a, b); } else push(t * 2, l, mid, a, b); } update(t, l, r); } void modify(int t, int l, int r, int ql, int qr, ll a, ll b) { if (ql <= l && r <= qr) push(t, l, r, a, b); else { int mid = l + r >> 1; if (ql <= mid) modify(t * 2, l, mid, ql, qr, a, b); if (qr > mid) modify(t * 2 + 1, mid + 1, r, ql, qr, a, b); } update(t, l, r); } void tree_modify(int x, int y, ll a, ll b) { for (; top[x] != top[y]; x = fa[top[x]]) modify(1, 1, n, pos[top[x]], pos[x], a, b); modify(1, 1, n, pos[y], pos[x], a, b); } void operation_add(int x, int y, ll a, ll b) { int l = lca(x, y); tree_modify(x, l, -a, b + a * dis[x]); tree_modify(y, l, a, b + a * (dis[x] - 2 * dis[l])); } ll query(int t, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return mn[t]; ll ans = mn[0]; if (tag[t]) ans = min(ans, min(tagA[t] * dis[id[max(ql, l)]], tagA[t] * dis[id[min(qr, r)]]) + tagB[t]); int mid = l + r >> 1; if (ql <= mid) ans = min(ans, query(t * 2, l, mid, ql, qr)); if (qr > mid) ans = min(ans, query(t * 2 + 1, mid + 1,r, ql, qr)); return ans; } ll tree_query(int x, int y) { ll ans = mn[0]; for (; top[x] != top[y]; x = fa[top[x]]) ans = min(ans, query(1, 1, n, pos[top[x]], pos[x])); return min(ans, query(1, 1, n, pos[y], pos[x])); } ll operation_ask(int a, int b) { int c = lca(a, b); return min(tree_query(a, c), tree_query(b, c)); } int main() { int i, a, b, op, n, m; ll c, d; scanf("%d%d", &n, &m); for (i = 1; i < n; ++i) { scanf("%d%d%lld", &a, &b, &c); add(a,b,c);add(b,a,c); } dfs(1); dfs2(1, 1); for (i = 0; i <= 4 * n; i++) mn[i] = 123456789123456789ll; while (m--) { scanf("%d", &op); if (op == 1) { scanf("%d%d%lld%lld", &a, &b, &c, &d); operation_add(a, b, c, d); } else { scanf("%d%d", &a, &b); printf("%lld\n", operation_ask(a, b)); } } return 0; }
4515: [Sdoi2016]游戏
Description
Alice 和 Bob 在玩一个游戏。游戏在一棵有 n 个点的树上进行。最初,每个点上都只有一个数字,那个数字是 123456789123456789。
有时,Alice 会选择一条从 s 到 t 的路径,在这条路径上的每一个点上都添加一个数字。对于路径上的一个点 r,
若 r 与 s 的距离是 dis,那么 Alice 在点 r 上添加的数字是 a×dis+b。有时,Bob 会选择一条从 s 到 t 的路径。
他需要先从这条路径上选择一个点,再从那个点上选择一个数字。
Bob 选择的数字越小越好,但大量的数字让 Bob 眼花缭乱。Bob 需要你帮他找出他能够选择的最小的数字。
Input
第一行两个数字 n、m,表示树的点数和进行的操作数。接下来 n−1 行,每行三个数字 u、v、w,表示树上有一条连接 u、v 的边,长度是 w。
接下来 m 行。每行第一个数字是 1 或 2。
若第一个数是 1,表示 Alice 进行操作,接下来四个数字 s、t、a、b。
若第一个数是 2,表示 Bob 进行操作,接下来四个数字 s、t。
Output
每当 Bob 进行操作,输出一行一个数,表示他能够选择的最小的数字Sample Input
3 5 1 2 10 2 3 20 2 1 3 1 2 3 5 6 2 2 3 1 2 3 -5 -6 2 2 3
Sample Output
123456789123456789 6 -106
HINT
n≤100000,m≤100000,∣a∣≤10000,0<=w,|b|<=109相关文章推荐
- [BZOJ2424][HAOI2010]订货(费用流)
- win10输入法问题 中英文切换切不出来
- Leetcode no. 78
- iOS 项目知识积累
- 十进制转二
- css去掉iPhone、iPad默认按钮样式
- CircleProgressBar For Android(圆形进度条)
- 关于Button点击两次才响应事件相关问题
- Hello World!
- 套接字原理----socket
- iOS 证书更新,打包APP
- easyUI使用准备
- Javascript函数节流
- 16
- NSDataDetector,字符串中检测URL
- python urllib爬取网页编码问题
- python urllib爬取网页编码问题
- python urllib爬取网页编码问题
- python urllib爬取网页编码问题
- python urllib爬取网页编码问题