BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分裸题)

题意就是一棵树,每个点上有个权值,有3个操作,一个要求u到v路径上的最大权值,一个是求u到v路径上权值和,还有一个是修改u的权值为x。

树链剖分入门题。

树链剖分有个很重要的定理就是,保证从根节点到某个节点u,经过的轻边和重路径不会超过logn,我当时看书的时候把重路径看成重边,然后觉得如果真是这样那还用得到什么线段树。。。其实证明相当简单,关键点就在于,每当到走到轻边,树的个数至少减少一半(不然他就是重边了),然后轻边是把重路径分开的唯一条件,所以重路径也不会超过logn条。

这题是按点建的树。

简单写下算法流程吧。

dfs1:算出num(该节点为根的节点总数),depth(深度),f(父亲节点),son(重边的儿子)

dfs2:算出top(如果是轻边,top等于自己,反之,等于那条重路径的根节点),tree(节点在线段树的位置,树链剖分的目的就是把重边放在线段树连续的地方),pos(线段树的位置上是哪个节点,可以看出是前面的tree数组的逆)。

然后建线段树求区间最大和区间和。

求区间最大及区间和:刘汝佳的树上说的是到LCA(x,y),这样的确可以,但是我参考别人的代码,他们用的办法则比较简单粗暴,对比2个点的top值的深度,先把深的往上爬,直到最后top值相同了,这意味着要么在一个重路径上,要么在轻边对应的一个点上。最后再查一下就OK了。

修改的话直接在线段树里改就可以了。

AC代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b; }
template<class T> inline T Min(T a, T b){ return a < b ? a : b; }
const int maxn = 30000+10;
struct Edge
{
    int v,next;
}e[maxn*2];
int head[maxn],nct;
int num[maxn],f[maxn],son[maxn],depth[maxn];
int top[maxn],pos[maxn],tree[maxn],tot;
int data[maxn];
void add_edge(int u, int v)
{
    e[nct].v = v; e[nct].next = head[u];
    head[u] = nct++;
    e[nct].v = u; e[nct].next = head[v];
    head[v] = nct++;
}
 
void dfs1(int u, int fa, int dep)
{
    num[u] = 1; depth[u] = dep; f[u] = fa;
    for(int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        if(v == fa) continue;
        dfs1(v,u,dep+1);
        num[u] += num[v];
        if(!son[u] || num[v] > num[son[u]]) son[u] = v;
    }
}
 
void dfs2(int u, int tp)
{
    top[u] = tp; tree[u] = ++tot;
    pos[tot] = u;
    if(!son[u]) return;
    dfs2(son[u],tp);
    for(int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        if(v == f[u] || v == son[u]) continue;
        dfs2(v,v);
    }
}
 
struct SegTree
{
    int mx,sum;
};
SegTree T[maxn<<2];
void pushup(int rt)
{
    T[rt].mx = Max(T[rt<<1].mx, T[rt<<1|1].mx);
    T[rt].sum = T[rt<<1].sum+T[rt<<1|1].sum;
}
 
void build(int l, int r, int rt)
{
    if(l == r)
    {
        T[rt].mx = T[rt].sum = data[pos[l]];
        return;
    }
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
 
void update(int L, int k, int l, int r, int rt)
{
    if(l == r)
    {
        T[rt].mx = T[rt].sum = k;
        return;
    }
    int mid = (l+r)>>1;
    if(L <= mid) update(L, k, lson);
    else update(L,k,rson);
    pushup(rt);
}
 
int query1(int L, int R, int l, int r, int rt)
{
    if(L <= l && R >= r)
        return T[rt].mx;
    int mid = (l+r)>>1,ret = -NMAX;
    if(L <= mid) ret = Max(ret,query1(L,R,lson));
    if(R > mid) ret = Max(ret,query1(L,R,rson));
    return ret;
}
 
int query2(int L, int R, int l, int r, int rt)
{
    if(L <= l && R >= r) return T[rt].sum;
    int mid = (l+r)>>1,ret = 0;
    if(L <= mid) ret += query2(L, R, lson);
    if(R > mid) ret += query2(L, R, rson);
    return ret;
}
 
int ask_max(int x, int y)
{
    int t1 = top[x], t2 = top[y], tmp, ans = -NMAX;
    while(t1 != t2)
    {
        if(depth[t1] < depth[t2])
        {
            tmp = t1; t1 = t2; t2 = tmp;
            tmp = x; x = y; y = tmp;
        }
        ans = Max(ans, query1(tree[t1],tree[x],1,tot,1));
        x = f[t1]; t1 = top[x];
    }
    ans = Max(ans,depth[x] > depth[y] ? query1(tree[y],tree[x],1,tot,1) : query1(tree[x],tree[y],1,tot,1));
    return ans;
}
 
int ask_sum(int x, int y)
{
    int t1 = top[x], t2 = top[y], tmp, ans = 0;
    while(t1 != t2)
    {
        if(depth[t1] < depth[t2])
        {
            tmp = t1; t1 = t2; t2 = tmp;
            tmp = x; x = y; y = tmp;
        }
        ans += query2(tree[t1],tree[x],1,tot,1);
        x = f[t1]; t1 = top[x];
    }
    ans += depth[x] > depth[y] ? query2(tree[y],tree[x],1,tot,1) : query2(tree[x],tree[y],1,tot,1);
    return ans;
}
 
int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int n,q;
    char s[20];
    while(~scanf("%d",&n))
    {
        memset(head,-1,sizeof(head));
        memset(son,0,sizeof(son));
        nct = 0;
        tot = 0;
        for(int i = 1; i < n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
        }
        for(int i = 1; i <= n; i++)
            scanf("%d",&data[i]);
        dfs1(1,1,1);
        dfs2(1,1);
        build(1,tot,1);
        scanf("%d",&q);
        while(q--)
        {
            int a,b;
            scanf("%s%d%d",s,&a,&b);
            if(s[0] == 'C') update(tree[a],b,1,tot,1);
            else if(s[1] == 'M') printf("%d\n",ask_max(a,b));
            else printf("%d\n",ask_sum(a,b));
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值