P3038 [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
//树剖是在点上操作的,这道题是边
//那么怎么把边权转成点权呢?
//根据树的性质可以知道,一个点可以有多个儿子,但是只会有一个爸爸,
//所以我们可以把这个点和它爸爸之间的那条边的边权转移到这个点上来
//用这个点的点权来表示这条边的权值
//因为根节点没有爸爸,所以它不表示任何边权,点权为0
//但是我们怎么样才能不把两个点的公共祖先的权值算进去啊?
//node[fx].s+1? 不行,这是它的重儿子的位置
// 考虑一下,我们在Query或者Modify的时候,都是当x和y同时处于一条链了之后就break
//然后再把这条链加上,最近公共祖先不就是这条链的top嘛!
//所以,我们在while循环外边写node[x].s+1就可以不算上公共祖先了。
//但是也要注意,如果最后是条轻边,我们就要if特判一下,不能让他进线段树查询了
//因为如果是轻边的话,最后的那条链退化成了最近公共祖先这一个点,不能要! #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N=1e5+; int n,m;
int w[N];
int head[N],num_edge;
struct Edge
{
int v,nxt;
}edge[N<<];
struct Node
{
int fa,son;
int dep,top;
int size;
int s,t;
}node[N];
struct TREE
{
TREE *lson,*rson;
int l,r,mid,len;
int num,lazy;
}tree[N<<]; typedef TREE* Tree;
Tree Root,now_node=tree; inline int read()
{
char c=getchar();int num=;
for(;!isdigit(c);c=getchar())
if(c=='P') return ;
else if(c=='Q') return ;
for(;isdigit(c);c=getchar())
num=num*+c-'';
return num;
} inline void add_edge(int u,int v)
{
edge[++num_edge].v=v;
edge[num_edge].nxt=head[u];
head[u]=num_edge;
} void dfs1(int u)
{
node[u].size=;
for(int i=head[u],v;i;i=edge[i].nxt)
{
v=edge[i].v;
if(v==node[u].fa)
continue;
node[v].fa=u;
node[v].dep=node[u].dep+;
dfs1(v);
node[u].size+=node[v].size;
if(node[v].size>node[node[u].son].size)
node[u].son=v;
}
} int bound;
void dfs2(int u,int top)
{
node[u].top=top;
node[u].s=++bound;
if(node[u].son)
{
dfs2(node[u].son,top);
for(int i=head[u],v;i;i=edge[i].nxt)
{
v=edge[i].v;
if(v==node[u].son||v==node[u].fa)
continue;
dfs2(v,v);
}
}
node[u].t=bound;
} void build(Tree &root,int l,int r)
{
root=++now_node;
root->l=l,root->r=r,root->mid=l+r>>,root->len=r-l+;
if(l==r)
return;
build(root->lson,l,root->mid);
build(root->rson,root->mid+,r);
} inline void pushdown(Tree root)
{
if(root->lazy)
{
root->lson->lazy+=root->lazy;
root->rson->lazy+=root->lazy;
root->lson->num+=root->lson->len*root->lazy;
root->rson->num+=root->rson->len*root->lazy;
root->lazy=;
}
} void update(Tree root,int l,int r)
{
if(root->l==l&&r==root->r)
{
root->num+=root->len;
root->lazy+=;
return;
}
pushdown(root);
if(r<=root->mid)
update(root->lson,l,r);
else if(l>root->mid)
update(root->rson,l,r);
else
{
update(root->lson,l,root->mid);
update(root->rson,root->mid+,r);
}
root->num=root->lson->num+root->rson->num;
} int query(Tree root,int l,int r)
{
if(root->l==l&&root->r==r)
return root->num;
pushdown(root);
if(r<=root->mid)
return query(root->lson,l,r);
else if(l>root->mid)
return query(root->rson,l,r);
else
return query(root->lson,l,root->mid)+query(root->rson,root->mid+,r);
} inline void Modify(int x,int y)
{
int fx=node[x].top,fy=node[y].top;
while(fx!=fy)
{
if(node[fx].dep>node[fy].dep)
{
update(Root,node[fx].s,node[x].s);
x=node[fx].fa;
fx=node[x].top;
}
else
{
update(Root,node[fy].s,node[y].s);
y=node[fy].fa;
fy=node[y].top;
}
}
if(x!=y)
{
if(node[x].dep>node[y].dep)
update(Root,node[y].s+,node[x].s);
else
update(Root,node[x].s+,node[y].s);
}
} inline int Query(int x,int y)
{
int fx=node[x].top,fy=node[y].top;
int ans=;
while(fx!=fy)
{
if(node[fx].dep>node[fy].dep)
{
ans+=query(Root,node[fx].s,node[x].s);
x=node[fx].fa;
fx=node[x].top;
}
else
{
ans+=query(Root,node[fy].s,node[y].s);
y=node[fy].fa;
fy=node[y].top;
}
}
if(x!=y)
{
if(node[x].dep>node[y].dep)
return ans+query(Root,node[y].s+,node[x].s);
else
return ans+query(Root,node[x].s+,node[y].s);
}
return ans;
} int opt,u,v;
int main()
{
n=read(),m=read();
for(int i=;i<n;++i)
{
u=read(),v=read();
add_edge(u,v);
add_edge(v,u);
}
dfs1();
dfs2(,);
build(Root,,n);
for(int i=;i<=m;++i)
{
opt=read(),u=read(),v=read();
if(opt==)
Modify(u,v);
else
printf("%d\n",Query(u,v));
}
return ;
}
P3038 [USACO11DEC]牧草种植Grass Planting的更多相关文章
- 洛谷P3038 [USACO11DEC]牧草种植Grass Planting
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting(树链剖分)
题解:仍然是无脑树剖,要注意一下边权,然而这种没有初始边权的题目其实和点权也没什么区别了 代码如下: #include<cstdio> #include<vector> #in ...
- AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- 树链剖分【p3038】[USACO11DEC]牧草种植Grass Planting
表示看不太清. 概括题意 树上维护区间修改与区间和查询. 很明显树剖裸题,切掉,细节处错误T了好久 TAT 代码 #include<cstdio> #include<cstdlib& ...
- [USACO11DEC]牧草种植Grass Planting
图很丑.明显的树链剖分,需要的操作只有区间修改和区间查询.不过这里是边权,我们怎么把它转成点权呢?对于E(u,v),我们选其深度大的节点,把边权扔给它.因为这是树,所以每个点只有一个父亲,所以每个边权 ...
- 【LuoguP3038/[USACO11DEC]牧草种植Grass Planting】树链剖分+树状数组【树状数组的区间修改与区间查询】
模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...
- 洛谷P3038 牧草种植Grass Planting
思路: 首先,这道题的翻译是有问题的(起码现在是),查询的时候应该是查询某一条路径的权值,而不是某条边(坑死我了). 与平常树链剖分题目不同的是,这道题目维护的是边权,而不是点权,那怎么办呢?好像有点 ...
- USACO Grass Planting
洛谷 P3038 [USACO11DEC]牧草种植Grass Planting 洛谷传送门 JDOJ 2282: USACO 2011 Dec Gold 3.Grass Planting JDOJ传送 ...
随机推荐
- ASP.Net Core 返回的json数据,自定义日期格式
//代码位置:Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddMvc() .Ad ...
- 2019杭电多校三 C. Yukikaze and Demons (点分治)
大意: 给定树, 每个点有一个十进制数位, 求有多少条路径组成的十进制数被$k$整除. 点分治, 可以参考CF715C, 转化为求$10^a x+b\equiv 0(mod\space k)$的$x$ ...
- 在论坛中出现的比较难的sql问题:38(字符拆分 字符串检索问题)
原文:在论坛中出现的比较难的sql问题:38(字符拆分 字符串检索问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得 ...
- 在论坛中出现的比较难的sql问题:23(随机填充问题)
原文:在论坛中出现的比较难的sql问题:23(随机填充问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必要记录下 ...
- SVN_04建库
示范加入一个代码库[Repository] [1]点击Repository右键,创建一个新库 (常规FSFS存储库) [2]在下面所看到的文本框中输入库名称 只创建空的库 创建完库后,没有任何内容在里 ...
- javascript进度条实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- JacksonJson的使用
JacksonJson是SpringMVC内置的json处理工具,其中有一个ObjectMapper类,可以方便的实现对json的处理: //对象转字符串 // json处理工具 private Ob ...
- Android 在同一台设备上安装多个同一项目的apk
如果设备上已经安装了一个apk,再次安装这个apk就会提示覆盖前面的应用 解决办法: 方法一:手动改包名 不好改,改了几次都不成功(可能是代码在svn管理的原因,改完后文件夹里的代码就没了),确实不实 ...
- PyCharm提示ModuleNotFoundError: No module named 'pymysql'
初学python,连接mysql时遇到了提示pymysql模块导入有问题,本人虽是window系统,解决思路是一样的 代码如下: import pymysql#打开数据库,参数依次为:主机名/IP,用 ...
- (三)react-native开发系列之开发环境集成
先上图,由于是虚拟机中的ios虚拟器,所以有点卡 关于react-native的开发集成,主要包括以下几个方面 1.路由及页面跳转 2.数据请求的封装 3.状态的管理 4.公共方法和全局变量的封装 5 ...