洛谷P3038 牧草种植 [树链剖分]
牧草种植
题目描述
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.
输入输出样例
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
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 牧草种植 [树链剖分]的更多相关文章
- BZOJ2243 洛谷2486 [SDOI2011]染色 树链剖分
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2243 题目传送门 - 洛谷2486 题意概括 一棵树,共n个节点. 让你支持以下两种操作,共m次操 ...
- 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)
题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...
- 洛谷P3038 牧草种植Grass Planting
思路: 首先,这道题的翻译是有问题的(起码现在是),查询的时候应该是查询某一条路径的权值,而不是某条边(坑死我了). 与平常树链剖分题目不同的是,这道题目维护的是边权,而不是点权,那怎么办呢?好像有点 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
- 洛谷 P3950 部落冲突 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例1 输出样例1 输入样例2 输出样例2 输入样例3 输出样例3 说明 思路 AC代码 总结 题面 题目链接 P3 ...
- 洛谷 P2486 [SDOI2011]染色 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 思路 PushDown与Update Q AC代码 总结与拓展 题面 题目链接 P2486 ...
- 洛谷 P4211 [LNOI2014]LCA (树链剖分+离线)
题目:https://www.luogu.org/problemnew/solution/P4211 相当难的一道题,其思想难以用言语表达透彻. 对于每个查询,区间[L,R]中的每个点与z的lca肯定 ...
- 洛谷P4114 Qtree1(树链剖分+线段树)
传送门 LCT秒天秒地用什么树剖 这题可以算是树剖的比较裸的题目了 把每一条边的权值下放到他两边的点中深度较深的那个 然后直接用树剖+线段树带进去乱搞就可以了 //minamoto #include& ...
- 洛谷$P4211\ [LNOI2014]\ LCA$ 树链剖分+线段树
正解:树剖+线段树 解题报告: 传送门$QwQ$ 看到$dep[lca]$啥的就想到之前托腮腮$CSP$模拟$D1T3$的那个套路,,, 然后试下这个想法,于是$dep[lca(x,y)]=\sum_ ...
随机推荐
- 关于MyBatis一些小错误,元素内容必须由格式正确的字符数据或标记组成.
今天在Mapper.xml文件写查询语句报了个奇怪的错误 Caused by: org.apache.ibatis.builder.BuilderException: Error creating d ...
- 《A First Course in Abstract Algebra with Applications》-chaper1-数论-关于素数
由于笔者在别的专栏多次介绍过数论,这里在<抽象代数基础教程>的专栏下,对于chaper1数论这一章节介绍的方式不那么“入门”. 首先来介绍一个代数中常用也是非常重要的证明方法:数学归纳法. ...
- centos设置tomcat开机启动
1.编辑开机启动脚本 vi /etc/init.d/tomcat8 #!/bin/bash # tomcat8:start|stop|restart # chkconfig: 345 90 10 # ...
- 元类编程--__getattr__, __getattribute__
#__getattr__, __getattribute__ #__getattr__ 就是在查找不到属性的时候调用 from datetime import date class User: def ...
- Go语言的并发和并行
不知道你有没有注意到,这段代码如果我跑在两个goroutines里面的话: package main import ( "fmt" ) func loop(done chan bo ...
- ZOJ3229 Shoot the Bullet [未AC]
Time Limit: 2 Seconds Memory Limit: 32768 KB Special Judge Gensokyo is a world which exist ...
- 【BZOJ】1385 [Baltic2000]Division expression
[算法]欧几里德算法 [题解]紫书原题 #include<cstdio> #include<algorithm> using namespace std; ; int T,t, ...
- quick-cocos2dx 悬浮节点(NotificationNode)
cocos2dx 开发游戏时,有时某些节点不需要随着场景的切换而销毁.但cocos2dx的机制只允许同时只有一个运行的场景,如果你的所有节点都是依附于这个场景的,那场景的切换必然带来节点的销毁. 比如 ...
- ASP.NET 简单鼠标右键效果contextmenutrip
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- $.on方法与$.click()的区别
1.$.on("click") 支持动态元素绑定事件,该事件是绑定到document上,只要符合条件的元素即可绑定事件,同时$.on()可以绑定多个事件 on方法 on(event ...