zoukankan      html  css  js  c++  java
  • Bzoj4568: [Scoi2016]幸运数字

    题面

    传送门

    Sol

    裸的线性基
    当然是选择(log^3)(st)表+树剖辣

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(2e4 + 5);
    
    IL ll Input(){
    	RG ll x = 0, z = 1; RG char c = getchar();
    	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    	return x * z;
    }
    
    int n, q, lg[_], first[_], cnt;
    struct Edge{
    	int to, next;
    } edge[_ << 1];
    ll pw[63] = {1}, w[_];
    struct Bac{
    	ll b[63];
    	int num;
    
    	IL void Init(){
    		Fill(b, 0);
    	}
    	
    	IL void Insert(RG ll x){
    		if(num == 63) return;
    		for(RG int i = 62; ~i; --i){
    			if(~x & pw[i]) continue;
    			if(!x) break;
    			if(!b[i]){
    				b[i] = x, ++num;
    				break;
    			}
    			x ^= b[i];
    		}
    	}
    
    	IL ll Calc(){
    		RG ll x = 0;
    		for(RG int i = 62; ~i; --i) x = max(x, x ^ b[i]);
    		return x;
    	}
    } st[20][_], Ans;
    
    IL Bac operator +(RG Bac A, RG Bac B){
    	for(RG int i = 62; ~i; --i)
    		if(A.b[i]) B.Insert(A.b[i]);
    	return B;
    }
    
    IL void Add(RG int u, RG int v){
    	edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
    }
    
    int dfn[_], fa[_], top[_], size[_], son[_], idx, deep[_];
    
    IL void Dfs1(RG int u){
    	size[u] = 1;
    	for(RG int e = first[u]; e != -1; e = edge[e].next){
    		RG int v = edge[e].to;
    		if(size[v]) continue;
    		deep[v] = deep[u] + 1, fa[v] = u;
    		Dfs1(v);
    		size[u] += size[v];
    		if(size[v] > size[son[u]]) son[u] = v;
    	}
    }
    
    IL void Dfs2(RG int u, RG int Top){
    	top[u] = Top, dfn[u] = ++idx;
    	if(son[u]) Dfs2(son[u], Top);
    	for(RG int e = first[u]; e != -1; e = edge[e].next)
    		if(!dfn[edge[e].to]) Dfs2(edge[e].to, edge[e].to);
    }
    
    IL void Build(){
    	for(RG int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
    	Dfs1(1), Dfs2(1, 1);
    	for(RG int i = 1; i <= n; ++i) st[0][dfn[i]].Insert(w[i]);
    	for(RG int j = 1; j <= lg[n]; ++j)
    		for(RG int i = 1; i + (1 << j) - 1 <= n; ++i)
    			st[j][i] = st[j - 1][i] + st[j - 1][i + (1 << (j - 1))];
    }
    
    IL Bac Query(RG int l, RG int r){
    	RG int lg2 = lg[r - l + 1];
    	return st[lg2][l] + st[lg2][r - (1 << lg2) + 1];
    }
    
    IL Bac LCA(RG int u, RG int v){
    	Ans.Init();
    	while(top[u] ^ top[v]){
    		if(deep[top[u]] < deep[top[v]]) swap(u, v);
    		Ans = Ans + Query(dfn[top[u]], dfn[u]);
    		u = fa[top[u]];
    	}
    	if(deep[u] > deep[v]) swap(u, v);
    	return Ans + Query(dfn[u], dfn[v]);
    }
    
    int main(RG int argc, RG char *argv[]){
    	for(RG int i = 1; i < 63; ++i) pw[i] = pw[i - 1] << 1;
    	n = Input(), q = Input();
    	for(RG int i = 1; i <= n; ++i) w[i] = Input(), first[i] = -1;
    	for(RG int i = 1; i < n; ++i){
    		RG int u = Input(), v = Input();
    		Add(u, v), Add(v, u);
    	}
    	Build();
    	for(RG int i = 1; i <= q; ++i){
    		RG int u = Input(), v = Input();
    		printf("%lld
    ", LCA(u, v).Calc());
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    docker部署mysql Navicat远程连接
    window10安装nginx及请求转发到tomcat服务器访问项目及开机自启
    Keras笔记
    Spectral Clustering 并用silhouette指标值确定最优聚类数目
    Java Swing, paint(), paintComponent(), repaint()
    tensorflow(一):安装在Spyder上
    人工智能入门(七):artificial neural network
    人工智能入门(六):SVM
    人工智能入门(五):data mining的一些算法
    人工智能入门(四):uncertainty&基于统计的学习
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8585991.html
Copyright © 2011-2022 走看看