[ZJOI2008]树的统计(树链剖分+线段树)

题目

传送门

题解

板子题

代码

#include
#include
#include
using namespace std;
const int maxn=1000005;

int n,m,x,y,a[maxn],sum[maxn<<2],add[maxn<<2],maxq[maxn];
struct Edge{
    int next,to;
}edge[maxn<<1];
int num_edge,head[maxn];
int dfn[maxn],idfn[maxn],fa[maxn],top[maxn],deep[maxn],tot[maxn],son[maxn];

void add_edge(int from,int to)
{
    edge[++num_edge].next=head[from];
    edge[num_edge].to=to;
    head[from]=num_edge;
}

void dfs1(int x,int father,int depth)
{
    fa[x]=father;
    deep[x]=depth;
    tot[x]=1;

    int maxnow=-1;
    for (int i=head[x]; i!=0; i=edge[i].next)
    {
        int to=edge[i].to;
        if (to!=father)
        {
            dfs1(to,x,depth+1);
            tot[x]+=tot[to];
            if (tot[to]>maxnow)
            {
                maxnow=tot[to];//这里是找儿子而不是找x 
                son[x]=to;
            }
        }

    }
}

int cnt;//标记dfs序的计数器 
void dfs2(int x,int tp)
{
    top[x]=tp;
    dfn[x]=++cnt;
    idfn[cnt]=x;
    if (!son[x]) return;
    dfs2(son[x],tp);
    for (int i=head[x]; i!=0; i=edge[i].next)
    {
        int to=edge[i].to;
        if (to!=fa[x] && to!=son[x])
            dfs2(to,to);
    }
}

void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    maxq[rt]=max(maxq[rt<<1],maxq[rt<<1|1]);
}

void build_tree(int l,int r,int rt)
{
    if (l==r) 
    {
        sum[rt]=maxq[rt]=a[idfn[l]]; return;
    }
    int mid=(l+r)>>1;
    build_tree(l,mid,rt<<1);
    build_tree(mid+1,r,rt<<1|1);
    pushup(rt);
}

void change_point(int pos,int C,int l,int r,int rt)
{
    if (l==r)
    {
        sum[rt]=maxq[rt]=C; return;
    }
    int mid=(l+r)>>1;
//  pushdown(rt,mid-l+1,r-mid); pushdown_max(rt);
    if (pos<=mid) change_point(pos,C,l,mid,rt<<1);
    else change_point(pos,C,mid+1,r,rt<<1|1);
    pushup(rt);
}

int ques_sum(int L,int R,int l,int r,int rt)
{
    if (L<=l && r<=R)
        return sum[rt];
    int mid=(l+r)>>1;
//  pushdown(rt,mid-l+1,r-mid);
    int ans=0;//可能有负权 
    if (L<=mid) ans+=ques_sum(L,R,l,mid,rt<<1);
    if (R>mid) ans+=ques_sum(L,R,mid+1,r,rt<<1|1);
    pushup(rt);//忘记了pushup!!! 
    return ans;
}

int ques_max(int L,int R,int l,int r,int rt)
{
    if (L<=l && r<=R) return maxq[rt];
    int mid=(l+r)>>1;
    int ans=-1e9;
    if (L<=mid) ans=max(ans,ques_max(L,R,l,mid,rt<<1));
    if (R>mid) ans=max(ans,ques_max(L,R,mid+1,r,rt<<1|1));
    pushup(rt);
    return ans;
}

int ques_tree_sum(int x,int y)
{
    int ans=0;
    while (top[x]!=top[y])
    {
        if (deep[top[x]]y]]) swap(x,y);
        ans+=ques_sum(dfn[top[x]],dfn[x],1,n,1);
        x=fa[top[x]];//向上跳 
    }
    if (deep[x]>deep[y])
        swap(x,y);
    ans+=ques_sum(dfn[x],dfn[y],1,n,1);
    return ans;
}

int ques_tree_max(int x,int y)
{
    int ans=-1e9;
    while (top[x]!=top[y])
    {
        if (deep[top[x]]y]])
            swap(x,y);
        ans=max(ans,ques_max(dfn[top[x]],dfn[x],1,n,1));//应该是top[x]的dfn值较小 
        x=fa[top[x]];//向上跳 
    }
    if (deep[x]>deep[y]) swap(x,y);
    ans=max(ans,ques_max(dfn[x],dfn[y],1,n,1));
    return ans;
}

int main()
{
    memset(deep,-1,sizeof(deep));
    scanf("%d",&n);
    for (int i=1; i<=n-1; i++)
    {
        scanf("%d%d",&x,&y);
        add_edge(x,y); add_edge(y,x);
    }
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    scanf("%d",&m);
    dfs1(1,1,1);
    dfs2(1,1);
    build_tree(1,n,1);
    for (int i=1; i<=m; i++)
    {
        char s[10];
        scanf("%s",s);
        scanf("%d%d",&x,&y);
        if (s[1]=='M')
            printf("%d\n",ques_tree_max(x,y));
        if (s[1]=='S')
            printf("%d\n",ques_tree_sum(x,y));
        if (s[1]=='H')
            change_point(dfn[x],y,1,n,1);
    }
    return 0;
}

总结

我真是太菜了

  1. 这棵线段树里的点的编号是由dfs序得到的,传进点的编号时应该是 dfn【】;这个东西调了一上午!!a[idfn[l]]才是原来的数
  2. 可能有负权 所以要ques里面ans要初始化为 -inf
  3. 每次询问完成都要pushup!
  4. 到现在还没有A掉 AC %%%% sheng hao

[ZJOI2008]树的统计(树链剖分+线段树)_第1张图片

你可能感兴趣的:(线段树,树链剖分)