Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    for (auto x : v)                                                           \
      cout << x << ' ';                                                        \
    cout << endl;                                                              \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    for (int _i = 0; _i < n; _i++)                                             \
      cout << *(v + _i) << ' ';                                                \
    cout << endl;                                                              \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 1e5 + 20;

int n, q;
int a[MAXN], b[MAXN], t[MAXN], r[MAXN], p[MAXN], x[MAXN], csum[MAXN] = {};
unordered_set<int> graph[MAXN];
int vis[MAXN] = {};

int tt;
void dfs(int i) {
  t[i] = tt;
  vis[i] = 1;
  for (auto x : graph[i]) {
    if (!vis[x]) {
      tt++;
      dfs(x);
    }
  }
  r[t[i]] = tt;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    graph[a[i]].insert(b[i]);
    graph[b[i]].insert(a[i]);
  }
  for (int i = 0; i < q; i++)
    cin >> p[i] >> x[i];
  tt = 1;
  dfs(1);

  for (int i = 0; i < q; i++) {
    csum[t[p[i]]] += x[i];
    csum[r[t[p[i]]] + 1] -= x[i];
  }
  for (int i = 1; i <= n; i++) {
    csum[i] += csum[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    cout << csum[t[i]] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define rrep(i, a, b) for (auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define print(v)                                                               \
  {                                                                            \
    for (auto x : v)                                                           \
      cout << x << ' ';                                                        \
    cout << endl;                                                              \
  }
#define printn(v, n)                                                           \
  {                                                                            \
    for (int _i = 0; _i < n; _i++)                                             \
      cout << *(v + _i) << ' ';                                                \
    cout << endl;                                                              \
  }

typedef unsigned long long ull;
typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

const int MAXN = 2e5 + 20;

int n, q;
int a[MAXN], b[MAXN], t[MAXN], r[MAXN], p[MAXN], x[MAXN], csum[MAXN] = {};
unordered_set<int> graph[MAXN];
int vis[MAXN] = {};

int tt;
void dfs(int i) {
  t[i] = tt;
  vis[i] = 1;
  for (auto x : graph[i]) {
    if (!vis[x]) {
      tt++;
      dfs(x);
    }
  }
  r[t[i]] = tt;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> q;
  for (int i = 0; i < n - 1; i++) {
    cin >> a[i] >> b[i];
    graph[a[i]].insert(b[i]);
    graph[b[i]].insert(a[i]);
  }
  for (int i = 0; i < q; i++)
    cin >> p[i] >> x[i];
  tt = 1;
  dfs(1);

  for (int i = 0; i < q; i++) {
    csum[t[p[i]]] += x[i];
    csum[r[t[p[i]]] + 1] -= x[i];
  }
  for (int i = 1; i <= n; i++) {
    csum[i] += csum[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    cout << csum[t[i]] << endl;
  }

  return 0;
}