当前位置:网站首页>[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 .
边栏推荐
- Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
- Sed editor
- 爱可可AI前沿推介(6.26)
- Lamp compilation and installation
- 7-2 picking peanuts
- [MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
- Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
- 李航老师新作《机器学习方法》上市了!附购买链接
- character constants
- Taishan Office Technology Lecture: four cases of using bold font
猜你喜欢

使用 Performance 看看浏览器在做什么

去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程

Lamp compilation and installation

7.Consul服务注册与发现

三维向量的夹角

Es snapshot based data backup and restore

Calculate the distance between two points (2D, 3D)

Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached

8.Ribbon负载均衡服务调用

Zero basics of C language lesson 8: Functions
随机推荐
[node.js] MySQL module
windows版MySQL软件的安装与卸载
Postman自动化接口测试
Tips for using nexys A7 development board resources
ICML 2022 | limo: a new method for rapid generation of targeted molecules
Go language - pipeline channel
VTK 圆柱体的生成与渲染
Detailed practical sharing, two hours of funny videos after work, earning more than 7000 a month
What is the use of index aliases in ES
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
PHP非对称加密算法(RSA)加密机制设计
Mediapipe gestures (hands)
There are many contents in the widget, so it is a good scheme to support scrolling
Memory considerations under bug memory management
Luogu p3426 [poi2005]sza-template solution
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
Is expression of D
GC is not used in D
A few lines of code can realize complex excel import and export. This tool class is really powerful!
Wechat applet -picker component is repackaged and the disabled attribute is added -- above