HDU 5274 Dylans loves tree(树链剖分)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5274
【题目大意】
给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数的数,题目保证每次询问的链上最多只有一个数出现次数为奇数。如果不存在这样的数,就输出-1。
【题解】
题目等价于求链上点的异或和,树链剖分,线段树维护区间异或和,然后链上查询即可,注意到存在权值为0的特殊情况,所以我们将更新的数字都+1,在最后处理答案的时候-1即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <climits>
#include <cstring>
using namespace std;
const int N=300010;
int val[N],tot,op,x,d[N],num[N],ed=0,u,w,n,m,i,v[N],vis[N],f[N],g[N],nxt[N],size[N],son[N],st[N],en[N],dfn,top[N],t;char ch;
void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
void dfs(int x){
size[x]=1;
for(int i=g[x];i;i=nxt[i])if(v[i]!=f[x]){
f[v[i]]=x,d[v[i]]=d[x]+1;
dfs(v[i]),size[x]+=size[v[i]];
if(size[v[i]]>size[son[x]])son[x]=v[i];
}
}
void dfs2(int x,int y){
if(x==-1)return;
st[x]=++dfn;top[x]=y;
if(son[x])dfs2(son[x],y);
for(int i=g[x];i;i=nxt[i])if(v[i]!=son[x]&&v[i]!=f[x])dfs2(v[i],v[i]);
en[x]=dfn;
}
struct Node{int l,r;int sum;}T[N*4];
void build(int x,int l,int r){
T[x].l=l;T[x].r=r;T[x].sum=0;
if(l==r)return;int mid=(l+r)>>1;
build(x<<1,l,mid);build((x<<1)|1,mid+1,r);
}
void up(int x){T[x].sum=T[x<<1].sum^T[x<<1|1].sum;}
void update(int x,int k,int val){
if(T[x].l==k&&T[x].r==k){T[x].sum^=val;return;}
int mid=(T[x].l+T[x].r)>>1;
if(k<=mid)update(x<<1,k,val);
else update((x<<1)|1,k,val); up(x);
}
int sum(int x,int l,int r){
if(l<=T[x].l&&T[x].r<=r)return T[x].sum;
int mid=(T[x].l+T[x].r)>>1,tmp=0;
if(l<=mid)tmp^=sum(x<<1,l,r);
if(r>mid)tmp^=sum((x<<1)|1,l,r);
return tmp;
}
int chain(int x,int y){
int res=0;
for(;top[x]!=top[y];x=f[top[x]]){
if(d[top[x]]<d[top[y]]){int z=x;x=y;y=z;}
res^=sum(1,st[top[x]],st[x]);
}if(d[x]<d[y]){int z=x;x=y;y=z;}
res^=sum(1,st[y],st[x]);
return res;
}
void init(){
memset(g,dfn=ed=tot=0,sizeof(g));
memset(v,0,sizeof(v));
memset(nxt,0,sizeof(nxt));
memset(son,-1,sizeof(son));
}
int cas;
int main(){
scanf("%d",&cas);
while(cas--){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d",&u,&w);
add(u,w); add(w,u);
}dfs(1);dfs2(1,1);
build(1,1,dfn);
for(int i=1;i<=n;i++){
scanf("%d",&val[i]); val[i]++;
update(1,st[i],val[i]);
}
while(m--){
scanf("%d%d%d",&op,&u,&w);
if(op){
printf("%d\n",chain(u,w)-1);
}else{
update(1,st[u],val[u]^(w+1));
val[u]=w+1;
}
}
}return 0;
}
HDU 5274 Dylans loves tree(树链剖分)的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 5274 Dylans loves tree 树链剖分+线段树
Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)
Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...
- hdu 5274 Dylans loves tree
Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- HDU 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- HDU 5044 Tree --树链剖分
题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...
随机推荐
- Linux学习之服务器端口查看的方法
1.用netstat查看: [grid@rac121 admin]$ netstat -anp | grep oracle (Not all processes could be identified ...
- QF——OC内存管理详解
堆的内存管理: 我们所说的内存管理,其实就是堆的内存管理.因为栈的内存会自动回收,堆的内存需要我们手动回收. 栈中一般存储的是基本数据类型变量和指向对象的指针(对象的引用),而真实的对象存储在堆中.因 ...
- MySQL数据库mysqlcheck的使用方法
MySQL数据库mysqlcheck的使用方法的相关知识是本文我们主要要介绍的内容,我们知道,mysqlcheck,是mysql自带的可以检查和修复MyISAM表,并且它还可以优化和分析表,mysql ...
- Android ProgressBar实现加载进度条
progressBar Android进度条组件. progressBar的关键属性: android:max="100" 最大显示进度条 andr ...
- 安装GNUstep并运行第一个objc程序
在windows环境下安装GNUstep,运行objective-c程序,今天试了一下,记录一下操作步骤, 1,登陆http://ftpmain.gnustep.org/pub/gnustep/bin ...
- mysql函数操作(4)
<?php ... $query = "INSERT INTO contactInfo (name, address, phone) VALUES (?, ?, ?)"; $ ...
- 关于scala环境配置详解
首先从官网下载适合自身电脑配置的scala安装包.scala下载官网网址:http://www.scala-lang.org/download/ 同时scala还有自己集成好的IDE,例如eclips ...
- Storm csdn
blog.csdn.net/lonelytrooperblog.csdn.net/leytton http://my.oschina.net/apdplat/blog/308396
- golang之匿名函数
package main import "fmt" /* squares返回一个匿名函数 * 该匿名函数每次调用返回下一个数的平方 * func name(parameter-li ...
- 初识eclipse及配置相关
1. Eclipse 导入外部项目无法识别为Web项目并无法再部署到tomcat解决办法: http://www.cnblogs.com/heshan664754022/archive/2013/05 ...