省队集训Day1 睡觉困难综合征

时间:2021-02-23 10:10:57

传送门:https://www.luogu.org/problem/show?pid=3613

【题解】

按二进制位分开,对于每一位,用“起床困难综合征”的方法贪心做。

写棵LCT,维护正反两种权值,每个维护2种,代表全0的输出和全1的输出。

然后直接上LCT即可。

权值的合并有点trick,可以参考代码,需要压位。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 1e5 + ;
const int mod = 1e9+; int n, m, K; struct node {
ull p0, p1;
node () {}
node (ull p0, ull p1) : p0(p0), p1(p1) {}
friend node operator + (node a, node b) {
return node( (a.p0 & b.p1) | ((~a.p0) & b.p0), (a.p1 & b.p1) | ((~a.p1) & b.p0) );
}
}; inline node deal(int op, ull x) {
if(op == ) return node(, x);
if(op == ) return node(x, ull(-));
if(op == ) return node(x, ~x);
} struct LCT {
int ch[M][], fa[M];
bool rev[M];
node w[M][], val[M];
# define ls ch[x][]
# define rs ch[x][]
inline void up(int x) {
if(!x) return ;
w[x][] = w[x][] = val[x];
if(ls) w[x][] = w[ls][] + w[x][], w[x][] = w[x][] + w[ls][];
if(rs) w[x][] = w[x][] + w[rs][], w[x][] = w[rs][] + w[x][];
}
inline void pushrev(int x) {
if(!x) return ;
rev[x] ^= ;
swap(ch[x][], ch[x][]);
swap(w[x][], w[x][]);
}
inline void down(int x) {
if(!x || !rev[x]) return ;
pushrev(ls); pushrev(rs);
rev[x] = ;
}
# undef ls
# undef rs
inline bool isrt(int x) {
return ch[fa[x]][] != x && ch[fa[x]][] != x;
}
inline void rotate(int x) {
int y = fa[x], z = fa[y], ls = ch[y][] == x, rs = ls^;
if(!isrt(y)) ch[z][ch[z][] == y] = x;
fa[ch[x][rs]] = y, fa[y] = x, fa[x] = z;
ch[y][ls] = ch[x][rs]; ch[x][rs] = y;
up(y); up(x);
}
int st[M];
inline void splay(int x) {
int stn = , tx = x;
while(!isrt(tx)) st[++stn] = tx, tx = fa[tx];
st[++stn] = tx;
for (int i=stn; i; --i) down(st[i]);
while(!isrt(x)) {
int y = fa[x], z = fa[y];
if(!isrt(y)) {
if((ch[z][] == y)^(ch[y][] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
} inline int access(int x) {
int t = ;
for (; x; t = x, x = fa[x]) {
splay(x);
ch[x][] = t;
up(x);
}
return t;
} inline void makeroot(int x) {
access(x); splay(x); pushrev(x);
} inline void link(int x, int y) {
makeroot(y); fa[y] = x;
}
}T; # define bit(x, i) (((x) >> (i)) & ) int main() {
// freopen("sleep.in", "r", stdin);
// freopen("sleep.out", "w", stdout);
scanf("%d%d%d", &n, &m, &K);
for (int i=, op; i<=n; ++i) {
ull x;
scanf("%d%llu", &op, &x);
T.val[i] = T.w[i][] = T.w[i][] = deal(op, x);
T.ch[i][] = T.ch[i][] = T.fa[i] = T.rev[i] = ;
}
for (int i=, u, v; i<n; ++i) {
scanf("%d%d", &u, &v);
T.link(u, v);
}
int op, x, y; ull z, tx, ty, ans, t;
while(m--) {
scanf("%d%d%d%llu", &op, &x, &y, &z);
if(op == ) {
ans = ; t = ;
T.makeroot(x); T.access(y); T.splay(y);
tx = T.w[y][].p0, ty = T.w[y][].p1;
for (int i=; ~i; --i) {
if(bit(tx, i)) ans |= (1ull << i);
else if(bit(ty, i)) {
if(t + (1ull << i) <= z) t = t + (1ull << i), ans |= (1ull << i);
}
}
printf("%llu\n", ans);
} else {
T.splay(x);
T.val[x] = deal(y, z);
T.up(x);
}
}
return ;
}