题目传送门

牧草种植

题目描述

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

  分析:

  题目翻译还是不够好,容易让人误解。

  题目大意是,给你一棵树,初始边权值均为0。有两种操作,一种是把x到y之间的最短路径上的每一条边权+1,另一种是询问x到y之间的最短路径边权和。

  很明显的树剖。但是题目中的操作都是在边上进行的,所以要转换成点操作。因为一个父节点可能有多个子节点,而子节点只有一个父节点,所以可以直接把边权赋给子节点,这样就方便操作了。那后面就是树剖模板了。

  但是要注意一些小细节,换成点操作后,询问和修改时顶端的点都不要操作,否则就会有冗余边权。

  Code(比较模板的代码风格,将就着看吧):

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,m,dfn[N],num[N],id,fa[N];
int head[N],cnt,top[N],size[N];
int dep[N],hson[N],root,dg[N];
int seg[N<<],sign[N<<];
struct Node{int to,next;}edge[N<<];
inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
return flag?-num:num;
}
inline void add(int x,int y)
{
edge[++cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt;
}
inline void dfs1(int u)
{
size[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u])continue;
dep[v]=dep[u]+;fa[v]=u;
dfs1(v);size[u]+=size[v];
if(size[v]>size[hson[u]])
hson[u]=v;}
}
inline void dfs2(int u,int now)
{
dfn[++id]=u;num[u]=id;top[u]=now;
if(!hson[u])return;dfs2(hson[u],now);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u]||v==hson[u])continue;
dfs2(v,v);}
}
inline void pushup(int rt)
{seg[rt]=seg[rt<<]+seg[rt<<|];}
inline void pushdown(int l,int r,int rt)
{
if(!sign[rt])return;
int mid=(l+r)>>;
seg[rt<<]+=sign[rt]*(mid-l+);
seg[rt<<|]+=sign[rt]*(r-mid);
sign[rt<<]+=sign[rt];
sign[rt<<|]+=sign[rt];
sign[rt]=;
}
inline void build(int l,int r,int rt)
{
if(l>r)return;
if(l==r){seg[rt]=;return;}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
inline void update(int l,int r,int rt,int L,int R,int C)
{
if(l>R||r<L)return;
if(L<=l&&r<=R){
seg[rt]+=C*(r-l+);
sign[rt]+=C;return;}
int mid=(l+r)>>;
pushdown(l,r,rt);
if(L<=mid)update(l,mid,rt<<,L,R,C);
if(R>mid)update(mid+,r,rt<<|,L,R,C);
pushup(rt);
}
inline int quary(int l,int r,int rt,int L,int R)
{
if(l>R||r<L)return ;
if(L<=l&&r<=R){return seg[rt];}
int mid=(l+r)>>;int ret=;
pushdown(l,r,rt);
if(L<=mid)ret+=quary(l,mid,rt<<,L,R);
if(R>mid)ret+=quary(mid+,r,rt<<|,L,R);
return ret;
}
inline void plant(int x,int y)
{
int fax=top[x],fay=top[y];
while(fax!=fay){
if(dep[fax]<dep[fay]){
swap(fax,fay);swap(x,y);}
update(,n,,num[fax],num[x],);
x=fa[fax];fax=top[x];}
if(x!=y){if(dep[x]>dep[y])swap(x,y);
update(,n,,num[x]+,num[y],);}
}
inline int get(int x,int y)
{
int fax=top[x],fay=top[y];int ret=;
while(fax!=fay){
if(dep[fax]<dep[fay]){
swap(fax,fay);swap(x,y);}
ret+=quary(,n,,num[fax],num[x]);
x=fa[fax];fax=top[x];}
if(x!=y){if(dep[x]>dep[y])swap(x,y);
ret+=quary(,n,,num[x]+,num[y]);}
return ret;
}
int main()
{
n=read();m=read();
int x,y,maxx=-;char op[];
memset(head,-,sizeof(head));
for(int i=;i<n;i++){
x=read();y=read();
add(x,y);add(y,x);
dg[x]++;dg[y]++;}
for(int i=;i<=n;i++)
if(maxx<dg[i])maxx=dg[i],root=i;
dep[root]=;fa[root]=;
dfs1(root),dfs2(root,root);
build(,n,);
for(int i=;i<=m;i++){
scanf("%s",op);
x=read();y=read();
if(op[]=='P'){
plant(x,y);}
else
printf("%d\n",get(x,y));}
return ;
}

洛谷P3038 牧草种植 [树链剖分]的更多相关文章

  1. BZOJ2243 洛谷2486 [SDOI2011]染色 树链剖分

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2243 题目传送门 - 洛谷2486 题意概括 一棵树,共n个节点. 让你支持以下两种操作,共m次操 ...

  2. 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

    题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  3. 洛谷P3038 牧草种植Grass Planting

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

  4. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]

    题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...

  5. 洛谷 P3950 部落冲突 树链剖分

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例1 输出样例1 输入样例2 输出样例2 输入样例3 输出样例3 说明 思路 AC代码 总结 题面 题目链接 P3 ...

  6. 洛谷 P2486 [SDOI2011]染色 树链剖分

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 思路 PushDown与Update Q AC代码 总结与拓展 题面 题目链接 P2486 ...

  7. 洛谷 P4211 [LNOI2014]LCA (树链剖分+离线)

    题目:https://www.luogu.org/problemnew/solution/P4211 相当难的一道题,其思想难以用言语表达透彻. 对于每个查询,区间[L,R]中的每个点与z的lca肯定 ...

  8. 洛谷P4114 Qtree1(树链剖分+线段树)

    传送门 LCT秒天秒地用什么树剖 这题可以算是树剖的比较裸的题目了 把每一条边的权值下放到他两边的点中深度较深的那个 然后直接用树剖+线段树带进去乱搞就可以了 //minamoto #include& ...

  9. 洛谷$P4211\ [LNOI2014]\ LCA$ 树链剖分+线段树

    正解:树剖+线段树 解题报告: 传送门$QwQ$ 看到$dep[lca]$啥的就想到之前托腮腮$CSP$模拟$D1T3$的那个套路,,, 然后试下这个想法,于是$dep[lca(x,y)]=\sum_ ...

