NOI模拟(5.23) TJOID2T2 xor (bzoj5338)

Xor

题目背景:

5.23 模拟 TJOI2018D2T2

分析:树链剖分 + 可持久化trie

 

一个子树查询一个链查询,妥妥的树链剖分,对于异或最大的数,显然是trie上高低位贪心就可以了,然后因为区间查询,那么可持久化就好了,复杂度O(nlog2n),其实还可以把两种询问分开来建可持久化trie,对于第一种按照dfs序建,第二种每次继承自己的父亲来建,这样就可以做到一个log,但是并没有很快,再加上有3秒,两个log也是卡不掉的,所以直接上就好了。

 

Source:

 

/*
	created by scarlyw
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

/*
template
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

// /*
template
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
// */

const int MAXN = 100000 + 10;
const int MAXX = 2147483647;

int n, m, x, y, z, ind, cnt, type;
int v[MAXN];
int size[MAXN], dep[MAXN], father[MAXN], son[MAXN], top[MAXN];
int in[MAXN], out[MAXN], pos[MAXN], root[MAXN];
std::vector edge[MAXN];

inline void add_edge(int x, int y) {
    edge[x].push_back(y), edge[y].push_back(x);
}

inline void dfs1(int cur, int fa) {
    size[cur] = 1, dep[cur] = dep[fa] + 1, father[cur] = fa;
    for (int p = 0; p < edge[cur].size(); ++p) {
        int v = edge[cur][p];
        if (v != fa) {
            dfs1(v, cur), size[cur] += size[v];
            if (size[v] > size[son[cur]]) son[cur] = v;
        }
    }
}

inline void dfs2(int cur, int tp) {
    top[cur] = tp, in[cur] = ++ind, pos[ind] = cur;
    if (son[cur]) dfs2(son[cur], tp);
    for (int p = 0; p < edge[cur].size(); ++p) {
        int v = edge[cur][p];
        if (top[v] == 0) dfs2(v, v);
    }
    out[cur] = ind;
}

struct node {
    int left, right;
    int cnt;
} tree[MAXN * 35 + 1];

inline void insert(int &cur, long long l, long long r, int v) {
    tree[++cnt] = tree[cur], cur = cnt, tree[cur].cnt++;
    if (l == r) return ;
    long long mid = l + r >> 1;
    if (v <= mid) insert(tree[cur].left, l, mid, v);
    else insert(tree[cur].right, mid + 1, r, v);
}

inline int query(int x, int y, long long l, long long r, int v) {
    if (l == r) return (l ^ v);
    long long mid = l + r >> 1;
    if ((v ^ mid + 1) > (v ^ l)) {
        int ret = tree[tree[y].right].cnt - tree[tree[x].right].cnt;
        return ret ? (query(tree[x].right, tree[y].right, 
            mid + 1, r, v)) : (query(tree[x].left, tree[y].left, l, mid, v));
    } else {
        int ret = tree[tree[y].left].cnt - tree[tree[x].left].cnt;
        return ret ? (query(tree[x].left, tree[y].left, l, mid, v))
                   : (query(tree[x].right, tree[y].right, mid + 1, r, v));
    }
}

inline void solve_first_query(int u, int x) {
    W(query(root[in[u] - 1], root[out[u]], 0, MAXX, x)), write_char('\n');
}

inline void solve_second_query(int u, int v, int x) {
    int ans = 0;
    while (top[u] != top[v])
        (dep[top[u]] > dep[top[v]])
         ? (ans = std::max(ans, query(root[in[top[u]] - 1], root[in[u]]
            , 0, MAXX, x)), u = father[top[u]])
         : (ans = std::max(ans, query(root[in[top[v]] - 1], root[in[v]]
            , 0, MAXX, x)), v = father[top[v]]);
    (dep[u] > dep[v])
     ? (ans = std::max(ans, query(root[in[v] - 1], root[in[u]], 0, MAXX, x)))
     : (ans = std::max(ans, query(root[in[u] - 1], root[in[v]], 0, MAXX, x)));
    W(ans), write_char('\n');
}

inline void solve() {
    R(n), R(m);
    for (int i = 1; i <= n; ++i) R(v[i]);
    for (int i = 1; i < n; ++i) R(x), R(y), add_edge(x, y);
    dfs1(1, 0), dfs2(1, 1), root[0] = 0;
    for (int i = 1; i <= n; ++i)
        root[i] = root[i - 1], insert(root[i], 0, MAXX, v[pos[i]]);
    while (m--) {
        R(type);
        if (type == 1) R(x), R(y), solve_first_query(x, y);
        else R(x), R(y), R(z), solve_second_query(x, y, z);
    }
}

int main() {
    freopen("xor.in", "r", stdin);
    freopen("xor.out", "w", stdout);
    solve();
    flush();
    return 0;
}

你可能感兴趣的:(NOIP解题报告,树链剖分,trie)