prithivMLmods/Coder-Stat huggingface dataset:
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(obj) (obj).begin(), (obj).end()
typedef pair<int, int> P;
const int mod = 1e9 + 7;
vector<int> dp, ans, a, g[200005];
void dfs(int v, int p = -1) {
  int i = lower_bound(all(dp), a[v]) - dp.begin();
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);
  for (auto u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dp = vector<int>(n + 1, mod);
  dp[0] = -1;
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(obj) (obj).begin(), (obj).end()
typedef pair<int, int> P;
const int mod = 1e9 + 7;
vector<int> dp, ans, a, g[200005];
void dfs(int v, int p = -1) {
  int i = lower_bound(all(dp), a[v]) - dp.begin();
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]);
  for (auto u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  dp[i] = old;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dp = vector<int>(n + 1, mod);
  dp[0] = -1;
  dfs(0);
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
int ans[MAXN];
vector<int> G[MAXN];
vector<pii> tag___[MAXN];
vector<int> pool;
int VV[MAXN];

int bit[MAXN];
int Q(int x) {
  int ret = 0;
  while (x) {
    ret = max(ret, bit[x]);
    x -= x & (-x);
  }
  return ret;
}
int M(int x, int v, int u) {
  while (x <= (int)pool.size()) {
    tag___[u].push_back({x, bit[x]});
    bit[x] = max(bit[x], v);
    x += x & (-x);
  }
}

int magic(int x) {
  return lower_bound(pool.begin(), pool.end(), x) - pool.begin() + 1;
}
void dfs(int u, int p) {
  int x = magic(VV[u]);
  int nt = Q(x - 1) + 1;
  M(x, nt, u);
  ans[u] = Q(pool.size());
  for (auto &v : G[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  for (auto &p : tag___[u]) {
    bit[p.F] = p.S;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> VV[i];
    pool.push_back(VV[i]);
  }
  sort(pool.begin(), pool.end());
  pool.erase(unique(pool.begin(), pool.end()), pool.end());
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
int ans[MAXN];
vector<int> G[MAXN];
vector<pii> tag___[MAXN];
vector<int> pool;
int VV[MAXN];

int bit[MAXN];
int Q(int x) {
  int ret = 0;
  while (x) {
    ret = max(ret, bit[x]);
    x -= x & (-x);
  }
  return ret;
}
void M(int x, int v, int u) {
  while (x <= (int)pool.size()) {
    tag___[u].push_back({x, bit[x]});
    bit[x] = max(bit[x], v);
    x += x & (-x);
  }
}

int magic(int x) {
  return lower_bound(pool.begin(), pool.end(), x) - pool.begin() + 1;
}
void dfs(int u, int p) {
  int x = magic(VV[u]);
  int nt = Q(x - 1) + 1;
  M(x, nt, u);
  ans[u] = Q(pool.size());
  for (auto &v : G[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  for (auto &p : tag___[u]) {
    bit[p.F] = p.S;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> VV[i];
    pool.push_back(VV[i]);
  }
  sort(pool.begin(), pool.end());
  pool.erase(unique(pool.begin(), pool.end()), pool.end());
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\n';
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02698 (C++)

Buggy submission (status: 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;
}
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;
}


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;
  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;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 20005;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]); // exclude first turn

  for (int u : to[v]) {
    if (u == p)
      continue; // parent
    dfs(u, v);
  }
  // roll back
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) { cin >> a[i]; }

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

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const int INF = 1001001001;
const int MAX_N = 200005;

vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
  int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
  int old = dp[i];
  dp[i] = a[v];
  ans[v] = i;
  if (p != -1)
    ans[v] = max(ans[v], ans[p]); // exclude first turn

  for (int u : to[v]) {
    if (u == p)
      continue; // parent
    dfs(u, v);
  }
  // roll back
  dp[i] = old;
}

int main() {
  int n;
  cin >> n;
  a = ans = vector<int>(n);
  rep(i, n) { cin >> a[i]; }

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

  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;

  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
#define pb push_back
vector<ll> ans(1000001, 0);
vector<int> dp(1000001, INF);
vector<int> val(1000001);
vector<ll> adj[100001];

void dfs(ll u, ll p) {
  // cout<<"U::"<<u<<"P::"<<p<<endl;
  ll k = lower_bound(dp.begin(), dp.end(), val[u]) - dp.begin();
  ll temp = dp[k];
  dp[k] = val[u];
  ans[u] = max(k, ans[p]);
  for (int j = 0; j < adj[u].size(); j++) {
    if (adj[u][j] == p)
      continue;
    dfs(adj[u][j], u);
  }
  dp[k] = temp;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dp[0] = -INF;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1000000000
#define pb push_back
vector<ll> ans(2000001, 0);
vector<int> dp(2000001, INF);
vector<int> val(2000001);
vector<ll> adj[200001];

void dfs(ll u, ll p) {
  // cout<<"U::"<<u<<"P::"<<p<<endl;
  ll k = lower_bound(dp.begin(), dp.end(), val[u]) - dp.begin();
  ll temp = dp[k];
  dp[k] = val[u];
  ans[u] = max(k, ans[p]);
  for (int j = 0; j < adj[u].size(); j++) {
    if (adj[u][j] == p)
      continue;
    dfs(adj[u][j], u);
  }
  dp[k] = temp;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> val[i];
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dp[0] = -INF;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll inf = 1e18;
int N;
vector<int> g[100100];
ll a[200100];
ll dp[200100];
ll ans[200100];

void solve(int u, int pa = -1) {
  int ind = lower_bound(dp, dp + N, a[u]) - dp;
  ll prev = dp[ind];
  dp[ind] = a[u];
  /*
  for(int i=0;i<N;i++){
      if(dp[i]==inf) cout << "inf" << " ";
      else cout << dp[i] << " ";
  }
  cout << endl;
  */
  ans[u] = lower_bound(dp, dp + N, inf) - dp;
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    solve(v, u);
  }
  dp[ind] = prev;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < N; i++) {
    dp[i] = inf;
  }
  solve(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}

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

typedef long long ll;
const ll inf = 1e18;
int N;
vector<int> g[200100];
ll a[200100];
ll dp[200100];
ll ans[200100];

void solve(int u, int pa = -1) {
  int ind = lower_bound(dp, dp + N, a[u]) - dp;
  ll prev = dp[ind];
  dp[ind] = a[u];
  /*
  for(int i=0;i<N;i++){
      if(dp[i]==inf) cout << "inf" << " ";
      else cout << dp[i] << " ";
  }
  cout << endl;
  */
  ans[u] = lower_bound(dp, dp + N, inf) - dp;
  for (auto v : g[u]) {
    if (v == pa)
      continue;
    solve(v, u);
  }
  dp[ind] = prev;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < N; i++) {
    dp[i] = inf;
  }
  solve(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
using ll = long long;
const int INF = 1e9;
typedef pair<int, int> P;
const int mx = 200000;

vector<int> v[mx];
int an[mx], a[mx];
int n;
int pp[mx];

int dfs(int from, int now, int pc) {
  // cout<<from<<" "<<now<<" "<<pc<<" ";
  auto it = lower_bound(pp, pp + mx, a[now]);
  int pc2 = max(pc, (int)(it - pp));
  // cout<<pc2<<" ";
  int bf = *it;
  // cout<<bf<<endl;
  *it = a[now];
  an[now] = pc2;

  for (int x : v[now]) {
    if (x == from)
      continue;
    dfs(now, x, pc2);
  }

  *it = bf;
}

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

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

  fill(pp, pp + mx, INF);

  dfs(-1, 0, 0);

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
using ll = long long;
const int INF = 1e9;
typedef pair<int, int> P;
const int mx = 200000;

vector<int> v[mx];
int an[mx], a[mx];
int n;
int pp[mx];

void dfs(int from, int now, int pc) {
  // cout<<from<<" "<<now<<" "<<pc<<" ";
  auto it = lower_bound(pp, pp + mx, a[now]);
  int pc2 = max(pc, (int)(it - pp));
  // cout<<pc2<<" ";
  int bf = *it;
  // cout<<bf<<endl;
  *it = a[now];
  an[now] = pc2;

  for (int x : v[now]) {
    if (x == from)
      continue;
    dfs(now, x, pc2);
  }

  *it = bf;
}

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

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

  fill(pp, pp + mx, INF);

  dfs(-1, 0, 0);

  for (int i = 0; i < n; i++) {
    cout << an[i] + 1 << endl;
  }
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int a[2 * 100000];
int lis[2 * 100000];

void dfs(const vector<vector<int>> &edges, int node, vector<int> *state) {
  vector<int>::iterator it = lower_bound(state->begin(), state->end(), a[node]);
  bool extend = it == state->end();
  int original;
  if (extend) {
    state->push_back(a[node]);
  } else {
    original = *it;
    *it = a[node];
  }
  lis[node] = state->size();
  for (int i = 0; i < edges[node].size(); ++i) {
    int n = edges[node][i];
    if (lis[n] == -1) {
      dfs(edges, n, state);
    }
  }
  if (extend) {
    state->pop_back();
  } else {
    *it = original;
  }
}

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

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

  for (int i = 0; i < N; ++i) {
    lis[i] = -1;
  }
  vector<int> state;
  dfs(edges, 0, &state);
  for (int i = 0; i < N; ++i) {
    cout << lis[i] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int a[2 * 100000];
int lis[2 * 100000];

void dfs(const vector<vector<int>> &edges, int node, vector<int> *state) {
  vector<int>::iterator it = lower_bound(state->begin(), state->end(), a[node]);
  bool extend = it == state->end();
  int original;
  if (extend) {
    state->push_back(a[node]);
  } else {
    original = *it;
    *it = a[node];
  }
  lis[node] = state->size();
  for (int i = 0; i < edges[node].size(); ++i) {
    int n = edges[node][i];
    if (lis[n] == -1) {
      dfs(edges, n, state);
    }
  }
  if (extend) {
    state->pop_back();
  } else {
    *it = original;
  }
}

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

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

  for (int i = 0; i < N; ++i) {
    lis[i] = -1;
  }
  vector<int> state;
  state.reserve(N);
  dfs(edges, 0, &state);
  for (int i = 0; i < N; ++i) {
    cout << lis[i] << endl;
  }
}
Problem p02698 (C++)

Buggy submission (status: Time Limit Exceeded):
#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;
  }
}

Fixed submission:
#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 (status: 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);
  }

  dfs(1, 0);

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

  return 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);
  }

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

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

  return 0;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define MAX_N 100009
