当前位置:网站首页>[scoi2016] lucky numbers
[scoi2016] lucky numbers
2022-06-26 13:58:00 【__ LazyCat__】
Linear basis on tree
link :[P3292 SCOI2016] Lucky Numbers - Luogu | New ecology of computer science education (luogu.com.cn)
The question : Given a tree , seek x To y The maximum exclusive or sum of subsets on the path .
Answer key : Obviously , Linear bases on trees . Direct first-hand tree chain dissection to do it .
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=2e4+5;
ll a[maxn],n,m,u,v,w;
int dep[maxn],fa[maxn],sz[maxn],son[maxn];
int dfn[maxn],top[maxn],rk[maxn],now;
struct node{
int cnt;
ll p[61];
node():cnt(0){
memset(p,0,sizeof(p));}
}t[maxn<<2];
vector<int>ed[maxn];
void insert(node&t,ll x)
{
if(t.cnt>=61&&!x)return;
for(int i=60;i>=0;i--)
{
if(x>>i&1)
{
if(!t.p[i]){
t.p[i]=x,t.cnt++; break;}
x^=t.p[i];
}
}
return;
}
void update(int k,int l,int r,int pos,ll w)
{
insert(t[k],w);
if(l==r)return;
int mid=l+r>>1;
if(pos<=mid)update(k<<1,l,mid,pos,w);
else update(k<<1|1,mid+1,r,pos,w);
return;
}
void query(node&q,int k,int l,int r,int ql,int qr)
{
if(ql<=l&&r<=qr)
{
for(int i=60;i>=0;i--)insert(q,t[k].p[i]);
return;
}
int mid=l+r>>1;
if(ql<=mid)query(q,k<<1,l,mid,ql,qr);
if(mid<qr)query(q,k<<1|1,mid+1,r,ql,qr);
return;
}
void dfs1(int x,int f)
{
dep[x]=dep[f]+1,fa[x]=f,sz[x]=1;
for(auto y:ed[x])
{
if(y==f)continue;
dfs1(y,x),sz[x]+=sz[y];
if(sz[son[x]]<sz[y])son[x]=y;
}
return;
}
void dfs2(int x,int t)
{
dfn[x]=++now,rk[now]=x,top[x]=t;
if(!son[x])return;
dfs2(son[x],t);
for(auto y:ed[x])
{
if(y==fa[x]||y==son[x])continue;
dfs2(y,y);
}
return;
}
ll Query(int x,int y)
{
node q; ll ans=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
query(q,1,1,n,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
query(q,1,1,n,dfn[x],dfn[y]);
for(int i=60;i>=0;i--)ans=max(ans,ans^q.p[i]);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<n;i++)
{
cin>>u>>v;
ed[u].push_back(v);
ed[v].push_back(u);
}
dfs1(1,0),dfs2(1,1);
for(int i=1;i<=n;i++)update(1,1,n,i,a[rk[i]]);
for(int i=1;i<=m;i++)
{
cin>>u>>v;
cout<<Query(u,v)<<"\n";
}
return 0;
}
I'm sorry , Complexity is O ( n l o g 2 n l o g 2 w ) O(nlog^2nlog^2w) O(nlog2nlog2w) Of ,n It's the size of the tree ,p Is the range . It's easy to get stuck in time .
Found no modification , In fact, it can be maintained by multiplication , Complexity O ( n l o g n l o g 2 w ) O(nlognlog^2w) O(nlognlog2w), Time is greatly reduced , But the space will get bigger , But I can still live .
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=2e4+5;
ll a[maxn],n,m,u,v,w;
int dep[maxn],lg[maxn],fa[maxn][16];
struct node{
ll p[61]={
0};
}t[maxn][16];
vector<int>ed[maxn];
inline void insert(node&t,ll x)
{
if(!x)return;
for(int i=60;i>=0;i--)
{
if(x>>i&1)
{
if(!t.p[i]){
t.p[i]=x; break;}
x^=t.p[i];
}
}
return;
}
inline void add(node&t,node q)
{
for(int i=60;i>=0;i--)insert(t,q.p[i]);
}
void dfs(int x,int f)
{
dep[x]=dep[f]+1,fa[x][0]=f,insert(t[x][0],a[x]);
for(int i=1;i<=15;i++)
{
fa[x][i]=fa[fa[x][i-1]][i-1];
t[x][i]=t[x][i-1];
add(t[x][i],t[fa[x][i-1]][i-1]);
}
for(auto y:ed[x])
{
if(y!=f)dfs(y,x);
}
return;
}
inline ll LCA(int x,int y)
{
if(x==y)return a[x];
node q; ll ans=0;
if(dep[x]<dep[y])swap(x,y);
while(dep[x]>dep[y])
{
add(q,t[x][lg[dep[x]-dep[y]]-1]);
x=fa[x][lg[dep[x]-dep[y]]-1];
}
if(x==y)insert(q,a[x]);
else
{
for(int i=lg[dep[x]]-1;i>=0;i--)
{
if(fa[x][i]!=fa[y][i])
{
add(q,t[x][i]);
add(q,t[y][i]);
x=fa[x][i],y=fa[y][i];
}
}
insert(q,a[x]);
insert(q,a[y]);
insert(q,a[fa[x][0]]);
}
for(int i=60;i>=0;i--)ans=max(ans,ans^q.p[i]);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)lg[i]=lg[i-1]+(!(i&i-1));
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<n;i++)
{
cin>>u>>v;
ed[u].push_back(v);
ed[v].push_back(u);
}
dfs(1,0);
for(int i=1;i<=m;i++)
{
cin>>u>>v;
cout<<LCA(u,v)<<"\n";
}
return 0;
}
In fact, there is only 2 individual log Solution method , But there is no .
边栏推荐
- A collection of common tools for making we media videos
- Generation and rendering of VTK cylinder
- Input text to automatically generate images. It's fun!
- ICML 2022 | limo: a new method for rapid generation of targeted molecules
- Original code, inverse code and complement code
- Wechat applet -picker component is repackaged and the disabled attribute is added -- below
- 8. Ribbon load balancing service call
- 三维向量的夹角
- 虫子 STL string上
- Embedded virlog code running process
猜你喜欢
爱可可AI前沿推介(6.26)
李航老师新作《机器学习方法》上市了!附购买链接
2021-10-18 character array
Use performance to see what the browser is doing
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
Reprint - easy to use wechat applet UI component library
Global variable vs local variable
Input text to automatically generate images. It's fun!
7.Consul服务注册与发现
随机推荐
Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
Variable declaration of typescript
The most critical elements of team management
Sed editor
On insect classes and objects
Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
Wechat applet Registration Guide
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
Calculate the distance between two points (2D, 3D)
Is it safe to open a securities account? Is there any danger
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
Included angle of 3D vector
Network remote access using raspberry pie
shell脚本详细介绍(四)
Common operation and Principle Exploration of stream
Exercises under insect STL string
D check type is pointer
es常用语法一
Zero basics of C language lesson 7: break & continue
A must for programmers, an artifact utools that can improve your work efficiency n times