XOR 题解

· · 题解

废话不多说,直接看题。

Subtask1:

直接暴力模拟,不多说了,送分的。

Subtask2:

在这一档部分分里,我们发现m的值急剧增大,达到了10^6级别。显然,我们需要一种O(1)的做法。

不妨设询问的点为x,我们先看以1为根的情况。在以1为根时,我们单独拉出来以条从1到x的路径。我们发现,在这条路径上的所有点,它们的子树都包含x。并且,x也只被这些点的子树所包含。

因为x\operatorname{xor} x=0,所以,我们只需要知道x被包含的次数即可。

那么这个次数是什么呢?

深度!!!

那么,在以1为根时,所有深度为奇数的点就全都被包含,深度为偶数的就不包含。

再来考虑换根。我们直接考虑换根后的深度的奇偶性。不妨设将y换为了根。这样,对深度的影响无非就是全被减去y的深度。不用担心深度出现负数,因为负数并不会影响之前的讨论。

其实也可以换一种角度理解。根无论是哪个点,对最终答案有贡献的点一定是一个隔着一个的。这样一来,答案就只有两种情况。恰好,同样答案的点可以用深度来分类。那么就可以用深度做了。

具体的,开一个数组ans[2]记录答案,depth数组表示深度。查询时输出ans[depth[y]&1],修改时也修改一下即可。

Subtask3:

这一档部分分是留给会树剖的同学的暴力分的。

(厚颜无耻的宣传一波自己的树剖博客:重链剖分)

考虑到1操作较少,每次重新剖分即可。

Subtask4:

对于操作3,是经典的换根意义下的树剖例题,设根为z,查询的点为x和y,只需输出\operatorname{LCA}(x,y),\operatorname{LCA}(x,z),\operatorname{LCA}(z,y)中深度最大的哪个点。

对于操作4,由于路径与根无关,直接正常写。

对于操作5,只需讨论一下查询点与根的方向关系即可,比较简单,画图就可以发现查询的是整棵树挖去该查询点的一棵子树的异或值,不展开。

对于操作1,按调用Subtask2的方法进行处理。

那么,对于操作2,只需要维护一下操作4中的线段树即可。

结合Subtask2起来,即可拿到此档部分分。

Subtask5&6:

这两档部分分放在一起讲。

回顾一下单纯的树剖,在处理操作4时,我们跳链的过程带着两个\log,再看一下数据范围,在被构造数据卡的情况下,是不可能过的。我们需要优化。

怎么优化呢?

我们考虑前缀和的思想,假如我们把点i到1号点的路径存下来,令其为G_i,那么,4操作就可以看成是G_x \operatorname{xor} G_y \operatorname{xor} V_{\operatorname{LCA}(x,y)}。

我们再来看一看怎么维护这个信息。

在单点修改时,我们发现,有影响的只是该点的子树。具体来说,所有位于该点子树中的点的G值都要先异或上原来的值,再异或上现在的值。

那么问题就变成了一个单点查询,区间修改。

至此,4操作被我们成功的优化至了一个\log。

另外,假如你会树剖,想到了这个优化方法,但没有想到Subtask2的结论,努力卡一卡常,依然能拿到Subtask5的分。

最后要注意,由于洛谷的数据点大小限制(也可能是我造数据能力太差),在Subtask4和正解之间拉不开时间上的差距,因此,本题卡常。所以,树剖中的所有维护区间信息的工具,均要使用树状数组,不然不保证能够通过。

附上AC代码(比较丑,见谅):

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
using namespace std;

#define _N 1000001

#define lowbit(p) (p&-p)

struct Side
{
    int to;
    int next;
    Side()
    {
        to = next = 0;
    }
    Side(int a, int b)
    {
        to = a;
        next = b;
    }
};

int n, m, q;

int v[_N];
int res[2];

int root = 1;
int ncnt = 0;
int fa[_N];
int heavyson[_N];
int siz[_N];
int depth[_N];
int top[_N];
int id[_N];
int dfn[_N];

int treeval[_N];
int treesum[_N];

int sidecnt = 0;
int head[_N];
Side sides[2 * _N];

void inline Swap(int& a, int& b)
{
    int t = a;
    a = b;
    b = t;
}

void inline adde(int x, int y)
{
    sides[++sidecnt] = Side(y, head[x]);
    head[x] = sidecnt;
}

void xor_val(int p, int k)
{
    while (p <= n)
    {
        treeval[p] ^= k;
        p += lowbit(p);
    }
}
int query_val(int p)
{
    int ans = 0;
    while (p != 0)
    {
        ans ^= treeval[p];
        p -= lowbit(p);
    }
    return ans;
}
void xor_sum(int p, int k)
{
    while (p <= n)
    {
        treesum[p] ^= k;
        p += lowbit(p);
    }
}
int query_sum(int p)
{
    int ans = 0;
    while (p != 0)
    {
        ans ^= treesum[p];
        p -= lowbit(p);
    }
    return ans;
}

