[BZOJ2157] 旅游 边权树链剖分

本文介绍了一种结合重链剖分与线段树的数据结构优化方案,用于解决涉及树形结构上的区间查询与更新问题。通过将树转化为重链表示,并利用线段树进行区间操作,有效提升了查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于边权,我们将其赋值到树边的子节点上就好

查询,修改时,只需要在两点都在一条重链时,序号小的点+1即可

(x,y,min,max写混WA了一发,气,另外需要改一改线段树的写法了,又臭又长)

/**************************************************************
    Problem: 2157
    User: cabinfever
    Language: C++
    Result: Accepted
    Time:584 ms
    Memory:3592 kb
****************************************************************/
 
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
const int maxn = 20100;
 
int epos[maxn];
int ee[maxn][3];
struct edge
{
    int v,next;
}e[maxn << 1];
int h[maxn],num = 0;
struct node
{
    int l,r;
    int minn,summ,maxx,mark;
}t[maxn << 1];
int tot = 0;
int a[maxn];
bool vis[maxn];
int n,m,u,v,x,y,d,q;
int cnt = 0;
int fa[maxn];
int dep[maxn];
int son[maxn];
int pos[maxn],ppos[maxn];
int top[maxn];
int size[maxn];
 
void build_edge(int u,int v)
{
    num++;
    e[num].v = v;
    e[num].next = h[u];
    h[u] = num;
}
 
void dfs1(int x)
{
    dep[x] = dep[fa[x]] + 1;
    vis[x] = true;
    size[x] = 1;
    son[x] = 0;
    for(int i = h[x]; i; i = e[i].next)
    {
        if(vis[e[i].v])
            continue;
        fa[e[i].v] = x;
        dfs1(e[i].v);
        if(size[e[i].v] > size[son[x]])
            son[x] = e[i].v;
        size[x] += size[e[i].v];
    }
}
 
void dfs2(int x)
{
    vis[x] = true;
    if(son[fa[x]] == x)
        top[x] = top[fa[x]];
    else
    {
        top[x] = x;
        for(int i = x; i; i = son[i])
        {
            pos[i] = ++cnt;
            ppos[cnt] = i;
        }
    }
    for(int i = h[x]; i; i = e[i].next)
        if(!vis[e[i].v])
            dfs2(e[i].v);
}
 
void build(int p,int l,int r)
{
    int mid = l + r >> 1;
    if(l == r)
        return;
    t[p].l = ++tot;
    t[p].r = ++tot;
    build(t[p].l,l,mid);
    build(t[p].r,mid+1,r);
}
 
