题目描述

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. 基于阿里云上实现全站https的正确姿势(一)

    对应的网址:https://yq.aliyun.com/articles/65199 摘要: 目前主流大厂的网站和服务都已经实现了全站https, 例如: baidu, taobao, jd等. 关于 ...

  2. SPOJ:String Play (?)

    String Play Milo has a string S of length L. Tutu picks a random prefix and Mota picks a random suff ...

  3. CodeForces-816B:Karen and Coffee (简单线段树)

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  4. AutoIt获取Gridview中可以修改列的值

    有一个界面如上图:黑色框框部分是一个整体,也是一个gridview,如果我想把框框中右侧数据获取出来,该如何操作? 我尝试过了很多途径,都无法成功. 今天,我发现,当鼠标焦点在黑色框框左侧的部分的时候 ...

  5. JAVA线程同步 (一)wait(), notify()和notifyAll()使用

    wait(),notify()和notifyAll()都是java.lang.Object的方法: wait(): Causes the current thread to wait until an ...

  6. ASP.NET Core:Pages

    ylbtech-ASP.NET Core:Pages 1.返回顶部 1._Layout.cshtm <!DOCTYPE html> <html> <head> &l ...

  7. java链接sqlserver数据库

    需要用到sqljdbc.jar包 JSP代码 <%@ page language="java" import="java.sql.*" import=&q ...

  8. In-App Purchase Programming Guide----(六) ----Working with Subscriptions

    Working with Subscriptions Apps that use subscriptions have some additional behaviors and considerat ...

  9. Java 8中Collection转为Map的方法

    Java 8中java.util.stream.Collectors提供了几个方法可用于把Collection转为Map结构,本文记录了个人对其中三个的理解. Method Return Type g ...

  10. 3、HTML的body内标签1

    一.特殊符号的表示   #代指空格 < #代指,< > #代指,> ...... #这玩意有很多,记也记不完,用的时候查一下即可: 二.p和br标签 <p>< ...