Dylans loves tree(hdu 5274 树链剖分+线段树模板)

Dylans loves tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1884    Accepted Submission(s): 480



Problem Description
Dylans is given a tree with N nodes.

All nodes have a value A[i].Nodes on tree is numbered by 1N.

Then he is given Q questions like that:

0 x y:change node xs value to y

1 x y:For all the value in the path from x to y,do they all appear even times?

For each ② question,it guarantees that there is at most one value that appears odd times on the path.

1N,Q100000, the value A[i]N and A[i]100000
 

Input
In the first line there is a test number T.
( T3 and there is at most one testcase that N>1000)

For each testcase:

In the first line there are two numbers N and Q.

Then in the next N1 lines there are pairs of (X,Y) that stand for a road from x to y.

Then in the next line there are N numbers A1..AN stand for value.

In the next Q lines there are three numbers (opt,x,y).
 

Output
For each question ② in each testcase,if the value all appear even times output "-1",otherwise output the value that appears odd times.
 

Sample Input
 
 
13 21 22 31 1 11 1 21 1 3
 
Sample Output
 
 
-11
Hint
If you want to hack someone,N and Q in your testdata must smaller than 10000,and you shouldn't print any space in each end of the line.
 

题意:给定一棵树,每个节点都有一个权值,现在有2种操作: 0:把x节点的权值修改为y;  1:查询节点x到节点y的路径中是否有出现奇数次的权值,现保证最多只有一个权值出现奇数次,若有,输出这个权值,若没有,输出-1;

思路:由于保证最多只有一个权值出现奇数次,那么把路径中的所有权值异或一下就是那个出现奇数次的值,若没有出现奇数次的权值,就得到0。


有一个坑点:他给的权值是自然数,也就是说包含0,若出现奇数个0,异或一下还是为0,这与我们想要的不符。我们的解法是把他给的权值全部+1,修改时也把修改为的值+1,最后答案-1即可,这就巧妙的避开了0。


在一棵树上做路径的区间查询,自然想到树链剖分,然后用线段树去维护异或值即可。


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MAX = 1e5+10;

struct Edge{
    int to,next;
}edge[MAX*2];

int n,q;
int a[MAX],val[MAX];
int head[MAX],len;
int cnt;
//树链剖分
int siz[MAX],top[MAX],son[MAX],dep[MAX],faz[MAX],tid[MAX];
//线段树
int tree[MAX<<2];

void add(int from,int to)
{
    edge[len].to=to;
    edge[len].next=head[from];
    head[from]=len++;
}

//****树链剖分*******

void dfs1(int root,int fa,int depth)
{
    siz[root]=1;
    dep[root]=depth;
    faz[root]=fa;
    son[root]=0;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        if(edge[i].to==fa)
            continue;
        dfs1(edge[i].to,root,depth+1);
        siz[root]+=siz[edge[i].to];
        if(siz[edge[i].to]>siz[son[root]])
            son[root]=edge[i].to;
    }
}

void dfs2(int root,int tp)
{
    top[root]=tp;
    tid[root]=++cnt;
    if(son[root])
        dfs2(son[root],tp);
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        if(edge[i].to==son[root]||edge[i].to==faz[root])
            continue;
        dfs2(edge[i].to,edge[i].to);
    }
}

//*****************

//****线段树*******

void PushUp(int root)
{
    tree[root]=tree[root<<1]^tree[root<<1|1];
}

void build(int l,int r,int root)
{
    if(l==r)
    {
        tree[root]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
    PushUp(root);
}

void update(int x,int y,int l,int r,int root)
{
    if(l==r)
    {
        tree[root]=y;
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid)
        update(x,y,l,mid,root<<1);
    else
        update(x,y,mid+1,r,root<<1|1);
    PushUp(root);
}

int query(int L,int R,int l,int r,int root)
{
    if(L<=l&&r<=R)
    {
        return tree[root];
    }
    int mid=(l+r)>>1;
    int ans=0;
    if(L<=mid)
        ans^=query(L,R,l,mid,root<<1);
    if(R>mid)
        ans^=query(L,R,mid+1,r,root<<1|1);
    return ans;
}

//*****************

//基于树链的查询
int change(int x,int y)
{
    int ans=0;
    int f1=top[x];
    int f2=top[y];
    while(f1!=f2)
    {
        if(dep[f1]<dep[f2])
        {
            swap(x,y);
            swap(f1,f2);
        }
        
        //数据结构的修改/查询
        ans^=query(tid[f1],tid[x],1,n,1);

        x=faz[f1];
        f1=top[x];
    }
    if(tid[x]>tid[y])
        swap(x,y);

    //数据结构的修改/查询
    ans^=query(tid[x],tid[y],1,n,1);

    return ans;
}

int main()
{
    int T;
    int Case=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&q);
        memset(head,-1,sizeof(head));
        memset(a,0,sizeof(a));
        len=0;
        cnt=0;
        for(int i=1;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&val[i]);
            val[i]+=1; //+1
        }
        for(int i=1;i<=n;i++)
            a[tid[i]]=val[i];
        build(1,n,1);
        for(int i=1;i<=q;i++)
        {
            int opt,x,y;
            scanf("%d%d%d",&opt,&x,&y);
            if(opt==0)
            {
                update(tid[x],y+1,1,n,1);  //y+1
            }
            else
            {
                printf("%d\n",change(x,y)-1);  //ans-1
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值