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

/* Isn't passion overrated?*/

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
using namespace std;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
vector<vector<ll>> arr;
vector<ll> cost;
vector<ll> ans, out;
void dfs(ll root, ll parent) {
  bool b = false;
  ll val = -1;
  ll idx = -1;
  vector<ll>::iterator itr = lower_bound(ans.begin(), ans.end(), cost[root]);
  if (itr == ans.end()) {
    ans.pb(cost[root]);
  } else {
    idx = itr - ans.begin();
    val = ans[idx];
    ans[idx] = cost[root];
    b = true;
  }
  out[root] = ans.size();
  for (ll i = 0; i < arr[root].size(); i++) {
    if (arr[root][i] == parent)
      continue;
    dfs(arr[root][i], root);
  }
  if (b) {
    ans[idx] = val;
  } else {
    ans.pop_back();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  arr = vector<vector<ll>>(n + 1);
  cost = vector<ll>(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> cost[i];
  out = vector<ll>(n + 1);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    arr[a].pb(b);
    arr[b].pb(a);
  }
  dfs(1, -1);
  for (ll i = 1; i < out.size(); i++)
    cout << out[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

/* Isn't passion overrated?*/

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
using namespace std;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
vector<vector<ll>> arr;
vector<ll> cost;
vector<ll> ans, out;
void dfs(ll root, ll parent) {
  bool b = false;
  ll val = -1;
  vector<ll>::iterator itr = lower_bound(ans.begin(), ans.end(), cost[root]);
  if (itr == ans.end()) {
    ans.pb(cost[root]);
  } else {
    val = ans[itr - ans.begin()];
    ans[itr - ans.begin()] = cost[root];
    b = true;
  }
  out[root] = ans.size();
  for (ll i = 0; i < arr[root].size(); i++) {
    if (arr[root][i] == parent)
      continue;
    dfs(arr[root][i], root);
  }
  if (b) {
    ans[itr - ans.begin()] = val;
  } else {
    ans.pop_back();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  arr = vector<vector<ll>>(n + 1);
  cost = vector<ll>(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> cost[i];
  out = vector<ll>(n + 1);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    arr[a].pb(b);
    arr[b].pb(a);
  }
  dfs(1, 1);
  for (ll i = 1; i < out.size(); i++)
    cout << out[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<int> to[200005];
ll ans[200005];
ll a[200005];
vector<ll> vec(200005);

pair<int, ll> dfs(int x, int i, int p = -1) {
  pair<int, ll> res;
  if (a[x] > vec[i]) {
    res.first = i + 1;
    res.second = vec[i + 1];
    vec[i + 1] = a[x];
    i++;
  } else {
    auto it = lower_bound(vec.begin(), vec.begin() + i + 1, a[x]);
    int y = it - vec.begin();
    res.first = y;
    res.second = vec[y];
    vec[y] = a[x];
  }
  ans[x] = i + 1;
  for (int e : to[x]) {
    if (e == p)
      continue;
    pair<int, ll> temp = dfs(e, i, x);
    vec[temp.first] = temp.second;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }

  vec[0] = 1E+18;
  pair<int, ll> p = dfs(0, 0);

  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<int> to[200005];
ll ans[200005];
ll a[200005];

void dfs(int x, vector<ll> v, int p = -1) {
  if (a[x] > v.back())
    v.push_back(a[x]);
  else {
    auto it = upper_bound(v.begin(), v.end(), a[x]);
    int y = it - v.begin();
    v[y] = a[x];
  }
  ans[x] = v.size();
  for (int e : to[x]) {
    if (e == p)
      continue;
    dfs(e, v, x);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }
  vector<ll> temp(1);
  temp[0] = 1E+18;
  dfs(0, temp);

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

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

struct node {
  int a;
  vector<int> to;
  int ans;
};
stack<pair<int, int>> s;
void dfs(int v, int p, vector<node> &nodes, vector<int> &dp) {
  int changed =
      distance(dp.begin(), lower_bound(dp.begin(), dp.end(), nodes[v].a));
  s.push(make_pair(changed, dp[changed]));
  dp[changed] = nodes[v].a;
  for (int i = 0; i < nodes[v].to.size(); i++) {
    int v_to = nodes[v].to[i];
    if (v_to == p)
      continue;
    dfs(v_to, v, nodes, dp);
  }
  nodes[v].ans =
      (int)distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
  dp[s.top().first] = s.top().second;
  s.pop();
}
int main() {
  int N;
  cin >> N;
  vector<node> nodes(N);
  vector<int> dp(N, INF);
  for (int i = 0; i < N; i++)
    cin >> nodes[i].a;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    nodes[u - 1].to.push_back(v - 1);
    nodes[v - 1].to.push_back(u - 1);
  }
  dfs(0, -1, nodes, dp);
  for (int i = 0; i < N; i++) {
    cout << nodes[i].ans << endl;
  }
}

Error: TLE

Fixed submission:

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

struct node {
  int a;
  vector<int> to;
  int ans;
};
stack<pair<int, int>> s;
void dfs(int v, int p, vector<node> &nodes, vector<int> &dp) {
  int changed =
      distance(dp.begin(), lower_bound(dp.begin(), dp.end(), nodes[v].a));
  s.push(make_pair(changed, dp[changed]));
  dp[changed] = nodes[v].a;
  for (int i = 0; i < nodes[v].to.size(); i++) {
    int v_to = nodes[v].to[i];
    if (v_to == p)
      continue;
    dfs(v_to, v, nodes, dp);
  }
  nodes[v].ans =
      (int)distance(dp.begin(), lower_bound(dp.begin(), dp.end(), INF));
  dp[s.top().first] = s.top().second;
  s.pop();
}
int main() {
  int N;
  cin >> N;
  vector<node> nodes(N);
  vector<int> dp(N, INF);
  for (int i = 0; i < N; i++)
    cin >> nodes[i].a;
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    nodes[u - 1].to.push_back(v - 1);
    nodes[v - 1].to.push_back(u - 1);
  }
  dfs(0, -1, nodes, dp);
  for (int i = 0; i < N; i++) {
    cout << nodes[i].ans << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> v[200000], a, ret, tmp;
int n;

void dfs(int par, int pos) {
  int len = lower_bound(tmp.begin(), tmp.end(), a[pos]) - tmp.begin();
  int bef = tmp[len];
  tmp[len] = a[pos];
  ret[pos] = lower_bound(tmp.begin(), tmp.end(), 1 << 30) - tmp.begin();
  for (int next : v[pos]) {
    if (next == par)
      continue;
    dfs(pos, next);
  }
  tmp[len] = bef;
}

int main() {
  cin >> n;
  a.resize(n);
  ret.resize(n);
  tmp.resize(n + 1, 1 << 30);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(-1, 0);
  for (int i = 0; i < n; i++) {
    cout << ret[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> v[100000], a, ret, tmp;
int n;

void dfs(int par, int pos) {
  int len = lower_bound(tmp.begin(), tmp.end(), a[pos]) - tmp.begin();
  int bef = tmp[len];
  ret[pos] = len + 1;
  tmp[len] = a[pos];
  for (int next : v[pos]) {
    if (next == par)
      continue;
    dfs(pos, next);
  }
  tmp[len] = bef;
}

int main() {
  cin >> n;
  a.resize(n);
  ret.resize(n);
  tmp.resize(n, 1 << 30);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(-1, 0);
  for (int i = 0; i < n; i++) {
    cout << ret[i] << endl;
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int N = 2e5;
int a[N], r[N];
vector<int> e[N], b;

int find(int v) {
  int l = 0, r = b.size() - 1, s = b.size();
  while (l + 1 < r) {
    int m = (r + l) / 2;
    if (b[m] < v)
      l = m;
    else
      r = m;
  }
  if (l < s && v <= b[l])
    return l;
  else if (l < r && r < s && v <= b[r])
    return r;
  else
    return -1;
  return 0;
}

int dfs(int c, int p) {
  int x = find(a[c]), y;
  if (x == -1)
    b.push_back(a[c]);
  else {
    y = b[x];
    b[x] = a[c];
  }
  r[c] = b.size();
  for (auto i : e[c]) {
    if (i != p)
      dfs(i, c);
  }
  if (x == -1)
    b.pop_back();
  else
    b[x] = y;
  return 0;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    e[u].push_back(v);
    e[v].push_back(u);
  }

  dfs(0, -1);

  rep(i, n) cout << r[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int N = 2e5;
int a[N], r[N];
vector<int> e[N], b;

int insert(int v) {
  int l = 0, r = b.size() - 1, s = b.size();
  while (l + 1 < r) {
    int m = (r + l) / 2;
    if (b[m] < v)
      l = m;
    else
      r = m;
  }
  if (l < s && v <= b[l])
    b[l] = v;
  else if (l < r && r < s && v <= b[r])
    b[r] = v;
  else
    b.push_back(v);
  return 0;
}

int dfs(int c, int p) {
  insert(a[c]);
  r[c] = b.size();
  vector<int> pb = b;
  for (auto i : e[c]) {
    b = pb;
    if (i != p)
      dfs(i, c);
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    e[u].push_back(v);
    e[v].push_back(u);
  }

  dfs(0, -1);

  rep(i, n) cout << r[i] << endl;
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
//<(")
#define ll long long
using namespace std;

typedef pair<int, int> ii;
typedef pair<ii, ii> iii;

const ll mod = 1e9 + 7;
const ll siz = 2e5 + 10;
const ll base = 32;
const ll MAXX = 1e18;

ll n;
vector<ll> nxt;
vector<ll> graph[siz];
vector<ll> last;
ll val[siz];

ll tree[4 * siz];

void change() {
  sort(nxt.begin(), nxt.end());
  ll l = unique(nxt.begin(), nxt.end()) - nxt.begin();
  nxt.resize(l);
  for (ll i = 1; i <= n; i++) {
    val[i] = lower_bound(nxt.begin(), nxt.end(), val[i]) - nxt.begin() + 1;
    // cerr << val[i] << ' ';
  }
}

void update(ll node, ll left, ll right, ll pos, ll val) {
  if (pos < left || pos > right) {
    return;
  }
  if (left == right) {
    if (left == pos) {
      tree[node] = val;
    }
    return;
  }
  ll mid = (left + right) / 2;
  update(node * 2, left, mid, pos, val);
  update(node * 2 + 1, mid + 1, right, pos, val);
  tree[node] = max(tree[node * 2], tree[node * 2 + 1]);
}

ll get(ll node, ll left, ll right, ll gLeft, ll gRight) {
  if (gLeft > right || gRight < left) {
    return 0;
  }
  if (gLeft <= left && gRight >= right) {
    return tree[node];
  }
  ll mid = (left + right) / 2;
  return max(get(node * 2, left, mid, gLeft, gRight),
             get(node * 2 + 1, mid + 1, right, gLeft, gRight));
}

ll ans[siz];

void dfs(ll node, ll par) {
  ll stuff = get(1, 1, n, 1, val[node] - 1) + 1;
  last.push_back(get(1, 1, n, val[node], val[node]));
  if (stuff > last.back()) {
    update(1, 1, n, val[node], stuff);
  }
  ans[node] = get(1, 1, n, 1, n);
  for (ll i = 0; i < (int)graph[node].size(); i++) {
    ll v = graph[node][i];
    if (v != par) {
      dfs(v, node);
    }
  }
  update(1, 1, n, val[node], last.back());
  last.pop_back();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("INP.txt", "r", stdin);
  // freopen("OUT.txt", "w", stdout);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> val[i];
    nxt.push_back(val[i]);
  }
  change();
  for (ll i = 1; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(1, 1);
  for (ll i = 1; i <= n; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n;
int a[200001], ans[200001];

vector<int> tmp;
vector<int> adj[200001];

void dfs(int u, int p) {
  int pre = -1;
  vector<int>::iterator it = lower_bound(tmp.begin(), tmp.end(), a[u]);
  if (it == tmp.end())
    tmp.push_back(a[u]);
  else {
    pre = *it;
    *it = a[u];
  }
  ans[u] = tmp.size();
  for (int v : adj[u])
    if (v != p)
      dfs(v, u);
  if (pre != -1)
    *it = pre;
  else
    tmp.pop_back();
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

const int MAXN = 200005;

struct LIS {
  const int INF = 2e9;

  vector<pair<int, int>> history;
  int ans[MAXN];

  LIS() {
    for (int i = 0; i < MAXN; i++) {
      ans[i] = INF;
    }
    ans[0] = -INF;
    history.clear();
  }

  void Insert(int x) {
    int lb = lower_bound(ans, ans + MAXN, x) - ans;
    history.emplace_back(lb, ans[lb]);
    ans[lb] = x;
  }

  void Rollback() {
    ans[history.back().first] = history.back().second;
    history.pop_back();
  }

  int Query() { return lower_bound(ans, ans + MAXN, INF) - ans - 1; }
};

LIS Solver;

int N;
int A[MAXN];
int Answer[MAXN];
vector<int> adj[MAXN];

void dfs(int n, int p) {
  Solver.Insert(A[n]);
  Answer[n] = Solver.Query();
  for (auto i : adj[n])
    if (i != p)
      dfs(i, n);
  Solver.Rollback();
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> 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--;
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }

  dfs(0, -1);
  for (int i = 0; i < N; i++) {
    cout << Answer[i] << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

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

const int MAXN = 200005;

struct LIS {
  const int INF = 2e9;

  vector<pair<int, int>> history;
  int ans[MAXN];

  LIS() {
    for (int i = 0; i < MAXN; i++) {
      ans[i] = INF;
    }
    ans[0] = -INF;
    history.clear();
  }

  int Insert(int x) {
    int op = 0;
    int lb = upper_bound(ans, ans + MAXN, x) - ans;
    if (ans[lb - 1] < x && x < ans[lb]) {
      op++;
      history.emplace_back(lb, ans[lb]);
      ans[lb] = x;
    }
    return op;
  }

  void Rollback(int cnt) {
    for (int i = 0; i < cnt; i++) {
      ans[history.back().first] = history.back().second;
      history.pop_back();
    }
  }

  int Query() {
    int lo = 0, hi = MAXN - 1;
    while (lo < hi) {
      int mid = (lo + hi + 1) / 2;
      if (ans[mid] < INF) {
        lo = mid;
      } else {
        hi = mid - 1;
      }
    }
    return lo;
  }
};

LIS Solver;

int N;
int A[MAXN];
int Answer[MAXN];
vector<int> adj[MAXN];

void dfs(int n, int p) {
  static int tin = 1;
  Solver.Insert(A[n]);
  Answer[n] = Solver.Query();
  for (auto i : adj[n])
    if (i != p)
      dfs(i, n);
  Solver.Rollback(1);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> 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--;
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }

  dfs(0, -1);
  for (int i = 0; i < N; i++) {
    cout << Answer[i] << "\\n";
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = (ll)998244353;
const ll INF = (ll)1e18 + 10;
const double PI = 2.0 * acos(0);

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};

int N, M, Q;
int a[200000];
int ans[200000];
vector<int> tonari[200000];
vector<int> dp, arr;

void dfs(int n, int par) {
  int s = dp.size();
  arr.push_back(a[n]);
  dp.push_back(MOD);

  auto it = lower_bound(dp.begin(), dp.end(), arr[s]);
  int moto = *it;
  *it = arr[s];
  int place = it - dp.begin();
  ans[n] = lower_bound(dp.begin(), dp.end(), MOD) - dp.begin();
  // cout<<n<<"ok"<<endl;
  for (auto i : tonari[n]) {
    if (i == par)
      continue;
    dfs(i, n);
  }
  // cout<<n<<"out"<<endl;

  arr.pop_back();
  dp.pop_back();
  dp[place] = moto;
}

void solve(int ca) {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int u, v;
  for (int i = 0; i < N - 1; ++i) {
    cin >> u >> v;
    --u;
    --v;
    tonari[u].push_back(v);
    tonari[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < N; ++i) {
    printf("%d\\n", ans[i]);
  }
}

// #define MULTI
int main() {
#ifdef MULTI
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    solve(i + 1);
  }
#else
  solve(0);
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = (ll)998244353;
const ll INF = (ll)1e18 + 10;
const double PI = 2.0 * acos(0);

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dy[] = {-1, 1, 0, 0};
int dx[] = {0, 0, -1, 1};

int N, M, Q;
int a[200000];
int ans[200000];
vector<int> tonari[200000];
vector<int> dp, arr;

void dfs(int n, int par) {
  int s = dp.size();
  arr.push_back(a[n]);
  dp.push_back(MOD);

  auto it = lower_bound(dp.begin(), dp.end(), arr[s]);
  int moto = *it;
  *it = arr[s];
  ans[n] = lower_bound(dp.begin(), dp.end(), MOD) - dp.begin();
  // cout<<n<<"ok"<<endl;
  for (auto i : tonari[n]) {
    if (i == par)
      continue;
    dfs(i, n);
  }
  // cout<<n<<"out"<<endl;

  arr.pop_back();
  dp.pop_back();
  *it = moto;
}

void solve(int ca) {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  int u, v;
  for (int i = 0; i < N - 1; ++i) {
    cin >> u >> v;
    --u;
    --v;
    tonari[u].push_back(v);
    tonari[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < N; ++i) {
    printf("%d\\n", ans[i]);
  }
}

// #define MULTI
int main() {
#ifdef MULTI
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    solve(i + 1);
  }
#else
  solve(0);
#endif
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define rall(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define PB push_back
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll MOD = 998244353;

vi graph[200007];
int a[200007];
int ans[2000007];

vi v;

void dfs(int i, int pai = -1) {

  auto p = lower_bound(all(v), a[i]);
  int idx = distance(v.begin(), p);
  bool ins = false;
  int aux = -1;
  if (p == v.end()) {
    v.PB(a[i]);
    ins = true;
  } else {
    aux = v[idx];
    v[idx] = a[i];
  }

  ans[i] = v.size();
  for (auto &k : graph[i]) {
    if (k != pai) {
      dfs(k, i);
    }
  }

  if (ins)
    v.pop_back();
  else
    v[idx] = aux;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  FOR(i, n) cin >> a[i];

  FOR(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].PB(b);
    graph[b].PB(a);
  }

  dfs(0);

  FOR(i, n)
  cout << ans[i] << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define rall(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define PB push_back
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll MOD = 998244353;

vi graph[200007];
int a[200007];
int ans[2000007];

vi v;

void dfs(int i, int pai = -1) {

  auto p = lower_bound(all(v), a[i]);
  bool ins = false;
  int aux = -1;
  if (p == v.end()) {
    v.PB(a[i]);
    ins = true;
  } else {
    aux = *p;
    *p = a[i];
  }

  ans[i] = v.size();
  for (auto &k : graph[i]) {
    if (k != pai) {
      dfs(k, i);
    }
  }

  if (ins)
    v.pop_back();
  else
    (*p) = aux;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  FOR(i, n) cin >> a[i];

  FOR(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].PB(b);
    graph[b].PB(a);
  }

  dfs(0);

  FOR(i, n)
  cout << ans[i] << '\\n';

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> v[200000], a, ret, tmp;
int n;

void dfs(int par, int pos) {
  int len = lower_bound(tmp.begin(), tmp.end(), a[pos]) - tmp.begin();
  int bef = tmp[len];
  tmp[len] = a[pos];
  ret[pos] = lower_bound(tmp.begin(), tmp.end(), 1 << 30) - tmp.begin();
  for (int next : v[pos]) {
    if (next == par)
      continue;
    dfs(pos, next);
  }
  tmp[len] = bef;
}

int main() {
  cin >> n;
  a.resize(n);
  ret.resize(n);
  tmp.resize(n + 1, 1 << 30);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(-1, 0);
  for (int i = 0; i < n; i++) {
    cout << ret[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using T = tuple<int, int, int>;
const int MAX = 1e5 * 2;
const int INF = 1e9 + 10;

vector<int> dp(MAX, INF);
vector<int> par(MAX, -1);
vector<int> ans(MAX, 0);
vector<int> before(MAX, -1);
vector<int> where(MAX, -1);
vector<vector<int>> G(MAX, vector<int>(0));

vector<int>::iterator pos;
int idx;

int vec[MAX];
int last;
int N;
int decide = 0;

void dfs(const vector<vector<int>> &G, int s, vector<int> &dp, vector<int> &ans,
         vector<int> &before, vector<int> &where) {

  if (decide == N) {
    return;
  }

  if (ans[s] == 0) {
    pos = lower_bound(dp.begin(), dp.end(), vec[s]);
    idx = distance(dp.begin(), pos);

    before[s] = dp[idx];
    where[s] = idx;
    dp[idx] = vec[s];

    pos = lower_bound(dp.begin(), dp.end(), INF);
    idx = distance(dp.begin(), pos);
    ans[s] = idx;
    decide++;
  }

  for (int i = 0; i < G[s].size(); i++) {
    if (ans[G[s][i]] == 0) {
      if (par[G[s][i]] == -1) {
        par[G[s][i]] = s;
      }
      dfs(G, G[s][i], dp, ans, before, where);
    }
  }

  if (s != 0) {
    int x = before[s];
    dp[where[s]] = x;
    dfs(G, par[s], dp, ans, before, where);
  }
}

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

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  par[0] = -100;
  dfs(G, 0, dp, ans, before, where);

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