There is a tree with nn nodes. For each node, there is an integer value a_ia​i​​, (1 \le a_i \le 1,000,000,0001≤a​i​​≤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_mt​0​​,t​1​​,⋯t​m​​. You are supposed to calculate t_0t​0​​ xor t_kt​k​​ xor t_{2k}t​2k​​ xor ... xor t_{pk}t​pk​​ (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 uu and node vv.

In the next nn lines, There is an integer a_ia​i​​ (1 \le a_i \le 1,000,000,0001≤a​i​​≤1,000,000,000).

In the next qq lines, There is three integers a,ba,band 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
分析:求树上路径从距离起点为k的倍数的权值和;
   大于根号n暴力,小于的话预处理,处理到根的前缀异或和;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=5e4+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=f*p%mo;p=p*p%mo;q>>=;}return f;}
int n,m,k,t,dp[maxn][],fa[][maxn],dep[maxn],a[maxn],sz,q,head[maxn],tot;
struct node
{
int to,nxt;
}e[maxn<<];
void add(int x,int y)
{
e[tot].to=y;
e[tot].nxt=head[x];
head[x]=tot++;
}
int lca(int x,int y)
{
int i;
if(dep[x]<dep[y])swap(x,y);
for(i=;i>=;i--)if(dep[fa[i][x]]>=dep[y])x=fa[i][x];
if(x==y)return x;
for(i=;i>=;i--)
{
if(fa[i][x]!=fa[i][y])
{
x=fa[i][x],
y=fa[i][y];
}
}
return fa[][x];
}
int find(int x,int y)
{
int i;
for(i=;i>=;i--)
{
if(y>>i&)
{
x=fa[i][x];
if(x==)return ;
}
}
return x;
}
void dfs(int x,int y)
{
int i;
dep[x]=dep[y]+;
for(i=;fa[i-][fa[i-][x]];i++)
{
fa[i][x]=fa[i-][fa[i-][x]];
}
rep(i,,sz)
{
dp[x][i]=a[x];
dp[x][i]^=dp[find(x,i)][i];
}
for(i=head[x];i!=-;i=e[i].nxt)
{
int z=e[i].to;
if(z==y)continue;
fa[][z]=x;
dfs(z,x);
}
}
int main(){
int i,j;
while(~scanf("%d%d",&n,&q))
{
sz=round(sqrt(n));
rep(i,,n)
{
head[i]=-;
rep(j,,)fa[j][i]=;
}
tot=;
rep(i,,n-)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
rep(i,,n)scanf("%d",&a[i]);
dfs(,);
while(q--)
{
int x,y,k;
int ret=;
scanf("%d%d%d",&x,&y,&k);
if(x==y)
{
printf("%d\n",a[x]);
continue;
}
int fa=lca(x,y),len=dep[x]+dep[y]-*dep[fa],pos;
if((dep[x]-dep[fa])%k==&&fa!=x&&fa!=y)ret^=a[fa];
if(k>sz)
{
if(fa!=x)
{
pos=x;
ret^=a[pos];
while(dep[j=find(pos,k)]>=dep[fa])
{
pos=j;
ret^=a[pos];
}
}
if(fa!=y)
{
pos=find(y,len%k);
if(dep[pos]>=dep[fa])
{
ret^=a[pos];
while(dep[j=find(pos,k)]>=dep[fa])
{
pos=j;
ret^=a[pos];
}
}
}
}
else
{
int len1=dep[x]-dep[fa];
if(fa!=x)
{
len1=len1/k*k;
pos=find(x,len1);
ret=(ret^dp[x][k]^dp[pos][k]^a[pos]);
}
if(fa!=y)
{
int st=find(y,len%k);
if(dep[st]>=dep[fa])
{
len1=dep[st]-dep[fa];
len1=len1/k*k;
pos=find(st,len1);
ret=(ret^dp[st][k]^dp[pos][k]^a[pos]);
}
}
}
printf("%d\n",ret);
}
}
return ;
}
/*
6 1
3 2
1 4
4 6
6 2
1 5
60
2
75
34
60
15
4 6 1
ans:45
*/

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor的更多相关文章

  1. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  2. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

  3. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  4. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  5. 【分块】计蒜客17120 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor

    题意:给一棵树,每个点有权值.q次询问a,b,k,问你从a点到b点,每次跳距离k,权值的异或和? 预处理每个点往其根节点的路径上隔1~sqrt(n)的距离的异或和,然后把询问拆成a->lca(a ...

  6. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  7. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

  8. 2017 ACM/ICPC(西安)赛后总结

    早上8:00的高铁,所以不得不6点前起床,向火车站赶……到达西安后已经是中午,西工大距离西安北站大概3小时车程的距离,只好先解决午饭再赶路了……下午3.30的热身赛,一行人在3.35左右赶到了赛场,坐 ...

  9. 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit    1000 ms Memory li ...

随机推荐

  1. .NET平台下Redis使用(二)【StackExchange.Redis学习】

    Program.cs内容: using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Data; usi ...

  2. 为何Google这类巨头会认为敏捷开发原则是废话?

    [编者按]这是一个来自Quora的问题.Rocket程序员Jasmine Adamson在文中表达了敏捷开发原则是废话的观点,他觉得现实生活中没有什么人会推崇这些原则来工作,不过他们仍然在说其所做的是 ...

  3. 【POJ 3349】 Snowflake Snow Snowflakes

    [题目链接] http://poj.org/problem?id=3349 [算法] 哈希 若两片雪花相同,则它们六个角上的和一定相同,不妨令 H(A) = sigma(Ai) % P ,每次只要到哈 ...

  4. stm32I/O的8种工作模式

    转载自原子的论坛: 浮空,顾名思义就是浮在空中,上面用绳子一拉就上去了,下面用绳子一拉就沉下去了. 开漏,就等于输出口接了个NPN三极管,并且只接了e,b. c极 是开路的,你可以接一个电阻到3.3V ...

  5. target属性打开方式

    在HTML中target目标的四个参数的用法:1.target="_self"表示:将链接的画面内容,显示在目前的视窗中.(内定值) . 即:同(自己)窗口打开,别的数据还存在,相 ...

  6. HDU 5306 吉司机线段树

    思路: 后面nlogn的部分是伪证... 大家可以构造数据证明是这是nlog^2n的啊~ 吉老司机翻车了 //By SiriusRen #include <cstdio> #include ...

  7. Flume NG基本架构与Flume NG核心概念

    导读 Flume NG是一个分布式.可靠.可用的系统,它能够将不同数据源的海量日志数据进行高效收集.聚合.移动,最后存储到一个中心化数据存储系统中. 由原来的Flume OG到现在的Flume NG, ...

  8. Microsoft SQL Server 2008/2012 Internals 一处疑问

    Kalen Delaney 等著的深入解析 Microsoft SQL Server 系列,享有盛誉,深入研读,是管窥深奥复杂之 SQL Server 的阶梯与门径.手头有 Microsoft SQL ...

  9. Android ScrollView里嵌套RecyclerView时,在RecyclerView上滑动时出现卡顿(冲突)的现象

    最近在项目中遇到一个现象,一个界面有一个RecyclerView(GridView型的),外面套了一层ScrollView,通过ScrollView上下滚动,但是在滑动的时候如果是在RecyclerV ...

  10. 第一次android混淆实战

    第一次混淆,主要是因为引用本地第三方jar包的问题.虽然说本地第三方jar包自动避免混淆,但一些本地第三方jar包下的一些包要避免混淆.比如: 文中的com.org 这些包名都要避免混淆. 下面是我用 ...