雖然知道差分能做,還是練了一波鏈剖模板。(其實是noip要到了QAQ)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
char ch=getchar();int f=0;
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') {f=f*10+(ch^48);ch=getchar();}
return f;
}
int n,m;
struct node
{
int from;
int to;
int next;
}edge[400005];
struct segtree
{
int ls;
int rs;
int maxx;
int mark;
}tree[800005];
int tot,head[200005],dep[200005],fa[200005],top[200005],son[200005],size[200005],pos[200005],npos[200005];
int cnt,sum=1;
void add(int u,int v)
{
edge[tot].from=u;
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs1(int x)
{
size[x]=1;
dep[x]=dep[fa[x]]+1;
for(int i=head[x];i!=-1;i=edge[i].next)
{
if(fa[x]!=edge[i].to)
{
fa[edge[i].to]=x;
dfs1(edge[i].to);
size[x]+=size[edge[i].to];
if(size[edge[i].to]>size[son[x]])
son[x]=edge[i].to;
}
}
}
void dfs2(int x)
{
pos[x]=++cnt;
npos[cnt]=x;
if(son[fa[x]]==x) top[x]=top[fa[x]];
else top[x]=x;
if(son[x]) dfs2(son[x]);
for(int i=head[x];i!=-1;i=edge[i].next)
{
if(fa[x]!=edge[i].to&&son[x]!=edge[i].to)
dfs2(edge[i].to);
}
}
void build(int p,int l,int r)
{
if(l==r)
{
return;
}
int mid=l+r>>1;
tree[p].ls=++sum;
tree[p].rs=++sum;
build(tree[p].ls,l,mid);
build(tree[p].rs,mid+1,r);
}
void modify(int p,int l,int r,int x,int y)
{
int mid=l+r>>1;
if(l==x&&r==y)
{
tree[p].mark++;
tree[p].maxx++;
return;
}
tree[tree[p].ls].mark+=tree[p].mark;
tree[tree[p].rs].mark+=tree[p].mark;
tree[tree[p].ls].maxx+=tree[p].mark;
tree[tree[p].rs].maxx+=tree[p].mark;
tree[p].mark=0;
if(y<=mid) modify(tree[p].ls,l,mid,x,y);
else if(x>mid) modify(tree[p].rs,mid+1,r,x,y);
else
{
modify(tree[p].ls,l,mid,x,mid);
modify(tree[p].rs,mid+1,r,mid+1,y);
}
tree[p].maxx=max(tree[tree[p].ls].maxx,tree[tree[p].rs].maxx);
}
void modify(int x,int y)
{
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(dep[fx]<dep[fy]) swap(fx,fy),swap(x,y);
modify(1,1,n,pos[fx],pos[x]);
x=fa[fx];fx=top[x];
}
if(dep[x]<dep[y]) swap(x,y);
modify(1,1,n,pos[y],pos[x]);
}
int main()
{
memset(head,-1,sizeof(head));
n=read();m=read();
for(int i=1;i<n;i++)
{
int u=read(),v=read();
add(u,v);
add(v,u);
}
dfs1(1);
dfs2(1);
build(1,1,n);
for(int i=1;i<=m;i++)
{
int l=read(),r=read();
modify(l,r);
}
cout<<tree[1].maxx;
}