1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) x&(-x)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
const int maxn = 3e5+100;
int n, q;
int tot, dep[maxn], siz[maxn], id[maxn], fa[maxn], top[maxn], son[maxn];
vector<int>vec[maxn];
void dfs1(int x, int f){
fa[x] = f;
dep[x] = dep[f] + 1;
siz[x] = 1;
for(auto v: vec[x]){
if(v == f) continue;
dfs1(v, x);
siz[x] += siz[v];
if(siz[v] > siz[son[x]])
son[x] = v;
}
}
void dfs2(int x, int tp){
id[x] = ++ tot;
top[x] = tp;
if(son[x]) dfs2(son[x], tp);
for(auto v: vec[x]){
if(v == fa[x] || v == son[x]) continue;
dfs2(v, v);
}
}
struct tree {
ll sum[4];
ll add[4];
}z[maxn*4];
void pushdown(int rt, int l, int r, int t){
int mid = (l + r) >> 1;
if(z[rt].add[t] != 0){
ll k = z[rt].add[t];
z[rt<<1].add[t] += k;
z[rt<<1|1].add[t] += k;
z[rt<<1].sum[t] += k*(mid-l+1);
z[rt<<1|1].sum[t] += k*(r-mid);
z[rt].add[t] = 0;
}
}
void modify(int rt, int l, int r, int t, int nowl, int nowr, ll val){
if(nowl <= l && r <= nowr){
z[rt].sum[t] += val*(r-l+1);
z[rt].add[t] += val;
return ;
}
int mid = (l + r) >> 1;
pushdown(rt, l, r, t);
if(nowl <= mid) modify(lson, t, nowl, nowr, val);
if(nowr > mid) modify(rson, t, nowl, nowr, val);
z[rt].sum[t] = z[rt<<1].sum[t] + z[rt<<1|1].sum[t];
}
int getlca(int x, int y){
for(;top[x] != top[y];){
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
return x;
}
void change(int x, int y){
//cout << "new change" << endl;
int len = dep[x] + dep[y] - 2*dep[getlca(x, y)] + 1;
int L = 1, R = len;
//cout << "L R " << L << " " << R << endl;
for(;top[x] != top[y];){
if(dep[top[x]] > dep[top[y]]){
ll val = id[top[x]] - L;
modify(1, 1, n, 1, id[top[x]], id[x], 1);
modify(1, 1, n, 2, id[top[x]], id[x], val);
modify(1, 1, n, 3, id[top[x]], id[x], val*val);
L += dep[x] - dep[top[x]] + 1;
x = fa[top[x]];
}
else {
ll val = id[top[y]] + R;
modify(1, 1, n, 1, id[top[y]], id[y], 1);
modify(1, 1, n, 2, id[top[y]], id[y], val);
modify(1, 1, n, 3, id[top[y]], id[y], val*val);
R -= dep[y] - dep[top[y]] + 1;
y = fa[top[y]];
}
}
if(dep[x] < dep[y]){
ll val = id[x] - L;
modify(1, 1, n, 1, id[x], id[y], 1);
modify(1, 1, n, 2, id[x], id[y], val);
modify(1, 1, n, 3, id[x], id[y], val*val);
}
else {
ll val = id[y] + R;
modify(1, 1, n, 1, id[y], id[x], 1);
modify(1, 1, n, 2, id[y], id[x], val);
modify(1, 1, n, 3, id[y], id[x], val*val);
}
}
ll query(int rt, int l, int r, int t, int pos){
//cout << "query " << l << " " << r << " " << t << " " <<z[rt].sumendl;
if(l == r){
return z[rt].sum[t];
}
int mid = (l + r) >> 1;
pushdown(rt, l, r, t);
if(pos <= mid) return query(lson, t, pos);
else return query(rson, t, pos);
}
int main(){
scanf("%d",&n);
for(int i = 1;i < n;i ++){
int x, y;
scanf("%d %d",&x,&y);
vec[x].push_back(y);
vec[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 0);
scanf("%d",&q);
for(int i = 1;i <= q;i ++){
int op, a, b, x;
scanf("%d",&op);
if(op == 1){
scanf("%d %d",&a,&b);
change(a, b);
}
if(op == 2){
scanf("%d",&x);
ll ans1 = query(1, 1, n, 1, id[x]);
ll ans2 = query(1, 1, n, 2, id[x]);
ll ans3 = query(1, 1, n, 3, id[x]);
printf("%lld\n", ans1*id[x]*id[x] - 2*ans2*id[x] + ans3);
}
}
return 0;
}
|