void change1(int p,int l,int r,int q,int c)
{
    int mid = l + r >> 1;
    if(l == r)
    {
        t[p].maxx = c;
        t[p].minn = c;
        t[p].summ = c;
        return;
    }
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(q <= mid)
    {
        change1(t[p].l,l,mid,q,c);
        t[p].maxx = max(t[t[p].l].maxx,t[t[p].r].maxx);
        t[p].minn = min(t[t[p].l].minn,t[t[p].r].minn);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
    else
    {
        change1(t[p].r,mid+1,r,q,c);
        t[p].maxx = max(t[t[p].l].maxx,t[t[p].r].maxx);
        t[p].minn = min(t[t[p].l].minn,t[t[p].r].minn);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
}
 
void change2(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r == R)
    {
        t[p].summ = -t[p].summ;
        swap(t[p].minn,t[p].maxx);
        t[p].minn = -t[p].minn;
        t[p].maxx = -t[p].maxx;
        t[p].mark ^= 1;
        return;
    }
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
    {
        change2(t[p].l,l,mid,L,R);
        t[p].minn = min(t[t[p].r].minn,t[t[p].l].minn);
        t[p].maxx = max(t[t[p].r].maxx,t[t[p].l].maxx);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
    else if(L > mid)
    {
        change2(t[p].r,mid+1,r,L,R);
        t[p].minn = min(t[t[p].r].minn,t[t[p].l].minn);
        t[p].maxx = max(t[t[p].r].maxx,t[t[p].l].maxx);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
    else
    {
        change2(t[p].l,l,mid,L,mid);
        change2(t[p].r,mid+1,r,mid+1,R);
        t[p].minn = min(t[t[p].r].minn,t[t[p].l].minn);
        t[p].maxx = max(t[t[p].r].maxx,t[t[p].l].maxx);
        t[p].summ = t[t[p].r].summ + t[t[p].l].summ;
    }
}
 
int getMax(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r == R)
        return t[p].maxx;
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
        return getMax(t[p].l,l,mid,L,R);
    else if(L > mid)
        return getMax(t[p].r,mid+1,r,L,R);
    return max(getMax(t[p].l,l,mid,L,mid),getMax(t[p].r,mid+1,r,mid+1,R));
}
 
int getMin(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r == R)
        return t[p].minn;
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
        return getMin(t[p].l,l,mid,L,R);
    else if(L > mid)
        return getMin(t[p].r,mid+1,r,L,R);
    return min(getMin(t[p].l,l,mid,L,mid),getMin(t[p].r,mid+1,r,mid+1,R));
}
 
int getSum(int p,int l,int r,int L,int R)
{
    int mid = l + r >> 1;
    if(l == L && r ==  R)
        return t[p].summ;
    if(t[p].mark)
    {
        t[t[p].l].summ = -t[t[p].l].summ;
        t[t[p].r].summ = -t[t[p].r].summ;
        swap(t[t[p].l].minn,t[t[p].l].maxx);
        swap(t[t[p].r].minn,t[t[p].r].maxx);
        t[t[p].l].minn = -t[t[p].l].minn;
        t[t[p].r].minn = -t[t[p].r].minn;
        t[t[p].l].maxx = -t[t[p].l].maxx;
        t[t[p].r].maxx = -t[t[p].r].maxx;
        t[p].mark ^= 1;
        t[t[p].l].mark ^= 1;
        t[t[p].r].mark ^= 1;
    }
    if(R <= mid)
        return getSum(t[p].l,l,mid,L,R);
    else if(L > mid)
        return getSum(t[p].r,mid+1,r,L,R);
    return getSum(t[p].l,l,mid,L,mid) + getSum(t[p].r,mid+1,r,mid+1,R);
}
 
void updata(int x,int y)
{
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            change2(0,1,n,pos[fx],pos[x]);
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            change2(0,1,n,pos[fy],pos[y]);
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    change2(0,1,n,l,r);
    return;
}
 
int query_max(int x,int y)
{
    int ans = -1000000000;
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            ans = max(ans,getMax(0,1,n,pos[fx],pos[x]));
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            ans = max(ans,getMax(0,1,n,pos[fy],pos[y]));
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return ans;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    ans = max(ans,getMax(0,1,n,l,r));
    return ans;
}
 
int query_min(int x,int y)
{
    int ans = 1000000000;
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            ans = min(ans,getMin(0,1,n,pos[fx],pos[x]));
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            ans = min(ans,getMin(0,1,n,pos[fy],pos[y]));
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return ans;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    ans = min(ans,getMin(0,1,n,l,r));
    return ans;
}
 
int query_sum(int x,int y)
{
    int ans = 0;
    int fx = top[x];
    int fy = top[y];
    int l,r;
    while(fx != fy)
    {
        if(dep[fx] > dep[fy])
        {
            ans += getSum(0,1,n,pos[fx],pos[x]);
            x = fa[fx];
            fx = top[x];
        }
        else
        {
            ans += getSum(0,1,n,pos[fy],pos[y]);
            y = fa[fy];
            fy = top[y];
        }
    }
    if(x == y)
        return ans;
    if(pos[x] > pos[y])
        l = pos[y]+1, r = pos[x];
    else
        l = pos[x]+1, r = pos[y];
    ans += getSum(0,1,n,l,r);
    return ans;
}
 
int main()
{
    scanf("%d",&n);
    for(int i = 0; i < n - 1; i++)
    {
        scanf("%d%d%d",&ee[i][0],&ee[i][1],&ee[i][2]);
        ee[i][0]++;
        ee[i][1]++;
        build_edge(ee[i][0],ee[i][1]);
        build_edge(ee[i][1],ee[i][0]);
    }
    memset(vis,0,sizeof(vis));
    dfs1(1);
    memset(vis,0,sizeof(vis));
    dfs2(1);
    for(int i = 0; i < n - 1; i++)
        if(fa[ee[i][0]] == ee[i][1])
            epos[i+1] = ee[i][0];
        else
            epos[i+1] = ee[i][1];
    build(0,1,n);
    for(int i = 1; i < n; i++)
        change1(0,1,n,pos[epos[i]],ee[i-1][2]);
    char q[10];
    scanf("%d",&m);
    while(m--)
    {
        scanf("%s",q);
        scanf("%d%d",&x,&y);
        x++,y++;
        if(!strcmp(q,"C"))
        {
            y--;
            x--;
            change1(0,1,n,pos[epos[x]],y);
        }
        else if(!strcmp(q,"MAX"))
        {
            printf("%d\n",query_max(x,y));
        }
        else if(!strcmp(q,"N"))
        {
            updata(x,y);
        }
        else if(!strcmp(q,"MIN"))
        {
            printf("%d\n",query_min(x,y));
        }
        else
        {
            printf("%d\n",query_sum(x,y));
        }
    }
    return 0;
}

题目描述 有一个 $n$ 个点的棋盘,每个点上有一个数字 $a_i$,你需要从 $(1,1)$ 走到 $(n,n)$,每次只能往右或往下走,每个格子只能经过一次,路径上的数字和为 $S$。定义一个点 $(x,y)$ 的值为 $a_x+a_y$,求所有满足条件的路径中,所有点的值和的最小值。 输入格式 第一行一个整数 $n$。 接下来 $n$ 行,每行 $n$ 个整数,表示棋盘上每个点的数字。 输出格式 输出一个整数,表示所有满足条件的路径中,所有点的值和的最小值。 数据范围 $1\leq n\leq 300$ 输入样例 3 1 2 3 4 5 6 7 8 9 输出样例 25 算法1 (树形dp) $O(n^3)$ 我们可以先将所有点的值求出来,然后将其看作是一个有值的图,问题就转化为了在这个图中求从 $(1,1)$ 到 $(n,n)$ 的所有路径中,所有点的值和的最小值。 我们可以使用树形dp来解决这个问题,具体来说,我们可以将这个图看作是一棵树,每个点的父节点是它的前驱或者后继,然后我们从根节点开始,依次向下遍历,对于每个节点,我们可以考虑它的两个儿子,如果它的两个儿子都被遍历过了,那么我们就可以计算出从它的左儿子到它的右儿子的路径中,所有点的值和的最小值,然后再将这个值加上当前节点的值,就可以得到从根节点到当前节点的路径中,所有点的值和的最小值。 时间复杂度 树形dp的时间复杂度是 $O(n^3)$。 C++ 代码 算法2 (动态规划) $O(n^3)$ 我们可以使用动态规划来解决这个问题,具体来说,我们可以定义 $f(i,j,s)$ 表示从 $(1,1)$ 到 $(i,j)$ 的所有路径中,所有点的值和为 $s$ 的最小值,那么我们就可以得到如下的状态转移方程: $$ f(i,j,s)=\min\{f(i-1,j,s-a_{i,j}),f(i,j-1,s-a_{i,j})\} $$ 其中 $a_{i,j}$ 表示点 $(i,j)$ 的值。 时间复杂度 动态规划的时间复杂度是 $O(n^3)$。 C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值