Language: C++

#include<unordered_set>
#include<unordered_map>
using namespace std;

int n;
int q;

struct query{
	int ty;
	int v;
	int d;
	int x;
	query(int ty_,int v_,int d_,int x_=0){
		ty = ty_;
		v = v_;
		d = d_;
		x = x_;
	}
};

#define MAX 500002

vector<int> v[MAX];

long long int cost[MAX];

vector<query> Q;

bool ex[MAX];

vector<pair<int, long long int> > ev[MAX];

long long int ans[MAX];


struct ASK{
	int id;
	int d;
	ASK(int id_, int d_){
		id = id_;
		d = d_;
	}
};
vector<ASK> ask[MAX];

int dep[MAX];

struct BIT{
	vector<long long int> bit;
	void resize(int N){
		bit.assign(N, 0);
	}
	void add(int i, long long int j){
		i++;
		while (i < bit.size()){
			bit[i] += j;
			i += i&-i;
		}
	}
	long long int sum(int i){
		long long int r = 0;
		i++;
		while (i){
			r += bit[i];
			i -= i&-i;
		}
		return r;
	}
};

BIT B;

long long int flag[MAX];

inline void dfs(int b,long long int cur_add=0){
	if (ex[b] == false)return;
	cur_add += flag[dep[b]];
	for (int i = 0; i < ev[b].size(); i++){
		cur_add += ev[b][i].second;
		flag[dep[b] + ev[b][i].first + 1] -= ev[b][i].second;
	}
	cost[b] += cur_add;
	for (int i = 0; i < v[b].size(); i++){
		dfs(v[b][i],cur_add);
	}
	for (int i = 0; i < ev[b].size(); i++){
		cur_add -= ev[b][i].second;
		flag[dep[b] + ev[b][i].first + 1] += ev[b][i].second;
	}
	ev[b].clear();
}

long long int sum[MAX];
int us[MAX];
int u_s;
int mx_dep;

int child[MAX];
int star[MAX];
int en[MAX];
int ord;

inline void adding_all(int b){
	if (us[dep[b]] != u_s){
		us[dep[b]] = u_s;
		sum[dep[b]] = 0;
	}
	sum[dep[b]] += cost[b];
	for (int i = 0; i < v[b].size(); i++){
		adding_all(v[b][i]);
	}
	mx_dep = max(mx_dep, dep[b]);
}

inline void dfs3(int b, bool del=false){
	if (ex[b] == false)return;
	u_s++;
	mx_dep = 0;
	if (v[b].size() == 0){
		for (int i = 0; i < ask[b].size(); i++){
			ans[ask[b][i].id] += cost[b];
		}
		return;
	}
	int mx = -1;
	int node = 0;
	for (int i = 0; i < v[b].size(); i++){
		if (mx < child[v[b][i]]){
			mx = child[v[b][i]];
			node = v[b][i];
		}
	}
	for (int i = 0; i < v[b].size(); i++){
		if (v[b][i] != node){
			dfs3(v[b][i]);
		}
	}
	dfs3(node, true);
	for (int i = 0; i < v[b].size(); i++){
		if (v[b][i] != node){
			adding_all(v[b][i]);
		}
	}
	mx_dep = max(mx_dep, dep[b]);
	for (int i = dep[b]+1; i <= mx_dep; i++){
		sum[i] += sum[i - 1];
	}
	for (int i = 0; i < ask[b].size(); i++){
		int go = dep[b] + ask[b][i].d;
		go = min(go, mx_dep);
		ans[ask[b][i].id] = sum[go];
	}
}
void init(){
	dfs(0);
	dfs3(0);
}


inline void dfs2(int b,int d=0){
	star[b] = ord;
	ord++;
	dep[b] = d;
	for (int i = 0; i < v[b].size(); i++){
		int go = v[b][i];
		dfs2(go,d+1);
		child[b] += child[go];
	}
	en[b] = ord;
	child[b]++;
}


int main(){
	cin >> n >> q;
	B.resize(2*MAX);
	for (int i = 0; i < n; i++){
		ex[i] = true;
		int p, a;
		scanf("%d%d", &p, &a);
		if (p >= 0){
			v[p].push_back(i);
		}
		cost[i] = a;
	}
	int cur=n;
	for (int i = 0; i < q; i++){
		int ty;
		scanf("%d", &ty);
		if (ty == 1){
			int vv, d,xx;
			scanf("%d%d%d", &vv, &d,&xx);
			Q.push_back(query(ty, vv, d,xx));
			continue;
		}
		if (ty == 2){
			int vv,dd;
			scanf("%d%d", &vv,&dd);
			Q.push_back(query(ty, vv,dd));
			continue;
		}
		if (ty == 3){
			int pr, ar;
			scanf("%d%d", &pr, &ar);
			Q.push_back(query(3, cur,ar));
			v[pr].push_back(cur);
			cur++;
		}
	}
	dfs2(0);
	int SQ = 220;
	/*for (int i = 0; i < q; i+=SQ){
		int rig = min(q, i + SQ);
		for (int j = 0; j < rig; j++){
			if (Q[j].ty == 1){
				ev[Q[j].v].push_back(make_pair(Q[j].d, Q[j].x));
				continue;
			}
			if (Q[j].ty == 2){
				ask[Q[j].v].push_back(ASK(j,Q[j].d));
				continue;
			}
			ex[Q[j].v] = true;
			cost[Q[j].v] = Q[j].d;
		}
		init();
		for (int j = 0; j < rig; j++){
			if (Q[j].ty == 1){
				for (int jj = j + 1; jj < rig; jj++){
					if (Q[jj].ty == 3){
						if (dep[Q[jj].v]-dep[Q[j].v]<=Q[j].d&&star[Q[j].v] <= star[Q[jj].v] && en[Q[jj].v] <= en[Q[j].v]){
							cost[Q[jj].v] -= Q[j].x;
						}
					}
				}
				continue;
			}
			if (Q[j].ty == 2){
				for (int jj = j + 1; jj < rig; jj++){
					if (Q[jj].ty == 1){
						int v1 = Q[j].v;  //
						int v2 = Q[jj].v;
						if (star[v1] <= star[v2] && en[v2] <= en[v1]){

						}
						if (star[v2] <= star[v1] && en[v1] <= en[v2]){

						}
					}
					if (Q[jj].ty == 3){
						if (dep[Q[jj].v] - dep[Q[j].v] <= Q[j].d&&star[Q[j].v] <= star[Q[jj].v] && en[Q[jj].v] <= en[Q[j].v]){
							ans[j] -= cost[Q[jj].v];
						}
						continue;
					}
				}
			}
		}
	}*/
	return 0;
}

