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. 全排列(传统&&黑科技)

    近期几次考试的一些题目暴力分都有用到全排列. 全排列是个好东西啊... 回想一下,我们最开始学到全排列是什么时候呢? 大概是学搜索的时候罢... 一.传统搜索算法 想复习可以戳 https://www ...

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

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

  3. [POI2007]石头花园SKA

    Description Blue Mary是一个有名的石头收藏家.迄今为止,他把他的藏品全部放在他的宫殿的地窖中.现在,他想将他的藏品陈列在他的花园中.皇家花园是一个边长为1000000000单位的平 ...

  4. _bzoj1013 [JSOI2008]球形空间产生器sphere【高斯消元】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1013 保存高斯消元模版. ps,这一题的英文名字是ヨスガノソラ的开发商~^_^ #inclu ...

  5. [ZPG TEST 110] 多边形个数【DP】

    1. 多边形个数 (polygons.pas/c/cpp) [问题描述] 给定N线段,编号1到n.并给出这些线段的长度,用这些线段组成一个K边形,并且每个线段做多使用一次.若使用了一条不同编号的线段, ...

  6. ORA-01144_表空间数据文件超出最大限制

    Oracle11gR2扩展表空间报ORA-01144错误. 数据块大小为8K的数据库,单个数据文件大小限制在32GB内. 解决办法: 1.增加表空间数据文件的方式: 2.创建BIGFILE表空间:

  7. apache反向代理配置

    apache简单的反向代理配置 Proxypass /api /http://locahost:3000 反向代理-1.jpg

  8. redis 其他特性

    1.消息订阅与发布 subscribe my1 订阅频道 psubscribe my1* 批量订阅频道,订阅以my1开头的所有频道 publish my1 hello 在指定频道中发布消息,返回值为接 ...

  9. chvt - 修改虚拟终端的前台环境

    SYNOPSIS(总览) chvtN DESCRIPTION(描述) chvt N 命令用来生成 /dev/ttyN 的前台终端.如果它本来不存在,即创建相应的屏幕.为了删除掉不用的VT(虚拟终端), ...

  10. java实现zip,gzip,7z,zlib格式的压缩打包

    本文主要介绍的是通过使用java的相关类可以实现对文件或文件夹的压缩. zlib是一种数据压缩程序库,它的设计目标是处理单纯的数据(而不管数据的来源是什么). 7z 是一种新的压缩格式,它拥有目前最高 ...