Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

//  RAKSHIT KADAM
// ALWAYS USE &IT FOR ITERATING USING AUTO ...
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,
// tree_order_statistics_node_update>
#warning !!!check the size of arrayss!!!

// NUMERIC DEFINITIONS :
#define INF 2000000000000000005
#define MOD 1000000007
#define newMOD 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795

#define ll long long
#define int long long
#define P(gg)                                                                  \\
  for (auto rax : gg) {                                                        \\
    cout << rax << " ";                                                        \\
  }                                                                            \\
  cout << endl;
#define Need_for_speed(activated)                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define satisfy                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define inp(n)                                                                 \\
  int n;                                                                       \\
  cin >> n
#define mp make_pair
#define pb push_back
#define endl "\\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>
#define input                                                                  \\
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;
// ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
// int power(int x, unsigned int y, unsigned int m){ if (y == 0) return 1;int p
// = power(x, y/2, m) % m;  p = (p * p) % m; return (y%2 == 0)? p : (x * p) %
// m;} ll modInverse(int a, int m){int g = gcd(a, m); { return power(a, m-2,
// m);}}

vi v[MAX];
ll val[MAX];
vi h;
ll ans[MAX];
void dfs(ll root, ll par) {
  ll f = 0;
  ll rev = -INF;
  ll j = upper_bound(h.begin(), h.end(), val[root]) - h.begin();
  ll dd = j;
  if (h[j - 1] < val[root] && val[root] < h[j]) {
    rev = h[j];
    h[j] = val[root];
    f = 1;
  }
  j = lower_bound(h.begin(), h.end(), INF) - h.begin();
  j--;
  ans[root] = j;

  for (auto it : v[root]) {
    if (it != par) {
      dfs(it, root);
    }
  }
  if (f)
    h[dd] = rev;
}
void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> val[i];
  }
  fore(i, 0, n - 1) {
    ll a, b;
    cin >> a >> b;
    v[b].pb(a);
    v[a].pb(b);
  }
  for (ll i = 0; i <= n + 1; i++)
    h.pb(INF);
  h[0] = -INF;
  dfs(1, 0);
  for (ll i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
signed main() {

  Need_for_speed(activated);
  // satisfy
  //     {
  //    solve();
  //     }

  solve();
  return 0;
}


Error: TLE

Fixed submission:

//  RAKSHIT KADAM
// ALWAYS USE &IT FOR ITERATING USING AUTO ...
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int ,  null_type ,  less<int> ,  rb_tree_tag ,
// tree_order_statistics_node_update>
#warning !!!check the size of arrayss!!!

// NUMERIC DEFINITIONS :
#define INF 2000000000000000005
#define MOD 1000000007
#define newMOD 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795

#define ll long long
#define int long long
#define P(gg)                                                                  \\
  for (auto rax : gg) {                                                        \\
    cout << rax << " ";                                                        \\
  }                                                                            \\
  cout << endl;
#define Need_for_speed(activated)                                              \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define satisfy                                                                \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define inp(n)                                                                 \\
  int n;                                                                       \\
  cin >> n
#define mp make_pair
#define pb push_back
#define endl "\\n"
#define x first
#define y second
#define fore(i, a, b) for (ll i = a; i < b; i++)
#define forci(i, n) for (ll i = 0; i < n; i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll, ll>
#define input                                                                  \\
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
using namespace std;
// ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
// int power(int x, unsigned int y, unsigned int m){ if (y == 0) return 1;int p
// = power(x, y/2, m) % m;  p = (p * p) % m; return (y%2 == 0)? p : (x * p) %
// m;} ll modInverse(int a, int m){int g = gcd(a, m); { return power(a, m-2,
// m);}}

vi v[MAX];
ll val[MAX];

ll ans[MAX];
void dfs(ll root, ll par, vi h) {
  ll j = upper_bound(h.begin(), h.end(), val[root]) - h.begin();
  if (h[j - 1] < val[root] && val[root] < h[j])
    h[j] = val[root];
  j = lower_bound(h.begin(), h.end(), INF) - h.begin();
  j--;
  ans[root] = j;
  for (auto it : v[root]) {
    if (it != par) {
      dfs(it, root, h);
    }
  }
}
void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> val[i];
  }
  fore(i, 0, n - 1) {
    ll a, b;
    cin >> a >> b;
    v[b].pb(a);
    v[a].pb(b);
  }
  vi help(n + 2, INF);
  help[0] = -INF;
  dfs(1, 0, help);
  for (ll i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
signed main() {

  Need_for_speed(activated);
  // satisfy
  //     {
  //    solve();
  //     }

  solve();
  return 0;
}