#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 3e5 + 10, inf = 0x3f3f3f3f;
int f[N], ch[N][3], bin[N], tot, bintot, n, tagRev[N], q;
inline int max(int x, int y) {return x > y ? x : y;}
inline int min(int x, int y) {return x < y ? x : y;}
struct M1
{
int t[2][2];
M1() {t[0][0] = 1, t[0][1] = 0, t[1][0] = 0, t[1][1] = 1;}
M1(int a, int b, int c, int d) {t[0][0] = a, t[0][1] = b, t[1][0] = c, t[1][1] = d;}
inline friend M1 operator * (M1 x, M1 y) {return M1(x.t[0][0] * y.t[0][0], x.t[0][0] * y.t[0][1] + x.t[0][1] * y.t[1][1], 0, x.t[1][1] * y.t[1][1]);}
inline friend M1 operator + (M1 x, M1 y) {return M1(x.t[0][0] + y.t[0][0], x.t[0][1] + y.t[0][1], 0, x.t[1][1] + y.t[1][1]);}
};
struct M2
{
int t[2][2];
M2() {t[0][0] = 0, t[0][1] = -inf, t[1][0] = -inf, t[1][1] = 0;}
M2(int a, int b, int c, int d) {t[0][0] = a, t[0][1] = b, t[1][0] = c, t[1][1] = d;}
inline friend M2 operator * (M2 x, M2 y) {return M2(max(x.t[0][0] + y.t[0][0], x.t[0][1] + y.t[1][0]), max(x.t[0][0] + y.t[0][1], x.t[0][1] + y.t[1][1]), max(x.t[1][0] + y.t[0][0], x.t[1][1] + y.t[1][0]), max(x.t[1][0] + y.t[0][1], x.t[1][1] + y.t[1][1]));}
inline friend M2 operator + (M2 x, M2 y) {M2 res(-inf, -inf, -inf, -inf); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) res.t[i][j] = max(x.t[i][j], y.t[i][j]); return res;}
};
struct M3
{
int t[2][2];
M3() {t[0][0] = 0, t[0][1] = inf, t[1][0] = inf, t[1][1] = 0;}
M3(int a, int b, int c, int d) {t[0][0] = a, t[0][1] = b, t[1][0] = c, t[1][1] = d;}
inline friend M3 operator * (M3 x, M3 y) {return M3(min(x.t[0][0] + y.t[0][0], x.t[0][1] + y.t[1][0]), min(x.t[0][0] + y.t[0][1], x.t[0][1] + y.t[1][1]), min(x.t[1][0] + y.t[0][0], x.t[1][1] + y.t[1][0]), min(x.t[1][0] + y.t[0][1], x.t[1][1] + y.t[1][1]));}
inline friend M3 operator + (M3 x, M3 y) {M3 res(inf, inf, inf, inf); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) res.t[i][j] = min(x.t[i][j], y.t[i][j]); return res;}
};
struct Data
{
M1 x; M2 y; M3 z;
Data(int op) {x = M1(0, 0, 0, 0), y = M2(-inf, -inf, -inf, -inf), z = M3(inf, inf, inf, inf);}
Data() {x = M1(), y = M2(), z = M3();}
inline friend Data operator * (Data X, Data Y) {Data res = Data(); res.x = X.x * Y.x, res.y = X.y * Y.y, res.z = X.z * Y.z; return res;}
inline friend Data operator + (Data X, Data Y) {Data res = Data(); res.x = X.x + Y.x, res.y = X.y + Y.y, res.z = X.z + Y.z; return res;}
}tag[N], val[N], _tag[N], _val[N], cval[N];
void Del(int x)
{
f[x] = ch[x][0] = ch[x][1] = ch[x][2] = tagRev[x] = 0;
val[x] = _val[x] = cval[x] = Data(0);
tag[x] = _tag[x] = Data();
bin[++bintot] = x;
}
int New()
{
if (bintot) return bin[bintot--];
return ++tot;
}
void PushUp(int x)
{
if (x > n)
{
_val[x] = _val[ch[x][0]] + val[ch[x][0]] + _val[ch[x][1]] + val[ch[x][1]];
}
else
{
val[x] = val[ch[x][0]] + cval[x] + val[ch[x][1]];
_val[x] = _val[ch[x][0]] + _val[ch[x][2]] + _val[ch[x][1]];
}
}
bool IsRoot(int x) {return !x || (ch[f[x]][0] != x && ch[f[x]][1] != x) || ((f[x] > n) != (x > n));}
bool Dir(int x) {return ch[f[x]][1] == x;}
void Rotate(int x)
{
int y = f[x], z = f[y], k = Dir(x);
ch[z][(ch[z][2] == y) ? 2 : Dir(y)] = x;
ch[y][k] = ch[x][!k], ch[x][!k] = y;
f[ch[y][k]] = y, f[y] = x, f[x] = z;
PushUp(y), PushUp(x);
ch[0][0] = ch[0][1] = ch[0][2] = f[0] = 0;
}
void PushRev(int x)
{
tagRev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
}
void Push(int x, Data v)
{
tag[x] = tag[x] * v;
val[x] = val[x] * v;
cval[x] = cval[x] * v;
}
void _Push(int x, Data v)
{
_val[x] = _val[x] * v;
_tag[x] = _tag[x] * v;
}
void PushDown(int x)
{
if (x > n)
{
if (ch[x][0] <= n) Push(ch[x][0], _tag[x]);
if (ch[x][1] <= n) Push(ch[x][1], _tag[x]);
_Push(ch[x][0], _tag[x]), _Push(ch[x][1], _tag[x]);
tag[x] = _tag[x] = Data();
}
else
{
Push(ch[x][0], tag[x]), Push(ch[x][1], tag[x]), tag[x] = Data();
_Push(ch[x][0], _tag[x]), _Push(ch[x][1], _tag[x]), _Push(ch[x][2], _tag[x]), _tag[x] = Data();
}
if (tagRev[x])
{
PushRev(ch[x][0]), PushRev(ch[x][1]);
tagRev[x] = 0;
}
}
void PushAll(int x)
{
if (!IsRoot(x)) PushAll(f[x]);
PushDown(x);
if (x > n && (!ch[x][0] || !ch[x][1]) && f[x] > n)
{
if (!ch[x][0] && !ch[x][1]);
else f[ch[x][0] + ch[x][1]] = f[x], ch[f[x]][Dir(x)] = ch[x][0] + ch[x][1], PushUp(f[x]), Del(x);
}
}
void _PushAll(int x)
{
if (f[x]) _PushAll(f[x]);
PushDown(x);
if (x > n && (!ch[x][0] || !ch[x][1]) && f[x] > n)
{
if (!ch[x][0] && !ch[x][1]);
else f[ch[x][0] + ch[x][1]] = f[x], ch[f[x]][Dir(x)] = ch[x][0] + ch[x][1], PushUp(f[x]), Del(x);
}
}
void Splay(int x) // x > n or x <= n
{
PushAll(x);
for (int y = f[x]; y = f[x], !IsRoot(x); Rotate(x)) if (!IsRoot(y)) Rotate(Dir(y) == Dir(x) ? y : x);
}
void InsertInv(int x, int y)
{
PushDown(x), PushDown(y);
int z = New();
ch[z][0] = ch[x][2], ch[z][1] = y;
f[ch[x][2]] = z, f[y] = z;
ch[x][2] = z, f[z] = x;
PushUp(y), PushUp(z), PushUp(x);
}
void Delete(int x)
{
ch[f[x]][ch[f[x]][2] == x ? 2 : Dir(x)] = 0, x = f[x];
PushUp(x);
while (x > n)
{
int w = f[x];
if (!ch[x][0] && !ch[x][1]) ch[w][ch[w][2] == x ? 2 : Dir(x)] = 0, Del(x);
x = w, PushUp(x);
}
}
int Access(int x)
{
int p = 0;
_PushAll(x);
Splay(x);
if (ch[x][1])
{
InsertInv(x, ch[x][1]);
ch[x][1] = 0;
PushUp(x);
}
while (x)
{
Splay(x), p = f[x];
if (!p) return x;
Splay(p);
int y = f[p];
Splay(y);
Delete(x);
if (ch[y][1]) InsertInv(y, ch[y][1]);
ch[y][1] = x, f[x] = y, x = y;
PushUp(y);
}
return 0;
}
void MakeRoot(int x)
{
Access(x), Splay(x);
PushRev(x);
}
void Link(int x, int y)
{
MakeRoot(y);
InsertInv(x, y);
}
void Cut(int x, int y)
{
MakeRoot(x);
Access(y), Splay(x);
PushDown(x);
ch[x][1] = f[y] = 0;
PushUp(x);
}
int Find(int x)
{
while (!IsRoot(x)) x = f[x];
return x;
}
int FindFa(int x)
{
x = ch[x][0];
while (ch[x][1]) x = ch[x][1];
return x;
}
int rt = 1;
int P1[N], P2[N];
signed main()
{
// freopen("data.in", "r", stdin); freopen("data.out", "w", stdout);
ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q, tot = n;
for (int i = 1, x, y; i < n; i++) cin >> x >> y, P1[i] = x, P2[i] = y;
for (int i = 0; i < N; i++) val[i] = _val[i] = cval[i] = Data(0);
for (int i = 1; i <= n; i++)
{
int x; cin >> x;
cval[i].x = M1(1, x, 0, 0), cval[i].y = M2(0, x, -inf, -inf), cval[i].z = M3(0, x, inf, inf);
}
for (int i = 1; i < n; i++) Link(P1[i], P2[i]);
cerr << tot << '\n';
cin >> rt;
for (int i = 1; i <= q; i++)
{
int op; cin >> op;
if (op == 0)
{
int x, y; cin >> x >> y;
MakeRoot(rt), Access(x), Splay(x);
M1 r1(1, y, 0, 0); M2 r2(0, y, -inf, -inf); M3 r3(0, y, inf, inf);
Data t; t.x = r1, t.y = r2, t.z = r3;
cval[x] = cval[x] * t;
_Push(ch[x][2], t), Push(ch[x][2], t);
PushUp(x);
}
if (op == 1)
{
int x; cin >> x;
rt = x;
}
if (op == 2)
{
int x, y, z; cin >> x >> y >> z;
M1 r1(1, z, 0, 0); M2 r2(0, z, -inf, -inf); M3 r3(0, z, inf, inf);
MakeRoot(x), Access(y), Splay(y);
Data t; t.x = r1, t.y = r2, t.z = r3;
Push(y, t);
}
if (op == 3)
{
int x; cin >> x;
MakeRoot(rt), Access(x), Splay(x);
cout << min(_val[ch[x][2]].z.t[0][1], cval[x].z.t[0][1]) << '\n';
}
if (op == 4)
{
int x; cin >> x;
MakeRoot(rt), Access(x), Splay(x);
cout << max(_val[ch[x][2]].y.t[0][1], cval[x].y.t[0][1]) << '\n';
}
if (op == 5)
{
int x, y; cin >> x >> y;
MakeRoot(rt), Access(x), Splay(x);
M1 r1(1, y, 0, 1); M2 r2(0, -inf, -inf, y); M3 r3(0, inf, inf, y);
Data t; t.x = r1, t.y = r2, t.z = r3;
cval[x] = cval[x] * t;
_Push(ch[x][2], t), Push(ch[x][2], t);
PushUp(x);
}
if (op == 6)
{
int x, y, z; cin >> x >> y >> z;
MakeRoot(x), Access(y), Splay(y);
M1 r1(1, z, 0, 1); M2 r2(0, -inf, -inf, z); M3 r3(0, inf, inf, z);
Data t; t.x = r1, t.y = r2, t.z = r3;
Push(y, t);
}
if (op == 7)
{
int x, y; cin >> x >> y;
MakeRoot(y), Access(x), Splay(x);
cout << val[x].z.t[0][1] << '\n';
}
if (op == 8)
{
int x, y; cin >> x >> y;
MakeRoot(y), Access(x), Splay(x);
cout << val[x].y.t[0][1] << '\n';
}
if (op == 9)
{
int x, y; cin >> x >> y;
MakeRoot(rt), Access(y), Splay(x);
if (Find(y) == x) continue;
Access(x), Splay(x);
int fa = FindFa(x);
Cut(x, fa), Link(x, y);
}
if (op == 10)
{
int x, y; cin >> x >> y;
MakeRoot(y), Access(x), Splay(x);
cout << val[x].x.t[0][1] << '\n';
}
if (op == 11)
{
int x; cin >> x;
MakeRoot(rt), Access(x), Splay(x);
cout << _val[ch[x][2]].x.t[0][1] + cval[x].x.t[0][1] << '\n';
}
}
cerr << "\n" << clock() / 1000.0 << "s\n";
return 0;
}剪贴板
剪贴板 2ucej40a
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新