alexjercan/bugnet huggingface dataset:
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  REP(i, N) { cin >> a[i]; }
  vector<vector<int>> g(N);
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  vector<int> ans(N);
  auto dfs = [&](auto &&f, int now, int par, vector<int> &dp) -> void {
    auto itr = lower_bound(ALL(dp), a[now]);
    int prev = dp[itr - dp.begin()];
    dp[itr - dp.begin()] = a[now];
    ans[now] = lower_bound(ALL(dp), INF) - dp.begin();
    for (auto e : g[now]) {
      if (e == par)
        continue;
      f(f, e, now, dp);
    }
    dp[itr - dp.begin()] = prev;
  };
  auto tmp = vector<int>(N, INF);
  dfs(dfs, 0, -1, tmp);
  REP(i, ans.size()) out(ans[i]);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<int> a(N);
  REP(i, N) { cin >> a[i]; }
  vector<vector<int>> g(N);
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  vector<int> ans(N);
  auto dfs = [&](auto &&f, int now, int par, vector<int> dp) -> void {
    auto itr = lower_bound(ALL(dp), a[now]);
    int prev = dp[itr - dp.begin()];
    dp[itr - dp.begin()] = a[now];
    ans[now] = lower_bound(ALL(dp), INF) - dp.begin();
    for (auto e : g[now]) {
      if (e == par)
        continue;
      f(f, e, now, dp);
    }
    dp[itr - dp.begin()] = prev;
  };
  dfs(dfs, 0, -1, vector<int>(N, INF));
  REP(i, ans.size()) out(ans[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

const int N = 200010;
const int INF = INT_MAX;

int a[N];
vector<int> G[N];

int ans[N];
int dp[N];
void dfs(int v, int p) {
  int idx = lower_bound(dp, dp + N, a[v]) - dp;
  int t = dp[idx];
  dp[idx] = a[v];
  ans[v] = lower_bound(dp, dp + N, INF) - dp - 1;

  for (int c : G[v])
    if (c != p)
      dfs(c, v);

  dp[idx] = t;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].pb(v);
    G[v].pb(u);
  }

  fill(dp, dp + N, INF);
  dp[0] = 0;
  dfs(0, -1);

  rep(i, n) cout << ans[i] << "\\n";
  return 0;
}


Error: 0

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

const int N = 100010;
const int INF = INT_MAX;

int a[N];
vector<int> G[N];

int ans[N];
int dp[N];
void dfs(int v, int p) {
  int idx = upper_bound(dp, dp + N, a[v]) - dp;
  int t = dp[idx];
  dp[idx] = a[v];
  ans[v] = lower_bound(dp, dp + N, INF) - dp - 1;

  for (int c : G[v])
    if (c != p)
      dfs(c, v);

  dp[idx] = t;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];

  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].pb(v);
    G[v].pb(u);
  }

  fill(dp, dp + N, INF);
  dp[0] = 0;
  dfs(0, -1);

  rep(i, n) cout << ans[i] << "\\n";
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7;

int h[N], e[N * 2], ne[N * 2], idx;
int w[N], n;
int ans[N];
int low[N];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void dfs(int s, int fa) {
  int pos = lower_bound(low + 1, low + 1 + n, w[s]) - low;
  int cnt = low[pos];
  low[pos] = w[s];
  ans[s] = max(ans[fa], pos);
  for (int i = h[s]; ~i; i = ne[i]) {
    int j = e[i];
    if (j == fa)
      continue;
    dfs(j, s);
  }
  low[pos] = cnt;
}

int main() {
  int a, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &w[i]);
  memset(h, -1, sizeof h);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }
  memset(low, 0x3f, sizeof low);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
typedef long long LL;
typedef unsigned long long ULL;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1e9 + 7;
const ULL P = 131;
const int N = 2e5 + 7;

int h[N], e[N * 2], ne[N * 2], idx;
int w[N], in[N];
int arr[N], ele[N], st[N];
int low[N];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void fun(int n) {
  //	for(int i=1;i<=n;i++) cout<<arr[i]<<" ";
  //	cout<<"\\n";
  //	for(int i=1;i<=n;i++) cout<<st[i]<<" ";
  //	cout<<"\\n";

  int ans = 1;
  low[ans] = arr[1];
  ele[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (low[ans] < arr[i])
      low[++ans] = arr[i];
    else {
      int j = lower_bound(low + 1, low + ans + 1, arr[i]) - low;
      low[j] = arr[i];
    }
    ele[st[i]] = ans;
  }
}

