P1505 [国家集训队]旅游

哈!树剖+线段树的水题!

讲道理前面的那道“月下毛景树”已经把这种化边权为点权的考的淋漓尽致了。

特别在于这些题的码量

我早上就直接打了一遍,然后就美妙地WA了。

下午再随便地打一遍,突然明白了早上错了哪里。

重新摸一遍板子,交上去,A了。。。

把早上那个唯一写错的地方改了,交上去,也A了。。。

这个故事告诉我们细心写好当前的每一句代码是多么重要。

不然你就要调得心态爆炸了哦

这道题的线段树修改也简单,最大最小值互换后取相反数,区间和也取相反数就完事了。

代码:

#include<cstdio>
#include<algorithm>
#define ll long long 
const ll maxn = 20005;
const ll INF = 0x3f3f3f3f;// ????????????????????????????///
struct Edges
{
    ll next, to, weight, id;
} e[maxn << 1];
ll head[maxn], tot;
ll n, m;
//
ll dep[maxn], size[maxn], wson[maxn], fa[maxn], weight[maxn], id[maxn];
ll top[maxn], dfn[maxn], pre[maxn], dtot;
//
struct segTree
{
    ll sum[maxn << 2], minv[maxn << 2], maxv[maxn << 2], lazy[maxn << 2];
    #define lson (root << 1)
    #define rson (root << 1 | 1)
    void pushup(ll root)
    {
        sum[root] = sum[lson] + sum[rson];
        minv[root] = std::min(minv[lson], minv[rson]);
        maxv[root] = std::max(maxv[lson], maxv[rson]);
    }
    void pushdown(ll root)
    {
        if(lazy[root])
        {
            ll temp;
            sum[lson] = -sum[lson];
            temp = minv[lson];
            minv[lson] = -maxv[lson];
            maxv[lson] = -temp;
            lazy[lson] ^= 1;
            sum[rson] = -sum[rson];
            temp = minv[rson];
            minv[rson] = -maxv[rson];
            maxv[rson] = -temp;
            lazy[rson] ^= 1;
            lazy[root] = 0;
        }
    }
    void build(ll root, ll l, ll r)
    {
        lazy[root] = 0;
        if(l == r) sum[root] = minv[root] = maxv[root] = weight[pre[l]];
        else
        {
            ll mid = (l + r) >> 1;
            build(lson, l, mid);
            build(rson, mid + 1, r);
            pushup(root);
        }
    }
    void singal_update(ll root, ll l, ll r, ll p, ll k)
    {
        if(l == r) sum[root] = minv[root] = maxv[root] = k;
        else
        {
            pushdown(root);
            ll mid = (l + r) >> 1;
            if(p <= mid) singal_update(lson, l, mid, p, k);
            else singal_update(rson, mid + 1, r, p, k);
            pushup(root);
        }
    }
    void interval_update(ll root, ll l, ll r, ll x, ll y)
    {
        if(r < x || y < l) return;
        if(x <= l && r <= y)
        {
            sum[root] = -sum[root];
            ll temp = minv[root];
            minv[root] = -maxv[root];
            maxv[root] = -temp;
            lazy[root] ^= 1;
            return;
        }
        pushdown(root);
        ll mid = (l + r) >> 1;
        interval_update(lson, l, mid, x, y);
        interval_update(rson, mid + 1, r, x, y);
        pushup(root);
    }
    ll querysum(ll root, ll l, ll r, ll x, ll y)
    {
        if(r < x || y < l) return 0;
        if(x <= l && r <= y) return sum[root];
        pushdown(root);
        ll mid = (l + r) >> 1;
        return querysum(lson, l, mid, x, y) + querysum(rson, mid + 1, r, x, y);
    }
    ll querymin(ll root, ll l, ll r, ll x, ll y)
    {
        if(r < x || y < l) return INF;
        if(x <= l && r <= y) return minv[root];
        pushdown(root);
        ll mid = (l + r) >> 1;
        return std::min(querymin(lson, l, mid, x, y), querymin(rson, mid + 1, r, x, y));
    }
    ll querymax(ll root, ll l, ll r, ll x, ll y)
    {
        if(r < x || y < l) return -INF;
        if(x <= l && r <= y) return maxv[root];
        pushdown(root);
        ll mid = (l + r) >> 1;
        return std::max(querymax(lson, l, mid, x, y), querymax(rson, mid + 1, r, x, y));
    }
} seg;