using ll = long long;

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

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

int main() {
  int n;
  cin >> n;
  a = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    to[s].push_back(t);
    to[t].push_back(s);
  }
  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
#define MAX_N 200009
using ll = long long;

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

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

int main() {
  int n;
  cin >> n;
  a = vector<int>(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    to[s].push_back(t);
    to[t].push_back(s);
  }
  rep(i, MAX_N) dp[i] = INF;
  dp[0] = -INF;
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return (0);
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int r[200001] = {0};
ll inf = 1e18;
vec G[200001], A(200001), dp(200001, inf), ans(200001);
int dfs(int x) {
  if (r[x])
    return 0;
  r[x] = 1;
  ll a = lower_bound(all(dp), A[x]) - dp.begin(),
     b = *lower_bound(all(dp), A[x]);
  *lower_bound(all(dp), A[x]) = A[x];
  ans[x] = lower_bound(all(dp), inf) - dp.begin();
  for (auto i : G[x])
    dfs(i);
  dp[a] = b;
}
int main() {
  int n, u, v;
  cin >> n;
  rep(i, n) cin >> A[i + 1];
  rep(i, n - 1) cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
  dfs(1);
  rep(i, n) cout << ans[i + 1] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int r[200001] = {0};
ll inf = 1e18;
vec G[200001], A(200001), dp(200001, inf), ans(200001);
int dfs(int x) {
  if (r[x])
    return 0;
  r[x] = 1;
  ll a = lower_bound(all(dp), A[x]) - dp.begin(),
     b = *lower_bound(all(dp), A[x]);
  *lower_bound(all(dp), A[x]) = A[x];
  ans[x] = lower_bound(all(dp), inf) - dp.begin();
  for (auto i : G[x])
    dfs(i);
  dp[a] = b;
  return 0;
}
int main() {
  int n, u, v;
  cin >> n;
  rep(i, n) cin >> A[i + 1];
  rep(i, n - 1) cin >> u >> v, G[u].push_back(v), G[v].push_back(u);
  dfs(1);
  rep(i, n) cout << ans[i + 1] << "\n";
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\n", (int)n)
#define lprin(n) printf("%lld\n", n)
#define deb(x) cout << #x << ' ' << x << '\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc                                                                     \
  int tt;                                                                      \
  scn(tt);                                                                     \
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 1e5 + 2;

vector<int> v[N];
set<int> s;
int val[N], ans[N], removed[N];

void dfs(int node, int par) {
  auto i = s.lower_bound(val[node]);

  if (i != s.end()) {
    removed[node] = *i;
    s.erase(*i);
  } else
    removed[node] = -1;

  s.insert(val[node]);

  ans[node] = (int)s.size();

  for (int i : v[node])
    if (i != par)
      dfs(i, node);

  s.erase(val[node]);

  if (removed[node] != -1)
    s.insert(removed[node]);
}

int main() {
  int n;
  scn(n);

  rep(i, 1, n + 1) scn(val[i]);

  rep(i, 1, n) {
    int a, b;
    scn(a);
    scn(b);

    v[a].pb(b);
    v[b].pb(a);
  }

  dfs(1, -1);

  rep(i, 1, n + 1) prin(ans[i]);

  return 0;
}

Fixed submission:
/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\n", (int)n)
#define lprin(n) printf("%lld\n", n)
#define deb(x) cout << #x << ' ' << x << '\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc                                                                     \
  int tt;                                                                      \
  scn(tt);                                                                     \
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 2e5 + 2;

vector<int> v[N];
set<int> s;
int val[N], ans[N], removed[N];

void dfs(int node, int par) {
  auto i = s.lower_bound(val[node]);

  if (i != s.end()) {
    removed[node] = *i;
    s.erase(*i);
  } else
    removed[node] = -1;

  s.insert(val[node]);

  ans[node] = (int)s.size();

  for (int i : v[node])
    if (i != par)
      dfs(i, node);

  s.erase(val[node]);

  if (removed[node] != -1)
    s.insert(removed[node]);
}

int main() {
  int n;
  scn(n);

  rep(i, 1, n + 1) scn(val[i]);

  rep(i, 1, n) {
    int a, b;
    scn(a);
    scn(b);

    v[a].pb(b);
    v[b].pb(a);
  }

  dfs(1, -1);

  rep(i, 1, n + 1) prin(ans[i]);

  return 0;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long long N;
long long A[200005];
vector<long long> tree[200005];
long long dp[100005];
long long ans[100005];

// dp[i]:= 長さが i+1
// であるような増加部分列における最終要素の最小値(存在しない場合は INF)
void dfs(long long pa, long long cur) {
  // cout << "pa " << pa << " cur " << cur << endl;
  long long num = A[cur];

  auto it = lower_bound(dp, dp + N, num);
  long long old = *it;
  *it = num;
  ans[cur] = lower_bound(dp, dp + N, LONG_MAX) - dp;
  for (long long i = 0; i < tree[cur].size(); i++) {
    if (tree[cur][i] != pa) {
      dfs(cur, tree[cur][i]);
    }
  }
  *it = old;
}

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

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

  fill(dp, dp + N, LONG_MAX);
  dfs(-1, 0);

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


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

long long N;
long long A[200005];
vector<long long> tree[200005];
long long dp[200005];
long long ans[200005];

// dp[i]:= 長さが i+1
// であるような増加部分列における最終要素の最小値(存在しない場合は INF)
void dfs(long long pa, long long cur) {
  // cout << "pa " << pa << " cur " << cur << endl;
  long long num = A[cur];

  auto it = lower_bound(dp, dp + N, num);
  long long old = *it;
  *it = num;
  ans[cur] = lower_bound(dp, dp + N, LONG_MAX) - dp;
  for (long long i = 0; i < tree[cur].size(); i++) {
    if (tree[cur][i] != pa) {
      dfs(cur, tree[cur][i]);
    }
  }
  *it = old;
}

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

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

  fill(dp, dp + N, LONG_MAX);
  dfs(-1, 0);

  for (long long i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;

const int MAX_V = 1e5;
vector<int> es[MAX_V];
ll dp[MAX_V];
int ans[MAX_V];
ll A[MAX_V];

void dfs(int now, int pre) {
  int j = lower_bound(dp, dp + MAX_V, A[now]) - dp;
  ll memo = dp[j];
  dp[j] = A[now];
  ans[now] = lower_bound(dp, dp + MAX_V, INF) - dp;
  for (auto &e : es[now]) {
    if (e == pre)
      continue;
    dfs(e, now);
  }
  dp[j] = memo;
}

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    es[a].pb(b), es[b].pb(a);
  }
  fill(dp, dp + MAX_V, INF);
  dfs(0, -1);
  rep(i, N) { cout << ans[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e9;
const ll INF = 1e18;
const ld EPS = 1e-10;

const int MAX_V = 2e5;
vector<int> es[MAX_V];
ll dp[MAX_V];
int ans[MAX_V];
ll A[MAX_V];

void dfs(int now, int pre) {
  int j = lower_bound(dp, dp + MAX_V, A[now]) - dp;
  ll memo = dp[j];
  dp[j] = A[now];
  ans[now] = lower_bound(dp, dp + MAX_V, INF) - dp;
  for (auto &e : es[now]) {
    if (e == pre)
      continue;
    dfs(e, now);
  }
  dp[j] = memo;
}

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    es[a].pb(b), es[b].pb(a);
  }
  fill(dp, dp + MAX_V, INF);
  dfs(0, -1);
  rep(i, N) { cout << ans[i] << endl; }
}
Problem p02698 (C++)

Buggy submission (status: 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 = 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;
}

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 = 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;
}
Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02698 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
vvi to;
vi ans;
vi a;

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

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

  int n;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> 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);
  ans[0] = 1;

  vi dp(n, INF);
  dp[0] = a[0];
  dfs(0, dp);
  rep(i, n) cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;
vvi to;
vi ans;
vi a;

void dfs(int v, vi &dp, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    int idx = lower_bound(dp.begin(), dp.end(), a[u]) - dp.begin();
    int pre = dp[idx];
    dp[idx] = a[u];
    ans[u] = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
    dfs(u, dp, v);
    dp[idx] = pre;
  }
}

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

  int n;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> 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);
  ans[0] = 1;

  vi dp(n, INF);
  dp[0] = a[0];
  dfs(0, dp);
  rep(i, n) cout << ans[i] << endl;
}
Problem p02698 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

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

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

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

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


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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  int S;
  int W;
  S = atoi(argv[1]);
  W = atoi(argv[2]);
  if (W >= S) {
    printf("unsafe");
  } else {
    printf("safe");
  }
  // printf("羊の数は%d、オオカミの数は%d",S,W);
}


