2017 ICPC网络赛(西安)--- Xor
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的更多相关文章
- 2017乌鲁木齐网络赛 j 题
题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...
- 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 ...
- hdu 6152 : Friend-Graph (2017 CCPC网络赛 1003)
题目链接 裸的结论题.百度 Ramsey定理.刚学过之后以为在哪也不会用到23333333333,没想到今天网络赛居然出了.顺利在题面更改前A掉~~~(我觉得要不是我开机慢+编译慢+中间暂时死机,我还 ...
- 【2018ACM/ICPC网络赛】沈阳赛区
这次网络赛没有打.生病了去医院了..尴尬.晚上回来才看了题补简单题. K Supreme Number 题目链接:https://nanti.jisuanke.com/t/31452 题意:输入一个 ...
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor
There is a tree with nn nodes. For each node, there is an integer value a_iai, (1 \le a_i \le 1,0 ...
- 【分块】计蒜客17120 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor
题意:给一棵树,每个点有权值.q次询问a,b,k,问你从a点到b点,每次跳距离k,权值的异或和? 预处理每个点往其根节点的路径上隔1~sqrt(n)的距离的异或和,然后把询问拆成a->lca(a ...
- 2017 ICPC区域赛(西安站)--- J题 LOL(DP)
题目链接 problem description 5 friends play LOL together . Every one should BAN one character and PICK o ...
- 2017 ACM区域赛(西安) 参赛流水账
day 0: 周五, 鸽了概统课,早上和紫金港的几位小伙伴一起打车去萧山机场,从咸阳机场到西北工业大学坐了五十多个站的公交车,感觉身体被掏空.晚上在宾馆本来打算补之前训练的一个题,想想还是先花个十来分 ...
- Trace 2018徐州icpc网络赛 (二分)(树状数组)
Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...
随机推荐
- 01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类
1==>控制字体图标的大小用 font-size:16px; <i class="el-icon-arrow-left right-show-aside-icon"&g ...
- 深浅拷贝及 join set
1.join s="**".join (['风清扬',"独孤求败"] ) join 把里面的东西拿出来,进行拼接 s="_". ...
- 并发编程学习笔记(七、Thread源码分析)
目录: 常见属性 构造函数 start() run() 常见属性: /** * 线程名称 */ private volatile String name; /** * 线程优先级 */ private ...
- 第十二周Scrum会议
本次照片 总结上周所达成的工作 做到的工作 1. 将前端页面进行了比较美观的美化 2. 实现了后台的代码的整合,同时将flask项目的整体框架搭建完成 3. 进行了数据库的建表等一些工作 遇到的难点 ...
- 对CNN 的理解
CNN 的强大之处在于它的多层结构能自动学习特征,并且可以学习到多个层次的特征:较浅的卷积层感知域较小,学习到一些局部区域的特征. 较深的卷积层具有较大的感知域,能够学习到更加抽象一些的特征.这些抽象 ...
- 洛谷 P1381 单词背诵
洛谷 P1381 单词背诵 洛谷传送门 题目描述 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的 ...
- html行级元素与块级元素以及meta标签的使用
块级元素的特性: 永远都会占满父级元素的宽度(块级元素的宽度永远都等于它父级元素的宽度) 行级元素的特性: 所占的空间刚好等于内容的大小 常见的块级元素: h1~h6.p.ul.div.li.form ...
- Asp.Net Core 工作单元 UnitOfWork UOW
Asp.Net Core 工作单元示例 来自 ABP UOW 去除所有无用特性 代码下载 : 去除所有无用特性版本,原生AspNetCore实现 差不多 2278 行代码: 链接:https://pa ...
- VBA基础 - 数据类型
概要 学习一种新语言, 数据类型和关键字是第一步. 数据类型 常用的数据类型如下: 类型 存储空间 范围 Boolean 2 bytes True 或者 False Byte 1 byte 0 ~ 2 ...
- Springboot概述
目录 什么是springboot Springboot的优点 SpringBoot的缺点 一:什么是springboot Springboot是Spring开源组织下的子项目,是Spring组件一站式 ...