当前位置:网站首页>[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 .

原网站

版权声明
本文为[__ LazyCat__]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170511166938.html