ll read()
{
    ll ans = 0, s = 1;
    char ch = getchar();
    while(ch > '9' || ch < '0'){ if(ch == '-') s = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
    return s * ans;
}
void link(ll u, ll v, ll w, ll i)
{
    e[++tot] = (Edges){head[u], v, w, i};
    head[u] = tot;
}
void dfs1(ll u, ll f)
{
    dep[u] = dep[f] + 1; fa[u] = f; size[u] = 1;
    for(ll i = head[u]; i; i = e[i].next)
    {
        ll v = e[i].to;
        if(v == f) continue;
        dfs1(v, u);
        size[u] += size[v];
        weight[v] = e[i].weight;
        id[e[i].id] = v;
        if(size[v] > size[wson[u]]) wson[u] = v;
    }
}
void dfs2(ll u, ll topf)
{
    top[u] = topf; dfn[u] = ++dtot; pre[dtot] = u;
    if(wson[u]) dfs2(wson[u], topf);
    for(ll i = head[u]; i; i = e[i].next)
    {
        ll v = e[i].to;
        if(v == fa[u] || v == wson[u]) continue;
        dfs2(v, v);
    }
}
void N(ll u, ll v)
{
    while(top[u] != top[v])
    {
        if(dep[top[u]] < dep[top[v]]) std::swap(u, v);
        seg.interval_update(1, 1, n, dfn[top[u]], dfn[u]);
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) std::swap(u, v);
    seg.interval_update(1, 1, n, dfn[u] + 1, dfn[v]);
}
ll SUM(ll u, ll v)
{
    ll ans = 0;
    while(top[u] != top[v])
    {
        if(dep[top[u]] < dep[top[v]]) std::swap(u, v);
        ans += seg.querysum(1, 1, n, dfn[top[u]], dfn[u]);
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) std::swap(u, v);
    ans += seg.querysum(1, 1, n, dfn[u] + 1, dfn[v]);
    return ans;
}
ll MAX(ll u, ll v)
{
    ll ans = -INF;
    while(top[u] != top[v])
    {
        if(dep[top[u]] < dep[top[v]]) std::swap(u, v);
        ans = std::max(ans, seg.querymax(1, 1, n, dfn[top[u]], dfn[u]));
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) std::swap(u, v);
    ans = std::max(ans, seg.querymax(1, 1, n, dfn[u] + 1, dfn[v]));
    return ans;
}
ll MIN(ll u, ll v)
{
    ll ans = INF;
    while(top[u] != top[v])
    {
        if(dep[top[u]] < dep[top[v]]) std::swap(u, v);
        ans = std::min(ans, seg.querymin(1, 1, n, dfn[top[u]], dfn[u]));
        u = fa[top[u]];
    }
    if(dep[u] > dep[v]) std::swap(u, v);
    ans = std::min(ans, seg.querymin(1, 1, n, dfn[u] + 1, dfn[v]));
    return ans;
}
int main()
{
    n = read();
    for(int i = 1; i < n; i++)
    {
        int u = read(), v = read(), w = read();
        link(u + 1, v + 1, w, i); link(v + 1, u + 1, w, i);
    }
    dfs1(1, 0); dfs2(1, 1);
    seg.build(1, 1, n);
    m = read();
    char opt[10];
    while(m--)
    {
        scanf("%s", opt); ll u = read(), v = read();
        if(opt[0] == 'C')
        {
            seg.singal_update(1, 1, n, dfn[id[u]], v);
        }
        else if(opt[0] == 'N')
        {
            N(u + 1, v + 1);
        }
        else if(opt[0] == 'S')
        {
            printf("%lld\n", SUM(u + 1, v + 1));
        }
        else if(opt[1] == 'A')
        {
            printf("%lld\n", MAX(u + 1, v + 1));
        }
        else if(opt[1] == 'I')
        {
            printf("%lld\n", MIN(u + 1, v + 1));
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/Garen-Wang/p/9892802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值