题目描述

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

树剖裸题

链上修改+查询

屠龙宝刀点击就送

#include <ctype.h>
#include <cstdio>
#define M 100005
void read(int &x)
{
x=;register char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
}
struct Edge
{
int next,to;
Edge (int next=,int to=):next(next),to(to) {}
}edge[M<<];
int n,m,head[M],cnt,top[M],belong[M],tim,size[M],dad[M],dep[M];
void insert(int u,int v)
{
edge[++cnt]=Edge(head[u],v);
head[u]=cnt;
}
void swap(int &x,int &y)
{
int tmp=y;
y=x;
x=tmp;
}
struct node
{
int mid,l,r,dis,flag;
node *left,*right;
node ()
{
left=right=NULL;
dis=flag=;
}
}*root;
class t
{
public:
void pushup(node *&k)
{
k->dis=k->left->dis+k->right->dis;
}
void pushdown(node *&k)
{
if(k->l==k->r) return;
k->left->flag+=k->flag;
k->right->flag+=k->flag;
k->left->dis+=k->flag*(k->left->r-k->left->l+);
k->right->dis+=k->flag*(k->right->r-k->right->l+);
k->flag=;
}
void build(node *&k,int l,int r)
{
k=new node;
k->l=l;k->r=r;k->mid=(l+r)>>;
if(l==r) return;
build(k->left,l,k->mid);
build(k->right,k->mid+,r);
}
void Tree_change(node *&k,int l,int r)
{
if(k->l==l&&k->r==r)
{
k->flag++;
k->dis+=(r-l+);
return;
}
if(l>k->mid) Tree_change(k->right,l,r);
else if(r<=k->mid) Tree_change(k->left,l,r);
else Tree_change(k->left,l,k->mid),Tree_change(k->right,k->mid+,r);
pushup(k);
}
int Tree_query(node *&k,int l,int r)
{
if(k->l==l&&k->r==r) return k->dis;
if(k->flag) pushdown(k);
if(l>k->mid) return Tree_query(k->right,l,r);
else if(r<=k->mid) return Tree_query(k->left,l,r);
else return Tree_query(k->left,l,k->mid)+Tree_query(k->right,k->mid+,r);
pushup(k);
}
};
class t Tree;
class sp
{
public :
void dfs1(int x)
{
size[x]=;
dep[x]=dep[dad[x]]+;
for(int i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if(dad[x]!=v)
{
dad[v]=x;
dfs1(v);
size[x]+=size[v];
}
}
}
void dfs2(int x)
{
int pos=;
belong[x]=++tim;
if(!top[x]) top[x]=x;
for(int i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if(dad[x]!=v&&size[pos]<size[v]) pos=v;
}
if(pos) top[pos]=top[x],dfs2(pos);
for(int i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if(dad[x]!=v&&v!=pos) dfs2(v);
}
}
void Chain_change(int x,int y)
{
for(;top[x]!=top[y];x=dad[top[x]])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
Tree.Tree_change(root,belong[top[x]],belong[x]);
}
if(x==y) return;
if(dep[x]>dep[y]) swap(x,y);
Tree.Tree_change(root,belong[x]+,belong[y]);
}
int Chain_query(int x,int y)
{
int ans=;
for(;top[x]!=top[y];x=dad[top[x]])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
ans+=Tree.Tree_query(root,belong[top[x]],belong[x]);
}
if(x==y) return ans;
if(dep[x]>dep[y]) swap(x,y);
ans+=Tree.Tree_query(root,belong[x]+,belong[y]);
return ans;
}
};
class sp Chain;
int main()
{
read(n);
read(m);
for(int x,y,i=;i<n;i++)
{
read(x);
read(y);
insert(x,y);
insert(y,x);
}
Chain.dfs1();Chain.dfs2();
char str[];
root=new node;
Tree.build(root,,n);
for(int x,y;m--;)
{
scanf("%s",str+);
read(x);
read(y);
if(str[]=='P') Chain.Chain_change(x,y);
else printf("%d\n",Chain.Chain_query(x,y));
}
return ;
}

洛谷 P3038 [USACO11DEC]牧草种植Grass Planting的更多相关文章

  1. 洛谷P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  2. 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting(树链剖分)

    题解:仍然是无脑树剖,要注意一下边权,然而这种没有初始边权的题目其实和点权也没什么区别了 代码如下: #include<cstdio> #include<vector> #in ...

  3. P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  4. AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  5. 树链剖分【p3038】[USACO11DEC]牧草种植Grass Planting

    表示看不太清. 概括题意 树上维护区间修改与区间和查询. 很明显树剖裸题,切掉,细节处错误T了好久 TAT 代码 #include<cstdio> #include<cstdlib& ...

  6. [USACO11DEC]牧草种植Grass Planting

    图很丑.明显的树链剖分,需要的操作只有区间修改和区间查询.不过这里是边权,我们怎么把它转成点权呢?对于E(u,v),我们选其深度大的节点,把边权扔给它.因为这是树,所以每个点只有一个父亲,所以每个边权 ...

  7. 【LuoguP3038/[USACO11DEC]牧草种植Grass Planting】树链剖分+树状数组【树状数组的区间修改与区间查询】

    模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...

  8. 洛谷P3038 牧草种植Grass Planting

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

  9. 洛谷P3038 牧草种植 [树链剖分]

    题目传送门 牧草种植 题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirec ...

随机推荐

  1. CodeForces-884D:Boxes And Balls(合并石子)

    Ivan has n different boxes. The first of them contains some balls of n different colors. Ivan wants ...

  2. iOS中NSNotification、delegate、KVO三者之间的区别与联系?

    前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有个疑问,他们的功能比较类似,那么在实际的编程中,如何选择这些方式呢? 在网上看到一个博客上详细 ...

  3. redhat 关机注销命令详解

    一.注销,关机,重启 注销系统的logout命令 1,Logout 注销是登陆的相对操作,登陆系统后,若要离开系统,用户只要直接下达logout命令即可: [root@localhost root]# ...

  4. python学习笔记5-自定义函数

    1 自定义函数  (1)函数代码块以def关键字开头,然后函数标识符名称和圆括号  (2)任何传入参数和自变量必须放在圆括号中间.圆括号之间可以用于定义参数  (3)函数的第一行语句可以选择性的使用文 ...

  5. HDU 5884 Sort (二分+k叉哈夫曼树)

    题意:n 个有序序列的归并排序.每次可以选择不超过 k 个序列进行合并,合并代价为这些序列的长度和.总的合并代价不能超过T, 问 k最小是多少. 析:首先二分一下这个 k .然后在给定 k 的情况下, ...

  6. H.264(MPEG-4 AVC)级别(Level)、DPB 与 MaxDpbMbs 详解(转载)

    转自:http://www.cnblogs.com/zyl910/archive/2011/12/08/h264_level.html 对于H.264(MPEG-4 AVC)而言,级别(Level)是 ...

  7. POJ2365【几何】

    因为给出的点已经是顺时针了, 整个长度=相邻点距离+一个圆周长: C++ac代码-G++wa-因为标准不一样.G++用f //#include <bits/stdc++.h> #inclu ...

  8. hdoj1596【spfa,松弛】

    积压很久的一道...一看直接spfa水过..但是看那个safest怎么求得?松弛的时候取大. #include <bits/stdc++.h> using namespace std; t ...

  9. 关于js变量作用域

    先来看一段代码 var ss=1;function sss(){ alert(ss);}$(document).ready(function(){ var ss=2; alert(ss); sss() ...

  10. poj 3710 Christmas Game【博弈论+SG】

    也就是转换到树形删边游戏,详见 https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html #include<iostream> ...