void update_treesum(int l, int r, int k)
{
    xor_sum(l, k);
    xor_sum(r + 1, k);
}
void update_treeval(int p, int k)
{
    xor_val(p, k);
}
int query_treesum(int p)
{
    return query_sum(p);
}
int query_treeval(int l, int r)
{
    return query_val(r) ^ query_val(l - 1);
}

void cut(int x, int father, int deep)
{
    siz[x] = 1;
    fa[x] = father;
    depth[x] = deep;
    res[depth[x] & 1] ^= v[x];
    int next = head[x];
    while (next != 0)
    {
        int y = sides[next].to;
        if (y != father)
        {
            cut(y, x, deep + 1);
            siz[x] += siz[y];
            if (siz[y] > siz[heavyson[x]])
            {
                heavyson[x] = y;
            }
        }
        next = sides[next].next;
    }
}
void inf(int x, int topfather)
{
    top[x] = topfather;
    id[x] = ++ncnt;
    dfn[ncnt] = x;
    if (heavyson[x] == 0)
    {
        return;
    }
    inf(heavyson[x], topfather);
    int next = head[x];
    while (next != 0)
    {
        int y = sides[next].to;
        if (y != fa[x] && y != heavyson[x])
        {
            inf(y, y);
        }
        next = sides[next].next;
    }
}

int lca(int x, int y)
{
    while (top[x] != top[y])
    {
        if (depth[top[x]] < depth[top[y]])
        {
            Swap(x, y);
        }
        x = fa[top[x]];
    }
    if (depth[x] > depth[y])
    {
        Swap(x, y);
    }
    return x;
}
int selca(int x, int y)
{
    while (top[x] != top[y])
    {
        if (depth[top[x]] < depth[top[y]])
        {
            Swap(x, y);
        }
        if (fa[top[x]] == y)
        {
            return top[x];
        }
        x = fa[top[x]];
    }
    if (depth[x] > depth[y])
    {
        Swap(x, y);
    }
    return heavyson[x];
}

int makeroot(int x)
{
    root = x;
    return res[depth[x] & 1];
}
void update_val(int x, int y)
{
    res[depth[x] & 1] ^= v[x] ^ y;
    update_treesum(id[x], id[x] + siz[x] - 1, v[x] ^ y);
    update_treeval(id[x], v[x] ^ y);
    v[x] = y;
}
int lca(int x, int y, int k)
{
    int a = lca(x, y);
    int b = lca(x, root);
    int c = lca(y, root);
    if (depth[a] > depth[b])
    {
        if (depth[a] > depth[c])
        {
            return a;
        }
        else
        {
            return c;
        }
    }
    else
    {
        if (depth[b] > depth[c])
        {
            return b;
        }
        else
        {
            return c;
        }
    }
}
int query_path(int x, int y)
{
    return query_treesum(id[x]) ^ query_treesum(id[y]) ^ v[lca(x, y)];
}
int query_sub(int x)
{
    if (x == root)
    {
        return query_treeval(id[1], id[1] + siz[1] - 1);
    }
    int a = lca(x, root);
    if (a == x)
    {
        int b = selca(root, x);
        return query_treeval(id[1], id[1] + siz[1] - 1) ^ query_treeval(id[b], id[b] + siz[b] - 1);
    }
    else
    {
        return query_treeval(id[x], id[x] + siz[x] - 1);
    }
}


int main()
{
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &v[i]);
    }
    for (int i = 1; i < n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        adde(x, y);
        adde(y, x);
    }
    cut(1, 0, 1);
    inf(1, 1);
    for (int i = 1; i <= n; i++)
    {
        update_treeval(id[i], v[i]);
        update_treesum(id[i], id[i] + siz[i] - 1, v[i]);
    }
    for (int i = 1; i <= m + q; i++)
    {
        int opt, x, y;
        scanf("%d", &opt);
        switch (opt)
        {
        case 1:
            scanf("%d", &x);
            printf("%d\n", makeroot(x));
            break;
        case 2:
            scanf("%d%d", &x, &y);
            update_val(x, y);
            break;
        case 3:
            scanf("%d%d", &x, &y);
            printf("%d\n", lca(x, y, root));
            break;
        case 4:
            scanf("%d%d", &x, &y);
            printf("%d\n", query_path(x, y));
            break;
        case 5:
            scanf("%d", &x);
            printf("%d\n", query_sub(x));
            break;
        }
    }
    return 0;
}