Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename tn> void read(tn &a) {
  tn x = 0, f = 1;
  char c = ' ';
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  a = x * f;
}
template <typename tn> void print(tn a) {
  if (a < 0)
    putchar('-'), a = -a;
  if (a > 9)
    print(a / 10);
  putchar(a % 10 + '0');
}
const int NR = 1e5 + 10;
struct Edge {
  int next, to;
} edge[NR * 2];
struct segment_tree {
  int l, r, sum, add;
} t[NR * 4];
int w[NR], fa[NR], hson[NR], dep[NR], siz[NR], top[NR], num;
int xu[NR], xu2[NR], head[NR], cnt;
void add_edge(int x, int y) {
  edge[++cnt].next = head[x];
  edge[cnt].to = y;
  head[x] = cnt;
}
void dfs1(int x, int father, int depth) {
  dep[x] = depth;
  fa[x] = father;
  siz[x] = 1;
  int max_x = 0;
  for (int i = head[x]; i; i = edge[i].next) {
    if (edge[i].to == father)
      continue;
    dfs1(edge[i].to, x, depth + 1);
    siz[x] += siz[edge[i].to];
    if (siz[edge[i].to] > max_x)
      max_x = siz[edge[i].to], hson[x] = edge[i].to;
  }
}
void dfs2(int x, int topp) {
  xu[x] = ++num;
  xu2[num] = w[x];
  top[x] = topp;
  if (!hson[x])
    return;
  dfs2(hson[x], topp);
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != fa[x] && edge[i].to != hson[x])
      dfs2(edge[i].to, edge[i].to);
}
void build(int p, int l, int r) {
  // printf("%d %d\n",l,r);
  t[p].l = l;
  t[p].r = r;
  if (l == r) {
    t[p].sum = xu2[l];
    return;
  }
  int mid = (l + r) / 2;
  build(p * 2, l, mid);
  build(p * 2 + 1, mid + 1, r);
  t[p].sum = 1ll * (t[p * 2].sum + t[p * 2 + 1].sum);
}
void spread(int p) {
  if (t[p].add) {
    t[p * 2].sum += 1ll * t[p].add * (t[p * 2].r - t[p * 2].l + 1);
    t[p * 2 + 1].sum += 1ll * t[p].add * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
    t[p * 2].add += t[p].add;
    t[p * 2 + 1].add += t[p].add;
    t[p].add = 0;
  }
}
void change(int p, int l, int r, int d) {
  if (l <= t[p].l && r >= t[p].r) {
    t[p].sum += 1ll * d * (t[p].r - t[p].l + 1);
    t[p].add += d;
    return;
  }
  spread(p);
  int mid = t[p].l + t[p].r >> 1;
  if (l <= mid)
    change(p * 2, l, r, d);
  if (r > mid)
    change(p * 2 + 1, l, r, d);
  t[p].sum = (t[p * 2].sum + t[p * 2 + 1].sum);
}
int ask(int p, int l, int r) {
  if (l <= t[p].l && r >= t[p].r)
    return t[p].sum;
  spread(p);
  int mid = t[p].l + t[p].r >> 1;
  int val = 0;
  if (l <= mid)
    val += ask(p * 2, l, r), val;
  if (r > mid)
    val += ask(p * 2 + 1, l, r), val;
  return val;
}
void ask1(int x, int y, int z) {
  z;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]])
      swap(x, y);
    change(1, xu[top[x]], xu[x], z);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y])
    swap(x, y);
  change(1, xu[x], xu[y], z);
}
int ask2(int x, int y) {
  int ans = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]])
      swap(x, y);
    ans = ans + ask(1, xu[top[x]], xu[x]);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y])
    swap(x, y);
  ans = (ans + ask(1, xu[x], xu[y]));
  return ans;
}
void ask3(int x, int z) { change(1, xu[x], xu[x] + siz[x] - 1, z); }
int ask4(int x) { return ask(1, xu[x], xu[x] + siz[x] - 1); }
int n, x, y, q, s1, s2;
int main() {
  read(n);
  read(q);
  for (int i = 1; i < n; i++) {
    read(x);
    read(y);
    add_edge(x, y);
    add_edge(y, x);
  }
  dfs1(1, 0, 1);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    read(s1);
    read(s2);
    ask3(s1, s2);
  }
  for (int i = 1; i <= n; i++)
    print(ask(1, xu[i], xu[i])), putchar(' ');
  puts("");
  /*
  scanf("%d%d%d%d",&n,&m,&r,&MOD);
  for(int i=1;i<=n;i++) scanf("%d",&w[i]);
  int x,y;
  for(int i=1;i<n;i++){
          scanf("%d%d",&x,&y);
          add_edge(x,y);
          add_edge(y,x);
  }
  dfs1(r,0,1);
  dfs2(r,r);
  build(1,1,n);
  int s1,z;
  while(m--){
          scanf("%d",&s1);
          if(s1==1) scanf("%d%d%d",&x,&y,&z),ask1(x,y,z);
          if(s1==2) scanf("%d%d",&x,&y),printf("%d\n",ask2(x,y));
          if(s1==3) scanf("%d%d",&x,&z),ask3(x,z);
          if(s1==4) scanf("%d",&x),printf("%d\n",ask4(x));
  }
  */
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename tn> void read(tn &a) {
  tn x = 0, f = 1;
  char c = ' ';
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  a = x * f;
}
template <typename tn> void print(tn a) {
  if (a < 0)
    putchar('-'), a = -a;
  if (a > 9)
    print(a / 10);
  putchar(a % 10 + '0');
}
const int NR = 2e5 + 10;
struct Edge {
  int next, to;
} edge[NR * 2];
struct segment_tree {
  int l, r, sum, add;
} t[NR * 4];
int w[NR], fa[NR], hson[NR], dep[NR], siz[NR], top[NR], num;
int xu[NR], xu2[NR], head[NR], cnt;
void add_edge(int x, int y) {
  edge[++cnt].next = head[x];
  edge[cnt].to = y;
  head[x] = cnt;
}
void dfs1(int x, int father, int depth) {
  dep[x] = depth;
  fa[x] = father;
  siz[x] = 1;
  int max_x = 0;
  for (int i = head[x]; i; i = edge[i].next) {
    if (edge[i].to == father)
      continue;
    dfs1(edge[i].to, x, depth + 1);
    siz[x] += siz[edge[i].to];
    if (siz[edge[i].to] > max_x)
      max_x = siz[edge[i].to], hson[x] = edge[i].to;
  }
}
void dfs2(int x, int topp) {
  xu[x] = ++num;
  xu2[num] = w[x];
  top[x] = topp;
  if (!hson[x])
    return;
  dfs2(hson[x], topp);
  for (int i = head[x]; i; i = edge[i].next)
    if (edge[i].to != fa[x] && edge[i].to != hson[x])
      dfs2(edge[i].to, edge[i].to);
}
void build(int p, int l, int r) {
  // printf("%d %d\n",l,r);
  t[p].l = l;
  t[p].r = r;
  if (l == r) {
    t[p].sum = xu2[l];
    return;
  }
  int mid = (l + r) / 2;
  build(p * 2, l, mid);
  build(p * 2 + 1, mid + 1, r);
  t[p].sum = 1ll * (t[p * 2].sum + t[p * 2 + 1].sum);
}
void spread(int p) {
  if (t[p].add) {
    t[p * 2].sum += 1ll * t[p].add * (t[p * 2].r - t[p * 2].l + 1);
    t[p * 2 + 1].sum += 1ll * t[p].add * (t[p * 2 + 1].r - t[p * 2 + 1].l + 1);
    t[p * 2].add += t[p].add;
    t[p * 2 + 1].add += t[p].add;
    t[p].add = 0;
  }
}
void change(int p, int l, int r, int d) {
  if (l <= t[p].l && r >= t[p].r) {
    t[p].sum += 1ll * d * (t[p].r - t[p].l + 1);
    t[p].add += d;
    return;
  }
  spread(p);
  int mid = t[p].l + t[p].r >> 1;
  if (l <= mid)
    change(p * 2, l, r, d);
  if (r > mid)
    change(p * 2 + 1, l, r, d);
  t[p].sum = (t[p * 2].sum + t[p * 2 + 1].sum);
}
int ask(int p, int l, int r) {
  if (l <= t[p].l && r >= t[p].r)
    return t[p].sum;
  spread(p);
  int mid = t[p].l + t[p].r >> 1;
  int val = 0;
  if (l <= mid)
    val += ask(p * 2, l, r), val;
  if (r > mid)
    val += ask(p * 2 + 1, l, r), val;
  return val;
}
void ask1(int x, int y, int z) {
  z;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]])
      swap(x, y);
    change(1, xu[top[x]], xu[x], z);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y])
    swap(x, y);
  change(1, xu[x], xu[y], z);
}
int ask2(int x, int y) {
  int ans = 0;
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]])
      swap(x, y);
    ans = ans + ask(1, xu[top[x]], xu[x]);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y])
    swap(x, y);
  ans = (ans + ask(1, xu[x], xu[y]));
  return ans;
}
void ask3(int x, int z) { change(1, xu[x], xu[x] + siz[x] - 1, z); }
int ask4(int x) { return ask(1, xu[x], xu[x] + siz[x] - 1); }
int n, x, y, q, s1, s2;
int main() {
  read(n);
  read(q);
  for (int i = 1; i < n; i++) {
    read(x);
    read(y);
    add_edge(x, y);
    add_edge(y, x);
  }
  dfs1(1, 0, 1);
  dfs2(1, 1);
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    read(s1);
    read(s2);
    ask3(s1, s2);
  }
  for (int i = 1; i <= n; i++)
    print(ask(1, xu[i], xu[i])), putchar(' ');
  puts("");
  /*
  scanf("%d%d%d%d",&n,&m,&r,&MOD);
  for(int i=1;i<=n;i++) scanf("%d",&w[i]);
  int x,y;
  for(int i=1;i<n;i++){
          scanf("%d%d",&x,&y);
          add_edge(x,y);
          add_edge(y,x);
  }
  dfs1(r,0,1);
  dfs2(r,r);
  build(1,1,n);
  int s1,z;
  while(m--){
          scanf("%d",&s1);
          if(s1==1) scanf("%d%d%d",&x,&y,&z),ask1(x,y,z);
          if(s1==2) scanf("%d%d",&x,&y),printf("%d\n",ask2(x,y));
          if(s1==3) scanf("%d%d",&x,&z),ask3(x,z);
          if(s1==4) scanf("%d",&x),printf("%d\n",ask4(x));
  }
  */
  return 0;
}