[USACO11DEC] Grass Planting (树链剖分)
题目描述
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
Solution
树剖板子题,关键是注意统计的是边的权值,不是点的权值。
只需要在每次修改或者查询的时候将其 LCA 的 id +1,即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=100008;
int n,m;
struct sj{
int to;
int next;
}a[maxn*2];
int size,head[maxn];
void add(int x,int y)
{
a[++size].to=y;
a[size].next=head[x];
head[x]=size;
}
int dep[maxn],fa[maxn];
int top[maxn],son[maxn];
int siz[maxn];
void dfs(int x)
{
siz[x]=1;
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!siz[tt])
{
dep[tt]=dep[x]+1;
fa[tt]=x;
dfs(tt);
siz[x]+=siz[tt];
if(siz[tt]>siz[son[x]])
son[x]=tt;
}
}
}
int id[maxn],num;
void dfs1(int x,int y)
{
top[x]=y;
id[x]=++num;
if(son[x])
dfs1(son[x],y);
for(int i=head[x];i;i=a[i].next)
{
int tt=a[i].to;
if(!top[tt])
if(tt!=son[x])
dfs1(tt,tt);
}
}
int sgm[maxn*4],lazy[maxn*4];
void push_down(int node,int l,int r)
{
int kk=lazy[node],mid=(l+r)/2;
lazy[node*2]+=kk;
lazy[node*2+1]+=kk;
sgm[node*2]+=(mid-l+1)*kk;
sgm[node*2+1]+=(r-mid)*kk;
lazy[node]=0;
}
void change(int node,int left,int right,int l,int r)
{
int v=1;
if(left>r||right<l)
return;
if(left>=l&&right<=r)
{
sgm[node]+=v*(right-left+1);
lazy[node]+=v;
return;
}
push_down(node,left,right);
int dist=(right+left)/2;
change(node*2,left,dist,l,r);
change(node*2+1,dist+1,right,l,r);
sgm[node]=sgm[node*2]+sgm[node*2+1];
return;
}
int query(int node,int left,int right,int l,int r)
{
if(l>right||r<left)
return 0;
if(right<=r&&left>=l)
return sgm[node];
push_down(node,left,right);
int dist=(left+right)/2;
return query(node*2,left,dist,l,r)+query(node*2+1,dist+1,right,l,r);
}
void kuai(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
change(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
change(1,1,n,id[x]+1,id[y]);
return;
}
int check(int x,int y)
{
int ans=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
ans+=query(1,1,n,id[top[x]],id[x]);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
ans+=query(1,1,n,id[x]+1,id[y]);
return ans;
}
int main()
{
cin>>n>>m;
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y); add(y,x);
}
dep[1]=1;
dfs(1);
dfs1(1,1);
while(m--)
{
char ch;
int x,y;
cin>>ch; scanf("%d%d",&x,&y);
if(ch=='Q')
cout<<check(x,y)<<endl;
else
kuai(x,y);
}
}
[USACO11DEC] Grass Planting (树链剖分)的更多相关文章
- 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting(树链剖分)
题解:仍然是无脑树剖,要注意一下边权,然而这种没有初始边权的题目其实和点权也没什么区别了 代码如下: #include<cstdio> #include<vector> #in ...
- 【LuoguP3038/[USACO11DEC]牧草种植Grass Planting】树链剖分+树状数组【树状数组的区间修改与区间查询】
模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...
- spoj - Grass Planting(树链剖分模板题)
Grass Planting 题意 给出一棵树,树有边权.每次给出节点 (u, v) ,有两种操作:1. 把 u 到 v 路径上所有边的权值加 1.2. 查询 u 到 v 的权值之和. 分析 如果这些 ...
- 树链剖分好(du)题(liu)选做
1.luogu P4315 月下"毛景树" 题目链接 前言: 这大概是本蒟蒻A掉的题里面码量最大的一道题了.我自认为码风比较紧凑,但还是写了175行. 从下午2点多调到晚上8点.中 ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- codevs 1228 苹果树 树链剖分讲解
题目:codevs 1228 苹果树 链接:http://codevs.cn/problem/1228/ 看了这么多树链剖分的解释,几个小时后总算把树链剖分弄懂了. 树链剖分的功能:快速修改,查询树上 ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
- 树链剖分+线段树 CF 593D Happy Tree Party(快乐树聚会)
题目链接 题意: 有n个点的一棵树,两种操作: 1. a到b的路径上,给一个y,对于路径上每一条边,进行操作,问最后的y: 2. 修改某个条边p的值为c 思路: 链上操作的问题,想树链剖分和LCT,对 ...
随机推荐
- [大坑]Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
报错tensorflow/stream_executor/cuda/cuda_dnn.cc:338] Could not create cudnn handle: CUDNN_STATUS_INTER ...
- xpath定位和css定位对比
xpath定位和css定位对比 实际项目中使用较多的是xpath定位和css定位.XPath是XML文档中查找结点的语法,换句话就是通过元素的路径来查找这个元素.xpath比较强大,而css选择器 ...
- JAVA并发编程:相关概念及VOLATILE关键字解析
一.内存模型的相关概念 由于计算机在执行程序时都是在CPU中运行,临时数据存在主存即物理内存,数据的读取和写入都要和内存交互,CPU的运行速度远远快于内存,会大大降低程序执行的速度,于是就有了高速缓存 ...
- SQLSTATE=42000 #42000
在使用PowerDesigner生成数据库表的时候遇到了这个问题. 原来是在填写属性的类型的时候, 少了两个括号, 造成了mysql数据类型错误 本来应该是varchar(50)的,写成了varcha ...
- Idea01 Idea2018中集成Tomcat9导致OutPut乱码
版本和平台 idea2018.3 tomcat9 jdk1.8 windows7 64位 output乱码 经过测试,项目编码格式设置为utf-8,在main方法中输出中文正常. 而iedea集成to ...
- CornerStone使用跳坑总结
Cornerstone是专门为Mac用户设计的Subversion(SVN)的控制,使版本控制更加透明.cornerstone根Xcode相比,能够更好的忽略文件,所以除了项目经理第一次初始化项目的时 ...
- sencha touch 入门学习资料大全
现在sencha touch已经更新到2.3.1版本了 重新整理一下资料 官方网站:http://www.sencha.com/products/touch/ 在线文档:http://docs.sen ...
- 【期望dp】bzoj4832: [Lydsy1704月赛]抵制克苏恩
这个题面怎么这么歧义…… Description 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一 ...
- vmware虚拟机安装Windows 7后虚拟机自动挂起
vmware虚拟机安装windows7后在一段时间中没有操作,虚拟机会自动挂起,是因为windows7中的设置的自动睡眠,打开[控制面板]=>[电源选项]=>[选择关闭显示器时间]将下面两 ...
- 【android】android对位图文件的支持
Android 支持以下三种格式的位图文件:.png(首选)..jpg(可接受)..gif(不建议).