洛谷P3038 [USACO11DEC]牧草种植Grass Planting

Posted 自为

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷P3038 [USACO11DEC]牧草种植Grass Planting相关的知识,希望对你有一定的参考价值。

题目描述

Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how there is no grass on the roads between pastures. Farmer John loves Bessie very much, and today he is finally going to plant grass on the roads. He will do so using a procedure consisting of M steps (1 <= M <= 100,000).

At each step one of two things will happen:

  • FJ will choose two pastures, and plant a patch of grass along each road in between the two pastures, or,

  • Bessie will ask about how many patches of grass on a particular road, and Farmer John must answer her question.

Farmer John is a very poor counter -- help him answer Bessie\'s questions!

给出一棵n个节点的树,有m个操作,操作为将一条路径上的边权加一或询问某条边的权值。

输入输出格式

输入格式:

 

  • Line 1: Two space-separated integers N and M

  • Lines 2..N: Two space-separated integers describing the endpoints of a road.

  • Lines N+1..N+M: Line i+1 describes step i. The first character of the line is either P or Q, which describes whether or not FJ is planting grass or simply querying. This is followed by two space-separated integers A_i and B_i (1 <= A_i, B_i <= N) which describe FJ\'s action or query.

 

输出格式:

 

  • Lines 1..???: Each line has the answer to a query, appearing in the same order as the queries appear in the input.

 

输入输出样例

输入样例#1: 复制
4 6 
1 4 
2 4 
3 4 
P 2 3 
P 1 3 
Q 3 4 
P 1 4 
Q 2 4 
Q 1 4 
输出样例#1: 复制
2 
1 
2 

 

树链剖分的裸题

但是这个题是在边上进行操作

我们考虑把边上的操作转移到点上

首先想一下最简单的链的情况

对于区间$[l,r]$的操作会影响$r-l+1$个点,但只会影响$r-l$条边

那么我们可以把每条边的边权都放在与它相连的两个点中深度较深的点上

所以我们每次修改的时候都对$(l,r]$进行修改

查询的时候也如此,

具体怎么实现呢?so easy:joy:

只需要在查询/修改的时候把左区间+1即可

注意特判一下x==y的情况

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#define ls k<<1
#define rs k<<1|1
#define LL long long int 
using namespace std;
const int MAXN=1e6+10;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<\'0\'||c>\'9\'){if(c==\'-\')f=-1;c=getchar();}
    while(c>=\'0\'&&c<=\'9\'){x=x*10+c-\'0\',c=getchar();}
    return x*f;
}
int root=1;
struct node
{
    int u,v,w,nxt;
}edge[MAXN];
int head[MAXN];
int num=1;
inline void AddEdge(int x,int y)
{
    edge[num].u=x;
    edge[num].v=y;
    edge[num].nxt=head[x];
    head[x]=num++;
}
struct Tree
{
    int l,r,f,w,siz;
}T[MAXN];
int a[MAXN],b[MAXN],tot[MAXN],idx[MAXN],deep[MAXN],son[MAXN],top[MAXN],fa[MAXN],cnt=0;
void update(int k)
{
    T[k].w=T[ls].w+T[rs].w;
}
void PushDown(int k)
{
    if(!T[k].f) return ;
    T[ls].w+=T[k].f*T[ls].siz;
    T[rs].w+=T[k].f*T[rs].siz;
    T[ls].f+=T[k].f;
    T[rs].f+=T[k].f;
    T[k].f=0;
}
int dfs1(int now,int f,int dep)
{
    deep[now]=dep;    
    tot[now]=1;
    fa[now]=f;
    int maxson=-1;
    for(int i=head[now];i!=-1;i=edge[i].nxt)
    {
        if(edge[i].v==f) continue;
        tot[now]+=dfs1(edge[i].v,now,dep+1);
        if(tot[edge[i].v]>maxson) maxson=tot[edge[i].v],son[now]=edge[i].v;
    }
    return tot[now];
}
void dfs2(int now,int topf)
{
    idx[now]=++cnt;
    a[cnt]=b[now];
    top[now]=topf;
    if(!son[now]) return ;
    dfs2(son[now],topf);
    for(int i=head[now];i!=-1;i=edge[i].nxt)
        if(!idx[edge[i].v])
            dfs2(edge[i].v,edge[i].v);
}
void Build(int k,int ll,int rr)
{
    T[k].l=ll;T[k].r=rr;T[k].siz=rr-ll+1;
    if(ll==rr)
    {
        T[k].w=a[ll];
        return ;
    }
    int mid=(ll+rr)>>1;
    Build(ls,ll,mid);
    Build(rs,mid+1,rr);
    update(k);
}
void IntervalAdd(int k,int ll,int rr,int val)
{
    if(ll<=T[k].l&&T[k].r<=rr)
    {
        T[k].w+=T[k].siz*val;
        T[k].f+=val;
        return ;
    }
    PushDown(k);
    int mid=(T[k].l+T[k].r)>>1;
    if(ll<=mid) IntervalAdd(ls,ll,rr,val);
    if(rr>mid)  IntervalAdd(rs,ll,rr,val);
    update(k);
}
int IntervalAsk(int k,int ll,int rr)
{
    int ans=0;
    if(ll<=T[k].l&&T[k].r<=rr)
    {
        ans+=T[k].w;
        return ans;
    }
    PushDown(k);
    int mid=(T[k].l+T[k].r)>>1;
    if(ll<=mid) ans+=IntervalAsk(ls,ll,rr);
    if(rr>mid)  ans+=IntervalAsk(rs,ll,rr);
    return ans;
}
int TreeSum(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])//不在同一条链内 
    {
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        ans+=IntervalAsk(1,idx[top[x]],idx[x]);
        x=fa[top[x]]; 
    }
    if(deep[x]>deep[y]) swap(x,y);
    if(x==y) return ans;
    ans+=IntervalAsk(1,idx[x]+1,idx[y]);//需要修改的地方 
    return ans;
}
void TreeAdd(int x,int y)
{
    while(top[x]!=top[y])//不在同一条链内 
    {
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        IntervalAdd(1,idx[top[x]],idx[x],1);
        x=fa[top[x]]; 
    }
    if(deep[x]>deep[y]) swap(x,y);
    if(x==y) return ;
    IntervalAdd(1,idx[x]+1,idx[y],1);//需要修改的地方 
}
int main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #else
    #endif
    memset(head,-1,sizeof(head));
    int N=read(),M=read();
    for(int i=1;i<=N-1;i++)
    {
        int x=read(),y=read();
        AddEdge(x,y);AddEdge(y,x);
    }
    dfs1(root,0,1);
    dfs2(root,root);
    Build(1,1,N);
    while(M--)
    {
        char opt[3];int x,y;
        scanf("%s",opt);x=read();y=read();
        if(opt[0]==\'P\')
            TreeAdd(x,y);
        else
            printf("%d\\n",TreeSum(x,y));
        
    }
    return 0;
}

 

 

 

 

 

以上是关于洛谷P3038 [USACO11DEC]牧草种植Grass Planting的主要内容,如果未能解决你的问题,请参考以下文章

P3038 [USACO11DEC]牧草种植Grass Planting

[USACO11DEC] 牧草种植Grass Planting

LuoguP3038/[USACO11DEC]牧草种植Grass Planting树链剖分+树状数组树状数组的区间修改与区间查询

bzoj1691[Usaco2007 Dec]挑剔的美食家*

贪心+TreapBZOJ1691-[Usaco2007 Dec]挑剔的美食家

BZOJ_1691_[Usaco2007 Dec]挑剔的美食家_贪心