题目描述
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.
输入输出样例
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
2 1 2
树剖裸题
链上修改+查询
#include <ctype.h> #include <cstdio> #define M 100005 void read(int &x) { x=0;register char ch=getchar(); for(;!isdigit(ch);ch=getchar()); for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; } struct Edge { int next,to; Edge (int next=0,int to=0):next(next),to(to) {} }edge[M<<1]; 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=0; } }*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+1); k->right->dis+=k->flag*(k->right->r-k->right->l+1); k->flag=0; } void build(node *&k,int l,int r) { k=new node; k->l=l;k->r=r;k->mid=(l+r)>>1; if(l==r) return; build(k->left,l,k->mid); build(k->right,k->mid+1,r); } void Tree_change(node *&k,int l,int r) { if(k->l==l&&k->r==r) { k->flag++; k->dis+=(r-l+1); 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+1,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+1,r); pushup(k); } }; class t Tree; class sp { public : void dfs1(int x) { size[x]=1; dep[x]=dep[dad[x]]+1; 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=0; 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]+1,belong[y]); } int Chain_query(int x,int y) { int ans=0; 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]+1,belong[y]); return ans; } }; class sp Chain; int main() { read(n); read(m); for(int x,y,i=1;i<n;i++) { read(x); read(y); insert(x,y); insert(y,x); } Chain.dfs1(1);Chain.dfs2(1); char str[5]; root=new node; Tree.build(root,1,n); for(int x,y;m--;) { scanf("%s",str+1); read(x); read(y); if(str[1]=='P') Chain.Chain_change(x,y); else printf("%d\n",Chain.Chain_query(x,y)); } return 0; }