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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
const int INF = 1001001001;

int n;
vector<int> dp(200010);
vector<int> ans(200010);
vector<int> a(200010);
vector<vector<int>> tr(200010);

void dfs(int v, int p = -1) {
  int i = lower_bound(dp.begin(), dp.end(), a[v]) - dp.begin();
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
  for (int u : tr[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}

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--;
    tr[u].push_back(v);
    tr[v].push_back(u);
  }
  rep(i, 200010) dp[i] = INF;
  dfs(0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
typedef long long ll;
const int inf = 200010;

int n;
vector<int> li(200010, inf);
vector<int> a(200010);
vector<vector<int>> tr(200010);

int lis(vector<ll> a) {
  int l = a.size();
  vector<int> dp(l + 1, inf);
  for (int i = 0; i < l; i++) {
    *lower_bound(dp.begin(), dp.end(), a[i]) = a[i];
  }
  return lower_bound(dp.begin(), dp.end(), inf) - dp.begin();
}

void dfs(ll x, vector<int> v = vector<int>(200010, inf)) {
  *lower_bound(v.begin(), v.end(), a[x]) = a[x];
  li[x] = lower_bound(v.begin(), v.end(), inf) - v.begin();
  for (auto u : tr[x]) {
    if (li[u] != inf)
      continue;
    dfs(u, v);
  }
}

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--;
    tr[u].push_back(v);
    tr[v].push_back(u);
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    cout << li[i] << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;
int n;
vector<int> uv[200001];
int a[200001];
int ans[200001];
void solve(int src, int fr, int *l, int ln) {

  int index = -1, orig;

  int *match = lower_bound(l, l + ln, a[src]);
  index = match - l;

  if (l[index] >= a[src] &&
      ((index > 0 && l[index - 1] < a[src]) || (index == 0))) {
    index = index;
    orig = l[index];
    l[index] = a[src];
  }

  if (a[src] > l[ln - 1]) {
    index = ln;
    orig = -1;
    l[ln] = a[src];
    ln++;
  }
  ans[src] = ln;

  for (auto dest : uv[src]) {
    if (dest == fr)
      continue;
    solve(dest, src, l, ln);
  }

  if (index >= 0) {
    l[index] = orig;
    if (orig < 0)
      ln--;
  }
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i + 1];
  int u, v;
  REP(i, n - 1) {
    cin >> u >> v;
    uv[v].push_back(u);
    uv[u].push_back(v);
  }
  int l[200001] = {a[1], -1};
  solve(1, 0, l, 1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;
int n;
int u[200001], v[200001];
int a[200001];
int ans[200001];
void solve(int src, int fr, int *l, int ln) {

  int index = -1, orig;

  int *match = lower_bound(l, l + ln, a[src]);
  index = match - l;

  if (l[index] >= a[src] &&
      ((index > 0 && l[index - 1] < a[src]) || (index == 0))) {
    index = index;
    orig = l[index];
    l[index] = a[src];
  }

  if (a[src] > l[ln - 1]) {
    index = ln;
    orig = -1;
    l[ln] = a[src];
    ln++;
  }
  ans[src] = ln;

  for (int i = 0; i < n - 1; i++) {
    int dest = -1;
    if (u[i] == src)
      dest = v[i];
    else if (v[i] == src)
      dest = u[i];
    if (dest == -1 || dest == fr)
      continue;
    solve(dest, src, l, ln);
  }

  if (index >= 0) {
    l[index] = orig;
    if (orig < 0)
      ln--;
  }
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i + 1];
  REP(i, n - 1) cin >> u[i] >> v[i];
  int l[200001] = {a[1], -1};
  solve(1, 0, l, 1);
  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 ll maxs = 2e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'

void pre() {}

ll n;
ll a[maxs];
vector<ll> tree[maxs];
ll ans[maxs];
vector<ll> v;

void dfs(ll s, ll p) {
  ll pos = lower_bound(v.begin(), v.end(), a[s]) - v.begin();
  ll tmp = v[pos];
  v[pos] = a[s];
  ans[s] = lower_bound(v.begin(), v.end(), oo) - v.begin();
  for (auto d : tree[s]) {
    if (d != p) {
      dfs(d, s);
    }
  }
  v[pos] = tmp;
}

void solve() {

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

  for (ll i = 0; i < n - 1; i++) {
    ll s, d;
    cin >> s >> d;
    s--;
    d--;
    tree[s].pb(d);
    tree[d].pb(s);
  }
  v.resize(n, oo);
  dfs(0, -1);

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

int main() {
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

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

const ll maxs = 1e5 + 5;
const ll lmaxs = 20;
ll mod = 1e9 + 7;
ll oo = 1e15;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\\n'

void pre() {}

ll n;
ll a[maxs];
vector<ll> tree[maxs];
ll ans[maxs];
vector<ll> v;

void dfs(ll s, ll p) {
  ll pos = lower_bound(v.begin(), v.end(), a[s]) - v.begin();
  ll tmp = v[pos];
  v[pos] = a[s];
  ans[s] = lower_bound(v.begin(), v.end(), oo) - v.begin();
  for (auto d : tree[s]) {
    if (d != p) {
      dfs(d, s);
    }
  }
  v[pos] = tmp;
}

void solve() {

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

  for (ll i = 0; i < n - 1; i++) {
    ll s, d;
    cin >> s >> d;
    s--;
    d--;
    tree[s].pb(d);
    tree[d].pb(s);
  }
  v.resize(n, oo);
  dfs(0, -1);

  for (ll i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
  IOS;
  pre();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define m_k make_pair
#define inf 0x3f3f3f3f
using namespace std;
const int N = 2 * 1e5 + 100;
int n, a[N], b[N], dp[N];
int tot, first[N], nxt[N * 2], point[N * 2];
vector<int> s;
void add_edge(int x, int y) {
  tot++;
  nxt[tot] = first[x];
  first[x] = tot;
  point[tot] = y;
}
void dfs(int x, int fa) {
  int kind, p, v;
  if (s.empty() || s[(int)s.size() - 1] < a[x]) {
    kind = 1;
    s.push_back(a[x]);
  } else {
    int pos = lower_bound(s.begin(), s.end(), a[x]) - s.begin();
    p = pos;
    v = s[pos];
    s[pos] = a[x];
    kind = 2;
  }
  dp[x] = (int)s.size();
  for (int i = first[x]; i != -1; i = nxt[i]) {
    int u = point[i];
    if (u == fa)
      continue;
    dfs(u, x);
  }
  if (kind == 1)
    s.pop_back();
  else
    s[p] = v;
}
void dfs1(int x, int fa) {
  dp[x] = max(dp[x], dp[fa]);
  for (int i = first[x]; i != -1; i = nxt[i]) {
    int u = point[i];
    if (u == fa)
      continue;
    dfs1(u, x);
  }
}
signed main() {
  tot = -1;
  memset(first, -1, sizeof(first));
  memset(nxt, -1, sizeof(nxt));
  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_edge(u, v);
    add_edge(v, u);
  }
  dfs(1, 1);
  dfs1(1, 1);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", dp[i]);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define m_k make_pair
#define inf 0x3f3f3f3f
using namespace std;
const int N = 3 * 1e5 + 100;
int n, a[N], b[N], dp[N];
int tot, first[N], nxt[N * 2], point[N * 2];
struct node {
  int val;
} sh[N * 4];
void add_edge(int x, int y) {
  tot++;
  nxt[tot] = first[x];
  first[x] = tot;
  point[tot] = y;
}
void pushup(int x) { sh[x].val = max(sh[x + x].val, sh[x + x + 1].val); }
void change(int x, int l, int r, int wh, int v) {
  if (l == r) {
    sh[x].val = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (wh <= mid)
    change(x + x, l, mid, wh, v);
  else
    change(x + x + 1, mid + 1, r, wh, v);
  pushup(x);
}
int query(int x, int l, int r, int ll, int rr) {
  if (l >= ll && r <= rr)
    return sh[x].val;
  int mid = (l + r) >> 1, ans = 0;
  if (ll <= mid)
    ans = query(x + x, l, mid, ll, rr);
  if (rr > mid)
    ans = max(ans, query(x + x + 1, mid + 1, r, ll, rr));
  return ans;
}
void dfs(int x, int fa) {
  if (x == 1)
    dp[x] = 1;
  else
    dp[x] = query(1, 1, n, 1, a[x] - 1) + 1;
  change(1, 1, n, a[x], dp[x]);
  for (int i = first[x]; i != -1; i = nxt[i]) {
    int u = point[i];
    if (u == fa)
      continue;
    dfs(u, x);
  }
  change(1, 1, n, a[x], 0);
}
signed main() {
  tot = -1;
  memset(first, -1, sizeof(first));
  memset(nxt, -1, sizeof(nxt));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    b[i] = a[i];
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n; i++) {
    int pos = lower_bound(b + 1, b + 1 + n, a[i]) - b;
    a[i] = pos;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", dp[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

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

  // LIS
  ll inf = pow(10, 10);
  vector<ll> lis(n + 10, inf);
  // idx, lis_idx, lis_orig
  stack<array<ll, 3>> lis_updates;

  // stack (idx, p)
  stack<array<ll, 2>> st;
  st.push({0, -1});
  vector<bool> used(n, false), reverted(n, false);
  vector<int> ans(n);
  while (!st.empty()) {
    auto [idx, p] = st.top();
    st.pop();

    if (used[idx]) {
      continue;
    }

    used[idx] = true;

    // 元に戻す
    while (!lis_updates.empty() && lis_updates.top()[0] != p) {
      auto [idx, lis_idx, org] = lis_updates.top();
      lis_updates.pop();
      lis[lis_idx] = org;
    }

    // LISを求める
    auto lw_it = lower_bound(lis.begin(), lis.end(), va[idx]);
    lis_updates.push({idx, distance(lis.begin(), lw_it), *lw_it});
    *lw_it = va[idx];
    ans[idx] = distance(lis.begin(), lower_bound(lis.begin(), lis.end(), inf));

    for (auto ch : tree[idx]) {
      st.push({ch, idx});
    }
  }

  for (auto a : ans) {
    cout << a << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

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

  // preorder[i]: ノードID `i` の行き掛け順
  vector<int> preorder(n), preorder_rev(n);

  // LIS
  ll inf = pow(10, 10);
  vector<ll> lis(n + 10, inf);
  vector<pair<ll, ll>> lis_updates(n);

  // stack (idx, p)
  stack<array<ll, 2>> st;
  st.push({0, -1});
  int cur_preorder = 0;
  vector<bool> used(n, false);
  vector<int> ans(n);
  while (!st.empty()) {
    auto [idx, p] = st.top();
    st.pop();

    if (used[idx]) {
      continue;
    }

    used[idx] = true;

    // 行き掛け順を更新
    preorder[idx] = cur_preorder;
    preorder_rev[cur_preorder] = idx;
    cur_preorder++;

    // 元に戻す
    for (int i = preorder[idx] - 1; i > preorder[p]; --i) {
      auto lis_update = lis_updates[preorder_rev[i]];
      lis[lis_update.first] = lis_update.second;
    }

    // LISを求める
    auto lw_it = lower_bound(lis.begin(), lis.end(), va[idx]);
    lis_updates[idx] = {distance(lis.begin(), lw_it), *lw_it};
    *lw_it = va[idx];
    ans[idx] = distance(lis.begin(), lower_bound(lis.begin(), lis.end(), inf));

    for (auto ch : tree[idx]) {
      st.push({ch, idx});
    }
  }

  for (auto a : ans) {
    cout << a << '\\n';
  }
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)

vector<ll> a(200005);
vector<ll> to[200005];
vector<ll> dp(200005, INF);
vector<ll> ans(200005, 0);

int dfs(int t, int p) {
  ll id = lower_bound(dp.begin(), dp.end(), a[t]) - dp.begin();
  ll prev = dp[id];
  dp[id] = a[t];
  if (p != -1)
    ans[t] = max(id, ans[p]);
  else
    ans[t] = id;
  //  cout<<t<<' '<<id<<' '<<a[t]<<endl;
  for (int u : to[t]) {
    if (u == p)
      continue;
    dfs(u, t);
  }
  dp[id] = prev;
  return 0;
}
int main() {
  ll n;
  cin >> n;

  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    ll u1, v1;
    cin >> u1 >> v1;
    u1--;
    v1--;
    to[u1].push_back(v1);
    to[v1].push_back(u1);
  }
  dp[0] = -INF;
  dfs(0, -1);
  rep(i, n) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e18;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)

vector<ll> a(200005);
vector<ll> to[200005];
vector<ll> dp(200005, INF);
vector<ll> ans(200005, 0);

int dfs(int t, int p) {
  ll id = lower_bound(dp.begin(), dp.end(), a[t]) - dp.begin();
  ll prev = dp[id];
  dp[id] = a[t];
  if (p != -1)
    ans[t] = max(id, ans[p]);
  else
    ans[t] = id;
  //  cout<<t<<' '<<id<<' '<<a[t]<<endl;
  for (int u : to[t]) {
    if (u == p)
      continue;
    dfs(u, t);
  }
  dp[id] = prev;
}
int main() {
  ll n;
  cin >> n;

  rep(i, n) { cin >> a[i]; }
  rep(i, n - 1) {
    ll u1, v1;
    cin >> u1 >> v1;
    u1--;
    v1--;
    to[u1].push_back(v1);
    to[v1].push_back(u1);
  }
  dp[0] = -INF;
  dfs(0, -1);
  rep(i, n) cout << ans[i] << endl;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 500005, INF = 0x3f3f3f3f;
int n, a[N], dp[N], len, ans[N];
int v[N], nxt[N], head[N];

void dfs(int x, int fa) {
  //	cout<<x<<' '<<fa<<endl;
  int L = 1, R = n, id, ls;
  while (L <= R) {
    int mid = (L + R) >> 1;
    if (dp[mid] < a[x])
      L = mid + 1;
    else
      id = mid, R = mid - 1;
  }
  ls = dp[id], dp[id] = a[x];
  if (ls == INF)
    len++;
  ans[x] = len;
  for (int i = head[x]; i > 0; i = nxt[i]) {
    if (v[i] == fa)
      continue;
    dfs(v[i], x);
  }
  dp[id] = ls;
  if (ls == INF)
    len--;
}

int main() {
  memset(dp, 0x3f, sizeof(dp));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &v[i], &v[i + n]);
    nxt[i] = head[v[i + n]], head[v[i + n]] = i;
    nxt[i + n] = head[v[i]], head[v[i]] = i + n;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}

Error: 0

Fixed submission:

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

const int N = 200005, INF = 0x3f3f3f3f;
int n, a[N], dp[N], len, ans[N];
int v[N], nxt[N], head[N];

void dfs(int x, int fa) {
  //	cout<<x<<' '<<fa<<endl;
  int L = 1, R = n, id, ls;
  while (L <= R) {
    int mid = (L + R) >> 1;
    if (dp[mid] <= a[x])
      L = mid + 1;
    else
      id = mid, R = mid - 1;
  }
  ls = dp[id], dp[id] = a[x];
  if (ls == INF)
    len++;
  ans[x] = len;
  for (int i = head[x]; i > 0; i = nxt[i]) {
    if (v[i] == fa)
      continue;
    dfs(v[i], x);
  }
  dp[id] = ls;
  if (ls == INF)
    len--;
}

int main() {
  memset(dp, 0x3f, sizeof(dp));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &v[i], &v[i + n]);
    nxt[i] = head[v[i + n]], head[v[i + n]] = i;
    nxt[i + n] = head[v[i]], head[v[i]] = i + n;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define mx 200005
vector<int> adj[mx];
vector<int> val(mx);
vector<int> lis(mx, -1);
vector<int> currseq;
void dfs(int a) {
  auto it = lower_bound(currseq.begin(), currseq.end(), val[a]);
  int prevval = -1;
  int index = -1;
  if (it == currseq.end()) {
    currseq.push_back(val[a]);
  } else {
    index = it - currseq.begin();
    prevval = currseq[index];
    currseq[index] = val[a];
  }
  lis[a] = currseq.size();
  for (int s : adj[a]) {
    if (lis[s] == -1) {
      dfs(s);
    }
  }
  if (prevval == -1) {
    currseq.pop_back();
  } else {
    currseq[index] = prevval;
  }
}

int main() {
  int n;
  cin >> n;
  for (int q = 1; q <= n; q++) {
    cin >> val[q];
  }
  for (int q = 0; q < n - 1; q++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1);
  for (int q = 1; q <= n; q++) {
    cout << lis[q] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mx 20005
vector<int> adj[mx];
vector<int> val(mx);
vector<int> lis(mx, -1);
vector<int> currseq;
void dfs(int a) {
  auto it = lower_bound(currseq.begin(), currseq.end(), val[a]);
  int prevval = -1;
  int index = -1;
  if (it == currseq.end()) {
    currseq.push_back(val[a]);
  } else {
    index = it - currseq.begin();
    prevval = currseq[index];
    currseq[index] = val[a];
  }
  lis[a] = currseq.size();
  for (int s : adj[a]) {
    if (lis[s] == -1) {
      dfs(s);
    }
  }
  if (prevval == -1) {
    currseq.pop_back();
  } else {
    currseq[index] = prevval;
  }
}

int main() {
  int n;
  cin >> n;
  for (int q = 1; q <= n; q++) {
    cin >> val[q];
  }
  for (int q = 0; q < n - 1; q++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1);
  for (int q = 1; q <= n; q++) {
    cout << lis[q] << endl;
  }
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 2e5 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320;
int N, a[MN];
vector<int> g[MN];
int ans[MN];

void dfs(int u, int p, vector<int> &lis) {
  bool back = false;
  int it = 0, before = 0;
  if (lis.empty() || a[u] > lis.back()) {
    lis.push_back(a[u]);
    back = true;
  } else {
    it = lower_bound(all(lis), a[u]) - lis.begin();
    before = lis[it];
    lis[it] = a[u];
  }
  ans[u] = lis.size();
  for (int v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u, lis);
  }
  if (back)
    lis.pop_back();
  else
    lis[it] = before;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> a[i];
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> lis;
  dfs(1, -1, lis);
  for (int i = 1; i <= N; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 2e5 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320;
int N, a[MN];
vector<int> g[MN];
int ans[MN];

void dfs(int u, int p, vector<int> lis) {
  if (lis.empty() || a[u] > lis.back())
    lis.push_back(a[u]);
  else
    *lower_bound(all(lis), a[u]) = a[u];
  ans[u] = lis.size();
  for (int v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u, lis);
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> a[i];
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> lis;
  dfs(1, -1, lis);
  for (int i = 1; i <= N; i++)
    cout << ans[i] << '\\n';
  return 0;
}
Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>
#define F first
#define S second
#define int long long
#define mod 1000000007
#define mmod 998244353
#define all(v) v.begin(), v.end()
#define INF LLONG_MAX / 3
using namespace std;
int mypow(int fa, int fb, int fc) {
  if (fb == 0)
    return 1;
  if (fb % 2)
    return mypow(fa, fb - 1, fc) * fa % fc;
  int z = mypow(fa, fb / 2, fc);
  return z * z % fc;
}
int k(int fa) {
  int x = 0;
  while (fa) {
    x++;
    fa /= 10;
  }
  return x;
}
int gcd(int fa, int fb) {
  if (fb == 0)
    return fa;
  return gcd(fb, fa % fb);
}
int a[200000], dp[200000], ans[200000], n;
bool used[200000];
vector<int> v[200000];
void mutu(int e) {
  used[e] = true;
  ans[e] = lower_bound(dp, dp + n, INF - 1) - dp;
  f(i, v[e].size()) {
    if (used[v[e][i]])
      continue;
    int z = lower_bound(dp, dp + n, a[v[e][i]]) - dp;
    int x = dp[z];
    dp[z] = a[v[e][i]];
    mutu(v[e][i]);
    dp[z] = x;
  }
}
signed main() {
  cin >> n;
  f(i, n) {
    cin >> a[i];
    dp[i] = INF;
  }
  f(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dp[0] = a[0];
  mutu(0);
  f(i, n) cout << ans[i] << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>
#define F first
#define S second
#define int long long
#define mod 1000000007
#define mmod 998244353
#define all(v) v.begin(), v.end()
#define INF LLONG_MAX / 3
using namespace std;
int mypow(int fa, int fb, int fc) {
  if (fb == 0)
    return 1;
  if (fb % 2)
    return mypow(fa, fb - 1, fc) * fa % fc;
  int z = mypow(fa, fb / 2, fc);
  return z * z % fc;
}
int k(int fa) {
  int x = 0;
  while (fa) {
    x++;
    fa /= 10;
  }
  return x;
}
int gcd(int fa, int fb) {
  if (fb == 0)
    return fa;
  return gcd(fb, fa % fb);
}
int a[200000], dp[200000], ans[200000], n;
bool used[200000];
vector<int> v[200000];
void mutu(int fi) {
  used[fi] = true;
  ans[fi] = lower_bound(dp, dp + n, INF - 1) - dp;
  f(i, v[fi].size()) {
    int z = lower_bound(dp, dp + n, a[v[fi][i]]) - dp;
    int x = dp[z];
    dp[z] = a[v[fi][i]];
    mutu(v[fi][i]);
    dp[z] = x;
  }
}
signed main() {
  cin >> n;
  f(i, n) {
    cin >> a[i];
    dp[i] = INF;
  }
  f(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dp[0] = a[0];
  mutu(0);
  f(i, n) cout << ans[i] << endl;
}