P3292 [SCOI2016]幸运数字(树剖 + 线段树维护线性基)
生活随笔
收集整理的這篇文章主要介紹了
P3292 [SCOI2016]幸运数字(树剖 + 线段树维护线性基)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
P3292 [SCOI2016]幸運數字
思路
如果這題是求x,yx, yx,y之間的距離顯然我們可以通過樹剖加線段樹來寫, 但是這里變成了求任意個數的異或最大值。如果給定區間我們顯然可以通過線性基來求解,但是這里是動態的區間,每次區間需要我們去找,于是我們想到可以通過線段樹加樹剖來維護一段區間內的線性基,然后再合并不同區間的線性基,最后對合并好了的線性基進行求解最大值。線性基區間的尋找就有點像是前面說的求這兩點間的點權和了。
然后整體復雜度分析一下log(n)log(n)log(n)的樹剖,nlog(n)nlog(n)nlog(n)的線段樹,再還有log(n)?log(n)log(n) * log(n)log(n)?log(n)的線性基合并,乘起來就是nlog4(n)n log ^ 4(n)nlog4(n),由于nnn比較小,勉強能沖一沖。
代碼
/*Author : lifehappy */ #pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h>#define mp make_pair #define pb push_back #define endl '\n' #define mid (l + r >> 1) #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r #define ls rt << 1 #define rs rt << 1 | 1using namespace std;typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii;const double pi = acos(-1.0); const double eps = 1e-7; const int inf = 0x3f3f3f3f;inline ll read() {ll f = 1, x = 0;char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f * x; }const int N = 2e4 + 10;int head[N], to[N << 1], nex[N << 1], cnt = 1;int son[N], fa[N], sz[N], dep[N], top[N], id[N], rk[N], tot, n, m;ll value[N], tree[N << 2][64], ans[64];void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++; }void dfs1(int rt, int f) {dep[rt] = dep[f] + 1;sz[rt] = 1, fa[rt] = f;for(int i = head[rt]; i; i = nex[i]) {if(to[i] == f) continue;dfs1(to[i], rt);if(!son[rt] || sz[son[rt]] < sz[to[i]]) son[rt] = to[i];sz[rt] += sz[to[i]];} }void dfs2(int rt, int tp) {top[rt] = tp; tot++;rk[tot] = rt, id[rt] = tot;if(!son[rt]) return ;dfs2(son[rt], tp);for(int i = head[rt]; i; i = nex[i]) {if(to[i] == fa[rt] || to[i] == son[rt]) continue;dfs2(to[i], to[i]);} }void insert(ll * base, ll x) {for(int i = 60; i >= 0; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return ;}x ^= base[i];}} }void merge(ll * a, ll * b) {for(int i = 60; i >= 0; i--) {if(b[i]) {insert(a, b[i]);}} }void push_up(int rt) {merge(tree[rt], tree[ls]);merge(tree[rt], tree[rs]); }void build(int rt, int l, int r) {if(l == r) {insert(tree[rt], value[rk[l]]);return ;}build(lson);build(rson);push_up(rt); }void query(int rt, int l, int r, int L, int R) {if(l >= L && r <= R) {merge(ans, tree[rt]);return ;}if(L <= mid) query(lson, L, R);if(R > mid) query(rson, L, R); }ll query_max() {ll res = 0;for(int i = 60; i >= 0; i--) {if((res ^ ans[i]) > res) {res ^= ans[i];}ans[i] = 0;//用完了就重置0方便下一次查找答案的線性基合并。}return res; }ll solve(int x, int y) {while(top[x] != top[y]) {if(dep[top[x]] < dep[top[y]]) swap(x, y);query(1, 1, n, id[top[x]], id[x]);x = fa[top[x]];}if(dep[x] > dep[y]) swap(x, y);query(1, 1, n, id[x], id[y]);return query_max(); }int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);n = read(), m = read();for(int i = 1; i <= n; i++) {value[i] = read();}for(int i = 1; i < n; i++) {int x = read(), y = read();add(x, y);add(y, x);}dfs1(1, 0);dfs2(1, 1);build(1, 1, n);for(int i = 1; i <= m; i++) {int x = read(), y = read();printf("%lld\n", solve(x, y));}return 0; }總結
以上是生活随笔為你收集整理的P3292 [SCOI2016]幸运数字(树剖 + 线段树维护线性基)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国电信、中兴通讯展示“5G 驭风云笔电
- 下一篇: 大量用户双11买苹果iPhone被背刺