Dylans loves tree

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

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Problem Description
Dylans is given a tree with N nodes.

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

Then he is given Q questions like that:

①0 x y :change node x′s 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.

1≤N,Q≤100000 , the value A[i]∈N and A[i]≤100000

 
 
Input
In the first line there is a test number T .
(T≤3 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 N−1 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
1
3 2
1 2
2 3
1 1 1
1 1 2
1 1 3
 
Sample Output
-1 1
 
题目大意:给出一颗n个点的树,有q个操作,点有点权。
操作① 0,x,y 修改x的点权为y
操作② 1,x,y 询问点x到点y是否有出现过奇数次的权值,有则输出,无则输出-1。输入保证至多有一个出现过一次的权值
基本框架:树链剖分+线段树
思路:单点修改+询问区间异或和
若k出现过偶数次,则这些k的异或和为0
若只有1个k出现过奇数次,那异或的结果=k
一个小细节:
因为0无论异或多少次都是0,所以将所有数都+1,输出答案-1
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100001
using namespace std;
struct node
{
int l,r,key;
void clear() {l=r=key=;}
}tr[N*];
struct edge{int next,to;}e[N*];
int id[N],dep[N],son[N],fa[N],bl[N],sz,e_tot,tr_tot,T,n,q,head[N];
inline void add(int u,int v)
{
e[++e_tot].to=v;e[e_tot].next=head[u];head[u]=e_tot;
e[++e_tot].to=u;e[e_tot].next=head[v];head[v]=e_tot;
}
inline void dfs1(int x,int f)
{
son[x]++;
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa[x]) continue;
dep[e[i].to]=dep[x]+;
fa[e[i].to]=x;
dfs1(e[i].to,x);
son[x]+=son[e[i].to];
}
}
inline void dfs2(int x,int chain)
{
sz++;
bl[x]=chain;
id[x]=sz;
int y=;
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa[x]) continue;
if(son[e[i].to]>son[y]) y=e[i].to;
}
if(!y) return;
dfs2(y,chain);
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa[x]||e[i].to==y) continue;
dfs2(e[i].to,e[i].to);
}
}
inline void build(int l,int r)
{
tr_tot++; tr[tr_tot].clear();
tr[tr_tot].l=l;tr[tr_tot].r=r;
if(l==r) return;
int mid=l+r>>;
build(l,mid);build(mid+,r);
}
inline void change(int k,int x,int w)
{
if(tr[k].l==tr[k].r) {tr[k].key=w;return;}
int mid=tr[k].l+tr[k].r>>,l=k+,r=k+(tr[k+].r-tr[k+].l+<<);
if(x<=mid) change(l,x,w);
else change(r,x,w);
tr[k].key=tr[l].key^tr[r].key;
}
inline int query(int k,int opl,int opr)
{
if(tr[k].l>=opl&&tr[k].r<=opr) {return tr[k].key;}
int mid=tr[k].l+tr[k].r>>,l=k+,r=k+(tr[k+].r-tr[k+].l+<<);
int tmp=;
if(opl<=mid) tmp=query(l,opl,opr);
if(opr>mid) tmp^=query(r,opl,opr);
return tmp;
}
inline void solve_query(int u,int v)
{
int ans=;
while(bl[u]!=bl[v])
{
if(dep[bl[u]]<dep[bl[v]]) swap(u,v);
ans^=query(,id[bl[u]],id[u]);
u=fa[bl[u]];
}
if(id[u]>id[v]) swap(u,v);
ans^=query(,id[u],id[v]);
printf("%d\n",ans-);
}
inline void solve()
{
int p,x,y;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
change(,id[i],x+);
}
for(int i=;i<=q;i++)
{
scanf("%d%d%d",&p,&x,&y);
if(!p) change(,id[x],y+);
else solve_query(x,y);
}
}
inline void pre()
{
memset(son,,sizeof(son));
memset(head,,sizeof(head));
memset(fa,,sizeof(fa));
memset(dep,,sizeof(dep));
sz=e_tot=tr_tot=;
}
int main()
{
scanf("%d",&T);
while(T--)
{
pre();
scanf("%d%d",&n,&q);
int u,v;
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
}
dfs1(,);
dfs2(,);
build(,n);
solve();
}
}

hdu 5274 Dylans loves tree的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 5274 Dylans loves tree(树链剖分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...

  6. 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 ...

  7. AC日记——Dylans loves tree hdu 5274

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

  8. hdu Dylans loves tree [LCA] (树链剖分)

    Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...

  9. hdu 5273 Dylans loves sequence

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...

随机推荐

  1. java设计模式-----11、代理模式

    Proxy模式又叫做代理模式,是构造型的设计模式之一,它可以为其他对象提供一种代理(Proxy)以控制对这个对象的访问. 所谓代理,是指具有与代理元(被代理的对象)具有相同的接口的类,客户端必须通过代 ...

  2. 如何降低90%Java垃圾回收时间?以阿里HBase的GC优化实践为例

    过去的一年里,我们准备在Ali-HBase上突破这个被普遍认知的痛点,为此进行了深度分析及全面创新的工作,获得了一些比较好的效果.以蚂蚁风控场景为例,HBase的线上young GC时间从120ms减 ...

  3. Hive数据仓库笔记(三)

    Joins: Inner  joins: hive> SELECT * FROM sales; Joe 2 Hank 4 Ali 0 Eve 3 Hank 2 hive> SELECT * ...

  4. Java 8 中 ConcurrentHashMap工作原理的要点分析

    简介: 本文主要介绍Java8中的并发容器ConcurrentHashMap的工作原理,和其它文章不同的是,本文重点分析了对不同线程的各类并发操作如get,put,remove之间是如何同步的,以及这 ...

  5. vue简单的自由拖拽

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 第一周CoreIDRAW课总结

      一. 问:这节课学到了什么知识? 答:这节课我学到了很多知识,作为初学者的我,老师给我讲了CorelDEAW的启动,安装,基础知识和应用与用途. 基础知识 位图:是由无数个像素点构成的图像,又叫点 ...

  7. [Luogu P1564] 膜拜

    Description 神牛有很多-当然-每个同学都有自己衷心膜拜的神牛. 某学校有两位神牛,神牛甲和神牛乙.新入学的N 位同学们早已耳闻他们的神话. 所以,已经衷心地膜拜其中一位了.现在,老师要给他 ...

  8. NodeJS FTP模块使用

    模块说明:https://www.npmjs.com/package/ftp 上传文件 建立连接-> 判断文件夹是否存在->创建文件夹->上传文件->End 核心代码: 连接参 ...

  9. 【Python】 http客户端库requests & urllib2 以及ip地址处理IPy

    requests requests是个HTTPClient库,相比于urllib,urllib2等模块比更加简洁易用 ■ get请求 作为示例,讲一下关于requests如何发起并处理一个get请求 ...

  10. Linux命令 ls -l s输出内容含义详解

    1. ls  只显示文件名或者文件目录 2. ls -l(这个参数是字母L的小写,不是数字1) 用来查看详细的文件资料 在某个目录下键入ls -l可能会显示如下信息: 文件属性(占10个字符空间)  ...