hdu 5274 Dylans loves tree
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)
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
(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) .
appear even times output "-1",otherwise output the value that appears odd
times.
#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的更多相关文章
- 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 (树链剖分模板)
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(树链剖分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...
- 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 ...
- AC日记——Dylans loves tree hdu 5274
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu Dylans loves tree [LCA] (树链剖分)
Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...
- hdu 5273 Dylans loves sequence
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Dylans loves sequence Description Dylans is give ...
随机推荐
- luogu【P2745】[USACO5.3]窗体面积Window Area
这个题 就是个工程题 (然而一开始我并不知道怎么做..还是看nocow的..qwq)(原题入口) 算法为 离散化 + 扫描线 将大坐标变小 并且 用横纵坐标进行扫描 来计算面积 一开始 我想边添加 ...
- BZOJ1001 狼抓兔子
最小割 代码 # include <bits/stdc++.h> # define IL inline # define RG register # define Fill(a, b) m ...
- Django使用Celery异步任务队列
1 Celery简介 Celery是异步任务队列,可以独立于主进程运行,在主进程退出后,也不影响队列中的任务执行. 任务执行异常退出,重新启动后,会继续执行队列中的其他任务,同时可以缓存停止期间接收 ...
- poi导入Excel,数字科学记数法转换
在这里分享一下使用poi 导入Excel时 把数字转换为科学记数法的解决方法: 就是使用DecimalFormat对 i 进行了格式化 结果为:
- 22.jQuery(实例)
1.开关灯效果 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Spring与Akka的集成
概述 近年来随着Spark的火热,Spark本身使用的开发语言Scala.用到的分布式内存文件系统Tachyon(现已更名为Alluxio)以及基于Actor并发编程模型的Akka都引起了大家的注意. ...
- Flume搭建
[root@hadoop01 src]# mkdir flume [root@hadoop01 src]# ll 总用量 0 drwxr-xr-x. 2 root root 6 4月 7 201 ...
- 功能性AI术语表
算法:一套计算机要遵循的指令.一个算法可以是一个简单的单步程序也可以是一个复杂的神经网络,但是通常被用来指一个模型. 人工智能:这是一个统称.广义上说,软件意味着模仿或取代人类智能的各个方面.人工智能 ...
- Spring data Redis
http://www.cnblogs.com/tankaixiong/p/3660075.html http://www.aboutyun.com/thread-20755-1-1.html
- 笔记:Hibernate 持久化类标注示例-双向1-N关系映射
1-N关系的1端持久化类 package org.drsoft.hibernate.model.oneToMany; import java.util.Date; import java.ut ...