AC日记——[WC2013]糖果公园 cogs 1817

[WC2013]糖果公园

 

思路:

  带修改树上莫队(模板);

 

来,上代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 100005
#define ll long long

struct QueryType {
    ll u,v,t,id;
};
struct QueryType qu[maxn];

struct ChangeType {
    ll to,x,h;
};
struct ChangeType cha[maxn];

ll n,m,q,ti[maxn],totq,totc,vi[maxn],wi[maxn];
ll siz,deep[maxn],f[maxn],top[maxn],bel[maxn],size[maxn];
ll E[maxn<<1],V[maxn<<1],head[maxn],cnt,dis[maxn],ans[maxn];

bool if_[maxn];

inline void in(ll &now)
{
    char Cget=getchar();now=0;
    while(Cget>'9'||Cget<'0') Cget=getchar();
    while(Cget>='0'&&Cget<='9')
    {
        now=now*10+Cget-'0';
        Cget=getchar();
    }
}

void pre(ll now,ll fa)
{
    deep[now]=deep[fa]+1,size[now]=1;
    bel[now]=((++cnt)+1)/siz,f[now]=fa;
    for(ll i=head[now];i;i=E[i])
    {
        if(V[i]==fa) continue;
        pre(V[i],now),size[now]+=size[V[i]];
    }
}

void dfs(ll now,ll chain)
{
    ll pos=0;top[now]=chain;
    for(ll i=head[now];i;i=E[i])
    {
        if(V[i]==f[now]) continue;
        if(size[V[i]]>size[pos]) pos=V[i];
    }
    if(pos==0) return ;
    dfs(pos,chain);
    for(ll i=head[now];i;i=E[i])
    {
        if(V[i]==pos||V[i]==f[now]) continue;
        dfs(V[i],V[i]);
    }
}

ll solve_lca(ll x,ll y)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        x=f[top[x]];
    }
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}

bool cmp(QueryType aa,QueryType bb)
{
    if(bel[aa.u]==bel[bb.u])
    {
        if(bel[aa.v]==bel[bb.v]) return aa.t<bb.t;
        else return bel[aa.v]<bel[bb.v];
    }
    else return bel[aa.u]<bel[bb.u];
}

inline void change(ll x)
{
    if(if_[cha[x].to])
    {
        cnt-=wi[ti[dis[cha[x].to]]]*vi[dis[cha[x].to]];
        ti[dis[cha[x].to]]--;
    }
    cha[x].h=dis[cha[x].to];
    dis[cha[x].to]=cha[x].x;
    if(if_[cha[x].to])
    {
        ti[cha[x].x]++;
        cnt+=wi[ti[cha[x].x]]*vi[cha[x].x];
    }
}

inline void unchange(ll x)
{
    if(if_[cha[x].to])
    {
        cnt-=wi[ti[cha[x].x]]*vi[cha[x].x];
        ti[cha[x].x]--;
    }
    dis[cha[x].to]=cha[x].h;
    if(if_[cha[x].to])
    {
        ti[cha[x].h]++;
        cnt+=wi[ti[cha[x].h]]*vi[cha[x].h];
    }
}

inline void updata(ll x)
{
    if(if_[x])
    {
        cnt-=wi[ti[dis[x]]]*vi[dis[x]];
        ti[dis[x]]--;
    }
    else
    {
        ti[dis[x]]++;
        cnt+=wi[ti[dis[x]]]*vi[dis[x]];
    }
    if_[x]=!if_[x];
}

inline void out(ll x)
{
    if(x>9) out(x/10);
    putchar(x%10+48);
}

int main()
{
    freopen("park.in","r",stdin);
    freopen("park.out","w",stdout);
    in(n),in(m),in(q);ll u,v;siz=sqrt(n);
    for(ll i=1;i<=m;i++) in(vi[i]);
    for(ll i=1;i<=n;i++) in(wi[i]);
    for(ll i=1;i<n;i++)
    {
        in(u),in(v);
        E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
        E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
    }
    for(ll i=1;i<=n;i++) in(dis[i]);
    cnt=0,pre(1,0),dfs(1,1);ll ty;
    for(ll i=1;i<=q;i++)
    {
        in(ty);
        if(ty)
        {
            in(qu[++totq].u),in(qu[totq].v),qu[totq].t=totc,qu[totq].id=totq;
            if(bel[qu[totq].u]>bel[qu[totq].v]) swap(qu[totq].u,qu[totq].v);
        }
        else in(cha[++totc].to),in(cha[totc].x);
    }
    sort(qu+1,qu+totq+1,cmp),u=1,v=1,cnt=0;ll t=0;
    for(ll no=1;no<=totq;no++)
    {
        while(t<qu[no].t) change(++t);
        while(t>qu[no].t) unchange(t--);
        ll lca=solve_lca(u,qu[no].u);
        while(u!=lca) updata(u),u=f[u];u=qu[no].u;
        while(u!=lca) updata(u),u=f[u];u=qu[no].u;
        lca=solve_lca(v,qu[no].v);
        while(v!=lca) updata(v),v=f[v];v=qu[no].v;
        while(v!=lca) updata(v),v=f[v];v=qu[no].v;
        lca=solve_lca(u,v);
        updata(lca),ans[qu[no].id]=cnt,updata(lca);
    }
    for(ll i=1;i<=totq;i++) out(ans[i]),putchar('\n');
    fclose(stdin),fclose(stdout);
    return 0;
}

 

转载于:https://www.cnblogs.com/IUUUUUUUskyyy/p/6763931.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值