Description
给定n个点以及每个点的权值,要你处理接下来的m个操作。操作有4种。操作从0到3编号。点从1到n编号。
0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。
1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接。
2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。
3:后接两个整数(x,y),代表将点x上的权值变成y。
Solution
重新复习LCT。
几个操作:
splay(u)
:将u旋转到它所在链对应的splay的根节点
access(u)
:将u到根节点的路径变成偏爱路径且u的下面没有偏爱边
makeroot(u)
:将u作为树的根。实现:先access,然后将其splay到根节点并修改rev
cut(u,v)
:将u,v断开。实现:先makeroot(u),然后access(v),splay(v),断开v和左儿子(即u)的关系。
link(u,v)
:连接u,v。实现:先makeroot(u),然后使fa[u]=v
findroot(u)
:找到u所在树的根。实现:access(u),splay(u),然后不停地找左儿子。
最后求和的时候直接makeroot(u),access(v),splay(v),节点v的权值即为答案。
感觉以前写的时候思路挺乱的,其实LCT有很多细节需要注意,比如:
- splay(u)
中记得调用pushdown(u)
- rotate(u)
、·access(u)
和修改时记得调用maintain(u)
- 连接、断开时记得判断本来的连接状况
- 断开时记得双向都要断开
- 注意rotate(u)
中的旋转顺序(这个其实算是splay的注意事项)
- rotate(u)
中注意判断u
是否有祖父
- 在判断两个点
x,y
x
,
y
是否直接相连时,先在LCT中分离出该链,然后不仅要满足
y
y
的左子节点是,还要满足
x
x
<script type="math/tex" id="MathJax-Element-12">x</script>没有右子节点。
Source
/************************************************
* Au: Hany01
* Date: Mar 26th, 2018
* Prob: [BZOJ3282] Tree
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 300005;
int n, m, w[maxn];
struct LCT
{
int fa[maxn], ch[maxn][2], rev[maxn], sum[maxn];
#define dir(o) (ch[fa[o]][1] == o)
#define isrt(o) (ch[fa[o]][1] != o && ch[fa[o]][0] != o)
inline void pushdown(int o) {
if (rev[o])
rev[ch[o][0]] ^= 1, rev[ch[o][1]] ^= 1,
swap(ch[o][0], ch[o][1]), rev[o] = 0;
}
inline void maintain(int o) { sum[o] = sum[ch[o][0]] ^ sum[ch[o][1]] ^ w[o]; }
inline void rotate(int u)
{
int f = fa[u], gf = fa[f], d = dir(u);
ch[f][d] = ch[u][d ^ 1], fa[ch[u][d ^ 1]] = f;
fa[u] = gf;
if (!isrt(f)) ch[gf][dir(f)] = u;
ch[u][d ^ 1] = f, fa[f] = u;
maintain(f), maintain(u);
}
int stk[maxn], top;
inline void splay(int o)
{
stk[top = 1] = o;
for (int t = o; !isrt(t); t = fa[t]) stk[++ top] = fa[t];
while (top) pushdown(stk[top --]);
for ( ; !isrt(o); rotate(o))
if (!isrt(fa[o])) rotate(dir(o) == dir(fa[o]) ? fa[o] : o);
}
inline void access(int o) {
for (int t = 0; o; t = o, o = fa[o])
splay(o), ch[o][1] = t, maintain(o);
}
inline void makeroot(int o) { access(o), splay(o), rev[o] ^= 1; }
inline void link(int u, int v) { makeroot(u), fa[u] = v; }
inline void cut(int u, int v) {
makeroot(u), access(v), splay(v);
if (ch[v][0] == u) ch[v][0] = fa[u] = 0;
}
inline int findroot(int x) {
access(x), splay(x);
while (ch[x][0]) x = ch[x][0];
return x;
}
inline int getans(int u, int v) {
makeroot(u), access(v), splay(v);
return sum[v];
}
}lct;
int main()
{
#ifdef hany01
File("bzoj3282");
#endif
n = read(), m = read();
For(i, 1, n) w[i] = lct.sum[i] = read();
while (m --)
{
register int ty = read(), u = read(), v = read();
if (!ty) printf("%d\n", lct.getans(u, v));
else if (ty == 1) { if (lct.findroot(u) != lct.findroot(v)) lct.link(u, v); }
else if (ty == 2) lct.cut(u, v);
else lct.access(u), lct.splay(u), w[u] = v, lct.maintain(u);
}
return 0;
}
//彼黍离离,彼稷之苗。行迈靡靡,中心摇摇。知我者,谓我心忧;不知我者,谓我何求。悠悠苍天,此何人哉?
//彼黍离离,彼稷之穗。行迈靡靡,中心如醉。知我者,谓我心忧;不知我者,谓我何求。悠悠苍天,此何人哉?
//彼黍离离,彼稷之实。行迈靡靡,中心如噎。知我者,谓我心忧;不知我者,谓我何求。悠悠苍天,此何人哉?