loj#2013. 「SCOI2016」幸运数字 点分治/线性基
题目链接
题解
和树上路径有管...点分治吧
把询问挂到点上
求出重心后,求出重心到每个点路径上的数的线性基
对于重心为lca的合并寻味,否则标记下传
对于每个询问,只需要暴力合并两个线性基即可
每个点只会被加到logn个线性基里,所以总复杂度为O(nlogn60 + q60*2)
然后我写了句memset(b,0,sizeof 0)...被卡了1h...
代码
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
inline long long Read() {
long long x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
#define LL long long
const int maxn = 200007;
int n,m;
long long val[maxn];
struct node {
int v,next;
} edge[maxn << 1];
int head[maxn],num = 0;
inline void add_edge(int u,int v) {
edge[++ num].v = v;edge[num].next = head[u],head[u] = num;
}
struct Base {
LL b[63];
inline void clear() {memset(b,0,sizeof b); }
inline void insert(LL x) {
for(int i = 60;~i;-- i)
if(x >> i & 1)
if(b[i]) x ^= b[i];
else {b[i] = x; break;}
}
inline void merge(const Base &x) {
for(int i = 60;~i;-- i)
if(x.b[i]) insert(x.b[i]);
}
inline LL query() {
LL ret = 0;
for(int i = 60;~i;-- i)
ret = std::max(ret ^ b[i],ret);
return ret;
}
} base[maxn];
int U[maxn],V[maxn],sz[maxn],bel[maxn];
LL ans[maxn];
std::vector<int>q[maxn];
bool vis[maxn];
int root = 0,mt;
void get_root(int x,int fa,int tot) {
sz[x] = 1; int mx = 0;
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
if(v == fa || vis[v]) continue;
get_root(v,x,tot);
sz[x] += sz[v];
mx = std::max(mx,sz[v]);
}
mx = std::max(tot - sz[x],mx);
if(mx < mt) root = x, mt = mx;
}
void dfs(int x,int fa,int Bel) {
bel[x] = Bel; base[x] = base[fa]; base[x].insert(val[x]);
for(int i = head[x];i;i = edge[i].next)
if(edge[i].v != fa && !vis[edge[i].v])
dfs(edge[i].v,x,Bel);
}
int tq[maxn];
void solve(int x) {
if(!q[x].size()) return;
mt = 20005; get_root(x,x,sz[x]);
vis[root] = 1;
bel[root] = root;
base[root].clear();
base[root].insert(val[root]);
for(int i = head[root];i;i = edge[i].next)
if(!vis[edge[i].v])
dfs(edge[i].v,root,edge[i].v);
int tot = q[x].size();
for(int i = 0;i <= tot;++ i) tq[i] = q[x][i];
q[x].clear();
Base tmp;
for(int i = 0,id;i < tot;++ i) {
if(bel[U[id = tq[i]]] == bel[V[id]])
q[bel[U[id]]].push_back(id);
else
tmp = base[U[id]],
tmp.merge(base[V[id]]),
ans[id] = tmp.query();
}
for(int i = head[root];i;i = edge[i].next)
if(!vis[edge[i].v]) solve(edge[i].v);
}
int main() {
//freopen("lucky1.in","r",stdin);
n = read();m = read();
for(int i = 1;i <= n;++ i) val[i] = Read();
for(int u,v,i = 1;i < n;++ i) {
u = read(),v = read();
add_edge(u,v); add_edge(v,u);
}
for(int i = 1;i <= m;++ i) {
U[i] = read(),V[i] = read();
if(U[i] == V[i]) ans[i] = val[U[i]];
else q[1].push_back(i);
}
sz[1] = n; solve(1);
for(int i = 1;i <= m;++ i)
printf("%lld\n",ans[i]);
return 0;
}
loj#2013. 「SCOI2016」幸运数字 点分治/线性基的更多相关文章
- loj #2013. 「SCOI2016」幸运数字
#2013. 「SCOI2016」幸运数字 题目描述 A 国共有 n nn 座城市,这些城市由 n−1 n - 1n−1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以 ...
- LOJ #2013「SCOI2016」幸运数字
时限为什么这么大啊 明摆着放多$ log$的做法过啊$QAQ$ LOJ #2013 题意 有$ Q$次询问,每次询问树上一条链,点有点权,你需要选择一些链上的点使得异或和尽量大 点数$ \leq 2* ...
- 【LOJ】 #2013. 「SCOI2016」幸运数字
题解 最大异或和,明显是个线性基 然而还有那么多路径--那就树分治,反正点数看起来很少,就是为了让人乘上一个60的常数嘛 把一个树的点分树记录下来,然后看看询问的两个点彼此相同的最后一个父亲是谁,把这 ...
- AC日记——「SCOI2016」幸运数字 LiBreOJ 2013
「SCOI2016」幸运数字 思路: 线性基: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 20005 # ...
- 「洛谷3292」「BZOJ4568」「SCOI2016」幸运数字【倍增LCA+线性基+合并】
[bzoj数据下载地址]不要谢我 先讲一下窝是怎么错的... \(MLE\)是因为数组开小了.. 看到异或和最大,那么就会想到用线性基. 如果不会线性基的可以参考一下我的学习笔记:「线性基」学习笔记a ...
- LOJ 2312(洛谷 3733) 「HAOI2017」八纵八横——线段树分治+线性基+bitset
题目:https://loj.ac/problem/2312 https://www.luogu.org/problemnew/show/P3733 原本以为要线段树分治+LCT,查了查发现环上的值直 ...
- loj2013 「SCOI2016」幸运数字
点分治+线性基 (为了这六个字窝调了一下午一晚上QAQ #include <iostream> #include <cstring> #include <cstdio&g ...
- luogu3292 幸运数字 (点分治+线性基)
首先第一眼是一个倍增套线性基,但是$O(Qlog^2Vlog^N)=10^{10}$的复杂度... 即使是st表也只是变成了$O(Nlog^2Vlog^N)$啊 考虑点分治,相对于倍增显著减少了线性基 ...
- loj#2015. 「SCOI2016」妖怪 凸函数/三分
题目链接 loj#2015. 「SCOI2016」妖怪 题解 对于每一项展开 的到\(atk+\frac{dnf}{b}a + dnf + \frac{atk}{a} b\) 令$T = \frac{ ...
随机推荐
- Linux 入门记录:十二、Linux 权限机制【转】
转自:https://www.cnblogs.com/mingc/p/7591287.html 一.权限 权限是操作系统用来限制资源访问的机制,权限一般分为读.写.执行. 系统中每个文件都拥有特定的权 ...
- c或c++的网络库
Asio C++ Library: Asio is a cross-platform C++ library for network and low-level I/O programming tha ...
- Centos6.5使用yum安装svn
1.测试环境 centos5.5 2.安装svn yum -y install subversion //测试SVN是否安装成功,执行:svnserve --version 3.配置 mkdir /w ...
- Python3学习笔记19-继承和多态
在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承, 新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Sup ...
- Appium+Java(一) Windows环境搭建篇
准备: Android版本 :4.2.2 nodejs版本:5.6.0 appium版本:v1.4.16 1. 安卓SDK及配置环境变量 1.1.先下载sdk安装包:installer_r24.4.1 ...
- PYTHON-流程控制之if/while/for-练习
# 1 练习题## 简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型# 编译型:C, 谷歌翻译,一次翻译后结果后重复使用# 解释型:Python, 同声传译,边执行边 ...
- JS实现购物车01
需求 使用JS实现购物车功能01 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- [转]centos7 下安装MongoDB
查看MongoDB的最新版官方下载地址: https://www.mongodb.com/download-center#community 使用wget命令下载安装包 wget https://fa ...
- MVC开发中的常见错误-03-System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。
return Db.SaveChanges()>0; return CurrentDBSession.SaveChanges(); RoleInfoService.EditEntity(role ...
- poj 1751 输出MST中新加入的边
给出结点的坐标 以及已建好的边 要输出MST中加入的边(已建好的边就不用输出了)结点的编号从1开始注意这题只有一组数据 不能用多组输入 否则就超时(在这被坑惨了Orz) Sample Input 91 ...