树剖萌新求助,AC了两个,RE了十个点
查看原帖
树剖萌新求助,AC了两个,RE了十个点
53022
银杉水杉秃杉楼主2021/9/1 19:20

rt,代码如下

#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        f = ch == '-' ? 0 : 1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return f ? x : -x;
}
const int N = 2e5 + 10, inf = 2147483647;
int n, m, t, cnt;
int head[N], f[N], dep[N], siz[N], son[N], top[N], id[N];
int a[N], b[N];
struct edge
{
    int to, next, val;
} e[N << 1];
struct tree
{
    int min, max, sum, tag;
} tr[N << 2];
void addedge(int u, int v, int w)
{
    e[++t] = (edge){v, head[u], w};
    head[u] = t;
}
void dfs1(int u, int fa)
{
    f[u] = fa;
    dep[u] = dep[fa] + 1;
    siz[u] = 1;
    for (int i = head[u]; i; i = e[i].next)
    {
        int v = e[i].to, w = e[i].val;
        if (v == fa)
            continue;
        a[v] = w;
        dfs1(v, u);
        siz[u] += siz[v];
        if (siz[v] > siz[son[u]])
            son[u] = v;
    }
}
void dfs2(int u, int tp)
{
    id[u] = ++cnt;
    b[cnt] = a[u];
    top[u] = tp;
    if (son[u])
        dfs2(son[u], tp);
    for (int i = head[u]; i; i = e[i].next)
    {
        int v = e[i].to;
        if (v == f[u] || v == son[u])
            continue;
        dfs2(v, v);
    }
}
void pushup(int p)
{
    tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
    tr[p].max = max(tr[p << 1].max, tr[p << 1 | 1].max);
    tr[p].min = min(tr[p << 1].min, tr[p << 1 | 1].min);
}
void build(int p, int l, int r)
{
    if (l == r)
    {
        tr[p].sum = tr[p].max = tr[p].min = b[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}
void change(int p)
{
    tr[p].tag ^= 1;
    tr[p].sum = -tr[p].sum;
    tr[p].max = -tr[p].max;
    tr[p].min = -tr[p].min;
    swap(tr[p].max, tr[p].min);
}
void pushdown(int p)
{
    if (tr[p].tag)
    {
        change(p << 1);
        change(p << 1 | 1);
        tr[p].tag = 0;
    }
}
void update1(int p, int l, int r, int x, int z)
{
    if (l == r)
    {
        tr[p].sum = tr[p].max = tr[p].min = z;
        return;
    }
    pushdown(p);
    int mid = (l + r) >> 1;
    if (x <= mid)
        update1(p << 1, l, mid, x, z);
    else
        update1(p << 1 | 1, mid + 1, r, x, z);
    pushup(p);
}
void update2(int p, int l, int r, int x, int y)
{
    if (x <= l && y >= r)
    {
        change(p);
        return;
    }
    pushdown(p);
    int mid = (l + r) >> 1;
    if (x <= mid)
        update2(p << 1, l, mid, x, y);
    if (y > mid)
        update2(p << 1 | 1, mid + 1, r, x, y);
    pushup(p);
}
int query1(int p, int l, int r, int x, int y)
{
    if (x <= l && y >= r)
        return tr[p].sum;
    pushdown(p);
    int mid = (l + r) >> 1, ans = 0;
    if (x <= mid)
        ans += query1(p << 1, l, mid, x, y);
    if (y > mid)
        ans += query1(p << 1 | 1, mid + 1, r, x, y);
    return ans;
}
int query2(int p, int l, int r, int x, int y)
{
    if (x <= l && y >= r)
        return tr[p].max;
    pushdown(p);
    int mid = (l + r) >> 1, ans = -inf;
    if (x <= mid)
        ans = max(ans, query2(p << 1, l, mid, x, y));
    if (y > mid)
        ans = max(ans, query2(p << 1 | 1, mid + 1, r, x, y));
    return ans;
}
int query3(int p, int l, int r, int x, int y)
{
    if (x <= l && y >= r)
        return tr[p].min;
    pushdown(p);
    int mid = (l + r) >> 1, ans = inf;
    if (x <= mid)
        ans = min(ans, query3(p << 1, l, mid, x, y));
    if (y > mid)
        ans = min(ans, query3(p << 1 | 1, mid + 1, r, x, y));
    return ans;
}
void update(int x, int y)
{
    while (top[x] != top[y])
    {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        update2(1, 1, n, id[top[x]], id[x]);
        x = f[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    if (x != y)
        update2(1, 1, n, id[x] + 1, id[y]);
}
int qsum(int x, int y)
{
    int ans = 0;
    while (top[x] != top[y])
    {
        if (dep[top[x]] != dep[top[y]])
            swap(x, y);
        ans += query1(1, 1, n, id[top[x]], id[x]);
        x = f[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    if (x != y)
        ans += query1(1, 1, n, id[x] + 1, id[y]);
    return ans;
}
int qmax(int x, int y)
{
    int ans = -inf;
    while (top[x] != top[y])
    {
        if (dep[top[x]] != dep[top[y]])
            swap(x, y);
        ans = max(ans, query2(1, 1, n, id[top[x]], id[x]));
        x = f[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    if (x != y)
        ans = max(ans, query2(1, 1, n, id[x] + 1, id[y]));
    return ans;
}
int qmin(int x, int y)
{
    int ans = inf;
    while (top[x] != top[y])
    {
        if (dep[top[x]] != dep[top[y]])
            swap(x, y);
        ans = min(ans, query3(1, 1, n, id[top[x]], id[x]));
        x = f[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    if (x != y)
        ans = min(ans, query3(1, 1, n, id[x] + 1, id[y]));
    return ans;
}
signed main()
{
    n = read();
    for (int i = 1; i < n; i++)
    {
        int x = read() + 1, y = read() + 1, z = read();
        addedge(x, y, z);
        addedge(y, x, z);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    build(1, 1, n);
    m = read();
    while (m--)
    {
        string s;
        cin >> s;
        int x = read() + 1, y = read() + 1;
        if (s == "C")
        {
            x--, y--;
            int u = e[2 * x - 1].to, v = e[2 * x].to;
            x = f[u] == v ? u : v;
            update1(1, 1, n, id[x], y);
        }
        else if (s == "N")
        {
            update(x, y);
        }
        else if (s == "SUM")
        {
            printf("%lld\n", qsum(x, y));
        }
        else if (s == "MAX")
        {
            printf("%lld\n", qmax(x, y));
        }
        else
        {
            printf("%lld\n", qmin(x, y));
        }
    }
    return 0;
}

调了两小时了,一直20分,呜呜呜,QwQ

2021/9/1 19:20
加载中...