JZOJ4388. 【GDOI2016模拟3.15】染色

这里写图片描述
这里写图片描述

题解

这题有很多做法,
其中一种比较经典的做法就是树链剖分。

询问x到所有黑点的距离和,
d i s ( x ) + d i s ( y ) 2 d i s ( l c a ( x , y ) ) 其中y为黑点
dis表示这个点到根节点的距离。
再化简一下,
c n t d i s ( x ) + d i s ( y ) 2 d i s ( l a c ( x , y ) )
其实另外两个东西都很好处理,
关键是 d i s ( l a c ( x , y ) )

每次加入一个黑点的时候,就将它到根的路径全部加1,
而查询这个的时候,就直接查询它到根节点路径上的权值和。

然后就可以愉快地链剖了。

code

#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define N 200003
#define M 103
#define db double
#define P putchar
#define G getchar
#define inf 998244353
#define LL long long
using namespace std;
char ch;
void read(int &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    int w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
    n*=w;
}

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
int abs(int x){return x<0?-x:x;}
int sqr(int x){return x*x;}
void write(LL x){if(x>9) write(x/10);P(x%10+'0');}

LL s[N*4],l[N*4],r[N*4],dis[N],w[N*4],lazy[N*4],V[N*4],ops,ans,sum;
int nxt[N],to[N],lst[N],v[N],tot;
int si[N],top[N],son[N],fa[N],id[N],rank[N],now;
int n,m,t,opl,opr,opx,cnt;
bool col[N];

void ins(int x,int y)
{
    nxt[++tot]=lst[x];
    to[tot]=y;
    lst[x]=tot;
}

void dfs(int x)
{
    int t=0;si[x]=1;
    for(int i=lst[x];i;i=nxt[i])
        if(to[i]!=fa[x])
        {
            fa[to[i]]=x;
            dis[to[i]]=dis[x]+v[to[i]];
            dfs(to[i]);
            si[x]+=si[to[i]];
            if(si[to[i]]>t)t=si[to[i]],son[x]=to[i];
        }
}

void dfs_(int x,int t)
{
    id[x]=++now;
    rank[now]=x;
    top[x]=t;
    if(son[x])dfs_(son[x],t);
    for(int i=lst[x];i;i=nxt[i])
        if(to[i]!=fa[x] && to[i]!=son[x])
            dfs_(to[i],to[i]);
}

void add(int x,LL v)
{
    s[x]+=V[x]*v;
    w[x]+=v;
    lazy[x]+=v;
}

void downdata(int x)
{
    if(!lazy[x])return;
    add(x<<1,lazy[x]);
    add(x<<1|1,lazy[x]);
    lazy[x]=0;
}

void updata(int x)
{
    s[x]=s[x<<1]+s[(x<<1)|1];
    w[x]=w[x<<1]+w[(x<<1)|1];
}

void build(int x,int ll,int rr)
{
    l[x]=ll;r[x]=rr;
    if(ll==rr)
    {
        w[x]=lazy[x]=s[x]=0;
        V[x]=v[rank[ll]];
        return;
    }
    int m=(ll+rr)>>1;
    build(x<<1,ll,m);
    build((x<<1)|1,m+1,rr);
    V[x]=V[x<<1]+V[x<<1|1];
}

void work(int x)
{
    downdata(x);
    if(opl<=l[x] && r[x]<=opr)
    {
        if(opx==1)add(x,1);else
        if(opx==2)ops+=s[x];
        return;
    }
    int m=(l[x]+r[x])>>1;
    if(opl<=m)work(x<<1);
    if(m<opr)work((x<<1)|1);
    updata(x);
}

void find(int x)
{
    for(;x;x=fa[top[x]])
        opl=id[top[x]],opr=id[x],work(1);
}

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);

    read(n);read(m);
    for(int i=2;i<=n;i++)
        read(t),ins(t+1,i);
    for(int i=2;i<=n;i++)
        read(v[i]);

    dfs(1);
    dfs_(1,1);
    build(1,1,n);
    memset(col,1,sizeof(col));

    for(int i=1;i<=m;i++)
    {
        read(opx);read(t);t++;
        if(opx==1)
        {
            if(col[t])find(t),cnt++,sum+=dis[t];
            col[t]=0;
        }
        else
        {
            ops=0;find(t);
            ans=sum-2*ops+dis[t]*cnt;
            write(ans),P('\n');
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/81134844