题目描述

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. JMagic 操作 ImageMagick 处理图片

    项目描述 imagemagick是功能强大的图片处理库,以稳定及高效率著称,众多语言对该库进行封装处理.比如php.java.由于我们是java项目,直接使用java通过JNI技术调用ImageMag ...

  2. TX-LCN分布式事务Demo实战

    1. TX-LCN分布式事务Demo实战 1.1. 原理介绍 1.1.1. 事务控制原理 TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供T ...

  3. 【sping揭秘】25、Spring远程方案

    分化:RMI,EJB,Hessian Spring有 Rmi,http,hessian,burlap 基于rmi的remoting方案 RMI要求远程类对象包路径和本地一致 基于HTTP的轻量级rem ...

  4. HP服务器设置iLO

    HP服务器设置iLO步凑 1.开机出现界面—按下F11进入Boot Menu: 2.选择Generic USB Boot回车: 3.选择System Configuration回车: 4.选择iLO ...

  5. mysql 开发基础系列7 流程函数与其它函数

    一.流程函数 -- 创建表来介绍 ,)); ,),(,), (,),(,),(,), (,NULL); SELECT * FROM salary 1. if(value,t,f)  超过2000的用h ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. ZOJ Problem Set - 2397 Tian Ji -- The Horse Racing

    #include<iostream> #include<cmath> #include<algorithm> #define REP(i,n) for(int i= ...

  8. leetcode — set-matrix-zeroes

    import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/set-matrix-zeroes/ * * * Gi ...

  9. 【Vue.js】vue项目目录作用

    1. build文件夹:打包配置的文件夹 1.1  webpack.base.conf.js :打包的核心配置 1.2  build.js:构建生产版本,项目开发完成之后,通过build.js打包(加 ...

  10. Python 的名称空间和作用域

    最开始对名称空间的了解是在学习函数的时候,那时候知道了作用域的查找顺序,以及全局名称空间和局部名称空间,产生疑惑的时候为学递归的时候,那时候还没有名称空间这个概念,只知道递归有个最大深度,那时候以后递 ...