题目传送门

牧草种植

题目描述

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. Mybatis xml 写sql如何判断集合的size

    在mybtis的映射文件中判断集合大小  list.size  例子如下: <if test="groupIds != null and groupIds.size>0" ...

  2. 使用python脚本配置zabbix发送报警邮件

    #前提得在zabbix_server配置文件中配置触发脚本的目录,例如,我配置的在/usr/local/zabbix/server/scripts目录下 编写python脚本如下 因为我的服务器在腾讯 ...

  3. Centos下Mysql密码忘记解决办法

    1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/m ...

  4. 【BZOJ4236】JOIOJI [DP]

    JOIOJI Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description JOIOJI桑是JOI君的叔叔 ...

  5. iOS-Apple苹果iPhone开发公开API

      iOS-Apple苹果iPhone开发 //技术博客http://www.cnblogs.com/ChenYilong/   新浪微博http://weibo.com/luohanchenyilo ...

  6. python学习笔记(八)之元组

    元组:和列表十分相似,可以说是一个受限的列表.最大的限制是,元组不能更改. 创建元组 >>> tuple1 = (123,'asd',(1,2,3)) >>> tu ...

  7. Http跨域时候预检没通过的几种原因

    网上大多数涉及的原因(直接复制粘帖): CORS把HTTP请求分成两类,不同类别按不同的策略进行跨域资源共享协商. 1. 简单跨域请求. 当HTTP请求出现以下两种情况时,浏览器认为是简单跨域请求: ...

  8. Computer(HDU2196+树形dp+树的直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...

  9. 更新ubuntu15.10后触摸板点击功能消失

    问题描述: 昨天升级了ubuntu15.10,升级之后很多15.04让人不爽的东西消失了,大快人心,但是突然发现自己的触摸板不怎么好用了,原来可以点击,双指点击代表右键,三指点击代表鼠标中键的功能不见 ...

  10. docker使用小记

    查看当前镜像:docker images 运行一个简单的镜像:docker run hello-world 拉取一个远程docker:docker pull centos docker中安装nginx ...