题目连接

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. easyui三

    陈旧的开发模式 美工(ui工程师:出一个项目模型) java工程师:将原有的html转成jsp,动态展示数据 缺点: 客户需要调节前端的展示效果 解决:由美工去重新排版,重新选色.Vs前后端分离 美工 ...

  2. 爬虫 crawlSpider 分布式 增量式 提高效率

    crawlSpider 作用:为了方便提取页面整个链接url,不必使用创参寻找url,通过拉链提取器,将start_urls的全部符合规则的URL地址全部取出 使用:创建文件scrapy startp ...

  3. JWT签名算法中HS256和RS256有什么区别 转载

    JWT签名算法中,一般有两个选择,一个采用HS256,另外一个就是采用RS256. 签名实际上是一个加密的过程,生成一段标识(也是JWT的一部分)作为接收方验证信息是否被篡改的依据. RS256 (采 ...

  4. day2_窗口句柄切换

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/7/16 14:21 # @Author : 大坏男孩 # @File : d ...

  5. imutils.path

    from imutils import paths # 要在哪条路径下查找 path = '...' # 查找图片,得到图片路径 imagePaths = list(imutils.paths.lis ...

  6. C getchar()

    C getchar() #include <stdio.h> int main() { ; char str[size]; ; char ch; printf("Enter wh ...

  7. 修改kile工程名字(转)

    假设原来的工程文件名是first,要改成second1.在工程文件目录中,将first.uvopt和first.uvproj名字改成second.uvopt和second.uvproj.2.其他fir ...

  8. Python基础-day02-3

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 -- 从上向下,顺序执行 ...

  9. linux 链接mysql并覆盖数据

    --链接mysql 命令格式: mysql -h 主机地址 -u 用户名 -p --按回车输入密码 --在服务器里登录到数据库里 ,使用某个库,gtmc 代表数据库名称 use gtmc --覆盖数据 ...

  10. LuoguP5290 [十二省联考2019]春节十二响 | 启发式合并

    还有33天就要高考了,我在干啥-- 题目概述 一棵有根树,每个节点有权值. 要求把所有节点分成组,具有祖先-后代关系的两个节点不能被分到同一组. 每一组的代价是所包含的节点的最大权值,最小化所有组的代 ...