# 【BZOJ4999】This Problem Is Too Simple！（线段树）

BZOJ

## 题解

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 120000
#define lson (t[x].ls)
#define rson (t[x].rs)
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Line{int v,next;}e[MAX<<1];
int h[MAX],cnt=1;
map<int,int> M;
int tim,dfn[MAX],hson[MAX],size[MAX],fa[MAX],top[MAX],C[MAX],dep[MAX];
void dfs1(int u,int ff)
{
fa[u]=ff;size[u]=1;dep[u]=dep[ff]+1;
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v;
if(v==ff)continue;
dfs1(v,u);
if(size[v]>size[hson[u]])hson[u]=v;
size[u]+=size[v];
}
}
void dfs2(int u,int tp)
{
top[u]=tp;dfn[u]=++tim;
if(hson[u])dfs2(hson[u],tp);
for(int i=h[u];i;i=e[i].next)
if(e[i].v!=fa[u]&&e[i].v!=hson[u])
dfs2(e[i].v,e[i].v);
}
struct Node{int ls,rs,v;}t[MAX<<7];
int tot,sum,rt[MAX<<2];
int n,Q;
void Modify(int &x,int l,int r,int p,int w)
{
if(!x)x=++tot;t[x].v+=w;
if(l==r)return;
int mid=(l+r)>>1;
if(p<=mid)Modify(lson,l,mid,p,w);
else Modify(rson,mid+1,r,p,w);
}
int Query(int x,int l,int r,int L,int R)
{
if((!t[x].v)||(!x))return 0;
if(L<=l&&r<=R)return t[x].v;
int mid=(l+r)>>1,ret=0;
if(L<=mid)ret+=Query(lson,l,mid,L,R);
if(R>mid)ret+=Query(rson,mid+1,r,L,R);
return ret;
}
int Route(int u,int v,int x)
{
int ret=0;
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])swap(u,v);
ret+=Query(rt[x],1,n,dfn[top[u]],dfn[u]);
u=fa[top[u]];
}
if(dep[u]<dep[v])swap(u,v);
ret+=Query(rt[x],1,n,dfn[v],dfn[u]);
return ret;
}
int main()
{
for(int i=1;i<n;++i)
{
}
dfs1(1,0);dfs2(1,1);
for(int i=1;i<=n;++i)
{
if(!M[C[i]])M[C[i]]=++sum;
Modify(rt[M[C[i]]],1,n,dfn[i],1);
}
char ch[5];
while(Q--)
{
scanf("%s",ch);
if(ch[0]=='C')
{
Modify(rt[M[C[u]]],1,n,dfn[u],-1);
if(!M[x])M[x]=++sum;
Modify(rt[M[x]],1,n,dfn[u],1);
C[u]=x;
}
else
{
if(!M[x])puts("0");
else printf("%d\n",Route(u,v,M[x]));
}
}
return 0;
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客