Dylans loves tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 747    Accepted Submission(s): 144

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.
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5275 5272 5271 5270 5268 

先考虑无改动的情况,令Xor[i]表示i到根节点路径上的异或和。则随意节点的(u,v)的异或和能够转化为Xor[u]^Xor[v]^a[LCA(u,v)].考虑改动的情况。改动节点u,仅仅会以u为根的子树的Xor值产生影响,由于一颗子树的dfs序是连续的我们非常自然的想到用线段树去维护他,pSeg[u]表示u在dfs序中的位置,siz[u]表示以u为根的子树大小,则这课颗子树相应的区间就是[pSeg[u],pSeg[u]+siz[u]-1],改动的时候仅仅须要将这段区间先异或上原来的值a[u],在异或上要变成的值y,然后改动a[u]
= y;两次异或能够一步到位。直接异或上a[u]^y即可。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 10;
#define to first
#define next second
#define foreach(it,v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
int pos[maxn],d[20][maxn<<1],wid[maxn<<1],head[maxn];
int a[maxn],depth[maxn],sid,pSeg[maxn],siz[maxn],Xor[maxn];
typedef pair<int,int> Edge;
Edge edges[maxn<<1];
int tot = 0,e = 0;
void AddEdge(int u,int v)
{
edges[++e] = make_pair(v,head[u]);head[u] = e;
edges[++e] = make_pair(u,head[v]);head[v] = e;
}
void pre(int u,int fa,int dep = 0,int Xo = 0)
{
Xo ^= a[u];
Xor[++sid] = Xo;
pSeg[u] = sid;
siz[u] = 1;
d[0][++tot] = u;
if(!pos[u]) {
pos[u] = tot;
depth[u] = dep;
}
for(int i = head[u]; i ; i = edges[i].next) {
int v = edges[i].to;
if(v == fa) continue;
pre(v,u,dep+1,Xo);
siz[u] += siz[v];
d[0][++tot] = u;
}
}
void RMQ_init(int n)
{
for(int i = 1,w = 1; i <= n; i++) {
if((1<<w)<=i) w++;
wid[i] = w - 1;
}
for(int i = 1; (1<<i) <= n; i++) {
for(int j = 1; j + (1<<i) - 1 <= n; j++) {
d[i][j] = depth[d[i-1][j]] < depth[d[i-1][j+(1<<(i-1))]] ? d[i-1][j] : d[i-1][j+(1<<(i-1))];
}
}
}
int LCA(int u,int v)
{
u = pos[u];
v = pos[v];
if(u > v) swap(u,v);
int k = wid[v-u+1];
return depth[d[k][u]] < depth[d[k][v-(1<<k)+1]] ? d[k][u] : d[k][v-(1<<k)+1];
}
int seg[maxn<<2];
int ql,qr,x;
void push_down(int o)
{
seg[o<<1] ^= seg[o];
seg[o<<1|1] ^= seg[o];
seg[o] = 0;
}
void Modify(int o,int L,int R)
{
if(ql<=L&&qr>=R) {
seg[o] ^= x;
return ;
}
push_down(o);
int mid = (L+R)>>1;
if(ql<=mid) Modify(o<<1,L,mid);
if(qr>mid) Modify(o<<1|1,mid+1,R);
}
int Query(int o,int L,int R)
{
if(L == R) {
return Xor[L] ^ seg[o];
}
int mid = (L+R) >>1;
push_down(o);
if(x<=mid)return Query(o<<1,L,mid);
return Query(o<<1|1,mid+1,R);
}
int main(int argc, char const *argv[])
{
int T;scanf("%d",&T);
while(T--) {
int N,Q;scanf("%d%d",&N,&Q);
e = sid = tot = 0;
memset(head,0,sizeof(head[0])*(N+1));
for(int i = 1; i < N; i++) {
int u,v;scanf("%d%d",&u,&v);
AddEdge(u,v);
}
for(int i = 1; i <= N; i++) {
scanf("%d",a+i);
++a[i];
}
pre(1,-1);
RMQ_init(tot);
memset(seg,0,sizeof(seg[0])*(2*N+10));
while(Q--) {
scanf("%d%d%d",&x,&ql,&qr);
if(x==0) {
qr++;
int L = pSeg[ql], R = pSeg[ql] + siz[ql] - 1;
x = a[ql] ^ qr;
a[ql] = qr;
ql = L,qr = R;
Modify(1,1,N);
}else {
x = pSeg[ql];
int ans = Query(1,1,N);
x = pSeg[qr];
ans ^= Query(1,1,N);
ans ^= a[LCA(ql,qr)];
if(ans==0)puts("-1");
else printf("%d\n", ans-1);
}
}
}
return 0;
}

HDU 5274(LCA + 线段树)的更多相关文章

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

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

  2. hdu 5266 pog loves szh III(lca + 线段树)

    I - pog loves szh III Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I ...

  3. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  4. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. BZOJ 2243: [SDOI2011]染色 树链剖分 倍增lca 线段树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  7. [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)

    [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...

  8. 2018多校第十场 HDU 6430 (线段树合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6430 题意:一棵树上每个节点权值为v[i],每个节点的heard值是:以它为LCA的两个节点的GCD的 ...

  9. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

随机推荐

  1. 【codeforces 234F】Fence

    [题目链接]:http://codeforces.com/problemset/problem/234/F [题意] 你有n块板要凃油漆; 然后每块板有高度h[i];(宽度都为1) 然后每块板只能凃同 ...

  2. Git学习总结(9)——如何构建你自己的 Git 服务器

    现在我们将开始学习如何构建一个Git服务器,如何在具体的事件中写一个针对特定的触发操作的自定义Git(例如通告),如何发布你的代码到一个网站. 目前为止,用户对Git的焦点主要在Git的使用上.这篇文 ...

  3. linux内核(二)内核移植(DM365-DM368开发攻略——linux-2.6.32的移植)

    一.介绍linux-2.6.32: Linux-2.6.32的网上介绍:增添了虚拟化内存 de-duplicacion.重写了 writeback 代码.改进了 Btrfs 文件系统.添加了 ATI ...

  4. [Javascript] Delegate JavaScript (ES6) generator iteration control

    We can execute generators from generators, and delegate the iteration control with the yield* keywor ...

  5. BZOJ 1264: [AHOI2006]基因匹配Match 树状数组+DP

    1264: [AHOI2006]基因匹配Match Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而成(地球 ...

  6. java监听多个组件

    import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.*; import javax.swing.*; pu ...

  7. Hbase项目(完整版)

    涉及概念梳理:命名空间 4.1.1.命名空间的结构 1) Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果没有指定,则在default默认的命名空间中. 2) RegionSe ...

  8. Java基础——protected访问修饰符探讨

    Java基础——protected访问修饰符探讨 根据官方说法:(如图) protected修饰符是可以修饰其他包中的子孙类的,但是我做了个实验,结果发现了一个有趣的现象! 具体请往下看: packa ...

  9. SQL SERVER 将一个数据库中的表和数据复制到另一个数据库中

    第一种情况:将A数据库.dbo.A表的数据追加到B数据库.dbo.B表中 (条件:此时B数据库中已创建好了B表) insert into B数据库.dbo.B表 select * from A数据库. ...

  10. POJ 1064 Cable master (二分答案,G++不过,C++就过了)

    题目: 这题有点坑,G++过不了,C++能过. 条件:n个数据a[],分成k段,结果精度要求两位小数. 问题:每段最长为多少? 思路:因为精度要求为两位小数,我先把所有的长度a[]*100. 我们对答 ...