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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #pragma GCC optimize(2) #define ll long long #define pii pair<int, int> using namespace std; const int N = 1400000 + 100; const int M = 1400100; int n, m, x, y;
vector <pii> e[N]; int op[N], U[N], L[N], R[N], X[N]; int cnt; ll ans[N];
int head[M * 2], Next[M * 2], tot, ver[M * 2];
inline int read(){ int s=0; char ch=getchar(); while(ch<'0'||ch>'9'){ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s; }
int Size[M], fa[M], deep[M], son[M]; int top[M], dfn[M], cnt1; void dfs1(int u, int pre){ Size[u] = 1; son[u] = 0; fa[u] = pre; deep[u] = deep[pre] + 1; for(int i = head[u]; i; i = Next[i]){ int v = ver[i]; if(v == pre) continue; dfs1(v, u); Size[u] += Size[v]; if(Size[v] > Size[son[u]]){ son[u] = v; } } } void dfs2(int u, int t){ top[u] = t; dfn[u] = ++cnt1; if(son[u]) dfs2(son[u], t); for(int i = head[u]; i; i = Next[i]){ int v = ver[i]; if(v != son[u] && v != fa[u]){ dfs2(v, v); } } }
struct node{ int l, r; ll sum, add; }tree[M * 4][2]; void pushup(int x, int id){ tree[x][id].sum = tree[x * 2][id].sum + tree[x * 2 + 1][id].sum ; } void pushdown(int x, int id){ if(tree[x][id].add){ tree[x * 2][id].sum += tree[x][id].add * (tree[x * 2][id].r - tree[x * 2][id].l + 1); tree[x * 2 + 1][id].sum += tree[x][id].add * (tree[x * 2 + 1][id].r - tree[x * 2 + 1][id].l + 1); tree[x * 2][id].add += tree[x][id].add; tree[x * 2 + 1][id].add += tree[x][id].add; tree[x][id].add = 0; } } void build(int x, int l, int r, int id){ tree[x][id].l = l, tree[x][id].r = r, tree[x][id].add = 0; if(l == r) { tree[x][id].sum = 0; return; } int mid = (l + r) / 2; build(x * 2, l, mid, id); build(x * 2 + 1, mid + 1, r, id); pushup(x, id); } void add(int x, int l, int r, ll c, int id){ if(l <= tree[x][id].l && r >= tree[x][id].r){ tree[x][id].sum += c * (tree[x][id].r - tree[x][id].l + 1); tree[x][id].add += c; return; } pushdown(x, id); int mid = (tree[x][id].l + tree[x][id].r) / 2; if(l <= mid) add(x * 2, l, r, c, id); if(r > mid) add(x * 2 + 1, l, r, c, id); } ll ask(int x, int l, int id){ if(tree[x][id].l == tree[x][id].r) return tree[x][id].sum; pushdown(x, id); int mid = (tree[x][id].l + tree[x][id].r) / 2; ll v = 0; if(l <= mid) v += ask(x * 2, l, id); else v += ask(x * 2 + 1, l, id); return v; }
void change(int x, int y, int c, int id){ while(top[x] != top[y]){ if(deep[top[x]] < deep[top[y]]){ swap(x, y); } add(1, dfn[top[x]], dfn[x], c, id); x = fa[top[x]]; } if(deep[x] > deep[y]) swap(x, y); add(1, dfn[x], dfn[y], c, id); } ll queryAns(int x, int id){ ll ans = 0; ans += ask(1, dfn[x], id); return ans; }
void solve(int u){ int len = e[u].size(); for(int i = 0; i < len; i++){ int x = e[u][i].first, xi = e[u][i].second; int u = U[x]; ans[x] += (ll)xi * (queryAns(u, 0) - queryAns(u, 1) * (ll)deep[u]); }
}
void add_edge(int x, int y){ ver[++tot] = y; Next[tot] = head[x]; head[x] = tot; swap(x, y); ver[++tot] = y; Next[tot] = head[x]; head[x] = tot; }
int main() { n = read(); m = read(); for(int i = 1; i < n; i++){ x = read(); y = read(); add_edge(x, y); } cnt = n; for(int i = 1; i <= m; i++){ op[i] = read(); if(op[i] == 1){ X[i] = read(); ++cnt; add_edge(X[i], cnt); } else{ U[i] = read(); L[i] = read(); R[i] = read(); if(L[i] > cnt || L[i] > R[i]) continue; R[i] = min(cnt, R[i]); e[R[i]].push_back(pii(i, 1)); e[L[i] - 1].push_back(pii(i, -1)); } } dfs1(1, 1); dfs2(1, 1); build(1, 1, cnt, 0); build(1, 1, cnt, 1);solve(0); for(int i = 1; i <= cnt; i++){ change(1, fa[i], deep[i], 0); change(1, fa[i], 1, 1); solve(i); }
for(int i = 1; i <= m; i++){ if(op[i] == 2){ printf("%lld\n", ans[i]); } }
return 0; }
|