J - Association of Cats and Magical Lights (dfs tree array sequencer +)

dfs after reordering to the number of inquiries about the range

tree [i] [c] represents the i-lowbit (i) c is the number of color

 

  

#include<bits/stdc++.h>

#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lowbit(x) x&(-x)
const int maxn=5e5+10;

using namespace std;

int tree[maxn][105],c[maxn],e[maxn],id[maxn];
int n,m,cnt=0;

vector<int>q[maxn];

void dfs(int u)
{
    id[u]=++cnt;
    int s=q[u].size();
    for(int i=0;i<s;i++)
    {
        dfs(q[u][i]);
    }
    and [u] = cnt;
}

void updata(int pos,int c,int k)
{
    for(int i=pos;i<=n;i+=lowbit(i))
        tree[i][c]+=k;
}

int query(int pos,int c)
{
    int ans=0;
    for(int i=pos;i>0;i-=lowbit(i))
        years + = tree [i] [c];
    return years;
}

int main ()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);
    for(int i=2;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        q[x].push_back(i);
    }
    dfs(1);
//    cout<<"id"<<endl;
//    for(int i=1;i<=n;i++)
//        cout<<e[i]<<" ";
//    cout<<endl;

    for(int i=1;i<=n;i++)
    {
        updata(id[i],c[i],1);
    }

    while(m--)
    {
        int cmd,pos;
        scanf("%d%d",&cmd,&pos);
        if(cmd)
        {
            updata (id [post], c [post], - 1 );
            updata(id[pos],cmd,1);
            c[pos]=cmd;
        }
        else
        {
            int ans=0;
            for(int i=1;i<=100;i++)
            {
                int t=query(e[pos],i)-query(id[pos]-1,i);
                if(t%2==1)
                    years ++ ;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/minun/p/11022364.html