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 ...
随机推荐
- 【JSOI2008】星球大战 (并查集)
题面 Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星 ...
- Vue-开发工具的安装
1. github官网下载vue工具:https://github.com/vuejs/vue-devtools.并解压 2. 在有package.json的文件夹下,按住shift右键,选择&qu ...
- Tomcat 设置开机自启
操作系统centos6.5: Vim /etc/rc.local在末尾添加一下两行 source /etc/profile /test/tomcat/bin/startup.sh 我解释下为什么要加s ...
- Python 终端输出字体颜色
终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表 ...
- 360加固apk并自动签名
我们知道Android加混淆之后,代码的安全性得到了提高,即使你hook,反编译得到的也是乱码的,对于阅读性造成了影响,为了增强代码的破解难度,我们通常退对apk进行加固,常见的有腾讯,360,爱加密 ...
- hadoop新增kerberos租户
第一步 在kerberos服务器所在主机 通过kadmin.local,也可以通过kadmin 输入用户密码登录 kadmin.local: addprinc -randkey it1@STARYEA ...
- Spring整合Shiro并扩展使用EL表达式
Shiro是一个轻量级的权限控制框架,应用非常广泛.本文的重点是介绍Spring整合Shiro,并通过扩展使用Spring的EL表达式,使@RequiresRoles等支持动态的参数.对Shiro的介 ...
- PAT乙级-1056. 组合数的和(15)
给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...
- 深入理解Java虚拟机到底是什么
摘自:http://blog.csdn.net/zhangjg_blog/article/details/20380971 什么是Java虚拟机 我们都知道Java程序必须在虚拟机上运行.那么虚拟机到 ...
- JSON基础(Java)
1.json maven 依赖(以下都以第一个包为例) <dependency> <groupId>org.json</groupId> <artifactI ...