void dfs(int s, int fa, int cnt) {
  if (in[s] == 1 && s != 1) {
    fun(cnt);
    return;
  }
  for (int i = h[s]; ~i; i = ne[i]) {
    int j = e[i];
    if (j == fa)
      continue;
    arr[++cnt] = w[j];
    st[cnt] = j;
    dfs(j, s, cnt);
    --cnt;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &w[i]);
  memset(h, -1, sizeof h);
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
    in[a]++, in[b]++;
  }
  arr[1] = w[1];
  st[1] = 1;
  dfs(1, 0, 1);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ele[i]);
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7;

int h[N], e[N * 2], ne[N * 2], idx;
int w[N];
int ans[N];
int low[N];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void dfs(int s, int fa, int pos) {
  int temp1 = -1, temp2 = 0, k = 0;
  if (low[pos] < w[s])
    temp1 = low[pos + 1], low[++pos] = w[s];
  else {
    int j = lower_bound(low + 1, low + pos + 1, w[s]) - low;
    temp2 = low[j], k = j;
    low[j] = w[s];
  }
  ans[s] = pos;
  for (int i = h[s]; ~i; i = ne[i]) {
    int j = e[i];
    if (j == fa)
      continue;
    dfs(j, s, pos);
  }
  if (temp1 != -1)
    low[pos] = temp1;
  else
    low[k] = temp2;
}

int main() {
  int n, a, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &w[i]);
  memset(h, -1, sizeof h);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 7;

int h[N], e[N * 2], ne[N * 2], idx;
int w[N];
int ans[N];
int low[N];

void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void dfs(int s, int fa, int pos) {
  int temp1 = 0, temp2 = 0, k;
  if (low[pos] < w[s])
    temp1 = low[pos + 1], low[++pos] = w[s];
  else {
    int j = lower_bound(low + 1, low + pos + 1, w[s]) - low;
    temp2 = low[j], k = j;
    low[j] = w[s];
  }
  ans[s] = max(ans[s], pos);
  for (int i = h[s]; ~i; i = ne[i]) {
    int j = e[i];
    if (j == fa)
      continue;
    dfs(j, s, pos);
  }
  if (temp1)
    low[pos] = temp1;
  else
    low[k] = temp2;
}

int main() {
  int n, a, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &w[i]);
  memset(h, -1, sizeof h);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 10;
