日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

BZOJ4568: [Scoi2016]幸运数字(线性基 倍增)

發(fā)布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ4568: [Scoi2016]幸运数字(线性基 倍增) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意

題目鏈接

Sol

線性基是可以合并的

倍增維護(hù)一下

然后就做完了??

喵喵喵?

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int MAXN = 2e4 + 10, B = 60;
inline LL read() {char c = getchar(); LL x = 0, f = 1;while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();return x * f;
}
int N, Q, g[MAXN][16], dep[MAXN];
LL a[MAXN];
vector<int> v[MAXN];
struct Node {LL P[62];Node() {memset(P, 0, sizeof(P));}void insert(LL x) {for(int i = B; i >= 0; i--) {if((!(x >> i & 1))) continue;if(P[i]) {x ^= P[i]; continue;}P[i] = x; break;}}LL QueryMax() {LL ans = 0;for(int i = B; i >= 0; i--) ans = max(ans, ans ^ P[i]);return ans;}Node operator + (const Node &rhs) const {Node ans; for(int i = 0; i <= B; i++) ans.P[i] = this -> P[i];for(int i = 0; i <= B; i++) if(rhs.P[i]) ans.insert(rhs.P[i]);return ans; }
}f[MAXN][16];
void dfs(int x, int fa) {dep[x] = dep[fa] + 1;g[x][0] = fa;f[x][0].insert(a[x]); f[x][0].insert(a[fa]);for(int i = 0; i < v[x].size(); i++) {int to = v[x][i];if(to == fa) continue;dfs(to, x);}
}
void Pre() {for(int j = 1; j <= 15; j++)for(int i = 1; i <= N; i++)g[i][j] = g[g[i][j - 1]][j - 1], f[i][j] = f[i][j - 1] + f[g[i][j - 1]][j - 1];
}
LL Query(int x, int y) {Node base; base.insert(a[x]); base.insert(a[y]);if(dep[x] < dep[y]) swap(x, y);for(int i = 15; i >= 0; i--)if(dep[g[x][i]] >= dep[y])base = base + f[x][i], x = g[x][i];if(x == y) return base.QueryMax();for(int i = 15; i >= 0; i--)if(g[x][i] != g[y][i]) {base = base + f[x][i];base = base + f[y][i];x = g[x][i], y = g[y][i];}base = base + f[x][0]; base = base + f[y][0];return base.QueryMax();
}
int main() {
#ifndef ONLINE_JUDGE//freopen("a.in", "r", stdin); freopen("b.out", "w", stdout);
#endifN = read(); Q = read();for(int i = 1; i <= N; i++) a[i] = read();for(int i = 1; i <= N - 1; i++) {int x = read(), y = read();v[x].push_back(y); v[y].push_back(x);}dfs(1, 0);Pre();while(Q--) {int x = read(), y = read();printf("%lld\n", Query(x, y));}return 0;
}
/*
8 4
45654 251 321 54687 321321 654 6321432 5646
1 2
2 3
2 4
1 5
4 6 
6 7
5 87 8
7 5
6 8
4 1
*/

轉(zhuǎn)載于:https://www.cnblogs.com/zwfymqz/p/10059651.html

總結(jié)

以上是生活随笔為你收集整理的BZOJ4568: [Scoi2016]幸运数字(线性基 倍增)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。