题目描述

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+;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'',c=getchar();}
return x*f;
}
int root=;
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN];
int num=;
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=;
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=;
}
int dfs1(int now,int f,int dep)
{
deep[now]=dep;
tot[now]=;
fa[now]=f;
int maxson=-;
for(int i=head[now];i!=-;i=edge[i].nxt)
{
if(edge[i].v==f) continue;
tot[now]+=dfs1(edge[i].v,now,dep+);
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!=-;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+;
if(ll==rr)
{
T[k].w=a[ll];
return ;
}
int mid=(ll+rr)>>;
Build(ls,ll,mid);
Build(rs,mid+,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)>>;
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=;
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)>>;
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=;
while(top[x]!=top[y])//不在同一条链内
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ans+=IntervalAsk(,idx[top[x]],idx[x]);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x==y) return ans;
ans+=IntervalAsk(,idx[x]+,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(,idx[top[x]],idx[x],);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x==y) return ;
IntervalAdd(,idx[x]+,idx[y],);//需要修改的地方
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
memset(head,-,sizeof(head));
int N=read(),M=read();
for(int i=;i<=N-;i++)
{
int x=read(),y=read();
AddEdge(x,y);AddEdge(y,x);
}
dfs1(root,,);
dfs2(root,root);
Build(,,N);
while(M--)
{
char opt[];int x,y;
scanf("%s",opt);x=read();y=read();
if(opt[]=='P')
TreeAdd(x,y);
else
printf("%d\n",TreeSum(x,y)); }
return ;
}

洛谷P3038 [USACO11DEC]牧草种植Grass Planting的更多相关文章

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

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  2. 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting(树链剖分)

    题解:仍然是无脑树剖,要注意一下边权,然而这种没有初始边权的题目其实和点权也没什么区别了 代码如下: #include<cstdio> #include<vector> #in ...

  3. P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  4. AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  5. 树链剖分【p3038】[USACO11DEC]牧草种植Grass Planting

    表示看不太清. 概括题意 树上维护区间修改与区间和查询. 很明显树剖裸题,切掉,细节处错误T了好久 TAT 代码 #include<cstdio> #include<cstdlib& ...

  6. [USACO11DEC]牧草种植Grass Planting

    图很丑.明显的树链剖分,需要的操作只有区间修改和区间查询.不过这里是边权,我们怎么把它转成点权呢?对于E(u,v),我们选其深度大的节点,把边权扔给它.因为这是树,所以每个点只有一个父亲,所以每个边权 ...

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

    模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...

  8. 洛谷P3038 牧草种植Grass Planting

    思路: 首先,这道题的翻译是有问题的(起码现在是),查询的时候应该是查询某一条路径的权值,而不是某条边(坑死我了). 与平常树链剖分题目不同的是,这道题目维护的是边权,而不是点权,那怎么办呢?好像有点 ...

  9. 洛谷P3038 牧草种植 [树链剖分]

    题目传送门 牧草种植 题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirec ...

随机推荐

  1. 吴恩达机器学习笔记36-正则化和偏差/方差(Regularization and Bias_Variance)

    在我们在训练模型的过程中,一般会使用一些正则化方法来防止过拟合.但是我们可能会正则化的程度太高或太小了,即我们在选择λ 的值时也需要思考与刚才选择多项式模型次数类似的问题. 我们选择一系列的想要测试的

  2. table-layout引起的探索——fixed和auto的区别

    问题:最近想把mui提供的底部导航组件样式单独抽出来,遇到一个问题:给底部图片下的文字设置了超出隐藏,但没有生效,如下图: 注:该底部导航为mui提供的组件 解决:这让我百思不得其解,经过一些琢磨后发 ...

  3. Powermock2.0.0 详细 总结

    目录 1 单元测试 2 Junit测试框架 2.1 Junit是什么 2.2 Junit 能做什么? 3 Junit测试的局限性 4 Mock技术 5 相关的Mock工具 5.1 Mockito.Ea ...

  4. 数字(Number)类型(一)

    多行语句 Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句,例如: total = item_one + \ item_two + \ item_three ...

  5. ubuntu 16.04 安装caffe2的方法及问题解决

    工作需要安装caffe2,从用户体验上来讲,caffe2的安装绝对是体验比较差的那种,花费了我那么多时间去倒腾,这样的用户体验的产品,估计后面是比较危险的. 废话少说,直接上步骤: 官网上有安装目录, ...

  6. [原创]IIS提权工具-VBS提权脚本免杀生成器

    [原创]添加系统用户 VBS提权脚本随机加密生成器[K.8] 2011-05-05 02:42:53|  分类: 原创工具 VBS提权脚本随机加密生成器[K.8]  Author: QQ吻 QQ:39 ...

  7. SVN+Axure版本管理&协同设计(一)

    1. SVN介绍 2.安装部署 环境介绍:Ubuntu16

  8. Ocelot简易教程(二)之快速开始2

    为什么这篇的标题叫"Ocelot简易教程(二)之快速开始2"呢,因为很多朋友跟我说上一篇" Ocelot简易教程(二)之快速开始1"内容太少了,只是简单介绍Oc ...

  9. RabbitMQ访问控制

    Access Control (Authentication, Authorisation) in RabbitMQ 认证和授权这两个概念经常容易被混淆,甚至被互换使用.在RabbitMQ中这是错的, ...

  10. 在SpringBoot中配置全局捕获异常

    前言 之前写过一篇博客是使用spring利用HandlerExceptionResolver实现全局异常捕获 里面使用spring的HandlerExceptionResolver接口来实现全局的异常 ...