Problem p02936 (C++)

Buggy submission (status: Runtime Error):
/*author : Yashvardhan
Sunday, August 18, 2019
5:58 PM
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#define printArr(name, from, to)                                               \
  for (int x = from; x < to; x++)                                              \
    cout << name[x] << " ";

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << "  ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    os << v[i] << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto it : v)
    os << it << " ";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.ff << " " << v.ss;
  return os;
}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

const int N = 1e5 + 5;

vi g[N];
int sz[N];
int vis[N];
int arr[N];
int segt[4 * N];
int lazy[4 * N];
int ind = 0;

int takemod(int a) { return ((a % mod) + mod) % mod; }

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }

int getsizes(int source) {
  arr[ind] = source;
  ind++;
  vis[source] = 1;
  int ans = 0;
  for (auto i : g[source]) {
    if (!vis[i]) {
      ans += (1 + getsizes(i));
    }
  }
  return sz[source] = ans;
}

void push(int v, int tl, int tr) {
  segt[v] += (tr - tl + 1) * lazy[v];
  if (tr != tl) {
    lazy[2 * v] += lazy[v];
    lazy[2 * v + 1] += lazy[v];
  }
  lazy[v] = 0;
}

void build(int v, int tl, int tr) {
  if (tl == tr)
    segt[v] = arr[tl];
  else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    segt[v] = segt[2 * v] + segt[2 * v + 1];
  }
}

void update(int v, int tl, int tr, int l, int r, int toadd) {
  push(v, tl, tr);
  if (l > tr || r < tl)
    return;
  if (l <= tl && r >= tr) {
    lazy[v] += toadd;
    push(v, tl, tr);
    return;
  }

  int tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, r, toadd);
  update(2 * v + 1, tm + 1, tr, l, r, toadd);
  segt[v] = segt[2 * v] + segt[2 * v + 1];
}

int query(int v, int tl, int tr, int l, int r) {
  push(v, tl, tr);
  if (l > tr || r < tl)
    return 0;
  if (l <= tl && r >= tr)
    return segt[v];
  int tm = (tl + tr) / 2;
  return query(2 * v, tl, tm, l, r) + query(2 * v + 1, tm + 1, tr, l, r);
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  time_t t1, t2;
  t1 = clock();

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }

  getsizes(1);

  map<int, int> mapi;
  for (int i = 0; i < n; i++) {
    mapi[arr[i]] = i;
  }

  while (q--) {
    int curr, val;
    cin >> curr >> val;
    update(1, 0, n - 1, mapi[curr], mapi[curr] + sz[curr], val);
  }

  for (int i = 1; i <= n; i++) {
    int val = query(1, 0, n - 1, mapi[i], mapi[i]);
    cout << val << " ";
  }

  cout << endl;

  t2 = clock();
  cerr << endl << t2 - t1 << endl;

  return 0;
}


Fixed submission:
/*author : Yashvardhan
Sunday, August 18, 2019
5:58 PM
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define rev(Y) reverse(all(Y))
#define printArr(name, from, to)                                               \
  for (int x = from; x < to; x++)                                              \
    cout << name[x] << " ";

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#ifdef __APPLE__
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << "  ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    os << v[i] << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto it : v)
    os << it << " ";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.ff << " " << v.ss;
  return os;
}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

const int N = 2e5 + 5;

vi g[N];
int sz[N];
int vis[N];
int arr[N];
int segt[4 * N];
int lazy[4 * N];
int ind = 0;

int takemod(int a) { return ((a % mod) + mod) % mod; }

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int modinv(int a) { return takemod(pow(takemod(a), mod - 2, mod)); }

int getsizes(int source) {
  arr[ind] = source;
  ind++;
  vis[source] = 1;
  int ans = 0;
  for (auto i : g[source]) {
    if (!vis[i]) {
      ans += (1 + getsizes(i));
    }
  }
  return sz[source] = ans;
}

void push(int v, int tl, int tr) {
  segt[v] += (tr - tl + 1) * lazy[v];
  if (tr != tl) {
    lazy[2 * v] += lazy[v];
    lazy[2 * v + 1] += lazy[v];
  }
  lazy[v] = 0;
}

void build(int v, int tl, int tr) {
  if (tl == tr)
    segt[v] = arr[tl];
  else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    segt[v] = segt[2 * v] + segt[2 * v + 1];
  }
}

void update(int v, int tl, int tr, int l, int r, int toadd) {
  push(v, tl, tr);
  if (l > tr || r < tl)
    return;
  if (l <= tl && r >= tr) {
    lazy[v] += toadd;
    push(v, tl, tr);
    return;
  }

  int tm = (tl + tr) / 2;
  update(2 * v, tl, tm, l, r, toadd);
  update(2 * v + 1, tm + 1, tr, l, r, toadd);
  segt[v] = segt[2 * v] + segt[2 * v + 1];
}

int query(int v, int tl, int tr, int l, int r) {
  push(v, tl, tr);
  if (l > tr || r < tl)
    return 0;
  if (l <= tl && r >= tr)
    return segt[v];
  int tm = (tl + tr) / 2;
  return query(2 * v, tl, tm, l, r) + query(2 * v + 1, tm + 1, tr, l, r);
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  time_t t1, t2;
  t1 = clock();

  int n, q;
  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }

  getsizes(1);

  map<int, int> mapi;
  for (int i = 0; i < n; i++) {
    mapi[arr[i]] = i;
  }

  while (q--) {
    int curr, val;
    cin >> curr >> val;
    update(1, 0, n - 1, mapi[curr], mapi[curr] + sz[curr], val);
  }

  for (int i = 1; i <= n; i++) {
    int val = query(1, 0, n - 1, mapi[i], mapi[i]);
    cout << val << " ";
  }

  cout << endl;

  t2 = clock();
  cerr << endl << t2 - t1 << endl;

  return 0;
}