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

时间:2021-09-17 16:42:18

题目描述

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 ;
}