Fixed submission:
#include <cstdio>
#include <stdlib.h>

int main() {
  int S, W;
  scanf("%d%d", &S, &W);
  if (W >= S) {
    printf("unsafe");
  } else {
    printf("safe");
  }
  // printf("羊の数は%d、オオカミの数は%d",S,W);
}
Problem p02699 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<string, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll S;
  cin >> S;
  ll W;
  cin >> W;
  for (ll i = 0; i < 10000000000; i++) {
    swap(S, W);
  }
  if (S <= W)
    cout << "unsafe" << endl;
  else
    cout << "safe" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<string, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll S;
  cin >> S;
  ll W;
  cin >> W;
  for (ll i = 0; i < 1000000000; i++) {
    swap(S, W);
  }
  if (S <= W)
    cout << "unsafe" << endl;
  else
    cout << "safe" << endl;
  return 0;
}
Problem p02699 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  vector<string> S(11);
  cin >> N;
  for (int i = 0; i < 11; i++) {
    S.at(i);
  }
  int count = N;
  sort(S.begin(), S.end());

  for (int i = 0; i < N - 1; i++) {

    if (S.at(i) == S.at(i + 1))
      count--;
  }
  cout << count << endl;
}

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

int main() {
  int S, W;
  cin >> S >> W;
  if (S <= W) {
    cout << "unsafe" << endl;
  } else
    cout << "safe" << endl;
}
Problem p02699 (Python)

