bzoj3757 苹果树(路径树上莫队)

题目链接

分析:
路径树上莫队

我们在莫队的时候记录没有色盲问题的计数
至于色盲问题,我们只要在记录答案的时候判断一下
如果该人是色盲而且a和b两种颜色都有,那么当前答案就是num-1

tip

不能提交。。。

在判断色盲的时候,直接判断a是否等于b就可以了
(这样就可以囊括所有情况了)

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

using namespace std;

const int N=100005;
const int M=100005;
struct node{
    int x,y,nxt;
}; 
node way[N<<1];
struct po{
    int x,y,a,b,id,p,block; 
};
po Q[N<<1];
int n,m,st[N],tot=0,lg,num=0,ans[N];
int cnt[N],deep[N],S[N<<1],pre[N][20],C[N],root,in[N],out[N],tt=0;
bool vis[N];

int cmp(const po &a,const po &b)
{
    if (a.block!=b.block) return a.block<b.block;
    else return a.y<b.y;
}

void add(int u,int w)
{
    tot++;
    way[tot].x=u;way[tot].y=w;way[tot].nxt=st[u];st[u]=tot;
    tot++;
    way[tot].x=w;way[tot].y=u;way[tot].nxt=st[w];st[w]=tot;
}

void dfs(int now,int fa,int dep)
{
    S[++tt]=now; in[now]=tt;
    pre[now][0]=fa; deep[now]=dep;
    for (int i=st[now];i;i=way[i].nxt)
        if (way[i].y!=fa)
            dfs(way[i].y,now,dep+1);
    S[++tt]=now; out[now]=tt;
}

int lca(int u,int w)
{
    if (deep[u]<deep[w]) swap(u,w);
    int d=deep[u]-deep[w];
    if (d)
        for (int i=0;i<=lg&&d;i++,d>>=1)
            if (d&1)
                u=pre[u][i];
    if (u==w) return u;
    for (int i=lg;i>=0;i--)
        if (pre[u][i]!=pre[w][i])
        {
            u=pre[u][i];
            w=pre[w][i];
        }
    return pre[u][0];
}

void update(int x)
{
    int co=C[x],z;
    if (vis[x]==0) vis[x]=1,z=1;
    else vis[x]=0,z=-1;
    if (cnt[co]==0&&z==1) num++;
    cnt[co]+=z;
    if (cnt[co]==0) num--;
}

void solve()
{
    int L=1,R=0;
    num=0;
    for (int i=1;i<=m;i++)
    {
        while (R<Q[i].y) R++,update(S[R]);
        while (R>Q[i].y) update(S[R]),R--;
        while (L<Q[i].x) update(S[L]),L++;
        while (L>Q[i].x) L--,update(S[L]);

        if (Q[i].p) update(Q[i].p);

        if (cnt[Q[i].a]&&cnt[Q[i].b]&&Q[i].a!=Q[i].b) ans[Q[i].id]=num-1;
        else ans[Q[i].id]=num;

        if (Q[i].p) update(Q[i].p);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&C[i]);
    for (int i=1;i<=n;i++)
    {
        int u,w;
        scanf("%d%d",&u,&w);
        if (u==0||w==0) root=(u==0? w:u);
        else add(u,w);
    }

    dfs(root,0,1);
    lg=log(n)/log(2);
    for (int i=1;i<=lg;i++)
        for (int j=1;j<=n;j++)
            pre[j][i]=pre[pre[j][i-1]][i-1];

    int unit=sqrt(n);
    for (int i=1;i<=m;i++)
    {
        int u,w,a,b;
        scanf("%d%d%d%d",&u,&w,&a,&b);
        int p=lca(u,w);
        if (in[u]>in[w]) swap(u,w);
        if (p==u||p==w)
        {
            Q[i].x=in[u]; Q[i].y=in[w];
            Q[i].a=a; Q[i].b=b; Q[i].block=(Q[i].x-1)/unit+1;
            Q[i].id=i;
        }
        else
        {
            Q[i].x=in[u]; Q[i].y=in[w];
            Q[i].a=a; Q[i].b=b; Q[i].block=(Q[i].x-1)/unit+1;
            Q[i].id=i; Q[i].p=p;
        }
    }

    sort(Q+1,Q+1+m,cmp);
    solve();
    for (int i=1;i<=m;i++) printf("%d\n",ans[i]); 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值