Hdu 5274 Dylans loves tree (树链剖分模板)
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 (树链剖分模板)的更多相关文章
- 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(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- hdu 5274 Dylans loves tree
Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...
- SPOJ 375 Query on a tree 树链剖分模板
第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...
- Query on a tree 树链剖分 [模板]
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
随机推荐
- SpringBoot入门-15(springboot配置freemarker使用YML)
https://blog.csdn.net/fengsi2009/article/details/78879924 application.yml spring: http: encoding: fo ...
- Python上下文管理器(Context managers)
上下文管理器(Context managers) 上下文管理器允许你在有需要的时候,精确地分配和释放资源. 使用上下文管理器最广泛的案例就是with语句了.想象下你有两个需要结对执行的相关操作,然后还 ...
- UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...
- APP增量更新
增量更新的概念: 当我们手机上安装的app版本与服务器的最新版本不一致的时候,传统做法是重新下载安装一个最新版的apk文件,不过这种方式比较耗流量,不利于用户体验.增量更新就是只下载当前app版本与最 ...
- [转]windows azure How to use Blob storage from .NET
本文转自:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/?rnd=1 ...
- git ---回到过去
git命令回顾 git checkout /git reset -git reset HEAD~ //~代表回滚到第几个版本.. 有多个的话可以在~后面加个数字 git reset --mixe ...
- Java集合框架源码(二)——hashSet
注:本人的源码基于JDK1.8.0,JDK的版本可以在命令行模式下通过java -version命令查看. 在前面的博文(Java集合框架源码(一)——hashMap)中我们详细讲了HashMap的原 ...
- 【PostgreSQL-9.6.3】表空间
在PostgreSQL中,表空间实际上是为表指定一个存储目录,这样方便我们把不同的表放在不同的存储介质或者文件系统中.在创建数据库.表.索引时都可以指定表空间. 1. 创建表空间 --表空间目录必须是 ...
- Farseer.net轻量级ORM开源框架 V1.2版本升级消息
V1.1到V1.2的更新,重构了很多类及方法,其中主要做了性能优化(取消所有反射,使用表达式树+缓存).解耦了SQL生成层(没有实体.队列的依赖,所有数据均通过表达式树传递解析) 先上内部更新历史记录 ...
- QQ感叹号是什么鬼?原来是服务器波动,腾讯官方来辟谣了
今天晚上很多网友在用QQ发送消息的时候发现,自己发送的消息一直是感叹号❗到底是怎么回事呢?是消息都发不出去了吗?马浩周通过手机测试后发现,其实消息是可以发出去的,而官方手机QQ出来已经通知了,是服务器 ...