BZOJ 4515|SDOI 2016|游戏|树链剖分

来源:互联网 发布:看美股行情软件 编辑:程序博客网 时间:2024/06/06 07:23

未完待续

#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[N], p[M], v[M], sz[N], fa[N], cnt, ts;int dep[N], pos[N], son[N], id[N], top[N], n;ll dis[N], 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 51 2 102 3 202 1 31 2 3 5 62 2 31 2 3 -5 -62 2 3

Sample Output

1234567891234567896-106

HINT

n100000m100000a10000,0<=w|b|<=109

0 0
原创粉丝点击