P4315 月下“毛景树”

Description

  • 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园。 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里。

    爬啊爬~爬啊爬毛毛虫爬到了一颗小小的“毛景树”下面,发现树上长着他最爱吃的毛毛果~ “毛景树”上有N个节点和N-1条树枝,但节点上是没有毛毛果的,毛毛果都是长在树枝上的。但是这棵“毛景树”有着神奇的魔力,他能改变树枝上毛毛果的个数:

    • Change k w:将第k条树枝上毛毛果的个数改变为w个。
    • Cover u v w:将节点u与节点v之间的树枝上毛毛果的个数都改变为w个。
    • Add u v w:将节点u与节点v之间的树枝上毛毛果的个数都增加w个。 由于毛毛虫很贪,于是他会有如下询问:
    • Max u v:询问节点u与节点v之间树枝上毛毛果个数最多有多少个。

Input

  • 第一行一个正整数N。

    接下来N-1行,每行三个正整数Ui,Vi和Wi,第i+1行描述第i条树枝。表示第i条树枝连接节点Ui和节点Vi,树枝上有Wi个毛毛果。 接下来是操作和询问,以“Stop”结束。

Output

  • 对于毛毛虫的每个询问操作,输出一个答案。

Sample Input

Sample Output

Data Size

  • 1<=N<=100,000,操作+询问数目不超过100,000。

    保证在任意时刻,所有树枝上毛毛果的个数都不会超过10^9个。

题解:

  • 树剖。
  • 这显然就是一个树剖模板嘛,主要是Cover操作有点麻烦。感谢ly学长的毒瘤题目让我yy出了区间覆盖lazytag操作。
  • 当一个节点接收到一个覆盖标记时,立即把其修改标记清零。这样在pushdown里就不会出现俩标记并存的情况。
  • 有几个细节要注意:首先它是边权,要转成点权。其次Change操作要操作的点是深度大的那个。
#include <iostream>
#include <cstdio>
#define p1 (p << 1)
#define p2 (p << 1 | 1)
#define N 100005
using namespace std;

struct T {int l, r, mx, tg1, tg2;} t[N * 4];
struct E {int next, to, dis;} e[N * 2];
int n, num, dex, cnt;
int a[N], head[N], fat[N], son[N], dep[N], size[N];
int h[N], dfn[N], top[N], val[N], t1[N], t2[N];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

void add(int u, int v, int w)
{
    e[++num].next = h[u];
    e[num].to = v;
    e[num].dis = w;
    h[u] = num;
}

void dfs1(int x, int fa, int de)
{
    size[x] = 1, fat[x] = fa, dep[x] = de;
    int maxSon = 0;
    for(int i = h[x]; i != 0; i = e[i].next)
        if(e[i].to != fa)
        {
            dfs1(e[i].to, x, de + 1);
            a[e[i].to] = e[i].dis;
            size[x] += size[e[i].to];
            if(size[e[i].to] > maxSon)
            {
                maxSon = size[e[i].to];
                son[x] = e[i].to;
            }
        }
}

void dfs2(int x, int head)
{
    top[x] = head, dfn[x] = ++dex, val[dex] = a[x];
    if(!son[x]) return;
    dfs2(son[x], head);
    for(int i = h[x]; i != 0; i = e[i].next)
        if(e[i].to != fat[x] && e[i].to != son[x])
            dfs2(e[i].to, e[i].to);
}

void up(int p) {t[p].mx = max(t[p1].mx, t[p2].mx);}
void down(int p)
{
    if(t[p].tg1)
    {
        t[p1].tg1 = t[p2].tg1 = t[p].tg1;
        t[p1].tg2 = t[p2].tg2 = 0;
        t[p1].mx = t[p2].mx = t[p].tg1;
        t[p].tg1 = 0;
    }
    if(t[p].tg2)
    {
        t[p1].tg2 += t[p].tg2, t[p2].tg2 += t[p].tg2;
        t[p1].mx += t[p].tg2, t[p2].mx += t[p].tg2;
        t[p].tg2 = 0;
    }
}

void build(int p, int l, int r)
{
    t[p].l = l, t[p].r = r;
    if(l == r) {t[p].mx = val[l]; return;}
    int mid = l + r >> 1;
    build(p1, l, mid), build(p2, mid + 1, r);
    up(p);
}

void upd1(int p, int l, int r, int val)
{
    if(t[p].l >= l && t[p].r <= r)
        {t[p].tg1 = t[p].mx = val, t[p].tg2 = 0; return;}
    down(p);
    int mid = t[p].l + t[p].r >> 1;
    if(l <= mid) upd1(p1, l, r, val);
    if(r > mid) upd1(p2, l, r, val);
    up(p);
}

void upd2(int p, int l, int r, int val)
{
    if(t[p].l >= l && t[p].r <= r)
        {t[p].tg2 += val, t[p].mx += val; return;}
    down(p);
    int mid = t[p].l + t[p].r >> 1;
    if(l <= mid) upd2(p1, l, r, val);
    if(r > mid) upd2(p2, l, r, val);
    up(p);
}

int ask(int p, int l, int r)
{
    if(t[p].l >= l && t[p].r <= r) return t[p].mx;
    down(p);
    int mid = t[p].l + t[p].r >> 1, res = 0;
    if(l <= mid) res = max(res, ask(p1, l, r));
    if(r > mid) res = max(res, ask(p2, l, r));
    return res;
}

void updLink(int x, int y, int val, int op)
{
    while(top[x] != top[y])
    {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        if(!op) upd1(1, dfn[top[x]], dfn[x], val);
        else upd2(1, dfn[top[x]], dfn[x], val);
        x = fat[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    if(!op) upd1(1, dfn[x] + 1, dfn[y], val);
    else upd2(1, dfn[x] + 1, dfn[y], val);
}

int askLink(int x, int y)
{
    int res = 0;
    while(top[x] != top[y])
    {
        if(dep[top[x]] < dep[top[y]]) swap(x, y);
        res = max(res, ask(1, dfn[top[x]], dfn[x]));
        x = fat[top[x]];
    }
    if(dep[x] > dep[y]) swap(x, y);
    return max(res, ask(1, dfn[x] + 1, dfn[y]));
}

int main()
{
    cin >> n;
    for(int i = 1; i < n; i++)
    {
        int u = read(), v = read(), w = read();
        add(u, v, w), add(v, u, w);
        t1[++cnt] = u, t2[cnt] = v;
    }
    dfs1(1, 0, 1), dfs2(1, 1), build(1, 1, n);
    while(1)
    {
        char c[8]; scanf("%s", c);
        if(c[0] == 'S') break;
        else if(c[0] == 'C' && c[1] == 'h')
        {
            int x = read(), w = read();
            int u = t1[x], v = t2[x];
            if(dep[u] < dep[v]) swap(u, v);
            upd1(1, dfn[u], dfn[u], w);
        }
        else if(c[0] == 'C' && c[1] == 'o')
        {
            int u = read(), v = read(), w = read();
            updLink(u, v, w, 0);
        }
        else if(c[0] == 'A')
        {
            int u = read(), v = read(), w = read();
            updLink(u, v, w, 1);
        }
        else if(c[0] == 'M')
        {
            int u = read(), v = read();
            printf("%lld\n", askLink(u, v));
        }
    }
    return 0;
}
01-07 15:27