点分治+线性基

(为了这六个字窝调了一下午一晚上QAQ

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cassert>
using namespace std;
typedef long long ll;
int n, uu, vv, m, hea[20005], cnt, sze, rot, rnd[20005], siz[20005], bel[20005];
bool vis[20005];
ll a[20005], ans[200005];
struct Edge{
int too, nxt;
}edge[40005];
struct Ques{
int u, v, idx;
};
struct LinearBase{
ll num[65];
void clear(){
memset(num, 0, sizeof(num));
}
void insert(ll x){
for(int i=60; i>=0; i--)
if(x&(1ll<<i)){
if(num[i]) x ^= num[i];
else{
num[i] = x;
break;
}
}
}
ll operator+(const LinearBase &x)const{
LinearBase c=x;
for(int i=60; i>=0; i--)
c.insert(num[i]);
ll re=0;
for(int i=60; i>=0; i--)
if((re^c.num[i])>re)
re ^= c.num[i];
return re;
}
}bas[20005];
vector<Ques> q[20005];
void add_edge(int fro, int too){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
hea[fro] = cnt;
}
void getRoot(int x, int f){
siz[x] = 1;
rnd[x] = 0;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f && !vis[t]){
getRoot(t, x);
siz[x] += siz[t];
rnd[x] = max(rnd[x], siz[t]);
}
}
rnd[x] = max(rnd[x], sze-siz[x]);
if(rnd[x]<rnd[rot]) rot = x;
}
void dfs1(int x, int f){
bas[x].insert(a[x]);
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f && !vis[t]){
bas[t] = bas[x];
dfs1(t, x);
}
}
}
void dfs2(int x, int f, int fro){
bel[x] = fro;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f && !vis[t]) dfs2(t, x, fro);
}
}
void work(int x){
vis[x] = true;
bas[x].clear();
dfs1(x, 0);
bel[x] = x;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(!vis[t]) dfs2(t, x, t);
}
for(int i=0; i<q[x].size(); i++)
if(q[x][i].u==x || q[x][i].v==x || bel[q[x][i].u]!=bel[q[x][i].v])
ans[q[x][i].idx] = bas[q[x][i].u] + bas[q[x][i].v];
else
q[bel[q[x][i].u]].push_back(q[x][i]);
q[x].clear();
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(!vis[t]){
rot = 0;
sze = siz[t];
getRoot(t, 0);
q[rot] = q[t];
if(rot!=t) q[t].clear();
work(rot);
}
}
}
int main(){
cin>>n>>m;
for(int i=1; i<=n; i++) scanf("%lld", &a[i]);
for(int i=1; i<n; i++){
scanf("%d %d", &uu, &vv);
add_edge(uu, vv);
add_edge(vv, uu);
}
sze = n;
rnd[0] = 0x3f3f3f3f;
getRoot(1, 0);
for(int i=1; i<=m; i++){
scanf("%d %d", &uu, &vv);
if(uu==vv) ans[i] = a[uu];
else q[rot].push_back((Ques){uu, vv, i});
}
work(rot);
for(int i=1; i<=m; i++)
printf("%lld\n", ans[i]);
return 0;
}

