Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
// End of template code
vector<vi> g;
vi ans, v;
int n, id;

void solve(int node, vector<int> &lis_vec, int lis_vec_sz, int parent) {
  // podem acontecer duas coisas
  vector<int>::iterator it = lower_bound(all(lis_vec), v[node]);
  if (it == lis_vec.end()) {
    lis_vec.push_back(v[node]);
    lis_vec_sz++;
    ans[node] = lis_vec_sz;
    for (const int &nei : g[node]) {
      if (nei != parent) {
        solve(nei, lis_vec, lis_vec_sz, node);
      }
    }
    lis_vec.pop_back();
    lis_vec_sz--;
  } else {
    int fid = (int)(it - lis_vec.begin());
    int fode = lis_vec[fid];
    lis_vec[fid] = v[node];
    ans[node] = lis_vec_sz;
    for (const int &nei : g[node]) {
      if (nei != parent) {
        solve(nei, lis_vec, lis_vec_sz, node);
      }
    }
    lis_vec[fid] = fode;
  }
}

int main() {
  buff();
  cin >> n;
  v.resize(n);
  ans.resize(n);
  g.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> v_lis;
  solve(0, v_lis, 0, -1);
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x " = " << x << endl)
#define PRINT(x) TRACE(printf(x))
#define WATCHR(a, b)                                                           \\
  TRACE(for (auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
#define WATCHC(V)                                                              \\
  TRACE({                                                                      \\
    cout << #V " = ";                                                          \\
    WATCHR(V.begin(), V.end());                                                \\
  })
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using pll = pair<ll, ll>;

void buff() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}

constexpr ll MOD = 1e9 + 7;

inline ll pow_mod(ll a, ll b, ll mod = MOD) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
// End of template code!
constexpr int ms = 2e5 + 13;
vi g[ms];
int v[ms], ans[ms], n;

void solve(int node, vector<int> &lis_vec, int lis_vec_sz, int parent) {
  // podem acontecer duas coisas
  auto it = lower_bound(all(lis_vec), v[node]);

  if (it == lis_vec.end()) {
    lis_vec.push_back(v[node]);
    ans[node] = lis_vec_sz + 1;
    for (const auto &nei : g[node]) {
      if (nei != parent) {
        solve(nei, lis_vec, (lis_vec_sz + 1), node);
      }
    }
    lis_vec.pop_back();
  } else {
    // esse caso eh um pouco mais chato
    int prev = *it;
    *it = v[node];
    ans[node] = lis_vec_sz;
    for (const auto &nei : g[node]) {
      if (nei != parent) {
        solve(nei, lis_vec, lis_vec_sz, node);
      }
    }
    *it = prev;
  }
}

int main() {
  buff();
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u); // checar pra nao dar problema dps
  }
  vector<int> v_lis;
  solve(0, v_lis, 0, -1);
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << '\\n';
  }

  return 0;
}