#include <cmath>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
#pragma warning(disable : 4996)
const int inf = 1512345678;
int N, Q, c, cr, p[500009], tp[500009], v[500009], d[500009], ql[500009], qr[500009], qd[500009], x[500009]; long long a[500009], at[500009];
vector<int> g[500009]; int depth[500009], ord[500009], gl[500009], gr[500009], rd[500009];
void rec(int pos) {
int pd = depth[cr];
gl[pos] = cr;
ord[cr++] = pos;
for (int i : g[pos]) rd[cr] = depth[cr] = pd + 1, rec(i);
gr[pos] = cr;
}
int main() {
cin >> N >> Q;
for (int i = 0; i < N; i++) scanf("%d %lld", &p[i], &at[i]);
c = N;
for (int i = 0; i < Q; i++) {
cin >> tp[i];
if (tp[i] == 1) scanf("%d %d %d", &v[i], &d[i], &x[i]);
if (tp[i] == 2) scanf("%d %d", &v[i], &d[i]);
if (tp[i] == 3) scanf("%d %lld", &p[c], &at[c]), c++;
}
for (int i = 1; i < c; i++) g[p[i]].push_back(i);
rec(0);
for (int i = 0; i < c; i++) a[i] = at[ord[i]];
int pl = 0, pr = N;
for (int i = 0; i < Q; i++) {
if (tp[i] != 3) ql[i] = gl[v[i]], qr[i] = gr[v[i]], qd[i] = depth[gl[v[i]]] + d[i] + 1;
else ql[i] = gl[pr], qr[i] = gl[pr] + 1, pr++;
}
for (int i = 0; i < c; i++) {
if (ord[i] >= N) depth[i] = inf;
}
// Query 1: l = ql[i], r = qr[i], d = qd[i], x = qx[i] (l <= i < r, y < d)
// Query 2: l = ql[i], r = qr[i], d = qd[i]
// Query 3: v = ql[i]
int B = sqrt(Q)/7+1;
for (int i = 0; i < B; i++) {
int bl = i * Q / B, br = (i + 1) * Q / B;
vector<int> v;
for (int j = bl; j < br; j++) {
v.push_back(ql[j]);
v.push_back(qr[j]);
}
v.push_back(0);
v.push_back(c);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
vector<vector<int> > vd(v.size() - 1), vperm(v.size() - 1);
vector<vector<long long> > bit1(v.size() - 1), bit0(v.size() - 1), gsum(v.size() - 1);
for (int j = 0; j < v.size() - 1; j++) {
vd[j].resize(v[j + 1] - v[j]);
vperm[j].resize(v[j + 1] - v[j]);
gsum[j].resize(v[j + 1] - v[j] + 1);
bit1[j].resize(v[j + 1] - v[j] + 1);
bit0[j].resize(v[j + 1] - v[j] + 1);
for (int k = v[j]; k < v[j + 1]; k++) vd[j][k - v[j]] = depth[k], vperm[j][k - v[j]] = k;
sort(vperm[j].begin(), vperm[j].end(),
[&](int k, int l) -> bool { return depth[k] < depth[l]; }
);
for (int k = v[j]; k < v[j + 1]; k++) vd[j][k - v[j]] = depth[vperm[j][k - v[j]]], gsum[j][k - v[j] + 1] = a[vperm[j][k - v[j]]];
for (int k = 0; k < v[j + 1] - v[j]; k++) gsum[j][k + 1] += gsum[j][k];
}
for (int j = bl; j < br; j++) {
if (tp[j] == 1) {
int vl = lower_bound(v.begin(), v.end(), ql[j]) - v.begin();
int vr = lower_bound(v.begin(), v.end(), qr[j]) - v.begin();
for (int k = vl; k < vr; k++) {
int ptr = lower_bound(vd[k].begin(), vd[k].end(), qd[j]) - vd[k].begin();
for (int l = ptr + 1; l <= v[k + 1] - v[k]; l += l & (-l)) {
bit1[k][l] -= x[j];
bit0[k][l] += 1LL * x[j] * ptr;
}
for (int l = 1; l <= v[k + 1] - v[k]; l += l & (-l)) {
bit1[k][l] += x[j];
}
}
}
if (tp[j] == 2) {
int vl = lower_bound(v.begin(), v.end(), ql[j]) - v.begin();
int vr = lower_bound(v.begin(), v.end(), qr[j]) - v.begin();
long long ret = 0;
for (int k = vl; k < vr; k++) {
int ptr = lower_bound(vd[k].begin(), vd[k].end(), qd[j]) - vd[k].begin();
for (int l = ptr; l >= 1; l -= l & (-l)) ret += bit1[k][l] * ptr + bit0[k][l];
ret += gsum[k][ptr];
}
printf("%lld\n", ret);
}
if (tp[j] == 3) {
int ptr = lower_bound(v.begin(), v.end(), ql[j] + 1) - v.begin() - 1;
depth[ql[j]] = rd[ql[j]];
vd[ptr][0] = rd[ql[j]];
}
}
for (int j = 0; j < v.size() - 1; j++) {
long long b_all = 0;
for (int k = v[j + 1] - v[j]; k >= 1; k -= k & (-k)) b_all += bit1[j][k] * (v[j + 1] - v[j]) + bit0[j][k];
vector<long long> b(v[j + 1] - v[j]);
for (int k = 0; k < v[j + 1] - v[j]; k++) {
b[k] += b_all;
for (int l = k + 1; l >= 1; l -= l & (-l)) b[k] -= bit1[j][l] * k + bit0[j][l];
}
for (int k = 0; k < v[j + 1] - v[j] - 1; k++) b[k] -= b[k + 1];
for (int k = v[j]; k < v[j + 1]; k++) {
a[vperm[j][k - v[j]]] = b[k - v[j]] + gsum[j][k - v[j] + 1] - gsum[j][k - v[j]];
}
}
}
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y2Fzc2VydD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojcHJhZ21hIHdhcm5pbmcoZGlzYWJsZSA6IDQ5OTYpCmNvbnN0IGludCBpbmYgPSAxNTEyMzQ1Njc4OwppbnQgTiwgUSwgYywgY3IsIHBbNTAwMDA5XSwgdHBbNTAwMDA5XSwgdls1MDAwMDldLCBkWzUwMDAwOV0sIHFsWzUwMDAwOV0sIHFyWzUwMDAwOV0sIHFkWzUwMDAwOV0sIHhbNTAwMDA5XTsgbG9uZyBsb25nIGFbNTAwMDA5XSwgYXRbNTAwMDA5XTsKdmVjdG9yPGludD4gZ1s1MDAwMDldOyBpbnQgZGVwdGhbNTAwMDA5XSwgb3JkWzUwMDAwOV0sIGdsWzUwMDAwOV0sIGdyWzUwMDAwOV0sIHJkWzUwMDAwOV07CnZvaWQgcmVjKGludCBwb3MpIHsKCWludCBwZCA9IGRlcHRoW2NyXTsKCWdsW3Bvc10gPSBjcjsKCW9yZFtjcisrXSA9IHBvczsKCWZvciAoaW50IGkgOi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