loj2013 「SCOI2016」幸运数字的更多相关文章

  1. loj#2013. 「SCOI2016」幸运数字 点分治/线性基

    题目链接 loj#2013. 「SCOI2016」幸运数字 题解 和树上路径有管...点分治吧 把询问挂到点上 求出重心后,求出重心到每个点路径上的数的线性基 对于重心为lca的合并寻味,否则标记下传 ...

  2. AC日记——「SCOI2016」幸运数字 LiBreOJ 2013

    「SCOI2016」幸运数字 思路: 线性基: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 20005 # ...

  3. loj #2013. 「SCOI2016」幸运数字

    #2013. 「SCOI2016」幸运数字 题目描述 A 国共有 n nn 座城市,这些城市由 n−1 n - 1n−1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以 ...

  4. 「洛谷3292」「BZOJ4568」「SCOI2016」幸运数字【倍增LCA+线性基+合并】

    [bzoj数据下载地址]不要谢我 先讲一下窝是怎么错的... \(MLE\)是因为数组开小了.. 看到异或和最大,那么就会想到用线性基. 如果不会线性基的可以参考一下我的学习笔记:「线性基」学习笔记a ...

  5. LOJ #2013「SCOI2016」幸运数字

    时限为什么这么大啊 明摆着放多$ log$的做法过啊$QAQ$ LOJ #2013 题意 有$ Q$次询问,每次询问树上一条链,点有点权,你需要选择一些链上的点使得异或和尽量大 点数$ \leq 2* ...

  6. 【LOJ】 #2013. 「SCOI2016」幸运数字

    题解 最大异或和,明显是个线性基 然而还有那么多路径--那就树分治,反正点数看起来很少,就是为了让人乘上一个60的常数嘛 把一个树的点分树记录下来,然后看看询问的两个点彼此相同的最后一个父亲是谁,把这 ...

  7. 「SCOI2010」幸运数字

    传送门 Luogu 解题思路 首先构造出所有的幸运数字. 然后考虑一个幸运数字会产生多少贡献. 对于一个数 \(x\),它在区间 \([l,r]\) 内的倍数的个数为 \(\lfloor \frac{ ...

  8. 「SCOI2016」围棋 解题报告

    「SCOI2016」围棋 打CF后困不拉基的,搞了一上午... 考虑直接状压棋子,然后发现会t 考虑我们需要上一行的状态本质上是某个位置为末尾是否可以匹配第一行的串 于是状态可以\(2^m\)压住了, ...

  9. 「SCOI2016」妖怪 解题报告

    「SCOI2016」妖怪 玄妙...盲猜一个结论,然后过了,事后一证,然后假了,数据真水 首先要最小化 \[ \max_{i=1}^n (1+k)x_i+(1+\frac{1}{k})y_i \] \ ...

随机推荐

  1. [整理]Magento2 开发中遇到的错误以及解决方案

    下面记录了一些我在二次开发Magento2是所遇到的错误.错误原因以及解决方案: 1. Object DOMDocument should be created type: report 日志摘要: ...

  2. Country roads take me home, to the place I belong.

    Country roads take me home, to the place I belong.故乡的路,带我回家吧,回到我期盼已久的归宿.

  3. hive中select中DISTINCT的技巧和使用

    hive中select中DISTINCT的技巧和使用 单表的唯一查询用:distinct 多表的唯一查询用:group by 在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 ...

  4. Redis数据库2

    一.存储list 1.概述     list类型是按照插入顺序排序的字符串链表,可以在头部(left)和尾部(right)添加新的元素,如果不存在,则自动创建;如果list中所有元素被移除,则list ...

  5. ADO.Net——防止SQL注入攻击

    规避SQL注入 如果不规避,在黑窗口里面输入内容时利用拼接语句可以对数据进行攻击 如:输入Code值 p001' union select * from Info where '1'='1 //这样可 ...

  6. WAMP安装提示缺少 msvcr100.dll文件解决方法

    WAMP安装提示缺少wamp msvcr100.dll文件解决方法 因为wamp基于vs c++2010开发,需要提前安装这个组件才可以正常运行 微软官方组件下载地址: 32位:http://www. ...

  7. checkbox绑定v-for的数据

    简述自己遇到的问题,觉得合适就拿去用 我在使用v-for遍历checked复选框数据的时候,数据分为两部分,一个主活动,主活动下面有多个子活动 我实体类的设计是里面加个list放子活动, 页面循环需要 ...

  8. WINDOWS-API:关于线程CreateThread,_beginthead(_beginthreadex),AfxBeginThread

    [转]windows多线程编程CreateThread,_beginthead(_beginthreadex)和AfxBeginThread的区别 在Windows的多线程编程中,创建线程的函数主要有 ...

  9. js学习笔记-字符串

    1.需要注意的是,JavaScript 的字符串是不可变的(immutable),String 类定义的方法都不能改变字符串的内容.像 String.toUpperCase() 这样的方法,返回的是全 ...

  10. MySQL Innodb表空间不足的处理方法

    官方给出的解决方案: 添加和删除 InnoDB 数据和日志文件 这一节描述在InnoDB表空间耗尽空间之时,或者你想要改变日志文件大小之时,你可以做的一些事情. 最简单的,增加InnoDB表空间大小的 ...