HDU 5274 Dylans loves tree 树链剖分+线段树
Dylans loves tree
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.
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 树链剖分+线段树的更多相关文章
- 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:把 ...
随机推荐
- hdu1394(Minimum Inversion Number)线段树
明知道是线段树,却写不出来,搞了半天,戳,没办法,最后还是得去看题解(有待于提高啊啊),想做道题还是难啊. 还是先贴题吧 HDU-1394 Minimum Inversion Number Time ...
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- HDU 1166 排兵布阵(线段树单点更新)
题意: 给定n个兵营的士兵初始值, 然后有最多40000个操作: 操作一共有两种, 一个是查询给定[a,b]区间兵营的士兵总和. 另一个是增加/减少指定兵营的士兵数目. 输出每次查询的值. 分析: 线 ...
- CI 安装时目录的安全处理
如果你想通过隐藏 CodeIgniter 的文件位置来增加安全性,你可以将 system 和 application 目录修改为其他的名字,然后打开主目录下的 index.php 文件将 $syste ...
- xtu read problem training 3 A - The Child and Homework
The Child and Homework Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Code ...
- POJ 2112: Optimal Milking【二分,网络流】
题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机 ...
- spoj 839 最小割+二进制
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...
- msp430入门编程41
msp430中C语言的软件工程--状态机建模
- 牛客网 牛客网暑期ACM多校训练营(第三场)E KMP
链接:https://www.nowcoder.com/acm/contest/141/E 题目描述 Eddy likes to play with string which is a sequenc ...
- zookeeper一二三
1.zookeeper介绍 ZooKeeper 是一个开源的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现.分布式应用程序可以基于 ZooKeeper 实现诸如数据发布/订阅. ...