题目连接

Problem

There is a tree with n nodes. For each node, there is an integer value ai, (1≤ai​≤1,000,000,000 for 1≤i≤n). There is q queries which are described as follow:

Assume the value on the path from node a to node b is t0​,t1​,⋯tm​. You are supposed to calculate t0 xor tk xor t2k xor ... xor tpk (pk≤m).

Input Format

There are multi datasets. (∑n≤50,000,∑q≤500,000).

For each dataset: In the first n−1 lines, there are two integers u,v, indicates there is an edge connect node uand node v.

In the next nn lines, There is an integer ai​ (1≤ai​≤1,000,000,000).

In the next q lines, There is three integers a,b and k. (1≤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 题意: 有一棵n个节点的树,每个节点上有个权值vi,现在q次询问:节点a到节点b的路径上,从a开始每k个节点跳一次所经过的所有节点的异或值为(a0,ak,a2k,a3k...)? 思路: 建树,倍增算法记录每个节点的深度和2^i的祖先,处理并记录每个节点i到根节点间隔为k(1,2,3……100)的异或值dp[i][k]。当k<=100时,使用记录的异或值dp计算a到b间隔为k的异或值;当k>100时,直接从a走到b,每次跳动使用倍增的信息(快速跳动)。 注:这道题是2017年打西安网络赛时没过的题,当时其实代码写的很接近了,测数据都没问题,一直检查不出来,我也一直惦记着这道题。本科毕业后读研,又看过一次还是没找出原因,今天五一放假,没啥事儿,我又看了一遍当时写的代码,突然发现没初始化fa数组,心想难道是计蒜客网站编译器不是默认未初始化的值为0?加上fa的初始化后提交了一把,过了!!!
My God! 心心念念的这道题竟然是这个原因没过,气呀。不过,今天总算是找到原因了,哈哈~ 又一次想起来西安正式赛的时候,一道铜牌题没过“LOL BP”导致没拿到银牌,可惜的是当时的银牌题都过了,唉~ 与银失之交臂。现在是研究生了,很少刷题了,以后要少看剧,多看看相关的图形学的专业书,充实自己,找个好工作。 代码如下:
 //https://nanti.jisuanke.com/t/A1273 《Xor》
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 5e4 + ;
int fa[N][], deep[N], head[N];
int v[N], cnt;
bool vis[N];
int dp[N][];
struct data
{
int to, next;
}e[ * N]; void insert(int u, int v)
{
e[++cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
e[++cnt].to = u;
e[cnt].next = head[v];
head[v] = cnt;
}
int cal(int x, int t)
{
for (int i = ; i <= ; i++)
if (t&( << i)) x = fa[x][i];
return x;
}
void dfs(int x)
{
vis[x] = ;
for (int i = ; i <= ; i++)
{
if (deep[x]<( << i))break;
fa[x][i] = fa[fa[x][i - ]][i - ];///倍增处理祖先信息
}
for (int k = ; k <= ; k++)
{
dp[x][k] = v[x];
if (deep[x]<k) continue;
int p = cal(x, k);
dp[x][k] ^= dp[p][k];
}
for (int i = head[x]; i; i = e[i].next)
{
if (vis[e[i].to]) continue;
deep[e[i].to] = deep[x] + ;
fa[e[i].to][] = x;
dfs(e[i].to);
}
}
int lca(int x, int y)///求lca
{
if (deep[x]<deep[y]) swap(x, y);
x = cal(x, deep[x] - deep[y]);
for (int i = ; i >= ; i--)
if (fa[x][i] != fa[y][i])
{
x = fa[x][i];
y = fa[y][i];
}
if (x == y)return x;
else return fa[x][];
} void init()
{
cnt = ;
memset(head, , sizeof(head));
memset(vis, , sizeof(vis));
memset(dp, , sizeof(dp));
memset(deep, , sizeof(deep));
memset(fa,,sizeof(fa));
} int main()
{
int n, q;
while (scanf("%d%d", &n, &q) != EOF)
{
init();
for (int i = ; i<n; i++)
{
int x, y; scanf("%d%d", &x, &y);
insert(x, y);
}
for (int i = ; i <= n; i++) scanf("%d", &v[i]);
dfs();
while (q--)
{
int x, y, k; scanf("%d%d%d", &x, &y, &k);
int pa = lca(x, y);
if (k <= )
{
int ans = dp[x][k];
int h = (deep[x] - deep[pa]) % k;
int t = k - h;
if (deep[pa] >= t)
{
int l = cal(pa, t);
ans ^= dp[l][k];
}
int r = k - h;
t = deep[y] - deep[pa] - r;
if (t<) goto endw2;
t %= k;
y = cal(y, t);///
ans ^= dp[y][k];
t = k - r;
if (deep[pa] >= t)
{
int l = cal(pa, t);
ans ^= dp[l][k];
}
endw2:;
printf("%d\n", ans);
}
else
{
int ans = ;
while ()
{
ans ^= v[x];
if (deep[x] - k<deep[pa]) break;
x = cal(x, k);
}
int l = k - (deep[x] - deep[pa]);
int t = deep[y] - deep[pa] - l;
if (t<) goto endw;
t %= k;
y = cal(y, t);
while ()
{
ans ^= v[y];
if (deep[y] - k <= deep[pa]) break;
y = cal(y, k);
}
endw:;
printf("%d\n", ans);
}
}
}
return ;
}
/**
8 11
1 2
2 3
2 4
1 5
5 6
5 7
4 8
3 5 6 2 7 0 1 10
1 8 1
answer=14
*/

2017 ICPC网络赛(西安)--- Xor的更多相关文章

  1. 2017乌鲁木齐网络赛 j 题

    题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...

  2. Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

    Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...

  3. hdu 6152 : Friend-Graph (2017 CCPC网络赛 1003)

    题目链接 裸的结论题.百度 Ramsey定理.刚学过之后以为在哪也不会用到23333333333,没想到今天网络赛居然出了.顺利在题面更改前A掉~~~(我觉得要不是我开机慢+编译慢+中间暂时死机,我还 ...

  4. 【2018ACM/ICPC网络赛】沈阳赛区

    这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K  Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...

  5. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor

    There is a tree with nn nodes. For each node, there is an integer value a_ia​i​​, (1 \le a_i \le 1,0 ...

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

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

  7. 2017 ICPC区域赛(西安站)--- J题 LOL(DP)

    题目链接 problem description 5 friends play LOL together . Every one should BAN one character and PICK o ...

  8. 2017 ACM区域赛(西安) 参赛流水账

    day 0: 周五, 鸽了概统课,早上和紫金港的几位小伙伴一起打车去萧山机场,从咸阳机场到西北工业大学坐了五十多个站的公交车,感觉身体被掏空.晚上在宾馆本来打算补之前训练的一个题,想想还是先花个十来分 ...

  9. Trace 2018徐州icpc网络赛 (二分)(树状数组)

    Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...

随机推荐

  1. 树型DP(2)

    声明 https://blog.csdn.net/no1_terminator/article/details/77824790 参考课件和讲授来自Accelerator 找树的直径 树的直径定义为一 ...

  2. 鱼嘤嘤小分队 Alpha冲刺阶段博客目录

    会议记录 周数 链接 主要工作 第六周 第六周链接  确定技术路线 第七周 第七周链接  讨论进展 最近的主要工作: 由于我们的代码能力以及pyhon的基础知识已经网络通信的知识储备是不够的,所以我们 ...

  3. CF812C Sagheer and Nubian Market

    CF812C Sagheer and Nubian Market 洛谷评测传送门 题目描述 On his trip to Luxor and Aswan, Sagheer went to a Nubi ...

  4. Luogu P4585 [FJOI2015]火星商店问题

    颓文化课作业到很晚写篇博客清醒一下 首先我们仔细阅读并猜测了题意之后,就会想到一个暴力的线段树套可持久化0/1Trie的做法,但是它显然是过不去的 由于最近再做线段树分治的题,我们可以想到用线段树分治 ...

  5. 第04组 Alpha冲刺(1/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.安排好各个组员的任务 2.收集各个组员的进度 3.写页面 4.写博客 展示Gi ...

  6. Nginx+Tomcat+Memcache 实现session共享

    Nginx + Tomcat + Memcache 实现session共享 1. Nginx 部署 1.上传源码包到服务器,解压安装 下载地址:http://nginx.org/en/download ...

  7. npm install说明

    一.常用简写 npm install=npm i.在git clone项目的时候,项目文件中并没有 node_modules文件夹,项目的依赖文件可能很大.直接执行,npm会根据package.jso ...

  8. git Filename too long

    # 全局 git config --global core.longpaths true # 当前仓库 git config core.longpaths true

  9. mysql批量更新数据(性能优化) 第一种方式

    首先想到的是,一条一条更新的速度太慢了,然后就想批量更新,一次更新N条数据.实践是检验真理的唯一标准,不一会儿,代码就敲完了,重新试了一下,效果依旧不理想.啊哦,真是要崩溃!后面又想到了利用异步,我一 ...

  10. Kafka学习笔记之Kafka High Availability(上)

    0x00 摘要 Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则宕机期间其上所有Partition都无法继续提供服务.若该Broker永 ...