V x y把點x的權值變成y
E x把有根樹的根變為x
Q x查詢點x的子樹的最小值

BY BPM

# Code

``````#include <stdio.h>
#define rep(i, st, ed) for (int i = st; i <= ed; i  = 1)
#define erg(i, st) for (int i = ls[st]; i; i = e[i].next)
#define min(x, y) (x)<(y)?(x):(y)
#define max(x, y) (x)>(y)?(x):(y)
#define INF 0x3f3f3f3f
#define N 100001
#define E N
struct edge{int x, y, next;}e[E];
struct treeNode{
int l, r, mn;
bool operator <(treeNode b){return (l >= b.l)&&(r < b.r)||(l > b.l)&&(r <= b.r);}
bool operator <=(treeNode b){return (l >= b.l)&&(r <= b.r);}
}t[N << 2 | 1], rc[N];
int father[N], ls[N], v[N];
int edgeCnt = 0, rCnt = 0;
int x = 0; char ch = getchar();
while (ch < '0' || ch > '9'){ch = getchar();}
while (ch <= '9' && ch >= '0'){x = (x << 1)   (x << 3)   ch - '0'; ch = getchar();}
return x;
}
inline void addEdge(int x, int y){
e[   edgeCnt] = (edge){x, y, ls[x]}; ls[x] = edgeCnt;
}
inline void modify(int now, int x, int v){
if (t[now].l == t[now].r){
t[now].mn = v;
return;
}
int mid = (t[now].l   t[now].r) >> 1;
if (x <= mid){modify(now << 1, x, v);}
if (x > mid){modify(now << 1 | 1, x, v);}
t[now].mn = min(t[now << 1].mn, t[now << 1 | 1].mn);
}
inline int query(int now, int l, int r){
if (l > r){return INF;}
if (t[now].l == l && t[now].r == r){return t[now].mn;}
int mid = (t[now].l   t[now].r) >> 1;
if (r <= mid){return query(now << 1, l, r);}
if (l > mid){return query(now << 1 | 1, l, r);}
return min(query(now << 1, l, mid), query(now << 1 | 1, mid   1, r));
}
inline void build(int now, int l, int r){
t[now] = (treeNode){l, r, 0};
if (l == r){return ;}
int mid = (l   r) >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid   1, r);
}
inline void dfs(int fa, int now){
rc[now].mn = rc[now].l = rc[now].r =    rCnt;
modify(1, rc[now].l, v[now]);
erg(i, now){
dfs(now, e[i].y);
rc[now].r = max(rc[now].r, rc[e[i].y].r);
}
}
int main(void){
rep(i, 1, n){
}
build(1, 1, n);
int root = 1;
dfs(0, root);
rep(i, 1, m){
char ch = getchar();
if (ch == 'V'){
modify(1, rc[x].l, y);
}else if (ch == 'Q'){
if (x == root){
printf("%d\n", query(1, 1, rCnt));
}else if (rc[root] < rc[x]){
int now;
erg(i, x){
if (rc[root] <= rc[e[i].y]){
now = e[i].y;
break;
}
}
printf("%d\n", min(query(1, 1, rc[now].l - 1), query(1, rc[now].r   1, rCnt)));
}else{
printf("%d\n", query(1, rc[x].l, rc[x].r));
}
}else if (ch == 'E'){
root = x;
}
}
return 0;
}``````