2017 ACM-ICPC 亚洲区(西安赛区)网络赛 xor (根号分治)
xor
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
题目来源
【题意】给您一棵树,每个节点有一个权值,q次询问,每次给出u,v,k,求从u到v的路径中,从u开始,每隔k个节点亦或一下的结果。
【分析】根号分治,大于根号n的暴力跳(倍增跳),小于根号n的用数组存起来,复杂度最高为O(N)*sqrt(N)*log(N).
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 5e4+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,q,sz;
int a[N],fa[N][],up[N][M+],dep[N];
vector<int>edg[N],vec;
int find(int u,int k){
for(int i=;i>=;i--){
if(k>>i&){
u=fa[u][i];
if(u==)return ;
}
}
return u;
}
void dfs(int u,int f){
fa[u][]=f;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int i=;i<=sz;i++){
up[u][i]=a[u];
int v=find(u,i);
up[u][i]^=up[v][i];
}
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(v==f)continue;
dep[v]=dep[u]+;
dfs(v,u);
}
}
int LCA(int u,int v){
int U=u,V=v;
if(dep[u]<dep[v])swap(u,v);
for(int i=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (u);
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (fa[u][]);
}
int solve(int u,int v,int k,int lca){
int res=(dep[u]+dep[v]-*dep[lca])%k;
int U=u,V=v;
v=find(v,res);
int ans=;
while(dep[u]>=dep[lca]){
ans^=a[u];
u=find(u,k);
if(!u)break;
}
if(V==lca||dep[v]<=dep[lca]||v==)return ans;
V=v;
while(dep[v]>=dep[lca]){
ans^=a[v];
v=find(v,k);
if(!v)break;
}
if((dep[U]-dep[lca])%k==&&(dep[V]-dep[lca])%k==)ans^=a[lca];
return ans;
}
void init(){
met(fa,);met(up,);
for(int i=;i<N;i++){
edg[i].clear();
}
}
int main(){
while(~scanf("%d%d",&n,&q)){
init();
sz=round(sqrt(n));
for(int i=,u,v;i<n;i++){
scanf("%d%d",&u,&v);
edg[u].pb(v);edg[v].pb(u);
}
for(int i=;i<=n;i++)scanf("%d",&a[i]);
dep[]=;
dfs(,);
while(q--){
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
int lca=LCA(u,v),ans=;;
if(k>sz){
ans=solve(u,v,k,lca);
}
else {
int dis=dep[u]-dep[lca];
int s=(dis/k+)*k;
int x=find(u,s);
ans=up[u][k]^up[x][k];
int res=(dep[u]+dep[v]-*dep[lca])%k;
if(lca!=v&&dep[v]-dep[lca]>res){
v=find(v,res);
dis=dep[v]-dep[lca];
s=(dis/k+)*k;
x=find(v,s);
ans^=up[v][k]^up[x][k];
if((dep[u]-dep[lca])%k==&&(dep[v]-dep[lca])%k==)ans^=a[lca];
}
}
printf("%d\n",ans);
}
}
return ;
}
2017 ACM-ICPC 亚洲区(西安赛区)网络赛 xor (根号分治)的更多相关文章
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- 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 ...
- 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)
摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...
- ICPC 2018 徐州赛区网络赛
ACM-ICPC 2018 徐州赛区网络赛 去年博客记录过这场比赛经历:该死的水题 一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. D. Easy Math 题意: ...
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
- [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题
第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...
- 2017 ACM/ICPC(西安)赛后总结
早上8:00的高铁,所以不得不6点前起床,向火车站赶……到达西安后已经是中午,西工大距离西安北站大概3小时车程的距离,只好先解决午饭再赶路了……下午3.30的热身赛,一行人在3.35左右赶到了赛场,坐 ...
- 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit 1000 ms Memory li ...
- 2014ACM/ICPC亚洲区鞍山赛区现场赛1009Osu!
鞍山的签到题,求两点之间的距离除以时间的最大值.直接暴力过的. A - Osu! Time Limit:1000MS Memory Limit:262144KB 64bit IO Fo ...
- [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players
Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...
随机推荐
- IDEA调试服务器上部署的程序
提出问题: 一个程序,部署在自己的电脑上,debug调试,相信大家都会,但是,如果我想debug调试非本地部署的程序怎么办呢.比如测试服务器上部署的程序. 其实这样的需求也是经常有的,比如一个大型的项 ...
- 洛谷 P2345 奶牛集会
https://www.luogu.org/problem/show?pid=2345 题目描述 约翰的N 头奶牛每年都会参加“哞哞大会”.哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅 ...
- LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...
- [洛谷P3293] [SCOI2016]美味
洛谷题目链接:[SCOI2016]美味 题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(二)
1. 创建一个Controller类 package com.example.demo; import org.springframework.web.bind.annotation.RequestM ...
- Eng1—English daily notes
English daily notes 2015年 4月 Phrases 1. As a side note #作为附注,顺便说句题外话,和by the way意思相近,例句: @1:As a sid ...
- Python面向对象学习 1 (什么是面向对象,面向对象的应用场景,待更新)
程序设计的三种基本结构: 面向对象,面向过程,函数式编程 1,什么是面向对象编程 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就 ...
- Mysql储存过程4:mysql变量设置
默认全局变量是两个@@开头, 可用show variables查看所有默认变量: @@user #declare定义变量只能用在储存过程中 #declare 变量名 数据类型 可选类型 declare ...
- 概述sysfs文件系统【转】
转自:http://blog.csdn.net/npy_lp/article/details/78933292 内核源码:linux-2.6.38.8.tar.bz2 目标平台:ARM体系结构 sys ...
- ProxySQL 监控和统计
ProxySQL 监控和统计 很多有价值的统计数据在stats和monitor库中. admin@127.0.0.1 [(none)]>SHOW TABLES FROM stats; +---- ...