There is a tree with nn nodes. For each node, there is an integer value a_iai, (1 \le a_i \le 1,000,000,0001≤ai≤1,000,000,000 for 1 \le i \le n1≤i≤n). There is qq queries which are described as follow: Assume the value on the path from node aa to node bb is t_0, t_1, \cdots t_mt0,t1,⋯tm. You are supposed to calculate t_0t0 xor t_ktk xor t_{2k}t2k xor ... xor t_{pk}tpk (pk \le m)(pk≤m).
Input Format
There are multi datasets. (\sum n \le 50,000, \sum q \le 500,000)(∑n≤50,000,∑q≤500,000).
For each dataset: In the first n-1n−1 lines, there are two integers u,vu,v, indicates there is an edge connect node uuand node vv.
In the next nn lines, There is an integer a_iai (1 \le a_i \le 1,000,000,0001≤ai≤1,000,000,000).
In the next qq lines, There is three integers a,ba,b and kk. (1 \le a,b,k \le n1≤a,b,k≤n).
Output Format
For each query, output an integer in one line, without any additional space.
样例输入复制
5 6 1 5 4 1 2 1 3 2 19 26 0 8 17 5 5 1 1 3 2 3 2 1 5 4 2 3 4 4 1 4 5
样例输出复制
17 19 26 25 0 19
题目来源
2017 ACM-ICPC 亚洲区(西安赛区)网络赛
题意:
给出一棵树,每个节点有个val,q个询问。
询问格式:u, v, k, 从u到v的路径, 每个k个点取一个,求所有点的val值异或和。
分析:
BZOJ修改版本,具体解析:BZOJ4381: [POI2015]Odwiedziny 分块+树链剖分
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
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;
}
typedef unsigned long long ULL;
const ULL INF=(ULL)0-(ULL)1;
const int N = 1e5+5;
int a[N],b[N],c[N];
int up[N][235],block;//up[x,y]表示从点x开始每次跳y步直到不能跳为止的权值和
int n;
int w[N];
int head[N], cnt;
int num;
int root[N];//保存结点 u 的父亲节点
int depth[N]; //保存结点 u 的深度值
int W_son[N];//保存重儿子
int size[N]; //保存以 u 为根的子树节点个数
int top[N];//保存当前节点所在链的顶端节点
int id[N];//保存树中每个节点剖分以后的新编号( DFS 的执行顺序)
int new_W[N]; //保存当前 dfs 标号在树中所对应的节点
int dfn[N];// 保存dfs序,dfn[i]表示dfs序号为i的原数组下标
struct Eddge
{
int nex, to;
} edge[N<<1];//这里卡了好久,一定要乘以2
void addEddge(int u, int v)
{
edge[++cnt].to=v;
edge[cnt].nex=head[u];
head[u] = cnt;
}
void dfs1(int u, int fa, int deep)
{
depth[u] = deep;
root[u] = fa;
size[u] = 1;
int v=u;
for (int i=1; i<=block; i++)
v=root[v],up[u][i]=a[u]^up[v][i];
int maxSon = -1;
for(int i=head[u]; i!=-1; i=edge[i].nex)
{
int v = edge[i].to;
if(v == fa)
continue;
dfs1(v, u, deep+1);
size[u] += size[v];
if(size[v] > maxSon)
{
maxSon = size[v];
W_son[u] = v;
}
}
}
void dfs2(int u, int topf)
{
id[u] = ++num;
dfn[num]=u;
new_W[num] = a[u];
top[u] = topf;
if(!W_son[u])
return;
dfs2(W_son[u], topf);
for(int i=head[u]; i!=-1; i=edge[i].nex)
{
int v = edge[i].to;
if(v == W_son[u] || v == root[u])
continue;
dfs2(v, v);
}
}
int get_lca(int x,int y)
{
while (top[x]!=top[y])
{
if (depth[top[x]]<depth[top[y]])
swap(x,y);
x=root[top[x]];
}
return depth[x]<depth[y]?x:y;
}
//获取x的k级祖先
int get_kth(int x,int k)
{
while (x&&depth[x]-depth[top[x]]<k)
k-=depth[x]-depth[top[x]]+1,x=root[top[x]];
return !x?0:dfn[id[x]-k];
}
//返回x到y,步长为z的异或和
int get_sum(int x,int y,int z)
{
int lca=get_lca(x,y);
if (z<=block)
{
int ans=up[x][z]^up[get_kth(lca,z-(depth[x]-depth[lca])%z)][z];
int tmp=get_kth(y,(depth[x]+depth[y]-depth[lca]*2)%z);
if (depth[tmp]>=depth[lca])
ans^=up[tmp][z]^up[get_kth(lca,z-(depth[tmp]-depth[lca])%z)][z];
/*if ((depth[x]+depth[y]-depth[lca]*2)%z>0)///x到y的步数无法整除z,敲好不能到终点y,
ans^=a[y]; */ ///终点y的值计算在内,则消除注释
if ((depth[x]-depth[lca])%z==0)
ans^=a[lca];
return ans;
}
else
{
int ans=0,tmp=x;
while (depth[tmp]>=depth[lca])
ans^=a[tmp],tmp=get_kth(tmp,z);
tmp=get_kth(y,(depth[x]+depth[y]-depth[lca]*2)%z);
while (depth[tmp]>=depth[lca])
ans^=a[tmp],tmp=get_kth(tmp,z);
/*if ((depth[x]+depth[y]-depth[lca]*2)%z>0)///x到y的步数无法整除z,敲好不能到终点y,
ans^=a[y]; */ ///终点y的值计算在内,则消除注释
if ((depth[x]-depth[lca])%z==0)
ans^=a[lca];
return ans;
}
}
void init()
{
cnt = num = 0;
memset(head, -1, sizeof(head));
memset(W_son, 0, sizeof(W_son));
}
int main()
{
while(scanf("%d",&n)!=-1)
{
init();
int m;
m=read();
for (int i=1; i<n; i++)
{
int x=read(),y=read();
addEddge(x,y);
addEddge(y,x);
}
block=sqrt(n+0.5);
for (int i=1; i<=n; i++)
a[i]=read();
dfs1(1,0,1);
dfs2(1,1);
while(m--)
{
int x,y,z;
x=read();
y=read();
z=read();
printf("%d\n",get_sum(x,y,z));
}
}
return 0;
}