Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout << (#x) << "=" << (x) << endl
#define mset(d, val) memset(d, val, sizeof(d))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define F first
#define S second
#define PI 3.14159265358979323846264338327
#define INF 2e14
#define MOD 998244353
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define MAXN 100005

class LazySegmentTree {
private:
  int size_;
  vector<ll> v, lazy;

  void update(int s, int e, ll val, int k, int l, int r) {
    push(k, l, r);
    if (r < s || e < l)
      return;
    if (s <= l && r <= e) {
      lazy[k] += val;
      push(k, l, r);
    } else {
      update(s, e, val, k * 2, l, (l + r) >> 1);
      update(s, e, val, k * 2 + 1, ((l + r) >> 1) + 1, r);
      v[k] = merge(v[k * 2], v[k * 2 + 1]);
    }
  }

  ll query(int s, int e, int k, int l, int r) {
    push(k, l, r);
    if (r < s || e < l)
      return 0;
    if (s <= l && r <= e)
      return v[k];
    ll lc = query(s, e, k * 2, l, (l + r) >> 1);
    ll rc = query(s, e, k * 2 + 1, ((l + r) >> 1) + 1, r);
    return merge(lc, rc);
  }

public:
  LazySegmentTree() : v(vector<ll>()), lazy(vector<ll>()){};
  LazySegmentTree(int n) {
    for (size_ = 1; size_ < n;)
      size_ <<= 1;
    v.resize(size_ * 4);
    lazy.resize(size_ * 4);
  }
  inline void push(int k, int l, int r) {
    if (lazy[k] != 0) {
      v[k] += lazy[k];
      if (l != r) {
        lazy[k * 2] += lazy[k];
        lazy[k * 2 + 1] += lazy[k];
      }
      lazy[k] = 0;
    }
  }
  inline ll merge(ll x, ll y) { return x + y; }
  inline void update(int l, int r, ll val) {
    update(l, r, val, 1, 0, size_ - 1);
  }
  inline ll query(int l, int r) { return query(l, r, 1, 0, size_ - 1); }
};

int n, q;
vi adj[MAXN];
ll arr[MAXN];
int in[MAXN], out[MAXN];
int tmr = 0;

void dfs(int u, int p) {
  in[u] = tmr++;
  for (int v : adj[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  out[u] = tmr;
}

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

  cin >> n >> q;
  LazySegmentTree st(n);

  forn(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  dfs(0, -1);

  forn(it, 0, q) {
    int u;
    ll inc;
    cin >> u >> inc;
    u--;
    st.update(in[u], out[u] - 1, inc);
  }

  forn(i, 0, n) { cout << st.query(in[i], in[i]) << " "; }
  cout << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout << (#x) << "=" << (x) << endl
#define mset(d, val) memset(d, val, sizeof(d))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define F first
#define S second
#define PI 3.14159265358979323846264338327
#define INF 2e14
#define MOD 998244353
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define MAXN 200005

class LazySegmentTree {
private:
  int size_;
  vector<ll> v, lazy;

  void update(int s, int e, ll val, int k, int l, int r) {
    push(k, l, r);
    if (r < s || e < l)
      return;
    if (s <= l && r <= e) {
      lazy[k] += val;
      push(k, l, r);
    } else {
      update(s, e, val, k * 2, l, (l + r) >> 1);
      update(s, e, val, k * 2 + 1, ((l + r) >> 1) + 1, r);
      v[k] = merge(v[k * 2], v[k * 2 + 1]);
    }
  }

  ll query(int s, int e, int k, int l, int r) {
    push(k, l, r);
    if (r < s || e < l)
      return 0;
    if (s <= l && r <= e)
      return v[k];
    ll lc = query(s, e, k * 2, l, (l + r) >> 1);
    ll rc = query(s, e, k * 2 + 1, ((l + r) >> 1) + 1, r);
    return merge(lc, rc);
  }

public:
  LazySegmentTree() : v(vector<ll>()), lazy(vector<ll>()){};
  LazySegmentTree(int n) {
    for (size_ = 1; size_ < n;)
      size_ <<= 1;
    v.resize(size_ * 4);
    lazy.resize(size_ * 4);
  }
  inline void push(int k, int l, int r) {
    if (lazy[k] != 0) {
      v[k] += lazy[k];
      if (l != r) {
        lazy[k * 2] += lazy[k];
        lazy[k * 2 + 1] += lazy[k];
      }
      lazy[k] = 0;
    }
  }
  inline ll merge(ll x, ll y) { return x + y; }
  inline void update(int l, int r, ll val) {
    update(l, r, val, 1, 0, size_ - 1);
  }
  inline ll query(int l, int r) { return query(l, r, 1, 0, size_ - 1); }
};

int n, q;
vi adj[MAXN];
ll arr[MAXN];
int in[MAXN], out[MAXN];
int tmr = 0;

void dfs(int u, int p) {
  in[u] = tmr++;
  for (int v : adj[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  out[u] = tmr;
}

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

  cin >> n >> q;
  LazySegmentTree st(n);

  forn(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].pb(v);
    adj[v].pb(u);
  }

  dfs(0, -1);

  forn(it, 0, q) {
    int u;
    ll inc;
    cin >> u >> inc;
    u--;
    st.update(in[u], out[u] - 1, inc);
  }

  forn(i, 0, n) { cout << st.query(in[i], in[i]) << " "; }
  cout << '\n';

  return 0;
}