1. 程式人生 > >[SCOI2016]幸運數字 樹鏈剖分,線性基

[SCOI2016]幸運數字 樹鏈剖分,線性基

org const 是我 efi 復雜度 ack push_back 插入 space

[SCOI2016]幸運數字

LG傳送門

為了快樂,我們用樹剖寫這題,你看樹剖寫的還可以帶插入

強行樹剖,線段樹上每個結點維護一個線性基,每次查詢暴力合並。

瞎分析一波復雜度:樹剖兩點之間\(\log n\)條重鏈,每條重鏈在線段樹上最多合並\(\log n\)次,合並兩個線性基最多需要\(\log m\)次插入,每次插入\(\log m\)(設\(m\)為值域)。總復雜度大概是\(O(n (\log n) ^ 2 (\log m) ^ 2)\)可能錯了不要怪我

算起來大概是\(1.62 * 10 ^ {10}\)的規模,但是我們是有信仰的oier,我們要堅信這個東西是跑不滿的。所以我就最慢的點\(1.1s\)

多過了這題。當然不吸氧是布星的。

//written by newbiechd
#include <cstdio>
#include <cctype>
#include <vector>
#define R register
#define I inline
#define B 1000000
#define L long long
using namespace std;
const int N = 20003;
char buf[B], *p1, *p2;
I char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, B, stdin), p1 == p2) ? EOF : *p1++; }
I L rd() {
    L f = 0;
    R char c = gc();
    while (c < 48 || c > 57)
        c = gc();
    while (c > 47 && c < 58)
        f = f * 10 + (c ^ 48), c = gc();
    return f;
}
int s[N], fa[N], dep[N], siz[N], son[N], dfn[N], top[N], n, tim;
L w[N], val[N];
vector <int> g[N];
I L min(L x, L y) { return x < y ? x : y; }
I L max(L x, L y) { return x > y ? x : y; }
I void swap(L &x, L &y) { x ^= y, y ^= x, x ^= y; }
I void swap(int &x, int &y) { x ^= y, y ^= x, x ^= y; }
struct base {
    vector <L> v;
    I void insert(L x) {
        R int i, s = v.size();
        for (i = 0; i < s; ++i)
            x = min(x, x ^ v[i]);
        if (x) {
            v.push_back(x);
            for (i = s; i; --i)
                if (v[i] > v[i - 1])
                    swap(v[i], v[i - 1]);
        }
    }
    I void merge(base x) {
        for (R int i = 0, s = x.v.size(); i < s; ++i)
            insert(x.v[i]);
    }
    I L query() {
        L o = 0;
        for (R int i = 0, s = v.size(); i < s; ++i)
            o = max(o, o ^ v[i]);
        return o;
    }
}e[N << 2], ans;
void dfs1(int x, int f) {
    fa[x] = f, dep[x] = dep[f] + 1, siz[x] = 1;
    for (R int i = 0, y, m = 0; i < s[x]; ++i)
        if ((y = g[x][i]) ^ f) {
            dfs1(y, x), siz[x] += siz[y];
            if (siz[y] > m)
                m = siz[y], son[x] = y;
        }
}
void dfs2(int x, int t) {
    dfn[x] = ++tim, val[tim] = w[x], top[x] = t;
    if (son[x])
        dfs2(son[x], t);
    for (R int i = 0, y; i < s[x]; ++i)
        if ((y = g[x][i]) ^ fa[x] && y ^ son[x])
            dfs2(y, y);
}
void build(int k, int l, int r) {
    for (R int i = l; i <= r; ++i)
        e[k].insert(val[i]);
    if (l == r)
        return ;
    R int p = k << 1, q = p | 1, m = (l + r) >> 1;
    build(p, l, m), build(q, m + 1, r);
}
void tquery(int k, int l, int r, int x, int y) {
    if (x <= l && r <= y) {
        ans.merge(e[k]);
        return ;
    }
    R int p = k << 1, q = p | 1, m = (l + r) >> 1;
    if (x <= m)
        tquery(p, l, m, x, y);
    if (m < y)
        tquery(q, m + 1, r, x, y);
}
I L query(int x, int y) {
    ans.v.clear();
    while (top[x] ^ top[y]) {
        if (dep[top[x]] < dep[top[y]])
            swap(x, y);
        tquery(1, 1, n, dfn[top[x]], dfn[x]), x = fa[top[x]];
    }
    if (dep[x] > dep[y])
        swap(x, y);
    tquery(1, 1, n, dfn[x], dfn[y]);
    return ans.query();
}
int main() {
    R int Q, i, x, y;
    n = rd(), Q = rd();
    for (i = 1; i <= n; ++i)
        w[i] = rd();
    for (i = 1; i < n; ++i)
        x = rd(), y = rd(), g[x].push_back(y), g[y].push_back(x);
    for (i = 1; i <= n; ++i)
        s[i] = g[i].size();
    dfs1(1, 0), dfs2(1, 1), build(1, 1, n);
    for (i = 1; i <= Q; ++i)
        x = rd(), y = rd(), printf("%lld\n", query(x, y));
    return 0;
}

\(O(n \log n \log m)\)的點分治寫法,但是我懶得寫了

[SCOI2016]幸運數字 樹鏈剖分,線性基