Compiler output:
a.cc:23:1: error: 'vector' does not name a type
   23 | vector<int> v[MAX];
      | ^~~~~~
a.cc:27:1: error: 'vector' does not name a type
   27 | vector<query> Q;
      | ^~~~~~
a.cc:31:1: error: 'vector' does not name a type
   31 | vector<pair<int, long long int> > ev[MAX];
      | ^~~~~~
a.cc:44:1: error: 'vector' does not name a type
   44 | vector<ASK> ask[MAX];
      | ^~~~~~
a.cc:49:9: error: 'vector' does not name a type
   49 |         vector<long long int> bit;
      |         ^~~~~~
a.cc: In member function 'void BIT::resize(int)':
a.cc:51:17: error: 'bit' was not declared in this scope
   51 |                 bit.assign(N, 0);
      |                 ^~~
a.cc: In member function 'void BIT::add(int, long long int)':
a.cc:55:28: error: 'bit' was not declared in this scope
   55 |                 while (i < bit.size()){
      |                            ^~~
a.cc: In member function 'long long int BIT::sum(int)':
a.cc:64:30: error: 'bit' was not declared in this scope
   64 |                         r += bit[i];
      |                              ^~~
a.cc: In function 'void dfs(int, long long int)':
a.cc:78:29: error: 'ev' was not declared in this scope; did you mean 'ex'?
   78 |         for (int i = 0; i < ev[b].size(); i++){
      |                             ^~
      |                             ex
a.cc:83:29: error: 'v' was not declared in this scope
   83 |         for (int i = 0; i < v[b].size(); i++){
      |                             ^
a.cc:86:29: error: 'ev' was not declared in this scope; did you mean 'ex'?
   86 |         for (int i = 0; i < ev[b].size(); i++){
      |                             ^~
      |                             ex
a.cc:90:9: error: 'ev' was not declared in this scope; did you mean 'ex'?
   90 |         ev[b].clear();
      |         ^~
      |         ex
a.cc: In function 'void adding_all(int)':
a.cc:109:29: error: 'v' was not declared in this scope
  109 |         for (int i = 0; i < v[b].size(); i++){
      |                             ^
a.cc: In function 'void dfs3(int, bool)':
a.cc:119:13: error: 'v' was not declared in this scope
  119 |         if (v[b].size() == 0){
      |             ^
a.cc:120:37: error: 'ask' was not declared in this scope
  120 |                 for (int i = 0; i < ask[b].size(); i++){
      |                                     ^~~
a.cc:127:29: error: 'v' was not declared in this scope
  127 |         for (int i = 0; i < v[b].size(); i++){
      |                             ^
a.cc:133:29: error: 'v' was not declared in this scope
  133 |         for (int i = 0; i < v[b].size(); i++){
      |                             ^
a.cc:139:29: error: 'v' was not declared in this scope
  139 |         for (int i = 0; i < v[b].size(); i++){
      |                             ^
a.cc:148:29: error: 'ask' was not declared in this scope
  148 |         for (int i = 0; i < ask[b].size(); i++){
      |                             ^~~
a.cc: In function 'void dfs2(int, int)':
a.cc:164:29: error: 'v' was not declared in this scope
  164 |         for (int i = 0; i < v[b].size(); i++){
      |                             ^
a.cc: In function 'int main()':
a.cc:175:9: error: 'cin' was not declared in this scope
  175 |         cin >> n >> q;
      |         ^~~
a.cc:3:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    2 | #include<unordered_map>
  +++ |+#include <iostream>
    3 | using namespace std;
a.cc:180:17: error: 'scanf' was not declared in this scope
  180 |                 scanf("%d%d", &p, &a);
      |                 ^~~~~
a.cc:182:25: error: 'v' was not declared in this scope
  182 |                         v[p].push_back(i);
      |                         ^
a.cc:189:17: error: 'scanf' was not declared in this scope
  189 |                 scanf("%d", &ty);
      |                 ^~~~~
a.cc:193:25: error: 'Q' was not declared in this scope
  193 |                         Q.push_back(query(ty, vv, d,xx));
      |                         ^
a.cc:199:25: error: 'Q' was not declared in this scope
  199 |                         Q.push_back(query(ty, vv,dd));
      |                         ^
a.cc:205:25: error: 'Q' was not declared in this scope
  205 |                         Q.push_back(query(3, cur,ar));
      |                         ^
a.cc:206:25: error: 'v' was not declared in this scope
  206 |                         v[pr].push_back(cur);
      |                         ^