XOR TREE【牛客练习赛58 F】【树链剖分】

题目链接


 这个问题很容易想到之间的关系,假设现在所要查询的这条链上有V1、V2、…… VK个点,那么第i个点的贡献在抑或中出现的次数XOR为\large F(i) = (K - i) * i + i - 1

  • 当K为偶数时候,F(i)恒定为奇数
  • 当K为奇数的时候,F(i)在i为偶数的时候F(i)为奇数
  • 只有F(i)为奇数的时候,在抑或XOR中才有作用

  于是,如果K为偶数的时候,我们直接求这条链上所有值的抑或XOR和即可,树链剖分就可以很好的维护了。

  如果K为奇数的时候,我们需要求第2、4、6、……这几个偶数点的抑或XOR和。如果我们直接在线段树上维护当然是不行的,因为dfs序号是没有这样的独立性的,可能会有两个同时为偶数dfs序的点却是连接在一起的。

  所以考虑深度,会发现,深度奇偶不同的点一定是不相连接的,并且中间只隔了一个点。于是,我们这里开两个线段树,维护一下每个奇偶深度的抑或XOR和。

  这样,我们查询的时候,只用查询于两个结点的deep的奇偶不同的点的抑或和即可。

  因为是奇数个点,所以也代表了两个查询结点一定是在深度上同奇偶。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
//#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define MAX_3(a, b, c) max(a, max(b, c))
#define Rabc(x) x > 0 ? x : -x
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
inline int read()
{
    int x=0; char c=getchar();
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') { x=(x<<1)+(x<<3)+c-'0'; c=getchar(); }
    return x;
}
const int maxN = 2e5 + 7;
int N, Q, a[maxN], head[maxN], cnt;
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN << 1];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int siz[maxN], deep[maxN], Wson[maxN], fa[maxN];
void dfs_1(int u, int father)
{
    fa[u] = father; deep[u] = deep[father] + 1; Wson[u] = 0; siz[u] = 1;
    int maxx = 0;
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(v == father) continue;
        dfs_1(v, u);
        siz[u] += siz[v];
        if(maxx < siz[v])
        {
            maxx = siz[v];
            Wson[u] = v;
        }
    }
}
int top[maxN], dfn[maxN], tot, rid[maxN];
void dfs_2(int u, int topy)
{
    top[u] = topy;
    dfn[u] = ++tot; rid[tot] = u;
    if(Wson[u]) dfs_2(Wson[u], topy);
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(v == fa[u] || v == Wson[u]) continue;
        dfs_2(v, v);
    }
}
int tree[maxN << 2][3] = {0};
inline void pushup(int rt)
{
    tree[rt][2] = tree[lsn][2] ^ tree[rsn][2];
    tree[rt][0] = tree[lsn][0] ^ tree[rsn][0];
    tree[rt][1] = tree[lsn][1] ^ tree[rsn][1];
}
void buildTree(int rt, int l, int r)
{
    if(l == r)
    {
        tree[rt][deep[rid[l]] & 1] = a[rid[l]];
        tree[rt][2] = a[rid[l]];
        return;
    }
    int mid = HalF;
    buildTree(Lson); buildTree(Rson);
    pushup(rt);
}
void update(int rt, int l, int r, int qx, int val)
{
    if(l == r) { tree[rt][deep[rid[l]] & 1] = val; tree[rt][2] = val; return; }
    int mid = HalF;
    if(qx <= mid) update(Lson, qx, val);
    else update(Rson, qx, val);
    pushup(rt);
}
int s[3] = {0};
void query(int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r)
    {
        s[0] ^= tree[rt][0];
        s[1] ^= tree[rt][1];
        s[2] ^= tree[rt][2];
        return;
    }
    int mid = HalF;
    if(qr <= mid) query(QL);
    else if(ql > mid) query(QR);
    else { query(QL); query(QR); }
}
inline int _LCA(int u, int v)
{
    while(top[u] ^ top[v])
    {
        if(deep[top[u]] < deep[top[v]]) swap(u, v);
        u = fa[top[u]];
    }
    if(deep[u] > deep[v]) swap(u, v);
    return u;
}
inline int Range_Query(int u, int v)
{
    int ans = 0, lca = _LCA(u, v), len, op = (deep[u] & 1) ^ 1;
    int dis = deep[u] + deep[v] - 2 * deep[lca] + 1; dis &= 1;
    if(dis)
    {
        while(top[u] ^ top[v])
        {
            if(deep[top[u]] < deep[top[v]]) swap(u, v);
            len = deep[u] - deep[top[u]] + 1; len &= 1;
            s[0] = s[1] = s[2] = 0;
            query(1, 1, N, dfn[top[u]], dfn[u]);
            ans ^= s[op];
            u = fa[top[u]];
        }
        if(deep[u] < deep[v]) swap(u, v);
        len = deep[u] - deep[v] + 1; len &= 1;
        s[0] = s[1] = s[2] = 0;
        query(1, 1, N, dfn[v], dfn[u]);
        ans ^= s[op];
    }
    else
    {
        s[2] = 0;
        while(top[u] ^ top[v])
        {
            if(deep[top[u]] < deep[top[v]]) swap(u, v);
            query(1, 1, N, dfn[top[u]], dfn[u]);
            u = fa[top[u]];
        }
        if(deep[u] < deep[v]) swap(u, v);
        query(1, 1, N, dfn[v], dfn[u]);
        ans = s[2];
    }
    return ans;
}
inline void init()
{
    cnt = tot = 0;
    for(int i=1; i<=N; i++) head[i] = -1;
}
int main()
{
    N = read(); Q = read();
    init();
    for(int i=1; i<=N; i++) a[i] = read();
    for(int i=1, u, v; i

 

你可能感兴趣的:(树链剖分,数据结构,线段树)