随机推荐

  1. [LeetCode] 2. Add Two Numbers ☆☆

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  2. bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle——小根堆+大根堆+贪心

    Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1& ...

  3. 【BZOJ】1500: [NOI2005]维修数列

    [算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...

  4. TCP之Nagle算法与延迟ACK

    (一)Nagle算法 为了减少网络中小分组的数目,减少网络拥塞的情况.Nagle算法要求在一条TCP连接上最多只能有一个未被确认的未完成小分组,在该分组ACK到达之前不能够发送其他的小分组,发送端需要 ...

  5. python实战===用python识别图片中的中文

    需要安装的模块 PIL pytesseract 需要下载的工具: http://download.csdn.net/download/bo_mask/10196285 因为之前百度云的链接总失效,所以 ...

  6. jython

    # -*- coding: utf-8 -*- import sys import json sys.path += ["C:/Users/yangbo/Desktop/restassure ...

  7. c语言简单实现telnet客户端

    c语言简单实现telnet客户端 http://blog.csdn.net/haiwenchen/article/details/69944118

  8. 利用keepalive+mysql replication 实现数据库的高可用

    利用keepalive+mysql replication 实现数据库的高可用 http://www.xuchanggang.cn/archives/866.html

  9. MS SQLServer 批量附加数据库

    /************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注 ...

  10. hit-testing机制介绍

    1.简介 寻找处理触摸事件的view的过程为hit-testing,找到的能够处理触摸事件的view叫做hit-test view. 2.机制介绍 假设下图为我们的手机屏幕,当我们假设点击了view ...