int n, a[maxN], ans[maxN];
vector<int> edge[maxN], v;
void dfs(int x, int par) {
  int tmp = 0, pos, flag = 0;
  if (v.empty() || a[x] > v.back()) {
    v.push_back(a[x]);
  } else {
    flag = 1;
    pos = lower_bound(v.begin(), v.end(), a[x]) - v.begin();
    tmp = v[pos];
    v[pos] = a[x];
  }
  ans[x] = v.size();
  for (auto to : edge[x]) {
    if (to != par) {
      dfs(to, x);
    }
  }
  if (!flag) {
    v.pop_back();
  } else {
    v[pos] = tmp;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, a[maxn], ans[maxn];
vector<int> edge[maxn];
vector<pair<int, int>> v;
bool vis[maxn];
void dfs(int x, int par) {
  vis[x] = true;
  for (auto to : edge[x]) {
    if (to != par && !vis[to]) {
      int mx = 1, mx2 = 1;
      for (auto it : v) {
        if (a[it.first] < a[to]) {
          mx = max(mx, it.second + 1);
        }
        mx2 = max(mx2, it.second);
      }
      mx2 = max(mx2, mx);
      ans[to] = mx2;
      v.push_back(make_pair(to, mx));
      dfs(to, x);
      v.pop_back();
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u, --v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  v.push_back(make_pair(0, 1));
  ans[0] = 1;
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 2e5 + 10;
vector<int> G[N];
int v[N], vis[N], ans[N];
int s[N], tot;
void dfs(int u) {
  static stack<int> st;
  if (tot == 0 || s[tot] < v[u]) {
    s[++tot] = v[u];
    ans[u] = tot;
    for (int e : G[u]) {
      if (vis[e])
        continue;
      vis[e] = 1;
      dfs(e);
    }
    tot--;
  } else {
    int tmp = lower_bound(s + 1, s + 1 + tot, v[u]) - s;
    swap(s[tmp], v[u]);
    ans[u] = tot;
    for (int e : G[u]) {
      if (vis[e])
        continue;
      vis[e] = 1;
      dfs(e);
    }
    swap(s[tmp], v[u]);
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  int u, v;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  vis[1] = 1;
  dfs(1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 1e5 + 10;
vector<int> G[N];
int v[N], vis[N], ans[N];

void dfs(int u) {
  static stack<int> st;
  stack<int> st2;
  while (st.size() && st.top() > v[u]) {
    st2.push(st.top());
    st.pop();
  }
  st.push(v[u]);
  ans[u] = st.size();
  for (int e : G[u]) {
    if (vis[e])
      continue;
    vis[e] = 1;
    dfs(e);
  }
  st.pop();
  while (st2.size()) {
    st.push(st2.top());
    st2.pop();
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  int u, v;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  vis[1] = 1;
  dfs(1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void rf(vector<vector<int>> &tree, vector<int> &a, vector<int> &lis,
        vector<int> &dp, vector<bool> &visited, int n, int x) {
  if (visited.at(x)) {
    return;
  } else {
    visited.at(x) = true;
    auto itr = lower_bound(dp.begin(), dp.end(), a.at(x));
    int old = *itr;
    *itr = a.at(x);
    lis.at(x) = distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
    for (int y : tree.at(x)) {
      if (!visited.at(y)) {
        rf(tree, a, lis, dp, visited, n, y);
      }
    }
    *itr = old;
    return;
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  vector<vector<int>> tree(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    tree.at(u - 1).push_back(v - 1);
    tree.at(v - 1).push_back(u - 1);
  }
  vector<int> lis(n);
  vector<int> dp(n, INF);
  vector<bool> visited(n, false);
  rf(tree, a, lis, dp, visited, n, 0);
  for (int i = 0; i < n; i++) {
    cout << lis.at(i) << endl;
  }
}

Error: TLE

Fixed submission:

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

void rf(vector<vector<int>> &tree, vector<int> &a, vector<int> &lis,
        vector<int> dp, vector<bool> &visited, int n, int x) {
  if (visited.at(x)) {
    return;
  } else {
    visited.at(x) = true;
    *lower_bound(dp.begin(), dp.end(), a.at(x)) = a.at(x);
    lis.at(x) = distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
    for (int y : tree.at(x)) {
      if (!visited.at(y)) {
        rf(tree, a, lis, dp, visited, n, y);
      }
    }
    return;
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  vector<vector<int>> tree(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    tree.at(u - 1).push_back(v - 1);
    tree.at(v - 1).push_back(u - 1);
  }
  vector<int> lis(n);
  vector<int> dp(n, INF);
  vector<bool> visited(n, false);
  rf(tree, a, lis, dp, visited, n, 0);
  for (int i = 0; i < n; i++) {
    cout << lis.at(i) << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

ll n;
vec a(2 << 17);
mat G(2 << 17);
vec dp(2 << 17);
vec ans(2 << 17);

void dfs(ll u, ll p, ll R) {
  ll l = 0, r = R;
  while (r - l > 1) {
    ll m = l + r >> 1;
    if (dp[m] < a[u])
      l = m;
    else
      r = m;
  }
  ll pre = dp[r];
  dp[r] = a[u];
  ans[u] = R == r ? R++ : R - 1;
  for (ll v : G[u])
    if (v != p)
      dfs(v, u, R);
  dp[r] = pre;
}

int main() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vec u(n - 1), v(n - 1);
  for (ll i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (ll i = 1; i <= n; i++)
    dp[i] = 2e9;
  dfs(0, -1, 1);
  for (ll i = 0; i < n; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 5e18;

ll n;
vec a(2 << 17);
mat G(2 << 17);
vec dp(2 << 17);
vec ans(2 << 17);

void dfs(ll u, ll p, ll R) {
  ll l = 0, r = R;
  while (r - l > 1) {
    ll m = l + r >> 1;
    if (dp[m] < a[u])
      l = m;
    else
      r = m;
  }
  ll pre = dp[r];
  dp[r] = a[u];
  ans[u] = R == r ? R++ : R - 1;
  for (ll v : G[u])
    if (v != p)
      dfs(v, u, R);
  dp[r] = pre;
}

int main() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vec u(n - 1), v(n - 1);
  for (ll i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
    for (ll i = 1; i <= n; i++)
      dp[i] = 2e9;
    dfs(0, -1, 1);
  }
  for (ll i = 0; i < n; i++)
    cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 2e9

using namespace std;
int connectSize[200000];
int connectPtr[200000];
int connect[400000];
long long dp[200000];
long long a[200000];
int answer[200000];
int N;

void dfs(int current, int parent, long long *dp, int maxL) {
  long long cA = a[current];
  long long *oldPtr = lower_bound(dp, dp + N, cA);
  long long oldV = *oldPtr;
  if (oldV == INF) {
    maxL += 1;
  }
  *oldPtr = cA;
  answer[current] = maxL;
  for (int i = 0; i < connectSize[current]; i++) {
    int forward = connect[connectPtr[current] + i];
    if (forward != parent) {
      dfs(forward, current, dp, maxL);
    }
  }
  *oldPtr = oldV;
}
int main() {
  int u[200000], v[200000];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  for (int i = 0; i < N - 1; i++) {
    cin >> u[i] >> v[i];
    connectSize[u[i] - 1]++;
    connectSize[v[i] - 1]++;
  }
  int ptr = 0;
  for (int i = 0; i < N; i++) {
    connectPtr[i] = ptr;
    ptr += connectSize[i];
  }
  {
    int cptr[200000];
    for (int i = 0; i < N; i++)
      cptr[i] = connectPtr[i];
    for (int i = 0; i < N - 1; i++) {
      connect[cptr[u[i] - 1]++] = v[i] - 1;
      connect[cptr[v[i] - 1]++] = u[i] - 1;
    }
  }

  for (int i = 0; i < N; i++)
    dp[i] = INF;
  dfs(0, -1, dp, 0);
  for (int i = 0; i < N; i++) {
    cout << answer[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF 10000000

using namespace std;
int connectSize[200000];
int connectPtr[200000];
int connect[400000];
int dp[200000];
long long a[200000];
int answer[200000];
int N;

void dfs(int current, int parent, int *dp, int maxL) {
  int cA = a[current];
  int *oldPtr = lower_bound(dp, dp + N, cA);
  int oldV = *oldPtr;
  if (oldV == INF) {
    maxL += 1;
  }
  *oldPtr = cA;
  answer[current] = maxL;
  for (int i = 0; i < connectSize[current]; i++) {
    int forward = connect[connectPtr[current] + i];
    if (forward != parent) {
      dfs(forward, current, dp, maxL);
    }
  }
  *oldPtr = oldV;
}
int main() {
  int u[200000], v[200000];
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  for (int i = 0; i < N - 1; i++) {
    cin >> u[i] >> v[i];
    connectSize[u[i] - 1]++;
    connectSize[v[i] - 1]++;
  }
  int ptr = 0;
  for (int i = 0; i < N; i++) {
    connectPtr[i] = ptr;
    ptr += connectSize[i];
  }
  {
    int cptr[200000];
    for (int i = 0; i < N; i++)
      cptr[i] = connectPtr[i];
    for (int i = 0; i < N - 1; i++) {
      connect[cptr[u[i] - 1]++] = v[i] - 1;
      connect[cptr[v[i] - 1]++] = u[i] - 1;
    }
  }

  for (int i = 0; i < N; i++)
    dp[i] = INF;
  dfs(0, -1, dp, 0);
  for (int i = 0; i < N; i++) {
    cout << answer[i] << endl;
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\\n"
typedef long long ll;

#define MX 200002
vector<int> g[MX];

ll vis[MX];
ll dp[MX];
ll arr[MX];
set<ll> s;
unordered_map<int, int> mp;
void dfs(int u) {
  vis[u] = 1;
  ll x = arr[u];
  set<ll>::iterator it = s.lower_bound(x);
  mp[x]++;
  ll y = -1;
  if (it == s.end()) {
    s.insert(x);
  } else {
    y = *it;
    mp[y]--;
    s.insert(x);
    if (mp[y] == 0)
      s.erase(it);
  }
  dp[u] = s.size();
  for (ll i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v])
      dfs(v);
  }
  if (y != -1) {
    s.insert(y);
    mp[y] += 1;
  }
  it = s.lower_bound(arr[u]);
  if (mp[arr[u]] == 1) {
    s.erase(it);
    mp[arr[u]] -= 1;
  } else
    mp[arr[u]] -= 1;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;

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

  dfs(1);
  for (int i = 1; i <= n; i++)
    cout << dp[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\\n"
typedef long long ll;

#define MX 200002
vector<int> g[MX];

int vis[MX];
int dp[MX];
int arr[MX];
set<int> s;
void dfs(int u) {
  vis[u] = 1;
  int x = arr[u];
  set<int>::iterator it = s.upper_bound(x);
  int y = -1;
  if (it == s.end()) {
    s.insert(x);
  } else {
    y = *it;
    s.insert(x);
    s.erase(it);
  }
  dp[u] = s.size();
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!vis[v])
      dfs(v);
  }
  if (y != -1)
    s.insert(y);

  it = s.find(x);
  s.erase(it);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;

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

  dfs(1);
  for (int i = 1; i <= n; i++)
    cout << dp[i] << endl;

  return 0;
}