P3292 [SCOI2016]幸运数字 线性基

正解:线性基+倍增

解题报告:

先放下传送门QAQ

然后这题,其实没什么太大的技术含量,,,?就几个知识点套在一起,除了代码长以外没任何意义,主要因为想复习下线性基的题目所以还是写下,,,

随便写下思路趴,首先多个数异或显然线性基,然后因为是在树上所以可以考虑倍增预处理线性基,插入合并查询都基操我不说了QAQ

然后因为我树剖不熟练所以我用的树剖,,,当然倍增一样的反正都差不多?反正就xxj[i][j]:第i个点向上跳j步的线性基,和普通树上跳lca什么都一样的做法,over

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define ll long long
#define int long long
#define gc getchar()
#define mp make_pair
#define t(i) edge[i].to
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ll i=x;i>=y;--i)
#define e(i,x) for(ri i=head[x];i;i=edge[i].nxt) const int N=;
int n,q,poww[]={},val[N],ed_cnt,head[N],hs[N],sz[N],dfn[N],dfn_cnt,top[N],lg[N],fa[N],dep[N];
struct ed{int to,nxt;}edge[N<<];
struct xxj
{
ll a[];int num_cnt;
il void clr(){memset(a,,sizeof(a));num_cnt=;}
il void insert(ll x){if(num_cnt==)return;my(i,,)if(x&poww[i]){x^=a[i];if(!a[i]){a[i]=x,++num_cnt;return;}}}
il ll mx(){ll ret=;my(i,,)ret=max(ret,ret^a[i]);return ret;}
}gdgs[][N],as; il int read()
{
rc ch=gc;ri x=;rb y=;
while(ch!='-' && (ch<'' || ch>''))ch=gc;
if(ch=='-')ch=gc,y=;
while(ch>='' && ch<='')x=(x<<)+(x<<)+(ch^''),ch=gc;
return y?x:-x;
}
il void ad(ri x,ri y){edge[++ed_cnt]=(ed){x,head[y]};head[y]=ed_cnt;edge[++ed_cnt]=(ed){y,head[x]};head[x]=ed_cnt;}
void dfs1(ri x,ri fat)
{
sz[x]=;fa[x]=fat;dep[x]=dep[fat]+;
e(i,x)if(fat^t(i)){dfs1(t(i),x);sz[x]+=sz[t(i)];if(sz[t(i)]>sz[hs[x]])hs[x]=t(i);}
}
void dfs2(ri x,ri tp)
{
top[x]=tp;dfn[x]=++dfn_cnt;gdgs[][dfn_cnt].insert(val[x]);
if(hs[x])dfs2(hs[x],tp);e(i,x)if(!dfn[t(i)])dfs2(t(i),t(i));
}
il xxj merg(xxj gd,xxj gs){rp(i,,){if(gd.num_cnt==)return gd;if(gs.a[i])gd.insert(gs.a[i]);}return gd;}
il xxj gt(ri l,ri r){ri ln=lg[r-l+];return merg(gdgs[ln][l],gdgs[ln][r-poww[ln]+]);} main()
{
// freopen("3292.in","r",stdin);freopen("3292.out","w",stdout);
rp(i,,)poww[i]=poww[i-]<<;
n=read();q=read();rp(i,,n)val[i]=read();rp(i,,n-){ri x=read(),y=read();ad(x,y);}dfs1(,);dfs2(,);
rp(i,,n)lg[i]=lg[i>>]+;rp(j,,lg[n])rp(i,,n+-poww[j])gdgs[j][i]=merg(gdgs[j-][i],gdgs[j-][i+poww[j-]]);
while(q--)
{
ri x=read(),y=read();as.clr();
while(top[x]!=top[y]){if(dep[top[x]]<dep[top[y]])swap(x,y);as=merg(gt(dfn[top[x]],dfn[x]),as);x=fa[top[x]];}
if(dep[x]<dep[y])swap(x,y);as=merg(gt(dfn[y],dfn[x]),as);printf("%lld\n",as.mx());
}
return ;
}

放下代码鸭QwQ

上一篇:Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 发表于 2017-01-03 | Spring框架 | Spri


下一篇:Markdown 11种基本语法