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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

vector<vi> g;
vi a, ans, lis;
vb seen;

void dfs(int v) {
  seen[v] = true;
  auto it = lower_bound(all(lis), a[v]);
  int tmp = *it;
  *it = a[v];
  ans[v] = lower_bound(all(lis), 1e9 + 1) - lis.begin();
  for (auto nv : g[v]) {
    if (seen[nv])
      continue;
    dfs(nv);
  }
  *it = tmp;
}

int main() {
  int n;
  cin >> n;
  a = vi(n);
  rep(i, n) cin >> a[i];
  g = vector<vi>(n);
  seen = vb(n, false);
  ans = vi(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  lis = vi(n, 1e9 + 1);
  dfs(0);
  rep(i, n) cout << ans[i] << ' ';
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

vector<vi> g;
vi a, ans;
vb seen;

void dfs(int v, vi lis) {
  seen[v] = true;
  auto it = lower_bound(all(lis), a[v]);
  *it = a[v];
  ans[v] = lower_bound(all(lis), 1e9 + 1) - lis.begin();
  for (auto nv : g[v]) {
    if (seen[nv])
      continue;
    dfs(nv, lis);
  }
}

int main() {
  int n;
  cin >> n;
  a = vi(n);
  rep(i, n) cin >> a[i];
  g = vector<vi>(n);
  seen = vb(n, false);
  ans = vi(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vi lis(n, 1e9 + 1);
  dfs(0, lis);
  rep(i, n) cout << ans[i] << ' ';
  cout << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

#define _LL long long

int *mallocint(int si) { return (int *)malloc(si * sizeof(int)); }

int *inputint(int n) {
  int *ans = mallocint(n);
  for (int i = 0; i < n; i++)
    cin >> ans[i];
  return ans;
}
vector<int> b[200000];
int ans[200000];
int L[200000];

void calc(int *a, int c, int p, int max) {
  int premax = max;
  int cindex = -1;
  int cpre = 0;
  if (p >= 0) {
    if (a[c] > L[max - 1]) {
      max++;
      L[max - 1] = a[c];
    } else {
      int s = -1;
      int b = max;
      while (s + 1 < b) {
        int m = s + (b - s) / 2;
        if (a[c] > L[m]) {
          s = m;
        } else {
          b = m;
          if (a[c] == L[m])
            break;
        }
      }
      cindex = b;
      cpre = L[b];
      L[b] = a[c];
    }
    ans[c] = max;
  } else {
    L[0] = a[c];
    ans[c] = 1;
    max = 1;
  }
  for (auto itr = b[c].begin(); itr != b[c].end(); itr++) {
    if (*itr != p)
      calc(a, *itr, c, max);
  }
  max = premax;
  if (cindex >= 0)
    L[cindex] = cpre;
}

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

  calc(a, 0, -1, 0);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}

Error: 0

Fixed submission:

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

#define _LL long long

int *mallocint(int si) { return (int *)malloc(si * sizeof(int)); }

int *inputint(int n) {
  int *ans = mallocint(n);
  for (int i = 0; i < n; i++)
    cin >> ans[i];
  return ans;
}
vector<int> b[20000];
int ans[20000];
int f[20000];

void calc(int *a, set<pair<int, int>> &parents, int c, int p) {
  if (p >= 0) {
    auto itr = parents.upper_bound(make_pair(-a[c], 20000));
    f[c] = f[(*itr).second] + 1;
    ans[c] = max(ans[p], f[c]);
  } else {
    f[c] = 1;
    ans[c] = 1;
  }
  parents.insert(make_pair(-a[c], c));
  for (auto itr = b[c].begin(); itr != b[c].end(); itr++) {
    if (*itr != p)
      calc(a, parents, *itr, c);
  }
  parents.erase(make_pair(-a[c], c));
}

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

  set<pair<int, int>> parents;
  calc(a, parents, 0, -1);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> a;
vector<vector<int>> graph;
vector<int> ans;
int inf = 1e9;

void dfs(int u, int p, vector<int> &dp) {
  int idx = -1;
  int value = -1;
  int targetIdx = lower_bound(dp.begin(), dp.end(), a[u]) - dp.begin();
  if (dp[targetIdx] > a[u]) {
    idx = targetIdx;
    value = dp[targetIdx];
    dp[targetIdx] = a[u];
  }
  ans[u] = lower_bound(dp.begin(), dp.end(), inf) - dp.begin() - 1;

  for (int v : graph[u]) {
    if (v == p) {
      continue;
    }
    dfs(v, u, dp);
  }
  if (idx != -1) {
    dp[idx] = value;
  }
}

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

  ans.resize(N);
  vector<int> dp(N + 1, inf);
  dp[0] = 0;
  dfs(0, -1, dp);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

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

vector<int> a;
vector<vector<int>> graph;
vector<int> ans;

void dfs(int u, int p, vector<int> dp) {
  if (dp.back() < a[u]) {
    dp.push_back(a[u]);
  } else {
    *lower_bound(dp.begin(), dp.end(), a[u]) = a[u];
  }
  ans[u] = (int)dp.size() - 1;

  for (int v : graph[u]) {
    if (v == p) {
      continue;
    }
    dfs(v, u, dp);
  }
}

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

  ans.resize(N);
  dfs(0, -1, {0, a[0]});
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;

vector<vector<ll>> g;
vector<ll> ret;
vector<ll> a;
vector<bool> used;

void dfs(vector<ll> &cur, ll pos) {
  if (used[pos]) {
    return;
  }
  used[pos] = 1;
  auto itr = lower_bound(cur.begin(), cur.end(), a[pos]);
  ll tmp = *itr;
  *itr = a[pos];
  auto p = lower_bound(cur.begin(), cur.end(), mod);
  ret[pos] = p - cur.begin();

  for (int i = 0; i < g[pos].size(); ++i) {
    dfs(cur, g[pos][i]);
  }
  *itr = tmp;

  return;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll n;
  cin >> n;

  a = vector<ll>(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  g = vector<vector<ll>>(n, vector<ll>());
  ret = vector<ll>(n);
  used = vector<bool>(n);
  for (int i = 0; i < n - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<ll> start(n, mod + 1ll);
  dfs(start, 0);

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;

vector<vector<ll>> g;
vector<ll> ret;
vector<ll> a;
vector<bool> used;

void dfs(vector<ll> &cur, ll pos) {
  if (used[pos]) {
    return;
  }
  vector<ll> tmp = cur;
  used[pos] = 1;
  auto itr = lower_bound(cur.begin(), cur.end(), a[pos]);
  *itr = a[pos];
  auto p = lower_bound(cur.begin(), cur.end(), mod);
  ret[pos] = p - cur.begin();

  for (int i = 0; i < g[pos].size(); ++i) {
    dfs(cur, g[pos][i]);
  }
  cur = tmp;

  return;
}

int main() {

  cin.tie(0);
  ios_base::sync_with_stdio(0);

  ll n;
  cin >> n;

  a = vector<ll>(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  g = vector<vector<ll>>(n, vector<ll>());
  ret = vector<ll>(n);
  used = vector<bool>(n);
  for (int i = 0; i < n - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<ll> start(n, mod + 1ll);
  dfs(start, 0);

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

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

#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mod 1000000007
#define hell 998244353
#define inf 1000000000000000000LL
using namespace std;
ll n, k, m, l, mx, sum, q;
ll a[200005];
vector<ll> adj[200005];
ll ans[200005];
void dfs(ll v, ll p, ll &len, vector<ll> &dp) {
  ll y;
  ll oldlen = len;
  auto it = lower_bound(dp.begin(), dp.begin() + len, a[v]);
  if (it == dp.begin() + len)
    dp[len++] = a[v];
  else {
    y = *it;
    *it = a[v];
  }
  ans[v] = len;
  for (auto x : adj[v]) {
    if (x == p)
      continue;
    dfs(x, v, len, dp);
  }
  if (oldlen == len)
    *it = y;
  else
    len--;
}
void solve() {
  ll x;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  vector<ll> dp(n + 10);
  ll len = 0;
  dfs(1, 0, len, dp);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cout<<primes.size();

  ll t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mod 1000000007
#define hell 998244353
#define inf 1000000000000000000LL
using namespace std;
ll n, k, m, l, mx, sum, q;
ll a[200005];
vector<ll> adj[200005];
ll ans[200005];
void dfs(ll v, ll p, vector<ll> &dp) {
  ll y;
  bool f = 0;
  auto it = lower_bound(dp.begin(), dp.end(), a[v]);
  if (it == dp.end())
    dp.pb(a[v]);
  else {
    y = *it;
    *it = a[v];
    f = 1;
  }
  ans[v] = dp.size();
  for (auto x : adj[v]) {
    if (x == p)
      continue;
    dfs(x, v, dp);
  }
  if (!f)
    dp.pop_back();
  else
    *it = y;
}
void solve() {
  ll x;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    ll x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  vector<ll> dp;
  dfs(1, 0, dp);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // cout<<primes.size();

  ll t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

const int INF = 1001001001;

vvi to;
vi a;
vi ans;
vi lis;

void dfs(int v, int p, int change, int value) {
  auto it = lower_bound(all(lis), a[v]);
  change = int(it - lis.begin());
  value = *it;
  // cout << v << " " << p << ": " << change << " " << value << endl;

  *it = a[v];
  int l = lower_bound(all(lis), INF) - lis.begin();
  ans[v] = l - 1;

  for (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v, change, value);
  }
  if (change != -1)
    lis[change] = value;
  return;
}

void Main() {
  int n;
  cin >> n;

  a.resize(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

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

  ans.resize(n);

  lis.resize(n + 1, INF);
  lis[0] = -INF;
  dfs(0, -1, -1, -1);

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

const int INF = 1001001001;

vvi to;
vi a;
vi ans;

void dfs(int v, int p, vi lis) {
  auto it = lower_bound(all(lis), a[v]);
  *it = a[v];
  int l = lower_bound(all(lis), INF) - lis.begin();
  ans[v] = l - 1;
  for (auto u : to[v]) {
    if (u == p)
      continue;
    dfs(u, v, lis);
  }
}

void Main() {
  int n;
  cin >> n;

  a.resize(n);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

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

  ans.resize(n);

  vi LIS(n + 1, INF);
  LIS[0] = -INF;
  dfs(0, -1, LIS);

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second

constexpr int MAXN = 1 << 18;

vector<int> grafo[MAXN];
int A[MAXN];
int res[MAXN];

vector<int> lis;
void dfs(int v, int u) {
  auto it = lower_bound(lis.begin(), lis.end(), A[v]);
  int prev = -1;
  if (it == lis.end()) {
    lis.push_back(A[v]);
  } else {
    prev = *it;
    *it = A[v];
  }
  res[v] = lis.size();
  for (int i : grafo[v]) {
    if (i != u)
      dfs(i, v);
  }
  if (prev == -1) {
    lis.pop_back();
  } else {
    *it = prev;
  }
}

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

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

  lis.reserve(N);
  dfs(1, 0);

  for (int i = 1; i <= N; i++) {
    cout << res[i] << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second

constexpr int MAXN = 1 << 18;

vector<int> grafo[MAXN];
int A[MAXN];
int res[MAXN];

vector<int> lis;
void dfs(int v, int u) {
  auto it = lower_bound(lis.begin(), lis.end(), A[v]);
  int prev = -1;
  if (it == lis.end()) {
    lis.push_back(A[v]);
  } else {
    prev = *it;
    *it = A[v];
  }
  res[v] = lis.size();
  for (int i : grafo[v]) {
    if (i != u)
      dfs(i, v);
  }
  if (prev == -1) {
    lis.pop_back();
  } else {
    *it = prev;
  }
}

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

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

  dfs(1, 0);

  for (int i = 1; i <= N; i++) {
    cout << res[i] << '\\n';
  }

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

// #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int N;
int A[200001], nozoki[200001];
vector<int> E[200001];
int kotae[200001];
deque<int> dq;

void dfs(int p, int mae) {

  auto itr = lower_bound(dq.begin(), dq.end(), A[p]);

  if (itr != dq.end()) {
    nozoki[p] = *itr;
    *itr = A[p];
  } else {
    dq.pb(A[p]);
  }

  kotae[p] = dq.size();

  for (auto to : E[p]) {
    if (to != mae) {
      dfs(to, p);
    }
  }

  if (!nozoki[p]) {
    dq.pop_back();
  } else {
    itr = upper_bound(dq.begin(), dq.end(), nozoki[p]);

    *(--itr) = nozoki[p];
  }
}

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

  cin >> N;
  rep1(i, N) cin >> A[i];
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    E[u].pb(v);
    E[v].pb(u);
  }

  dq.pb(2000000000);
  dfs(1, -1);

  rep1(i, N) co(kotae[i]);

  Would you please return 0;
}

Error: 0

Fixed submission:

// #pragma GCC optimize ("O3")
// #pragma GCC target ("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int N;
int A[200001], nozoki[200001];
vector<int> E[200001];
int kotae[200001];
deque<int> dq;

void dfs(int p, int mae) {

  auto itr = lower_bound(dq.begin(), dq.end(), A[p]);

  if (itr != dq.end()) {
    nozoki[p] = *itr;
    *itr = A[p];
  } else {
    dq.pb(A[p]);
  }

  kotae[p] = dq.size();

  for (auto to : E[p]) {
    if (to != mae) {
      dfs(to, p);
    }
  }

  if (!nozoki[p]) {
    dq.pop_back();
  } else {
    itr = lower_bound(dq.begin(), dq.end(), nozoki[p]);

    *(--itr) = nozoki[p];
  }
}

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

  cin >> N;
  rep1(i, N) cin >> A[i];
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    E[u].pb(v);
    E[v].pb(u);
  }

  dq.pb(2000000000);
  dfs(1, -1);

  rep1(i, N) co(kotae[i]);

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

#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
#define PR pair<int, int>
#define F first
#define S second
#define N 200005
using namespace std;

int n, sz, a[N], v[N], d[N];
vector<int> gr[N];
stack<PR> st;

void f(int p) {
  if (v[p])
    return;
  int k = lower_bound(d, d + sz, a[p]) - d;
  if (k == sz)
    sz++;
  v[p] = sz;
  st.push({k, d[k]});
  d[k] = a[p];
  for (auto x : gr[p])
    f(x);
  PR t = st.top();
  st.pop();
  d[t.F] = t.S;
  if (t.S == 1e9)
    sz--;
}

int main() {
  int i, t1, t2;
  cin >> n;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &t1, &t2);
    gr[t1].push_back(t2);
    gr[t2].push_back(t1);
  }
  for (i = 0; i < n; i++)
    d[i] = 1e9;
  f(1);
  for (i = 1; i <= n; i++)
    printf("%d\\n", v[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
#define PR pair<int, int>
#define F first
#define S second
#define N 100005
using namespace std;

int n, sz, a[N], v[N], d[N];
vector<int> gr[N];
stack<PR> st;

void f(int p) {
  if (v[p])
    return;
  int k = lower_bound(d, d + sz, a[p]) - d;
  if (k == sz)
    sz++;
  v[p] = sz;
  st.push({k, d[k]});
  d[k] = a[p];
  for (auto x : gr[p])
    f(x);
  PR t = st.top();
  st.pop();
  d[t.F] = t.S;
  if (t.S == 1e9)
    sz--;
}

int main() {
  int i, t1, t2;
  cin >> n;
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &t1, &t2);
    gr[t1].push_back(t2);
    gr[t2].push_back(t1);
  }
  for (i = 0; i < n; i++)
    d[i] = 1e9;
  f(1);
  for (i = 1; i <= n; i++)
    printf("%d\\n", v[i]);
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#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;
  int pos = lower_bound(tmp.begin(), tmp.end(), a[u]) - tmp.begin();
  if (pos == tmp.size())
    tmp.push_back(a[u]);
  else {
    pre = tmp[pos];
    tmp[pos] = a[u];
  }
  ans[u] = tmp.size();
  for (int v : adj[u])
    if (v != p)
      dfs(v, u);

  if (pre != -1) {
    assert(pos != tmp.size());
    tmp[pos] = pre;
  } else {
    assert(!tmp.empty());
    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';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define RE register
#define random(x) (rand() % x)
#define LOG(a, b) (log(a) / log(b))
#define N 100005
#define mod 998244353
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fll
int n, tot = 0, a[N], hd[N], t[N << 1], nxt[N << 1], ans[N], dp[N];
inline void add(int u, int v) {
  t[++tot] = v;
  nxt[tot] = hd[u];
  hd[u] = tot;
}
inline void dfs(int u, int fa, int len) {
  int v, l = 0, r = len, mid, tdp;
  while (l <= r) {
    mid = l + r >> 1;
    if (a[u] > dp[mid])
      l = mid + 1;
    else
      r = mid - 1;
  }
  // printf("%d:%d %d\\n",u,l,r);
  if (l > len)
    ++len;
  tdp = dp[l];
  dp[l] = a[u];
  ans[u] = len;
  for (int i = hd[u]; i; i = nxt[i]) {
    v = t[i];
    if (v == fa)
      continue;
    dfs(v, u, len);
  }
  dp[l] = tdp;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= n; ++i) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}