404_notfound @ 2019-08-21 14:34:33
WA39 QwQ
如何正确的统计路径和??
#include<bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
using namespace std;
int n,m;
#define maxn 200009
int dfn[maxn],pos[maxn],size[maxn],top[maxn],deep[maxn],fa[maxn],son[maxn],cnt=0;
vector<int> v[maxn];
struct node{
int l,r;
int val,mark;
}tr[maxn*4];
void dfs(int x)
{
size[x]=1;
for(int i=0;i<v[x].size();i++)
{
int to=v[x][i];
if(!size[to])
{
fa[to]=x;
deep[to]=deep[x]+1;
dfs(to);
size[x]+=size[to];
if(size[to]>size[son[x]])son[x]=to;
}
}
}
void dfs(int x,int tp)
{
top[x]=tp;
pos[x]=++cnt;
dfn[cnt]=x;
if(son[x])dfs(son[x],tp);
for(int i=0;i<v[x].size();i++)
{
int to=v[x][i];
if(!top[to])dfs(to,to);
}
}
void Build(int x,int l,int r)
{
tr[x].l=l,tr[x].r=r;
if(l==r)
{
tr[x].mark=0;
tr[x].val=0;
return;
}
int mid=(l+r)>>1;
Build(x*2,l,mid);
Build(x*2+1,mid+1,r);
tr[x].mark=tr[x].val=0;
}
void relese(int x)
{
if(tr[x].l==tr[x].r||tr[x].mark==0)return;
tr[x*2].val+=(tr[x*2].r-tr[x*2].l+1)*tr[x].mark;
tr[x*2].mark=tr[x].mark;
tr[x*2+1].val+=(tr[x*2+1].r-tr[x*2+1].l+1)*tr[x].mark;
tr[x*2+1].mark=tr[x].mark;
tr[x].mark=0;
}
void Add(int x,int l,int r,int val)
{
if(l<=tr[x].l&&tr[x].r<=r)
{
tr[x].mark+=val;
tr[x].val+=(tr[x].r-tr[x].l+1)*val;
return;
}
relese(x);
int mid=(tr[x].l+tr[x].r)>>1;
if(l<=mid)Add(x*2,l,r,val);
if(r>mid)Add(x*2+1,l,r,val);
tr[x].val=tr[x*2].val+tr[x*2+1].val;
}
int Sum(int x,int l,int r)
{
if(l<=tr[x].l&&tr[x].r<=r)
{
return tr[x].val;
}
relese(x);
int mid=(tr[x].l+tr[x].r)>>1;
int ans=0;
if(l<=mid)ans+=Sum(x*2,l,r);
if(r>mid)ans+=Sum(x*2+1,l,r);
return ans;
}
int LCA(int x,int y,bool f=0)
{
int res=0;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]])swap(x,y);
if(!f)res+=Sum(1,pos[top[x]],pos[x]);
if(f)Add(1,pos[top[x]],pos[x],1);
x=fa[top[x]];
}
if(x==y)return res;
if(pos[x]>pos[y])swap(x,y);
if(!f)res+=Sum(1,pos[x]+1,pos[y]);
if(f) Add(1,pos[x]+1,pos[y],1);
return res;
}
signed main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1),dfs(1,1);
Build(1,1,n);
while(m--)
{
char cz[9];
scanf("%s",cz);
int x,y;
scanf("%d%d",&x,&y);
if(cz[0]=='P')
{
LCA(x,y,1);
}else
{
printf("%d\n",LCA(x,y));
}
}
return 0;
}
by JA_yichao @ 2019-08-21 14:36:07
标题党
by _Misaka_Mikoto @ 2019-08-21 14:36:11
cout<<(a 1+b 3-b+a)/2<<endl ;
by Soledad_S @ 2019-08-21 14:36:19
Fake佬%%%
by xh39 @ 2019-08-21 14:36:26
39很吉利诶,看来这道题你过不了了
by O5_13 @ 2019-08-21 14:36:31
QNDGXOI
by schtonn @ 2019-08-21 14:41:30
by FallU @ 2019-08-21 14:51:38
by 404_notfound @ 2019-08-21 18:29:46
过了,标记下传写锅了
by _Felix @ 2020-08-04 18:41:29
@404_notfound 感谢,傻逼VScode第二次把我的lazy标记清零搞没了,我明明记得写了。一看您的回复我就去找
by 404_notfound @ 2020-08-04 21:46:19
哈哈哈这贴一年了竟然还有人看