「SCOI2016」幸运数字

思路:

  线性基;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 20005
#define ll long long
struct DataType {
ll d[],p[];
ll cnt;
DataType()
{
memset(d,,sizeof(d));
memset(p,,sizeof(p));
cnt=;
}
bool insert(long long val)
{
for(ll i=;i>=;i--)
{
if(val&(1LL<<i))
{
if(!d[i])
{
d[i]=val;
break;
}
val^=d[i];
}
}
return val>;
}
ll Max()
{
ll res=;
for(ll i=;i>=;i--)
{
if((res^d[i])>res) res^=d[i];
}
return res;
}
};
struct DataType ans;
struct TreeNodeType {
ll l,r,mid;
DataType data;
};
struct TreeNodeType tree[maxn<<];
ll n,m,deep[maxn],id[maxn],size[maxn],f[maxn],head[maxn];
ll E[maxn<<],V[maxn<<],tot,ai[maxn],bi[maxn],lar[maxn];
ll top[maxn];
inline void in(ll &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
void edge_add(ll u,ll v)
{
E[++tot]=head[u],V[tot]=v,head[u]=tot;
E[++tot]=head[v],V[tot]=u,head[v]=tot;
}
void merge(DataType &a,DataType b)
{
for(ll i=;i<=;i++)
{
if(b.d[i]) a.insert(b.d[i]);
}
}
void dfs1(ll now,ll fa)
{
f[now]=fa,deep[now]=deep[fa]+,size[now]=;
for(ll i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
dfs1(V[i],now),size[now]+=size[V[i]];
if(size[lar[now]]<size[V[i]]) lar[now]=V[i];
}
}
void dfs2(ll now,ll chain)
{
top[now]=chain,id[now]=++tot,bi[tot]=ai[now];
if(lar[now])
{
dfs2(lar[now],chain);
for(ll i=head[now];i;i=E[i])
{
if(V[i]==lar[now]||V[i]==f[now]) continue;
dfs2(V[i],V[i]);
}
}
}
void build(ll now,ll l,ll r)
{
tree[now].l=l,tree[now].r=r;
if(l==r)
{
tree[now].data.insert(bi[l]);
return;
}
tree[now].mid=l+r>>;
build(now<<,l,tree[now].mid),build(now<<|,tree[now].mid+,r);
tree[now].data=tree[now<<].data;
merge(tree[now].data,tree[now<<|].data);
}
void query(ll now,ll l,ll r)
{
if(tree[now].l>=l&&tree[now].r<=r)
{
merge(ans,tree[now].data);
return;
}
if(l<=tree[now].mid) query(now<<,l,r);
if(r>tree[now].mid) query(now<<|,l,r);
}
ll query(ll x,ll y)
{
for(int i=;i<=;i++) ans.d[i]=,ans.p[i]=;
ans.cnt=;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) query(,id[top[y]],id[y]),y=f[top[y]];
else query(,id[top[x]],id[x]),x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
query(,id[x],id[y]);
return ans.Max();
}
int main()
{
freopen("data.txt","r",stdin);
in(n),in(m);ll u,v;
for(ll i=;i<=n;i++) in(ai[i]);
for(ll i=;i<n;i++)in(u),in(v),edge_add(u,v);
dfs1(,),tot=,dfs2(,),build(,,n);
while(m--) in(u),in(v),printf("%lld\n",query(u,v));
return ;
}

AC日记——「SCOI2016」幸运数字 LiBreOJ 2013的更多相关文章

  1. AC日记——「SCOI2016」背单词 LiBreOJ 2012

    #2012. 「SCOI2016」背单词 思路: Orz: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...

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

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

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

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

  4. AC日记——「SCOI2016」美味 LiBreOJ 2016

    #2016. 「SCOI2016」美味 思路: 主席树: 代码: #include <bits/stdc++.h> using namespace std; #define maxa 26 ...

  5. AC日记——「HNOI2017」单旋 LiBreOJ 2018

    #2018. 「HNOI2017」单旋 思路: set+线段树: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  6. AC日记——「SCOI2015」国旗计划 LiBreOJ 2007

    #2007. 「SCOI2015」国旗计划 思路: 跪烂Claris 代码: #include <cstdio> #include <algorithm> #define ma ...

  7. AC日记——「SDOI2017」序列计数 LibreOJ 2002

    「SDOI2017」序列计数 思路: 矩阵快速幂: 代码: #include <bits/stdc++.h> using namespace std; #define mod 201704 ...

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

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

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

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

随机推荐

  1. sublime Text 块编辑方法

    比如我们要把SQL语句中的多表查询结果封装成pojo SQL: SELECT a.id, a.title, a.sell_point, a.price, a.image, b.`name` categ ...

  2. MongoDB插入数据的3种方法

    insert()方法: 下面是在inventory集合中插入一个三个字段的文档: db.inventory.insert( { _id: 10, type: "misc", ite ...

  3. uva 11424

    uva 11424 GCD - Extreme (I) 题意:思路:(见http://www.cnblogs.com/Duahanlang/p/3184994.html ) 差别在于数据规模和时间,其 ...

  4. mysql 数据库备份与还原,用户的创建与删除,用户的密码修改

    1.备份数据库 要退出mysql rimideiMac-23:~ rimi$    mysqldump -u root -p pro >pro.sql ls 查看路径 2.恢复数据库 2.1直接 ...

  5. 【BZOJ】1588: [HNOI2002]营业额统计

    [算法]平衡树(treap)||双向链表 [题解]treap知识见数据结构. #include<cstdio> #include<algorithm> #include< ...

  6. windows phone 8.1如何访问应用商店,商店评论的连接

    Windows Phone 8.1 中可以使用这个链接跳转到应用评论页面: await Windows.System.Launcher.LaunchUriAsync( new Uri("ms ...

  7. 【洛谷 P2042】 [NOI2005]维护数列(自闭记第一期)

    题目链接 首先,这题我是没A的..太毒瘤了 题目本身不难,都是\(Splay\)的基操,但是细节真的容易挂. 调了好久自闭了,果断放弃.. 希望本节目停更. 放上最终版本 #include <c ...

  8. 【洛谷 P2303】 [SDOi2012]Longge的问题 (欧拉函数)

    题目链接 题意:求\(\sum_{i=1}^{n}\gcd(i,n)\) 首先可以肯定,\(\gcd(i,n)|n\). 所以设\(t(x)\)表示\(gcd(i,n)=x\)的\(i\)的个数. 那 ...

  9. HashMap根据value获取key值

    public static String getCityId(HashMap<String,String> citys, String city){ Set set = citys.ent ...

  10. Codeforces Round #483 (Div. 1) 简要题解

    来自FallDream的博客,未经允许,请勿转载,谢谢. 为了证明一下我又来更新了,写一篇简要的题解吧. 这场比赛好像有点神奇,E题莫名是道原题,导致有很多选手直接过掉了(Claris 表演24s过题 ...