Buggy submission (status: Runtime Error):
A, B = map(int.input().split())

if A > B:
    print("safe")
else:
    print("unsafe")


Fixed submission:
A, B = map(int, input().split())

if A > B:
    print("safe")
else:
    print("unsafe")
Problem p02699 (Python)

Buggy submission (status: Runtime Error):
s, w = map(int, input().split(""))
res = "safe"
if w >= s:
    res = "unsafe"
print(res)


Fixed submission:
s, w = map(int, input().split(" "))
res = "safe"
if w >= s:
    res = "unsafe"
print(res)
Problem p02699 (Python)

Buggy submission (status: Runtime Error):
def main():
    s, w = map(int, input())
    if w >= s:
        print("unsafe")
    else:
        print("safe")


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    s, w = map(int, input().split())
    if w >= s:
        print("unsafe")
    else:
        print("safe")


if __name__ == "__main__":
    main()
Problem p02700 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;

ll n, m, k, a, b, c, d;
string s, t;

int main() {
  cin >> a >> b >> c >> d;
  int cnt = 0;
  while (a && c) {
    cnt % 2 == 0 ? c -= b : a -= d;
    cnt++;
  }
  cout << (cnt % 2 == 0 ? "No" : "Yes") << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;

ll n, m, k, a, b, c, d;
string s, t;

int main() {
  cin >> a >> b >> c >> d;
  int cnt = 0;
  while (a > 0 && c > 0) {
    cnt % 2 == 0 ? c -= b : a -= d;
    cnt++;
  }
  cout << (cnt % 2 == 0 ? "No" : "Yes") << endl;
}
Problem p02700 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  int x = (a + d - 1) / d;
  int y = (c + b - 1) / b;
  x >= y ? (cout << "YES\n") : (cout << "NO\n");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int i = 0;
  while (a > 0 && c > 0) {
    if (i)
      a -= d, i--;
    else
      c -= b, i++;
  }
  if (a > 0)
    cout << "Yes\n";
  else
    cout << "No\n";
}