Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

using i64 = long long;

#define REP(i, stop) for (int i = 0, i##_len = stop; i < i##_len; ++i)

const int is = 1 << 17;
char ib[is], *ip = ib + is, it;
inline char getc() {
  if (ip == ib + is) {
    fread_unlocked(ib, 1, is, stdin);
    ip = ib;
  }
  return *ip++;
}
inline int read() {
  int r = 0;
  if (ip + 16 > ib + is)
    while ((it = getc()) & 16)
      r = r * 10 + it - '0';
  else
    while ((it = *ip++) & 16)
      r = r * 10 + it - '0';
  return r;
}

const int os = 1 << 17;
char ob[os], *op = ob, ot;
inline void putc(const char c) {
  *op++ = c;
  if (op == ob + os) {
    fwrite_unlocked(ob, 1, os, stdout);
    op = ob;
  }
}
// x shoud be greater than or equal to zero
inline void write(int x) {
  char d[6];
  int i = 0;
  while (true) {
    d[i] = x % 10 + 48;
    x /= 10;
    ++i;
    if (x <= 0)
      break;
  }
  while (i--) {
    putc(d[i]);
  }
  putc(' ');
}

using P = pair<i64, i64>;
signed main() {
  i64 N = read(), Q = read();
  vector<vector<i64>> G(N);
  REP(_, N - 1) {
    i64 a = read(), b = read();
    a -= 1;
    b -= 1;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<i64> cnt(N);
  REP(_, Q) {
    i64 p = read(), x = read();
    p -= 1;
    cnt[p] += x;
  }
  vector<i64> ans(N);
  vector<bool> used(N, false);
  stack<P> st;
  st.push({0, 0});
  while (st.size()) {
    P a = st.top();
    st.pop();
    i64 v = cnt[a.first] + a.second;
    ans[a.first] = v;
    used[a.first] = true;
    for (i64 b : G[a.first]) {
      if (!used[b]) {
        st.push({b, v});
      }
    }
  }
  REP(i, N) { write(ans[i]); }
  fwrite_unlocked(ob, 1, op - ob, stdout);
}


Fixed submission:
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;

using i64 = long long;

#define REP(i, stop) for (int i = 0, i##_len = stop; i < i##_len; ++i)

const int is = 1 << 17;
char ib[is], *ip = ib + is, it;
inline char getc() {
  if (ip == ib + is) {
    fread_unlocked(ib, 1, is, stdin);
    ip = ib;
  }
  return *ip++;
}
inline int read() {
  int r = 0;
  if (ip + 16 > ib + is)
    while ((it = getc()) & 16)
      r = r * 10 + it - '0';
  else
    while ((it = *ip++) & 16)
      r = r * 10 + it - '0';
  return r;
}

const int os = 1 << 17;
char ob[os], *op = ob, ot;
inline void putc(const char c) {
  *op++ = c;
  if (op == ob + os) {
    fwrite_unlocked(ob, 1, os, stdout);
    op = ob;
  }
}
// x shoud be greater than or equal to zero
inline void write(int x) {
  char d[6];
  int i = 0;
  while (true) {
    d[i] = x % 10 + 48;
    x /= 10;
    ++i;
    if (x <= 0)
      break;
  }
  while (i--) {
    putc(d[i]);
  }
  putc(' ');
}

using P = pair<i64, i64>;
signed main() {
  i64 N = read(), Q = read();
  vector<vector<i64>> G(N);
  REP(_, N - 1) {
    i64 a = read(), b = read();
    a -= 1;
    b -= 1;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<i64> cnt(N);
  REP(_, Q) {
    i64 p = read(), x = read();
    p -= 1;
    cnt[p] += x;
  }
  vector<i64> ans(N);
  vector<bool> used(N, false);
  stack<P> st;
  st.push({0, 0});
  while (st.size()) {
    P a = st.top();
    st.pop();
    i64 v = cnt[a.first] + a.second;
    ans[a.first] = v;
    used[a.first] = true;
    for (i64 b : G[a.first]) {
      if (!used[b]) {
        st.push({b, v});
      }
    }
  }
  REP(i, N) {
    printf("%d ", ans[i]);
    //    write(ans[i]);
  }
  //  fwrite_unlocked(ob, 1, op - ob, stdout);
}