alexjercan/bugnet huggingface dataset:
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define endl '\\n'
#define y1 holyshit
#define all(x) x.begin(), x.end()
const int inf = 0x3f3f3f3f;

int N, d[200010], dp[200010], ans[200010];
vector<int> edge[200010];

void dfs(int now, int prv) {
  int curr = lower_bound(dp, dp + N, d[now]) - dp, save = dp[curr];
  ans[now] = max(ans[now], curr);
  dp[curr] = min(dp[curr], d[now]);
  for (auto i : edge[now])
    if (i != prv)
      ans[i] = max(ans[i], ans[now]), dfs(i, now);
  dp[curr] = save;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> d[i];
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  memset(dp, inf, sizeof(dp));
  dp[0] = -1;
  dfs(1, 0);
  for (int i = 1; i <= N; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define endl '\\n'
#define y1 holyshit
#define all(x) x.begin(), x.end()
const int inf = 0x3f3f3f3f;

int N, d[200010], dp[200010], ans[200010];
vector<int> edge[200010];

void dfs(int now, int prv) {
  ans[now] = lower_bound(dp, dp + N, d[now]) - dp;
  int save = dp[ans[now]];
  dp[ans[now]] = min(dp[ans[now]], d[now]);
  for (auto i : edge[now])
    if (i != prv)
      dfs(i, now);
  dp[ans[now]] = save;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  vector<int> comp;
  for (int i = 1; i <= N; i++)
    cin >> d[i], comp.push_back(d[i]);
  sort(all(comp));
  comp.erase(unique(all(comp)), comp.begin());
  for (int i = 1; i <= N; i++)
    d[i] = lower_bound(all(comp), d[i]) - comp.begin();
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  memset(dp, inf, sizeof(dp));
  dp[0] = -1;
  dfs(1, 0);
  for (int i = 1; i <= N; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

const int MAXN = 1 << 18; // 2.6e5
const int N = 200005;
ll seg[2 * MAXN], rj[N]; // root is seg[1]

int c[N], b[N];
ll n, a[N];

vector<int> g[N];

void build() {
  for (int i = N - 1; i >= 0; i--)
    seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}

void update(int p, ll val) {
  for (seg[p += N] = val; p > 0; p >>= 1)
    seg[p >> 1] = max(seg[p], seg[p ^ 1]);
}

ll query(int l, int r) // [l, r]
{
  ll res = -1e9;
  for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, seg[l++]);
    if (!(r & 1))
      res = max(res, seg[r--]);
  }
  return res;
}

void f(int p, int pr) {
  int v = query(a[p], a[p]);

  int r = query(0, a[p] - 1);

  update(a[p], r + 1);

  rj[p] = query(0, N - 1);

  for (int i : g[p])
    if (i != pr)
      f(i, p);

  update(a[p], v);
}

int main() {
  cin >> n;

  for (int i = 1; i <= n; ++i)
    cin >> a[i], c[i] = a[i];

  sort(c, c + n);
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(c, c + n, a[i]) - c;

  for (int i = 0; i < n - 1; ++i) {
    int t1, t2;
    cin >> t1 >> t2;

    g[t1].push_back(t2);
    g[t2].push_back(t1);
  }

  f(1, -1);

  for (int i = 1; i <= n; ++i)
    cout << rj[i] << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;

const int MAXN = 1 << 18; // 2.6e5
const int N = 200005;
ll seg[2 * MAXN], rj[N]; // root is seg[1]

ll n, a[N];

vector<int> g[N];

void build() {
  for (int i = N - 1; i >= 0; i--)
    seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}

void update(int p, ll val) {
  for (seg[p += N] = val; p > 0; p >>= 1)
    seg[p >> 1] = max(seg[p], seg[p ^ 1]);
}

ll query(int l, int r) // [l, r]
{
  ll res = -1e9;
  for (l += N, r += N; l <= r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, seg[l++]);
    if (!(r & 1))
      res = max(res, seg[r--]);
  }
  return res;
}

void f(int p, int pr) {
  int v = query(a[p], a[p]);

  int r = query(0, a[p]);

  update(a[p], r + 1);

  rj[p] = query(0, N - 1);

  for (int i : g[p])
    if (i != pr)
      f(i, p);

  update(a[p], v);
}

int main() {
  cin >> n;

  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  for (int i = 0; i < n - 1; ++i) {
    int t1, t2;
    cin >> t1 >> t2;

    g[t1].push_back(t2);
    g[t2].push_back(t1);
  }

  f(1, -1);

  for (int i = 1; i <= n; ++i)
    cout << rj[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define ll long long
int n, MAX, sz;
int segmax[800005];
void init() {
  sz = 1;
  while (sz < MAX)
    sz *= 2;
}
int getMax(int i) { return segmax[i += sz]; }
void updateMax(int i, int v) {
  i += sz;
  segmax[i] = v;
  while (i > 1) {
    i /= 2;
    segmax[i] = max(segmax[2 * i], segmax[2 * i + 1]);
  }
}
int queryMax(int l, int r) {
  l += sz;
  r += sz;
  int res = 0;
  while (l < r) {
    if (l % 2)
      res = max(res, segmax[l++]);
    if (r % 2)
      res = max(res, segmax[--r]);
    l /= 2;
    r /= 2;
  }
  return res;
}

vector<int> adj[200005];
vector<int> order[200005];
int dp[200005];
int x[200005];
int cnt = 0;
int m;
map<int, int> compress;
void dfsjump(int curr = 0, int par = -1) {
  int val = queryMax(0, x[curr]);
  int bef = getMax(x[curr]);
  updateMax(x[curr], val + 1);
  dp[curr] = val + 1;
  m = max(m, val + 1);
  for (int next : adj[curr]) {
    if (next != par) {
      dfsjump(next, curr);
    }
  }
  updateMax(x[curr], bef);
}
void dfs(int curr = 0, int par = -1) {
  if (par != -1)
    dp[curr] = max(dp[curr], dp[par]);
  for (int next : adj[curr])
    if (next != par)
      dfs(next, curr);
}
int main() {
  scanf("%d", &n);
  set<int> r;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    r.insert(x[i]);
  }
  int v = 0;
  for (auto &it : r)
    compress[it] = v++;
  for (int i = 0; i < n; i++) {
    x[i] = compress[x[i]];
    MAX = max(MAX, x[i]);
  }
  ++MAX;
  init();
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfsjump();
  dfs();
  for (int i = 0; i < n; i++)
    printf("%d\\n", dp[i]);
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define ll long long
int n, MAX;
int segmax[8000005];
int getMax(int i) { return segmax[i += MAX]; }
void updateMax(int i, int v) {
  i += MAX;
  segmax[i] = v;
  while (i > 1) {
    i /= 2;
    segmax[i] = max(segmax[2 * i], segmax[2 * i + 1]);
  }
}
int queryMax(int l, int r) {
  l += MAX;
  r += MAX;
  int res = 0;
  while (l < r) {
    if (l % 2)
      res = max(res, segmax[l++]);
    if (r % 2)
      res = max(res, segmax[--r]);
    l /= 2;
    r /= 2;
  }
  return res;
}

vector<int> adj[2000005];
vector<int> order[2000005];
int dp[2000005];
int x[2000005];
int cnt = 0;
int m;
void dfsjump(int curr = 0, int par = -1) {
  int val = queryMax(0, x[curr] + 1);
  int bef = getMax(x[curr]);
  updateMax(x[curr], val + 1);
  dp[curr] = val + 1;
  m = max(m, val + 1);
  for (int next : adj[curr]) {
    if (next != par) {
      dfsjump(next, curr);
    }
  }
  updateMax(x[curr], bef);
}
int main() {
  scanf("%d", &n);
  MAX = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    MAX = max(MAX, x[i]);
  }
  ++MAX;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfsjump();
  for (int i = 0; i < n; i++)
    printf("%d\\n", dp[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll N;
vector<ll> lcs;
vector<multiset<ll>> st;

vector<ll> ans;
vector<vector<ll>> connection;
vector<ll> A;

void dfs(ll now, ll before) {
  ll a = A.at(now);
  ll index = lower_bound(lcs.begin(), lcs.end(), a) - lcs.begin();
  lcs.at(index) = a;
  st.at(index).insert(a);
  ans.at(now) = lower_bound(lcs.begin(), lcs.end(), INF) - lcs.begin();
  for (ll i = 0; i < connection.at(now).size(); ++i) {
    ll next = connection.at(now).at(i);
    if (next == before) {
      continue;
    }
    dfs(next, now);
  }
  st.at(index).erase(st.at(index).lower_bound(a));
  lcs.at(index) = *st.at(index).begin();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  lcs = vector<ll>(N, INF);
  ans = vector<ll>(N);
  st = vector<multiset<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    st.at(i).insert(INF);
  }
  A = vector<ll>(N);
  connection = vector<vector<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (ll i = 0; i < N - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u -= 1;
    v -= 1;
    connection.at(u).push_back(v);
    connection.at(v).push_back(u);
  }
  dfs(0, -1);
  for (ll i = 0; i < N; ++i) {
    cout << ans.at(i) << "\\n";
  }
}


Error: TLE

Fixed submission:

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

ll N;
vector<ll> lcs;
vector<multiset<ll>> st;

vector<ll> ans;
vector<vector<ll>> connection;
vector<ll> A;

void dfs(ll now, ll before) {
  ll a = A.at(now);
  ll index = lower_bound(lcs.begin(), lcs.end(), a) - lcs.begin();
  lcs.at(index) = a;
  st.at(index).insert(a);
  ans.at(now) = lower_bound(lcs.begin(), lcs.end(), INF) - lcs.begin();
  for (ll i = 0; i < connection.at(now).size(); ++i) {
    ll next = connection.at(now).at(i);
    if (next == before) {
      continue;
    }
    dfs(next, now);
  }
  st.at(index).erase(lower_bound(st.at(index).begin(), st.at(index).end(), a));
  lcs.at(index) = *st.at(index).begin();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  lcs = vector<ll>(N, INF);
  ans = vector<ll>(N);
  st = vector<multiset<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    st.at(i).insert(INF);
  }
  A = vector<ll>(N);
  connection = vector<vector<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (ll i = 0; i < N - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u -= 1;
    v -= 1;
    connection.at(u).push_back(v);
    connection.at(v).push_back(u);
  }
  dfs(0, -1);
  for (ll i = 0; i < N; ++i) {
    cout << ans.at(i) << "\\n";
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll N;
vector<ll> lcs;
vector<stack<ll>> st;

vector<ll> ans;
vector<vector<ll>> connection;
vector<ll> A;

void dfs(ll now, ll before) {
  ll a = A.at(now);
  ll index = lower_bound(lcs.begin(), lcs.end(), a) - lcs.begin();
  lcs.at(index) = a;
  st.at(index).push(a);
  ans.at(now) = lower_bound(lcs.begin(), lcs.end(), INF) - lcs.begin();
  for (ll i = 0; i < connection.at(now).size(); ++i) {
    ll next = connection.at(now).at(i);
    if (next == before) {
      continue;
    }
    dfs(next, now);
  }
  st.at(index).pop();
  lcs.at(index) = st.at(index).top();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  lcs = vector<ll>(N, INF);
  ans = vector<ll>(N);
  st = vector<stack<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    st.at(i).push(INF);
  }
  A = vector<ll>(N);
  connection = vector<vector<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (ll i = 0; i < N - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u -= 1;
    v -= 1;
    connection.at(u).push_back(v);
    connection.at(v).push_back(u);
  }
  dfs(0, -1);
  for (ll i = 0; i < N; ++i) {
    cout << ans.at(i) << "\\n";
  }
}


Error: TLE

Fixed submission:

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

ll N;
vector<ll> lcs;
vector<multiset<ll>> st;

vector<ll> ans;
vector<vector<ll>> connection;
vector<ll> A;

void dfs(ll now, ll before) {
  ll a = A.at(now);
  ll index = lower_bound(lcs.begin(), lcs.end(), a) - lcs.begin();
  lcs.at(index) = a;
  st.at(index).insert(a);
  ans.at(now) = lower_bound(lcs.begin(), lcs.end(), INF) - lcs.begin();
  for (ll i = 0; i < connection.at(now).size(); ++i) {
    ll next = connection.at(now).at(i);
    if (next == before) {
      continue;
    }
    dfs(next, now);
  }
  st.at(index).erase(lower_bound(st.at(index).begin(), st.at(index).end(), a));
  lcs.at(index) = *st.at(index).begin();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  lcs = vector<ll>(N, INF);
  ans = vector<ll>(N);
  st = vector<multiset<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    st.at(i).insert(INF);
  }
  A = vector<ll>(N);
  connection = vector<vector<ll>>(N);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (ll i = 0; i < N - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u -= 1;
    v -= 1;
    connection.at(u).push_back(v);
    connection.at(v).push_back(u);
  }
  dfs(0, -1);
  for (ll i = 0; i < N; ++i) {
    cout << ans.at(i) << "\\n";
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define endl "\\n"
const int N = 200005;

vector<int> adj[N];
vector<bool> vis(N, false);

vector<int> a(N);

vector<int> ans(N);

multiset<int> ss;

int timer = 0;

vector<int> entrytimer(N, 0), exitimer(N, 0);

void dfs(int s) {
  if (!vis[s]) {
    vis[s] = true;

    auto it = ss.lower_bound(a[s]);

    int y = -1;

    ss.insert(a[s]);
    if (it != ss.end()) {
      y = *it;
      ss.erase(it);
    }

    ans[s] = ss.size();
    for (auto &i : adj[s]) {
      if (!vis[i])
        dfs(i);
    }

    auto it1 = ss.find(a[s]);
    if (it1 != ss.end())
      ss.erase(it1);

    if (y != -1)
      ss.insert(y);
  }
}

int32_t main() {

  IOS

      int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  dfs(1);

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define endl "\\n"
const int N = 200005;

vector<int> adj[N];
vector<bool> vis(N, false);

vector<int> a(N);

vector<int> ans(N);

void dfs(int s, vector<int> v) {
  if (!vis[s]) {

    vis[s] = true;

    auto it = lower_bound(v.begin(), v.end(), a[s]);
    if (it != v.end())
      *it = a[s];
    else
      v.push_back(a[s]);

    ans[s] = v.size();

    for (auto &i : adj[s]) {
      if (!vis[i])
        dfs(i, v);
    }
  }
}

int32_t main() {

  IOS

      int n;
  cin >> n;

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }

  vector<int> v;

  dfs(1, v);

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

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1000000007;

int N, A[200000], res[200000];
vector<int> v[200000], V;

void dfs(int x, int from) {
  int it = lower_bound(V.begin(), V.end(), A[x]) - V.begin();
  if (it == V.size()) {
    V.push_back(A[x]);
    res[x] = V.size();
    for (int i : v[x]) {
      if (i == from)
        continue;
      dfs(i, x);
    }
    V.pop_back();
  } else {
    res[x] = V.size();
    swap(V[it], A[x]);
    for (int i : v[x]) {
      if (i == from)
        continue;
      dfs(i, x);
    }
    swap(V[it], A[x]);
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0, 0);
  for (int i = 0; i < N; i++) {
    cout << res[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1000000007;

int N, A[200000], res[200000];
vector<int> v[200000], V;

void dfs(int x, int from) {
  auto it = lower_bound(V.begin(), V.end(), A[x]);
  if (it == V.end()) {
    V.push_back(A[x]);
    res[x] = V.size();
    for (int i : v[x]) {
      if (i == from)
        continue;
      dfs(i, x);
    }
    V.pop_back();
  } else {
    res[x] = V.size();
    int tmp = *it;
    *it = A[x];
    for (int i : v[x]) {
      if (i == from)
        continue;
      dfs(i, x);
    }
    *it = tmp;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0, 0);
  for (int i = 0; i < N; i++) {
    cout << res[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

#define INF 1000000009

using namespace std;

void dfs(const vector<vector<int>> &table, const vector<int> &values,
         vector<bool> &checked, int node, vector<int> &dp, vector<int> &ans) {
  checked[node] = true;

  int old_value;

  auto it = lower_bound(dp.begin(), dp.end(), values[node]);
  old_value = *it;
  *it = values[node];

  for (auto new_node : table[node]) {
    if (not checked[new_node])
      dfs(table, values, checked, new_node, dp, ans);
  }

  ans[node] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();

  *it = old_value;

  return;
}

int main() {
  int N, u, v;
  cin >> N;

  vector<int> values(N, 0);
  vector<vector<int>> table(N, vector<int>());

  vector<bool> checked(N, false);
  vector<int> dp(N, INF);
  vector<int> ans(N, 0);

  for (int i = 0; i < N; ++i) {
    cin >> values[i];
  }

  for (int i = 1; i < N; ++i) {
    cin >> u >> v;
    table[u - 1].push_back(v - 1);
    table[v - 1].push_back(u - 1);
  }

  dfs(table, values, checked, 0, dp, ans);

  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

#define INF 1000000009

using namespace std;

void dfs(const vector<vector<int>> &table, const vector<int> &values,
         vector<bool> &checked, int node, vector<int> &dp,
         stack<vector<int>> &stack_dp, vector<int> &ans) {
  checked[node] = true;

  stack_dp.push(dp);

  auto it = lower_bound(dp.begin(), dp.end(), values[node]);
  *it = values[node];

  for (auto new_node : table[node]) {
    if (not checked[new_node])
      dfs(table, values, checked, new_node, dp, stack_dp, ans);
  }

  ans[node] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();

  dp = stack_dp.top();
  stack_dp.pop();

  return;
}

int main() {
  int N, a, u, v;
  cin >> N;

  vector<int> values(N, 0);
  vector<vector<int>> table(N, vector<int>());

  vector<bool> checked(N, false);
  vector<int> dp(N, INF);
  stack<vector<int>> stack_dp;
  vector<int> ans(N, 0);

  for (int i = 0; i < N; ++i) {
    cin >> values[i];
  }

  for (int i = 1; i < N; ++i) {
    cin >> u >> v;
    table[u - 1].push_back(v - 1);
    table[v - 1].push_back(u - 1);
  }

  dfs(table, values, checked, 0, dp, stack_dp, ans);

  for (int i = 0; i < N; ++i) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200005;
const int INF = 1001001001;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];

void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = lower_bound(dp, dp + MAX_N, INF) - dp - 1;
  for (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}

int main() {
  int N;
  cin >> N;
  a = ans = vector<int>(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<vector<int>> G(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].push_back(v);
    to[v].push_back(u);
  }

  for (int i = 0; i < MAX_N; i++)
    dp[i] = INF;
  dp[0] = -INF;
  dfs(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<vector<int>> G(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  vector<int> prev(N, -1);
  vector<bool> seen(N, false);
  vector<int> reach;
  queue<int> que;
  seen[0] = true;
  que.push(0);
  reach.push_back(0);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto next_v : G[v]) {
      if (seen[next_v])
        continue;
      seen[next_v] = true;
      que.push(next_v);
      prev[next_v] = v;
      reach.push_back(next_v);
    }
  }

  vector<vector<int>> l(N);
  for (int i = 0; i < (int)reach.size(); i++) {
    if (prev[reach[i]] == -1)
      l[reach[i]].push_back(A[reach[i]]);
    else {
      l[reach[i]] = l[prev[reach[i]]];
      if (l[reach[i]][l[reach[i]].size() - 1] < A[reach[i]]) {
        l[reach[i]].push_back(A[reach[i]]);
      } else {
        int s =
            lower_bound(l[reach[i]].begin(), l[reach[i]].end(), A[reach[i]]) -
            l[reach[i]].begin();
        l[reach[i]][s] = A[reach[i]];
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << l[i].size() << endl;
  }

  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

int n;
int a[200000];
vector<int> g[200000];
int ans[200000], dp[200000];
stack<pair<int, int>> s;

void dfs(int cur, int p) {
  auto it = lower_bound(dp, dp + n, a[cur]);
  int idx = it - dp;
  s.push(make_pair(idx, dp[idx]));
  dp[idx] = min(dp[idx], a[cur]);
  ans[cur] = lower_bound(dp, dp + n, 1e9 + 1) - dp;
  for (int to : g[cur]) {
    if (to == p) {
      continue;
    }
    dfs(to, cur);
  }
  auto top = s.top();
  s.pop();
  dp[top.first] = top.second;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++)
    ans[i] = 0;
  for (int i = 0; i < n; i++)
    dp[i] = 1e9 + 1;
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

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

int n;
int a[100000];
vector<int> g[100000];
int ans[100000], dp[100000];
stack<pair<int, int>> s;

void dfs(int cur, int p) {
  auto it = upper_bound(dp, dp + n, a[cur]);
  int idx = it - dp;
  ans[cur] = idx + 1;
  s.push(make_pair(idx, dp[idx]));
  dp[idx] = min(dp[idx], a[cur]);
  for (int to : g[cur]) {
    if (to == p) {
      continue;
    }
    dfs(to, cur);
  }
  auto top = s.top();
  s.pop();
  dp[top.first] = top.second;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++)
    ans[i] = 0;
  for (int i = 0; i < n; i++)
    dp[i] = 1e9;
  dfs(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}