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

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define deb(x) cout << #x << " " << x << endl;

typedef long long int ll;
typedef vector<vector<int>> vvi;

void DFS(vvi &graph, int cur, ll weight[], multiset<ll> &acc, int res[],
         int par = 0) {

  acc.insert(weight[cur]);
  auto k = acc.lower_bound(weight[cur]);

  bool last_erased = false;
  ll last_erased_val;

  k++;
  if (k != acc.end()) {
    last_erased = true;
    last_erased_val = *k;
    acc.erase(k);
  }

  res[cur] = acc.size();

  for (int x : graph[cur])
    if (x != par)
      DFS(graph, x, weight, acc, res, cur);

  if (last_erased) {
    acc.insert(last_erased_val);
  }
  acc.erase(acc.find(weight[cur]));
}

void SOLVE(vvi &graph, ll weight[], int n) {

  int res[n + 1];

  multiset<ll> acc;

  DFS(graph, 1, weight, acc, res);

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

int main() {

  IOS

      int n;
  cin >> n;

  vvi graph(n + 1);
  ll weight[n + 1];

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

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

  SOLVE(graph, weight, n);

  return 0;
}

Error: 0

Fixed submission:

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

#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define deb(x) cout << #x << " " << x << endl;

typedef long long int ll;
typedef vector<vector<int>> vvi;

void DFS(vvi &graph, int cur, ll weight[], set<ll> &acc, int res[],
         int par = 0) {

  acc.insert(weight[cur]);
  auto k = acc.lower_bound(weight[cur]);

  bool last_erased = false;
  ll last_erased_val;

  k++;
  if (k != acc.end()) {
    last_erased = true;
    last_erased_val = *k;
    acc.erase(k);
  }

  res[cur] = acc.size();

  for (int x : graph[cur])
    if (x != par)
      DFS(graph, x, weight, acc, res, cur);

  if (last_erased) {
    acc.insert(last_erased_val);
  }
  acc.erase(acc.find(weight[cur]));
}

void SOLVE(vvi &graph, ll weight[], int n) {

  int res[n + 1];

  set<ll> acc;

  DFS(graph, 1, weight, acc, res);

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

int main() {

  IOS

      int n;
  cin >> n;

  vvi graph(n + 1);
  ll weight[n + 1];

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

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

  SOLVE(graph, weight, n);

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

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

int n, u, v, a[200000], d[200000];
vector<int> adj[200000];

vector<int> lis = {int(-1e9)};

void dfs(int u) {
  bool inserted = (a[u] > lis.back());
  int idx, replaced;
  if (inserted) {
    lis.push_back(a[u]);
  } else {
    auto it = lower_bound(lis.begin(), lis.end(), a[u]);
    idx = it - lis.begin();
    replaced = lis[idx];
    lis[idx] = a[u];
  }

  d[u] = lis.size() - 1;

  for (int v : adj[u]) {
    if (!d[v])
      dfs(v);
  }
  if (inserted)
    lis.pop_back();
  else
    lis[idx] = replaced;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0);
  for (int i = 0; i < n; ++i)
    printf("%d\\n", d[i]);
}


Error: 0

Fixed submission:

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

int n, u, v, a[200000], d[200000];
vector<int> adj[200000];

vector<int> lis = {int(-1e9)};

void dfs(int u) {
  bool inserted = (a[u] > lis.back());
  vector<int>::iterator it;
  int replaced;
  if (inserted) {
    lis.push_back(a[u]);
  } else {
    it = lower_bound(lis.begin(), lis.end(), a[u]);
    replaced = *it;
    *it = a[u];
  }

  d[u] = lis.size() - 1;

  for (int v : adj[u]) {
    if (!d[v])
      dfs(v);
  }
  if (inserted)
    lis.pop_back();
  else
    *it = replaced;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0);
  for (int i = 0; i < n; ++i)
    printf("%d\\n", d[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

const int INVALID = -1;
using Graph = vector<vector<int>>;
vector<int> a;
vector<int> len;

void dfs(const Graph &graph, int v, int p, vector<int> &dp) {
  for (auto nv : graph[v]) {
    if (nv == p)
      continue;
    auto itr = lower_bound(dp.begin(), dp.end(), a[nv]);
    int idx = itr - dp.begin();
    int pre = -1;
    if (itr == dp.end()) {
      dp.push_back(a[nv]);
    } else {
      pre = *itr;
      *itr = a[nv];
    }
    len[nv] = dp.size();

    dfs(graph, nv, v, dp);

    if (pre == -1) {
      dp.pop_back();
    } else {
      dp[idx] = pre;
    }
  }
}

int main() {
  int N;
  cin >> N;

  a.assign(N, 0);
  for (int ii = 0; ii < N; ++ii) {
    cin >> a[ii];
  }

  Graph graph(N);
  for (int ii = 0; ii < N - 1; ++ii) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  len.assign(N, 0);
  len[0] = 1;
  int root = 0;
  vector<int> dp;
  dp.push_back(a[0]);
  dfs(graph, /*v=*/root, /*p=*/-1, dp);

  for (int ii = 0; ii < N; ++ii) {
    cout << len[ii] << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

const int INVALID = -1;
using Graph = vector<vector<int>>;
vector<int> a;
vector<int> len;
// vector <int> dp;

void dfs(const Graph &graph, int v, int p, vector<int> dp) {
  for (auto nv : graph[v]) {
    if (nv == p)
      continue;

    vector<int> copy = dp;
    auto itr = lower_bound(copy.begin(), copy.end(), a[nv]);
    if (itr == copy.end()) {
      copy.push_back(a[nv]);
    } else {
      *itr = a[nv];
    }
    len[nv] = copy.size();

    dfs(graph, nv, v, copy);
  }
}

int main() {
  int N;
  cin >> N;

  a.assign(N, 0);
  for (int ii = 0; ii < N; ++ii) {
    cin >> a[ii];
  }

  Graph graph(N);
  for (int ii = 0; ii < N - 1; ++ii) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  len.assign(N, 0);
  len[0] = 1;
  int root = 0;
  vector<int> dp;
  dp.push_back(a[0]);
  dfs(graph, /*v=*/root, /*p=*/-1, dp);

  for (int ii = 0; ii < N; ++ii) {
    cout << len[ii] << "\\n";
  }

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

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) (vec).begin(), (vec).end()
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, a[200100], x, y, res[200100], dp[200100], inf;
vector<int> ga[200100], vec;
void dfs(int u, int p) {
  int prv = lower_bound(dp, dp + n, a[u]) - dp;
  int rrr = dp[prv];
  dp[prv] = a[u];
  res[u] = lower_bound(dp, dp + n, inf) - dp;
  for (int i = 0; i < ga[u].size(); i++) {
    int v = ga[u][i];
    if (v == p)
      continue;
    dfs(v, u);
  }
  dp[prv] = rrr;
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, 63, sizeof(dp));
  inf = dp[0];
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], vec.push_back(a[i]);
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    ga[x].push_back(y);
    ga[y].push_back(x);
  }
  SOR(vec);
  UNI(vec);
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
  dfs(1, -1);
  for (int i = 1; i <= n; i++)
    cout << res[i] << endl;
  cout << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) (vec).begin(), (vec).end()
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, a[200100], x, y, res[200100], dp[200100], inf;
vector<int> ga[200100], vec;
void dfs(int u, int p) {
  int prv = lower_bound(dp, dp + n, a[u]) - dp;
  int rrr = dp[prv];
  dp[prv] = a[u];
  res[u] = lower_bound(dp, dp + n, inf) - dp;
  for (int i = 0; i < ga[u].size(); i++) {
    int v = ga[u][i];
    dfs(v, u);
  }
  dp[prv] = rrr;
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dp, 63, sizeof(dp));
  inf = dp[0];
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], vec.push_back(a[i]);
  for (int i = 1; i < n; i++) {
    cin >> x >> y;
    ga[x].push_back(y);
    ga[y].push_back(x);
  }
  SOR(vec);
  UNI(vec);
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
  dfs(1, -1);
  for (int i = 1; i <= n; i++)
    cout << res[i] << ' ';
  cout << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

// g++  5.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define MAXX 1e9 + 5
vector<ll> adj[200005];
ll arr[200005];
ll LIS[200005];

void DFS(ll s, ll p, vector<ll> &L, ll count) {
  ll index = lower_bound(L.begin(), L.end(), arr[s]) - L.begin();

  ll prev = L[index];

  if (prev == LLONG_MAX)
    count++;

  L[index] = arr[s];

  LIS[s] = count;

  for (ll i = 0; i < adj[s].size(); ++i) {
    ll c = adj[s][i];
    if (c != p)
      DFS(c, s, L, count);
  }

  L[index] = prev; // rolling back changes
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll N;
  cin >> N;

  for (ll i = 1; i <= N; ++i)
    cin >> arr[i];

  vector<ll> L(N, LLONG_MAX);

  for (ll i = 0; i < (N - 1); ++i) {
    ll u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  DFS(1, -1, L, 0);

  for (ll i = 1; i <= N; ++i)
    cout << LIS[i] << endl;
}

Error: TLE

Fixed submission:

// g++  5.4.0

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef int ll;
#define MAXX 1e9 + 5
vector<ll> adj[200005];
ll arr[200005];
ll LIS[200005];

void DFS(ll s, ll p, vector<ll> L) {
  ll index = lower_bound(L.begin(), L.end(), arr[s]) - L.begin();

  if (index == (ll)L.size())
    L.push_back(arr[s]);
  else
    L[index] = arr[s];

  LIS[s] = L.size();

  for (ll i = 0; i < adj[s].size(); ++i) {
    ll c = adj[s][i];
    if (c != p)
      DFS(c, s, L);
  }
}

int main() {
  /*ios_base::sync_with_stdio(false);
  cin.tie(NULL);*/

  ll N;
  cin >> N;

  for (ll i = 1; i <= N; ++i)
    cin >> arr[i];

  vector<ll> L;
  L.push_back(MAXX);

  for (ll i = 0; i < (N - 1); ++i) {
    ll u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  DFS(1, -1, L);

  for (ll i = 1; i <= N; ++i)
    cout << LIS[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define _READ freopen("input.txt", "r", stdin);
#define _FAST                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
vector<int> edges[200001];
void dfs(int i, int parent, set<int> &seq, vector<int> &ar, vector<int> &ans) {
  int temp = -1;
  if (seq.lower_bound(ar[i]) != seq.end()) {
    temp = *seq.lower_bound(ar[i]);
    seq.erase(seq.lower_bound(ar[i]));
    seq.insert(ar[i]);
  } else
    seq.insert(ar[i]);
  ans[i] = max(ans[i], (int)seq.size());
  for (auto j : edges[i]) {
    if (j != parent) {
      dfs(j, i, seq, ar, ans);
    }
  }
  seq.erase(ar[i]);
  if (temp != -1)
    seq.insert(temp);
}
void solve() {
  int n;
  cin >> n;
  vector<int> ar(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> ar[i];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vector<int> ans(n + 1, 1);
  set<int> seq = {ar[1]};
  dfs(1, -1, seq, ar, ans);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}
int32_t main() {
  _FAST

  int t;
  t = 1;
  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define _READ freopen("input.txt", "r", stdin);
#define _FAST                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
vector<int> edges[200001];
void dfs(int i, int parent, set<int> &seq, vector<int> &ar, vector<int> &ans) {
  set<int> temp = seq;
  for (auto j : edges[i]) {
    if (j != parent) {
      seq = temp;
      if (seq.lower_bound(ar[j]) != seq.end()) {
        // auto p1=*seq.lower_bound(ar[j]);
        seq.erase(seq.lower_bound(ar[j]));
        seq.insert(ar[j]);
        ans[j] = max(ans[j], (int)seq.size());
        dfs(j, i, seq, ar, ans);
        // seq.erase(ar[j]);
        // seq.insert(p1);
      } else {
        seq.insert(ar[j]);
        ans[j] = max(ans[j], (int)seq.size());
        dfs(j, i, seq, ar, ans);
        // seq.erase(ar[j]);
      }
      // for(auto p:seq)
      //     cout<<p<<" ";
      // cout<<"\\n";
    }
  }
}
void solve() {
  int n;
  cin >> n;
  vector<int> ar(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> ar[i];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  vector<int> ans(n + 1, 1);
  set<int> seq = {ar[1]};
  dfs(1, -1, seq, ar, ans);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}
int32_t main() {
  _FAST

  int t;
  t = 1;
  while (t--)
    solve();

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

void impl(std::vector<int> &results, std::vector<int> &dp, int node, int parent,
          std::vector<int> const &A,
          std::vector<std::vector<int>> const &paths) {
  auto const a = A.at(node);

  auto iter = std::lower_bound(dp.begin(), dp.end(), a);
  int prev;
  bool pushed = false;
  if (iter == dp.end()) {
    dp.push_back(a);
    pushed = true;
  } else {
    prev = *iter;
    *iter = a;
  }

  results.at(node) = static_cast<int>(dp.size());

  for (auto child : paths.at(node)) {
    if (child != parent) {
      impl(results, dp, child, node, A, paths);
    }
  }

  if (pushed) {
    dp.pop_back();
  } else {
    *iter = prev;
  }
}

int main() {
  int N;
  std::cin >> N;

  std::vector<int> A;
  A.reserve(N);
  for (auto i = 0; i < N; i++) {
    int a;
    std::cin >> a;
    A.push_back(a);
  }

  std::vector<std::vector<int>> paths(N);
  for (auto i = 0; i < N - 1; i++) {
    int u, v;
    std::cin >> u >> v;
    u--;
    v--;

    paths.at(u).push_back(v);
    paths.at(v).push_back(u);
  }

  std::vector<int> results(N);
  std::vector<int> dp;
  dp.reserve(N);
  impl(results, dp, 0, -1, A, paths);

  for (auto result : results) {
    std::cout << result << std::endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

void impl(std::vector<int> &results, std::vector<int> &dp, int node, int parent,
          std::vector<int> const &A,
          std::vector<std::vector<int>> const &paths) {
  auto const a = A.at(node);

  auto iter = std::upper_bound(dp.begin(), dp.end(), a);
  int prev;
  bool pushed = false;
  if (iter == dp.end()) {
    dp.push_back(a);
    pushed = true;
  } else {
    prev = *iter;
    *iter = a;
  }

  results.at(node) = static_cast<int>(dp.size() - 1);

  for (auto child : paths.at(node)) {
    if (child != parent) {
      impl(results, dp, child, node, A, paths);
    }
  }

  if (pushed) {
    dp.pop_back();
  } else {
    *iter = prev;
  }
}

int main() {
  int N;
  std::cin >> N;

  std::vector<int> A;
  A.reserve(N);
  for (auto i = 0; i < N; i++) {
    int a;
    std::cin >> a;
    A.push_back(a);
  }

  std::vector<std::vector<int>> paths(N);
  for (auto i = 0; i < N - 1; i++) {
    int u, v;
    std::cin >> u >> v;
    u--;
    v--;

    paths.at(u).push_back(v);
    paths.at(v).push_back(u);
  }

  std::vector<int> results(N);
  std::vector<int> dp{0};
  impl(results, dp, 0, -1, A, paths);

  for (auto result : results) {
    std::cout << result << std::endl;
  }

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M (998244353)
// #define INF 1e16
typedef long long ll;
typedef pair<int, vector<ll>> P;
typedef pair<P, ll> Q;

const int MAX_N = 200005;
const int INF = 1001001001;
vector<int> g[MAX_N];
vector<int> a, ans;
int dp[MAX_N];

void dfs(int v, int p = -1) {
  int index = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  ll old = dp[index];
  dp[index] = a[v];
  ans[v] = index;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);
  for (int j = 0; j < (ll)g[v].size(); j++) {
    int next = g[v][j];
    if (next == p)
      continue;
    dfs(next, v);
  }
  dp[index] = old;
  return;
}

int main(void) {
  ll n;
  cin >> n;
  a = ans = vector<int>(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < n - 1; i++) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (ll 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 <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<int, vector<ll>> P;
typedef pair<P, ll> Q;

vector<int> g[200010];
bool visited[200010];
int ans[200010];

int main(void) {
  int n;
  cin >> n;
  vector<ll> a(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);
  }
  vector<ll> dp;
  dp.push_back(a[0]);
  ans[0] = 1;
  queue<P> que;
  que.push(P(0, dp));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll now = p.first;
    visited[now] = true;
    for (ll i = 0; i < (ll)g[now].size(); i++) {
      int next = g[now][i];
      if (visited[next])
        continue;
      vector<ll> dp1;
      dp1 = p.second;
      auto itr = lower_bound(dp1.begin(), dp1.end(), a[next]);
      if (itr == dp1.end())
        dp1.push_back(a[next]);
      else
        *itr = a[next];
      ans[next] = (ll)dp1.size();
      que.push(P(next, dp1));
    }
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> mv;

int a[200000];
int res[200000];
int v[200000];
int tail[200000];

int CI(int l, int r, int key) {
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (tail[m] >= key)
      r = m;
    else
      l = m;
  }

  return r;
}

void loo(int ac, int fa, int vs, int le) {
  v[vs] = a[ac];
  int m, f, im;
  if (ac != 0) {
    if (v[vs] < tail[0]) {
      f = tail[0];
      tail[0] = v[vs];
      m = 0;
    } else if (v[vs] > tail[le - 1]) {
      tail[le++] = v[vs];
      m = 1;
    } else {
      im = CI(-1, le - 1, v[vs]);
      f = tail[im];
      tail[im] = v[vs];
      m = 2;
    }
  } else
    tail[0] = v[vs];
  for (int i = 0; i < mv[ac].size(); i++)
    if (mv[ac][i] != fa) {
      loo(mv[ac][i], ac, vs + 1, le);
    }
  res[ac] = le;
  if (ac != 0)
    switch (m) {
    case 0: {
      tail[0] = f;
      break;
    }
    case 2: {

      tail[im] = f;
      break;
    }
    }
}
int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  mv.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    mv[u].push_back(v);
    mv[v].push_back(u);
  }
  int vs = 0;
  loo(0, -1, vs, 1);
  for (int i = 0; i < n; i++)
    cout << res[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> mv;

int a[200000];
int res[200000];
int v[200000];
int CI(vector<int> &v, int l, int r, int key) {
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (v[m] >= key)
      r = m;
    else
      l = m;
  }

  return r;
}

int LIS(int vs) {
  if (vs == 0)
    return 0;

  vector<int> tail(vs, 0);
  int length = 1;

  tail[0] = v[0];
  for (size_t i = 1; i < vs; i++) {

    if (v[i] < tail[0])
      tail[0] = v[i];

    else if (v[i] > tail[length - 1])
      tail[length++] = v[i];

    else
      tail[CI(tail, -1, length - 1, v[i])] = v[i];
  }

  return length;
}

void loo(int ac, int fa, int vs) {
  v[vs] = a[ac];
  vs++;
  res[ac] = LIS(vs);
  for (int i = 0; i < mv[ac].size(); i++)
    if (mv[ac][i] != fa) {
      loo(mv[ac][i], ac, vs);
    }
}
int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  mv.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    mv[u].push_back(v);
    mv[v].push_back(u);
  }
  int vs = 0;
  loo(0, -1, vs);
  for (int i = 0; i < n; i++)
    cout << res[i] << endl;

  return 0;
}