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

题目传送门

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=1e5+10;
const int M=2e5+10;
int n,q,cnt,sz;
int v[N],dep[N],size[N],head[N],fa[N];
int pos[N],bl[N];
struct data{int to,next;}e[M];
struct seg{int l,r,sum;}t[N<<2];
void insert(int u,int v)
{
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;
}
void init()
{
sz=cnt=0;
dep[1]=0;
memset(head,0,sizeof(head));
memset(size,0,sizeof(size));
n=read();q=read();
int x,y;
for(int i=1;i<n;i++)
{
x=read();y=read();
insert(x,y);
}
for(int i=1;i<=n;i++) v[i]=read();
}
void dfs1(int x)
{
size[x]=1;
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa[x])continue;
dep[e[i].to]=dep[x]+1;
fa[e[i].to]=x;
dfs1(e[i].to);
size[x]+=size[e[i].to];
}
}
void dfs2(int x,int chain)
{
int k=0;sz++;
pos[x]=sz;//分配x结点在线段树中的编号
bl[x]=chain;
for(int i=head[x];i;i=e[i].next)
if(dep[e[i].to]>dep[x]&&size[e[i].to]>size[k])
k=e[i].to;//选择子树最大的儿子继承重链
if(k==0)return;
dfs2(k,chain);
for(int i=head[x];i;i=e[i].next)
if(dep[e[i].to]>dep[x]&&k!=e[i].to)
dfs2(e[i].to,e[i].to);//其余儿子新开重链
}
void build(int k,int l,int r)//建线段树
{
t[k].l=l;t[k].r=r;
t[k].sum=0;
if(l==r)return;
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
}
void change(int k,int x,int y)//线段树单点修改
{
int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
if(l==r){t[k].sum=y;return;}
if(x<=mid)change(k<<1,x,y);
else change(k<<1|1,x,y);
t[k].sum=t[k<<1].sum^t[k<<1|1].sum;
}
int querysum(int k,int x,int y)//线段树区间求异或
{
int l=t[k].l,r=t[k].r,mid=(l+r)>>1;
if(l==x&&y==r)return t[k].sum;
if(y<=mid)return querysum(k<<1,x,y);
else if(x>mid)return querysum(k<<1|1,x,y);
else {return querysum(k<<1,x,mid)^querysum(k<<1|1,mid+1,y);}
}
int solvesum(int x,int y)
{
int sum=0;
while(bl[x]!=bl[y]) //bl[x] 点x的链哈希值
{
if(dep[bl[x]]<dep[bl[y]])swap(x,y);
sum^=querysum(1,pos[bl[x]],pos[x]);
x=fa[bl[x]];
}
if(pos[x]>pos[y])swap(x,y);
sum^=querysum(1,pos[x],pos[y]);
return sum;
}
void solve()
{
build(1,1,sz);
for(int i=1;i<=n;i++)
change(1,pos[i],v[i]+1); //pos[i]: i节点的哈希值
int op,ans,x,y;;
for(int i=1;i<=q;i++)
{
op=read();x=read();y=read();
if(op==0){v[x]=y;change(1,pos[x],y+1);}
else
{
ans=solvesum(x,y);
printf("%d\n",ans-1);
}
}
}
int main()
{
int T;
T=read();
while(T--){
init();
dfs1(1);
dfs2(1,1);
solve();
}
return 0;
}

Hdu 5274 Dylans loves tree (树链剖分模板)的更多相关文章

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

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

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

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

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

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

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

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

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

  6. hdu 5274 Dylans loves tree

    Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...

  7. SPOJ 375 Query on a tree 树链剖分模板

    第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...

  8. Query on a tree 树链剖分 [模板]

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  9. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

随机推荐

  1. Luogu P1462 通往奥格瑞玛的道路【二分/最短路】

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  2. 作为一个程序员,你了解 win 上有哪些必装的软件吗

    关于 win 的一些基础必知内容之前已经分享过,没有看过的可以戳此处→Windows 使用之那些你还不知道操作 新系统安装的第一个软件 Google Chrome 毫无疑问,作为程序员应该是首选的浏览 ...

  3. Git客户端使用教程

    课程地址 <版本控制入门 – 搬进 Github> 笔记参考 <搬进 Github> Git客户端的使用 Git for windows下载 新建一个仓库tata,使用subl ...

  4. Java键盘输入的方法

    转载:http://blog.csdn.net/u012249177/article/details/49586383 java输入的方法: import java.io.BufferedReader ...

  5. jmeter(一)工具介绍(二)

    1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...

  6. [转]在 Azure 云服务上设计大规模服务的最佳实践

    本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...

  7. OC 实现一个TODO宏

    实现一个TODO宏 转载http://blog.sunnyxx.com/2015/03/01/todo-macro/ 实现一个能产生warning的TODO宏,用于在代码里做备忘,效果: 下面一步步来 ...

  8. Tinking in Java ---Java的NIO和对象序列化

    前面一篇博客的IO被称为经典IO,因为他们大多数都是从Java1.0开始就有了的:然后今天这篇博客是关于NIO的,所以的NIO其实就是JDK从1.4开始,Java提供的一系列改进的输入/输出处理的新功 ...

  9. SQLite busy handler

    SQLite doesn't support high concurrency. In case of a lot of concurrent access from multi-process or ...

  10. ARM开发板如何选型-I.MX6Q开发板

    拥有丰富扩展能力,供货周期长的开发平台,省事安心   处理器:迅为-i.MX6开发板恩智浦Cortex-A9 四核i.MX6Q处理器,主频1GHz,内存2G,存储16GB. 系统支持:i.MX6开发板 ...