题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5274

题意:给一棵树和叶子的值,然后有单点修改操作和询问区间操作,询问的是每一个值出现的奇偶次数,如果全是偶数次,则输出-1,否则输出那个奇数次的数,题目保证只有一个数出现奇数次。

题解:这里的题很巧妙,要想到异或的特殊性质

 #include<cstdio>
#define F(i,a,b) for(int i=a;i<=b;i++)
#define root 1,n,1
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 const int N=1e5+;
int t,n,m,x,y,op,an,tr[*N],g[N],a[N],nxt[N*],v[N*],ed,sz[N],hs[N],fa[N],top[N],tid[N],dep[N],idx; inline void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;}
//线段树部分
void build(int l,int r,int rt){
if(l==r){tr[rt]=a[tid[l]]+;return;}
int m=(l+r)>>;
build(ls),build(rs);
tr[rt]=tr[rt<<]^tr[rt<<|];
} void update(int x,int k,int l,int r,int rt){
if(l==r){tr[rt]=k;return;}
int m=(l+r)>>;
if(x<=m)update(x,k,ls);
else update(x,k,rs);
tr[rt]=tr[rt<<]^tr[rt<<|];
} int query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R)return tr[rt];
int m=(l+r)>>,ans=;
if(L<=m)ans^=query(L,R,ls);
if(m<R)ans^=query(L,R,rs);
return ans;
}
//树链部分
void dfs1(int u,int pre){
sz[u]=,fa[u]=pre,hs[u]=,dep[u]=dep[pre]+;
for(int i=g[u];i;i=nxt[i]){
int vv=v[i];
if(vv!=pre){
dfs1(vv,u);
if(sz[vv]>sz[hs[u]])hs[u]=vv;
sz[u]+=sz[vv];
}
}
} void dfs2(int u,int tp){
top[u]=tp,tid[u]=++idx;
if(hs[u])dfs2(hs[u],tp);
for(int i=g[u];i;i=nxt[i]){
int vv=v[i];
if(vv!=fa[u]&&vv!=hs[u])dfs2(vv,vv);
}
} int ask(int x,int y){
int fx=top[x],fy=top[y],ans=;
while(fx!=fy)if(dep[fx]>dep[fy])ans=query(tid[fx],tid[x],root),x=fa[fx],fx=top[x];
else ans=query(tid[fy],tid[y],root),y=fa[fy],fy=top[y];
if(dep[x]>dep[y])x=x^y,y=x^y,x=x^y;
return ans^query(tid[x],tid[y],root);
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
F(i,,n)g[i]=;ed=,idx=;
F(i,,n-)scanf("%d%d",&x,&y),adg(x,y),adg(y,x);
F(i,,n)scanf("%d",a+i);
dfs1(,),dfs2(,),build(root);
while(m--){
scanf("%d%d%d",&op,&x,&y);
if(op)an=ask(x,y),printf("%d\n",an==?-:an-);
else update(tid[x],y+,root);
}
}
return ;
}

hdu_5274_Dylans loves tree(树剖)的更多相关文章

  1. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  2. SP375 QTREE - Query on a tree (树剖)

    题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...

  3. hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  4. CF 504 E —— Misha and LCP on Tree —— 树剖+后缀数组

    题目:http://codeforces.com/contest/504/problem/E 快速查询LCP,可以用后缀数组,但树上的字符串不是一个序列: 所以考虑转化成序列—— dfs 序! 普通的 ...

  5. HDU 5274 Dylans loves tree 树链剖分+线段树

    Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...

  6. BZOJ4353 Play with tree[树剖]

    复习几乎考不到的树剖.维护min以及min个数,打set和add标记即可,注意set优先级优于add. #include<iostream> #include<cstdio> ...

  7. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  8. SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)

    You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...

  9. SPOJ375Query on a tree I(树剖+线段树)(询问边)

    ιYou are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

随机推荐

  1. HDU 2176 取(m堆)石子游戏 尼姆博弈

    题目思路: 对于尼姆博弈我们知道:op=a[1]^a[2]--a[n],若op==0先手必败 一个简单的数学公式:若op=a^b 那么:op^b=a: 对于第i堆a[i],op^a[i]的值代表其余各 ...

  2. HDU 1847 Good Luck in CET-4 Everybody! 博弈

    题目思路: 写出SG函数(1表示先手胜,0表示先手负) 110110110110-- 发现n%3==0时,Cici胜 #include<stdio.h> #include<strin ...

  3. JMM内存管理

    原文地址http://www.cnblogs.com/BangQ/p/4045954.html 原本准备把内存模型单独放到某一篇文章的某个章节里面讲解,后来查阅了国外很多文档才发现其实JVM内存模型的 ...

  4. Linux常用命令及重要目录文件分析总结

    1.用户切换和更改密码 sudo -i / sudo su --->切换到root用户 su user --->从root用户切换回普通用户(/home/user) sudo passwd ...

  5. Struts2第四天

    Struts2第四天 昨天: 自定义的拦截器:继续methodFilterInterceptor,可以指定哪些方法需要拦截或者不拦截. Intercepters(配置拦截器),intercepter( ...

  6. hdu1008

    //c++// #includeusing namespace std;int main(){int n,j,t,start;while (cin >> n,n){start =0;t = ...

  7. log设置

    1.log的设置方式 .在 runtime setting中可以设置log的生成方式:默认的log方式:Enable logging选中,log option是Send messages only w ...

  8. 朴素贝叶斯算法 & 应用实例

    转载请注明出处:http://www.cnblogs.com/marc01in/p/4775440.html 引 和师弟师妹聊天时经常提及,若有志于从事数据挖掘.机器学习方面的工作,在大学阶段就要把基 ...

  9. 第一百零六节,JavaScript变量作用域及内存

    JavaScript变量作用域及内存 学习要点: 1.变量及作用域 2.内存问题 JavaScript的变量与其他语言的变量有很大区别.JavaScript变量是松散型的(不强制类型)本质,决定了它只 ...

  10. qt 汉化 国际化

    两种方法. 1. 使用 QTextCodec  setcodeforname 设置编码, 然后在程序中对于需要处理的字符使用 fromLocal8Bit . 2. 使用 Linguist. 首先把文件 ...