HDU5274 Dylans loves tree(树链剖分+异或)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5274

题意很简单,输入以后,查询的时候

0 x y,是把x点改成y。

1 x y,是查询[x,y]中,数字出现的次数是否都是偶数,出现奇数的数字<=1。

都是偶数的话,输出-1,有奇数的话,输出奇数的权值。

思路:这题也是一个裸题,但是这题的线段树维护的比较巧妙,因为一个数异或自己偶数次就是0,所以求一个区间异或和就行了。但是有个地方要注意,就是权值为0的话,异或自己,奇数次也是0。所以可以把所有的权值+1,输出的时候再-1就好了。如果+1的话,在修改权值的时候也不要忘记+1。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long LL;
typedef pair<int,int> pii;
#define PB push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define calm (l+r)>>1
const int INF=(int)1e9+7;

const int maxn=100010;
struct EE{
    int to,next;
    EE(){}
    EE(int to,int next):to(to),next(next){}
}edge[maxn*2];
int n,q,Ecnt,head[maxn],tot,val[maxn];
int top[maxn],fa[maxn],num[maxn],son[maxn],id[maxn],rev[maxn],deep[maxn];

inline void addedge(int a,int b){
    edge[Ecnt]=EE(b,head[a]);
    head[a]=Ecnt++;
}

void dfs1(int s,int pre,int d){
    fa[s]=pre;deep[s]=d;num[s]=1;son[s]=-1;
    for(int i=head[s];~i;i=edge[i].next){
        int t=edge[i].to;
        if(t==pre)continue;
        dfs1(t,s,d+1);num[s]+=num[t];
        if(son[s]==-1||num[t]>num[son[s]]){
            son[s]=t;
        }
    }
}
void dfs2(int s,int rt){
    top[s]=rt;id[s]=++tot;rev[tot]=s;
    if(son[s]==-1)return;
    dfs2(son[s],rt);
    for(int i=head[s];~i;i=edge[i].next){
        int t=edge[i].to;
        if(t==fa[s]||t==son[s])continue;
        dfs2(t,t);
    }
}
//SegmentTree
int sum[maxn<<2];
inline void pushup(int rt){
    sum[rt]=sum[rt<<1]^sum[rt<<1|1];
}
void build(int l,int r,int rt){
    if(l==r){
        sum[rt]=val[rev[l]];
        return;
    }
    int m=calm;
    build(lson);build(rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R){
        return sum[rt];
    }
    int m=calm,ans=0;
    if(L<=m)ans^=query(L,R,lson);
    if(R>m)ans^=query(L,R,rson);
    return ans;
}
void update(int x,int v,int l,int r,int rt){
    if(l==r){
        sum[rt]=v;
        return;
    }
    int m=calm;
    if(x<=m)update(x,v,lson);
    else update(x,v,rson);
    pushup(rt);
}
int findsum(int x,int y){
    int f1=top[x],f2=top[y],ans=0;
    while(f1!=f2){
        if(deep[f1]<deep[f2]){
            swap(f1,f2);swap(x,y);
        }
        ans^=query(id[f1],id[x],1,n,1);
        x=fa[f1];f1=top[x];
    }
    if(deep[x]>deep[y])swap(x,y);
    ans^=query(id[x],id[y],1,n,1);
    return ans;
}
int main()
{
    //freopen("/home/xt/code/acm/input.txt","r",stdin);
    int T;scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&q);
        memset(head,-1,sizeof head);
        Ecnt=0;tot=0;
        for(int i=1;i<n;i++){
            int a,b;scanf("%d%d",&a,&b);
            addedge(a,b);addedge(b,a);
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&val[i]);
            val[i]++;
        }
        dfs1(1,0,1);dfs2(1,1);
        build(1,n,1);
        while(q--){
            int op;scanf("%d",&op);
            int a,b;scanf("%d%d",&a,&b);
            if(op==0){
                update(id[a],b+1,1,n,1);//更新的也要+1
            }
            else{
                printf("%d\n",findsum(a,b)-1);//输出-1
            }
        }
    }
    //printf("[Run in %.1fs]\n",(double)clock()/CLOCKS_PER_SEC);
    return 0;
}
时间: 2024-10-04 06:13:25

HDU5274 Dylans loves tree(树链剖分+异或)的相关文章

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

HDU5274 Dylans loves tree(树链剖分)很巧的点权更新

Dylans loves tree Accepts: 49 Submissions: 262 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 Dylans有一棵N个点的树.每个点有点权.树上节点标号为1∼N. 他得到了Q个询问,形式如下: ①0 x y:把第x个点的点权修改为y. ②1 x y:对于x∼y路径上的每一种点权,是否都出现偶数次? 保证每次询问的路径上最多只

[Codechef - ADITREE] Adi and the Tree - 树链剖分,线段树

[Codechef - ADITREE] Adi and the Tree Description 树上每个节点有一个灯泡,开始所有灯泡都是熄灭的.每次操作给定两个数 \(a,b\) ,将 \(a,b\) 这两个节点的灯的状态改变.定义某个状态的权值为,将树上所有亮点两两配对,每个对的权值的总和最小值.其中一个配对的权值定义为这两个点之间的距离.求出每次操作后的权值. Solution 很容易发现如果我们将每个亮点到树根的路径染色,那么染色次数为奇数的路径就会被统计入答案. 所以只需要维护布尔值

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

spoj 375 Query on a tree (树链剖分)

Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to ti or Q

SPOJ Query on a tree 树链剖分 水题

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to tior QUERY a b : ask fo

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include

poj 3237 Tree 树链剖分+线段树

Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions