Dylans loves tree

Problem Description
Dylans is given a tree with N nodes.

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

 
Input
In the first line there is a test number T.
(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).

 
Output
For each question ② in each testcase,if the value all appear even times output "-1",otherwise output the value that appears odd times.
 
Sample Input
1
3 2
1 2
2 3
1 1 1
1 1 2
1 1 3
 
Sample Output
-1
1

Hint

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.

 

BC题目链接:点这里!

题解:

  

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e3+, mod = 1e9+, inf = 2e9; int dep[N],head[N],t=,sz[N],fa[N],indexS,top[N],pos[N];
struct ss{int to,next;}e[N*];
int n;
void add(int u,int v)
{e[t].to = v;e[t].next = head[u];head[u] = t++;}
void dfs(int u) {
sz[u] = ;
dep[u] = dep[fa[u]] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa[u]) continue;
fa[to] = u;
dfs(to);
sz[u] += sz[to];
}
}
void dfs(int u,int chain) {
int k = ;
pos[u] = ++indexS;
top[u] = chain;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(dep[to] > dep[u] && sz[to] > sz[k])
k = to;
}
if(k == ) return ;
dfs(k,chain);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(dep[to] > dep[u] && k != to)
dfs(to,to);
}
} int sum[N],fposs[N];
void push_up(int i,int ll,int rr) {
sum[i] = sum[ls] ^ sum[rs];
}
void build(int i,int ll,int rr) {
if(ll == rr) {
sum[i] = fposs[ll];
return ;
}
build(ls,ll,mid),build(rs,mid+,rr);
push_up(i,ll,rr);
}
void update(int i,int ll,int rr,int x,int c) {
if(ll == rr) {
sum[i] = c;
return ;
}
if(x <= mid) update(ls,ll,mid,x,c);
else update(rs,mid+,rr,x,c);
push_up(i,ll,rr);
}
int query(int i,int ll,int rr,int x,int y) {
if(ll == x && rr == y) {
return sum[i];
}
if(y <= mid) return query(ls,ll,mid,x,y);
else if(x > mid) return query(rs,mid+,rr,x,y);
else return query(ls,ll,mid,x,mid)^query(rs,mid+,rr,mid+,y);
}
int query(int x,int y) {
int res = ;
while(top[x] != top[y]) {
if(dep[top[x]] > dep[top[y]]) {
res ^= query(,,indexS,pos[top[x]],pos[x]);
x = fa[top[x]];
}
else {
res ^= query(,,indexS,pos[top[y]],pos[y]);
y = fa[top[y]];
}
}
if(dep[x] < dep[y])res ^= query(,,n,pos[x],pos[y]);
else res ^= query(,,n,pos[y],pos[x]);
return res;
}
int T,Q,a[N];
void init() {
t = ;
memset(fa,,sizeof(fa));
memset(head,,sizeof(head));
memset(dep,,sizeof(dep));
memset(sz,,sizeof(sz));
memset(top,,sizeof(top));
memset(pos,,sizeof(pos));
indexS = ;
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&Q);
init();
for(int i = ; i < n; ++i) {
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
dfs();
dfs(,);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) fposs[pos[i]] = a[i]+;
build(,,indexS);
while(Q--) {
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op == ) {
update(,,indexS,pos[x],y+);
} else {
printf("%d\n",query(x,y)-);
}
}
}
return ;
}

HDU 5274 Dylans loves tree 树链剖分+线段树的更多相关文章

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

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

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

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

  3. hdu 5274 Dylans loves tree

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

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

  5. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  6. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  7. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  8. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  9. 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, ...

  10. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

随机推荐

  1. 【Mysql数据库】知识点总结

    本文转载自:http://www.cnblogs.com/tonghun/p/7191131.html 一 数据库常用操作 mysql -u+username -p+password:登陆数据库管理系 ...

  2. shiro配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. 【实验吧】Just Click

    拿到答案需要正确地点击按钮 格式:simCTF{ } 解题链接: http://ctf5.shiyanbar.com/re/rev4.exe 由于最近在学数据库是c#编程,发现是c#,于是用.net ...

  4. python004 Python3 解释器

    Python3 解释器Linux/Unix的系统上,一般默认的 python 版本为 2.x,我们可以将 python3.x 安装在 /usr/local/python3 目录中.安装完成后,我们可以 ...

  5. FZU-2147-2147 A-B Game,规律题。。

    Problem 2147 A-B Game Time Limit: 1000 mSec Memory Limit : 32768 KB  Problem Description Fat brother ...

  6. 一篇文章告诉你,TLS 1.3 如何用性能为 HTTPS 正名

      序•魔戒再现   几天前,OpenSSL 官方宣布即将发布的新版本 (OpenSSL 1.1.1) 将会提供 TLS 1.3 的支持,而且还会和之前的 1.1.0 版本完全兼容,这当然是个好消息. ...

  7. bzoj 1500 [NOI 2005] 维修数列

    题目大意不多说了 貌似每个苦逼的acmer都要做一下这个splay树的模版题目吧 还是有很多操作的,估计够以后当模版了.... #include <cstdio> #include < ...

  8. 1027 stl

    #include<stdio.h> #include<queue> using namespace std; int main() {  int i,n,m,j,k,a[100 ...

  9. less的配置和使用

    less 的配置: 1.浏览器端的使用 <link rel="stylesheet/less" type="text/css" href="st ...

  10. APP后端处理表情的一些技巧

    app应用中文字夹带表情是个很常见的现象.甚至一些40多岁的大叔级用户,也喜欢在自己的昵称中夹带表情,在产品运营后发现这个现象,彻底颠覆了我的世界观. 在后台处理表情的时间,我遇到过下面3个问题: 1 ...