hdu_5274_Dylans loves tree(树剖)
题目连接: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(树剖)的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- SP375 QTREE - Query on a tree (树剖)
题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...
- hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- CF 504 E —— Misha and LCP on Tree —— 树剖+后缀数组
题目:http://codeforces.com/contest/504/problem/E 快速查询LCP,可以用后缀数组,但树上的字符串不是一个序列: 所以考虑转化成序列—— dfs 序! 普通的 ...
- HDU 5274 Dylans loves tree 树链剖分+线段树
Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...
- BZOJ4353 Play with tree[树剖]
复习几乎考不到的树剖.维护min以及min个数,打set和add标记即可,注意set优先级优于add. #include<iostream> #include<cstdio> ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- SPOJ375Query on a tree I(树剖+线段树)(询问边)
ιYou are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建
今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...
- VS编译wxWidgets
准备工作 下载wxWidgets源码包(官网),我用的是3.02版: 安装Visual Studio.我用的是VS 2015 RC: 编译源码 解压wxWidgets的源码包,会得到一大堆文件.进入b ...
- jquery学习笔记3 jq遍历
遍历方式:向上(父级元素) 向下(子元素) 水平(同胞元素) 一.向上遍历 parent() 向上一级 放回被选元素的直接父元素 parents() 返回被选元 ...
- ASCII码对应表chr(num)
chr(9) tab空格 chr(10) 换行 chr(13) 回车 Chr(13)&chr(10) 回车换行 chr(32) 空格符 ...
- jQuery(4)—— jQuery中的事件
jQuery中的事件 [加载DOM] 在常规的JavaScript代码中,通常使用window.onload方法,在jQuery中,使用的是$(document).ready()方法.极大地提高了we ...
- 淘淘商城_day11_课堂笔记
今日大纲 发布前的准备 实施发布 一部分是由我来发布 一部分是由你们来发布 讲解分布式部署架构 测试 功能测试 压力测试 项目实战的准备以及分组 分组 抽取功能 讲解所需要开发的功能 项目部署上线流程 ...
- hdu1020
#include <stdio.h> int main(void){ int n,i,c; char txt[10001]; scanf("%d", &n); ...
- L3-007. 天梯地图
L3-007. 天梯地图 题目链接:https://www.patest.cn/contests/gplt/L3-007 Dijstra 这题是Dijstra的变形,麻烦的是两种最短路的相同距离时的选 ...
- Sass入门:第四章
1.加法 加法运算是Sass中运算的一种,在变量或属性中都可以做加法运算.如: .box { width: 20px + 8in; } 编译出来的CSS: .box { width: 788px; } ...
- C#读书笔记之object类的基本方法
The ToString() Method For example:int i = 50;string str = i.ToString(); // returns "50" He ...