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. IE兼容之中文汉字请求乱码-network

    IE兼容之中文汉字请求乱码 IE控制台报错: IE网络报错: 解决方法: encodeURI('--- @ -- 子卿 -'); // "---%20@%20--%20%E5%AD%90%E ...

  2. docker保存对容器的修改

    我有一个Ubuntu的镜像用命令docker run -i -t -v /home/zzq/app/:/mnt/software/ 0ef2e08ed3fa /bin/bash登录进去发现没vi编辑器 ...

  3. 进击的Python【第十五章】:Web前端基础之DOM

    进击的Python[第十五章]:Web前端基础之DOM 简介:文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示 ...

  4. C++入门知识点总结

    阅读目录 1 C++中的命名空间 C++中使用命名空间来解决在相同文件或范围的同名变量问题,示例程序如下: #include <iostream> using namespace std; ...

  5. [CF997E] Good SubSegment

    Description Transmission Gate 给你一个长度为n的排列P,定义一段子区间是好的,当且仅当这个子区间内的值构成了连续的一段.例如对于排列\(\{1,3,2\}\),\([1, ...

  6. 题解报告:NYOJ 题目143 第几是谁?(逆康托展开)

    描述 现在有"abcdefghijkl”12个字符,将其按字典序排列,如果给出任意一种排列,我们能说出这个排列在所有的排列中是第几小的.但是现在我们给出它是第几小,需要你求出它所代表的序列. ...

  7. ORA-00445: Background Process "xxxx" Did Not Start After 120 Seconds

    Recent linux kernels have a feature called Address Space Layout Randomization (ASLR).ASLR  is a feat ...

  8. 移动端UI自动化Appium测试——DesiredCapabilities参数配置及含义

    一.DesiredCapabilities的作用: 负责启动服务端时的参数设置,启动session的时候是必须提供的. Desired Capabilities本质上是key value的对象,它告诉 ...

  9. 使用Glide加载Android图片

    一.概述 Glide是一个在Android端非常好的图片缓冲工具,总体上来说,他有以下优点 使用简单 自适应程度高 支持常见的图片格式,如jpg,png等 支持多种数据源,网络,本地,资源,Asset ...

  10. Node.js——express

    res.send(),比原生的 res.end() 强大,原生只支持字符串和Buffer对象,而且需要自己加响应报文头,send支持字符串.Buffer.Json对象.数组,而且自动加响应报文头 ap ...