hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)
Dylans loves tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1915 Accepted Submission(s): 492
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).
3 2
1 2
2 3
1 1 1
1 1 2
1 1 3
1
If you want to hack someone,N and Q in your testdata must smaller than 10000,and you shouldn't print any space in each end of the line.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+;
struct node{
int to,next;
}e[M];
int sum[M<<],son[M],fa[M],head[M],siz[M],top[M],dep[M],tid[M],rk[M],a[M];
int cnt1,cnt,n;
void add(int u,int v){
e[++cnt1].to = v;e[cnt1].next = head[u];head[u] = cnt1;
e[++cnt1].to = u;e[cnt1].next = head[v];head[v] = cnt1;
} void dfs1(int u,int faz,int deep){
dep[u] = deep;
fa[u] = faz;
siz[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa[u]){
dfs1(v,u,deep+);
siz[u] += siz[v];
if(son[u] == -||siz[v] > siz[son[u]])
son[u] = v;
}
}
} void dfs2(int u,int t){
top[u] = t;
tid[u] = cnt;
rk[cnt] = u;
cnt++;
if(son[u] == -) return;
dfs2(son[u],t);
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v != son[u]&&v != fa[u])
dfs2(v,v);
}
} void pushup(int rt){
sum[rt] = sum[rt<<]^sum[rt<<|];
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = a[rk[l]];
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int c,int l,int r,int rt){
if(l == r){
sum[rt] = c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
int ret = ;
if(L <= m) ret^=query(L,R,lson);
if(R > m) ret^=query(L,R,rson);
return ret;
} int ask(int x,int y){
int sum = ;
int fx = top[x],fy = top[y];
while(fx != fy){
if(dep[fx] < dep[fy]) swap(x,y),swap(fx,fy);
sum ^= query(tid[fx],tid[x],,n,);
x = fa[fx];fx = top[x];
}
if(dep[x] < dep[y]) swap(x,y);
sum^=query(tid[y],tid[x],,n,);
//cout<<sum<<endl;
return sum;
} void init()
{
memset(son,-,sizeof(son));
for(int i = ; i <= *n;i ++){
e[i].to = ;e[i].next = ;head[i] = ;
}
}
int main()
{
int t,u,v,x,y,op,q;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
cnt = ; cnt1 = ;
init();
for(int i = ; i < n-;i ++){
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i = ;i <= n;i ++)
scanf("%d",&x),a[i]=x+;
dfs1(,,); dfs2(,); build(,n,);
while(q--){
scanf("%d",&op);
if(op==){
scanf("%d%d",&x,&y);
//cout<<ask(x,y)<<endl;
if(ask(x,y)!=) printf("%d\n",ask(x,y)-);
else printf("-1\n");
}
else{
scanf("%d%d",&x,&y);
update(tid[x],y+,,n,);
}
}
}
return ;
}
hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- 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
Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- HDU 2460 Network(双连通+树链剖分+线段树)
HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
随机推荐
- TreeSet排序相关总结
java的集合这一块在工作中用得还比较多,有些东西老是忘,因此在此记录下来. TreeSet原理 1.特点 TreeSet是用来排序的, 可以指定一个顺序, 对象存入之后会按照指定的顺 ...
- Centos 定时任务发送smtp邮件
接着上一篇文章...... 1.首先创建一个sheel的脚本命令,我是在home文件夹下面创建的命令: touch a.sh 2.编辑a.sh脚本 vim a.sh ,键入键盘 i 键 准备插入 ...
- C#Unit单元测试之读取Web.config文件
长期一来,我们所完成的项目都没有写单元测试,今天我一时兴起,决定给自己写的代码写单元测试,简单的测试代码分分钟完成了,一运行测试,就懵逼了.没能达到我的预期效果,而是出现图1所示错误. 图1:单元测试 ...
- maven mvn package 打包项目时,出现错误导致失败的解决方法
解决思路:看报错时在maven打包过程中的哪一步,然后看报错内容,解决报错内容即可,如果是实在不好解决的部分,看看能不能设置不检测,能打包出来就行. 这里是因为mybatis逆向工程插件出现异常所以中 ...
- JavaScript快速入门-ECMAScript语句
JavaScript语句(if.for.for in.do...while.while.break.continue.switch) 一.if语句 if (condition) statement1 ...
- 全面掌握IO(输入/输出流)
File类: 程序中操作文件和目录都可以使用File类来完成即不管是文件还是目录都是使用File类来操作的,File能新建,删除,重命名文件和目录,但File不能访问文件内容本身,如果需要访问文件本身 ...
- Js_图片轮播
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 华为测试大牛Python+Django接口自动化怎么写的?
有人喜欢创造世界,他们做了开发者:有的人喜欢开发者,他们做了测试员.什么是软件测试?软件测试就是一场本该在用户面前发生的灾难提前在自己面前发生了,这会让他们生出一种救世主的感觉,拯救了用户,也就拯救者 ...
- Nginx 配置高可用
阅读本文需要安装Nginx 一 什么是高可用 nginx作为负载均衡服务器 所有请求都到了nginx 可见nginx处于非常重要的位置 如果nginx服务器宕机 后端web服务器将无法提供服务 影响严 ...
- GPT & UEFI Install Windows7
安装介质以FAT或者FAT32分区安装介质添加UEFI支持文件(Windows7及其以前的系统,不支持UEFI启动) 从Windows8的安装文件中提取Bootmgfw.efi文件,重命名为BOOTX ...