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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vli vector<ll>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define M_PI 3.14159265358979323846
#define mp make_pair
#define sz(x) (int)x.size()
#define matrix vector<vector<ll>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define eps 0.0000000001
#define all(a) (a).begin(), (a).end()
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

const long long INF = (long long)1e11 + 1;
const long long mod = (long long)998244353;
const long long hell = (long long)1000000007;

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2)
      res = (x * res);
    x = (x * x);
    y = y / 2;
  }
  return res;
}

ll modinv(ll x) { return power(x, hell - 2); }

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll gcd(ll a, ll b) {
  while (b != 0) {
    ll t = b;
    b = a % t;
    a = t;
  }
  return a;
}

bool comp1(pair<ll, ll> &a, pair<ll, ll> &b) { return a.F < b.F; }

bool comp2(pair<ll, ll> &a, pair<ll, ll> &b) { return a.F > b.F; }

ll val[200005], res[200005];
vi gr[200005];
vector<ll> vec;
void dfs(int v, int p) {
  auto it = lower_bound(all(vec), val[v]);
  bool flag = true;
  ll pre;
  if (it == vec.end()) {
    vec.pb(val[v]);
  } else {
    pre = *it;
    *it = val[v];
    flag = false;
  }
  res[v] = vec.size();

  for (auto u : gr[v]) {
    if (u != p) {
      dfs(u, v);
    }
  }
  if (flag)
    vec.pop_back();
  else {
    it = lower_bound(all(vec), val[v]);
    *it = pre;
  }
}

void solve() {
  int n;
  cin >> n;
  forn(i, 1, n + 1) cin >> val[i];

  forn(i, 1, n) {
    int u, v;
    cin >> u >> v;
    gr[u].pb(v);
    gr[v].pb(u);
  }
  dfs(1, -1);
  forn(i, 1, n + 1) cout << res[i] << "\\n";
}

int main() {
  fastIO();
  int test;
  // cin>>test;
  test = 1;

  while (test--)
    solve();
}

/// Before submit=>
///     *check for integer overflow,array bounds
///     *check for n=1

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

// gcd of strings
/*
string getgcd(string s1,string s2){

    while(true){
        if(s1.length()<s2.length()){
            swap(s1,s2);
            continue;
        }
        if(s2.empty())
        return s1;

        if(s1.substr(0,s2.length())!=s2)
        return "";

        s1=s1.substr(s2.length());
    }
}
*/
/*
// for user defined DS hashmap and set

struct hash_pair{
      size_t operator()(const pair<int,pair<int,int>>& p) const{
          auto hash1=hash<int>()(p.F) ;
          auto hash2=hash<int>()(p.S.F) ;
          auto hash3=hash<int>()(p.S.S) ;
          return (hash1^hash2)^hash3;
      }
};
struct pair_hash
{
        template <class T1, class T2>
        std::size_t operator () (std::pair<T1, T2> const &pair) const
        {
                std::size_t h1 = std::hash<T1>()(pair.first);
                std::size_t h2 = std::hash<T2>()(pair.second);

                return h1 ^ h2;
        }
};
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vli vector<ll>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define M_PI 3.14159265358979323846
#define mp make_pair
#define sz(x) (int)x.size()
#define matrix vector<vector<ll>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define eps 0.0000000001
#define all(a) (a).begin(), (a).end()
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

const long long INF = (long long)1e11 + 1;
const long long mod = (long long)998244353;
const long long hell = (long long)1000000007;

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2)
      res = (x * res);
    x = (x * x);
    y = y / 2;
  }
  return res;
}

ll modinv(ll x) { return power(x, hell - 2); }

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll gcd(ll a, ll b) {
  while (b != 0) {
    ll t = b;
    b = a % t;
    a = t;
  }
  return a;
}

bool comp1(pair<ll, ll> &a, pair<ll, ll> &b) { return a.F < b.F; }

bool comp2(pair<ll, ll> &a, pair<ll, ll> &b) { return a.F > b.F; }

ll val[200005], res[200005];
vi gr[200005];

void dfs(int v, int p, vector<ll> vec) {
  auto it = lower_bound(all(vec), val[v]);
  if (it == vec.end()) {
    vec.pb(val[v]);
  } else {
    *it = val[v];
  }
  res[v] = vec.size();

  for (auto u : gr[v]) {
    if (u != p) {
      dfs(u, v, vec);
    }
  }
}

void solve() {
  int n;
  cin >> n;
  forn(i, 1, n + 1) cin >> val[i];

  forn(i, 1, n) {
    int u, v;
    cin >> u >> v;
    gr[u].pb(v);
    gr[v].pb(u);
  }
  dfs(1, -1, vector<ll>());
  forn(i, 1, n + 1) cout << res[i] << "\\n";
}

int main() {
  fastIO();
  int test;
  // cin>>test;
  test = 1;

  while (test--)
    solve();
}

/// Before submit=>
///     *check for integer overflow,array bounds
///     *check for n=1

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

// gcd of strings
/*
string getgcd(string s1,string s2){

    while(true){
        if(s1.length()<s2.length()){
            swap(s1,s2);
            continue;
        }
        if(s2.empty())
        return s1;

        if(s1.substr(0,s2.length())!=s2)
        return "";

        s1=s1.substr(s2.length());
    }
}
*/
/*
// for user defined DS hashmap and set

struct hash_pair{
      size_t operator()(const pair<int,pair<int,int>>& p) const{
          auto hash1=hash<int>()(p.F) ;
          auto hash2=hash<int>()(p.S.F) ;
          auto hash3=hash<int>()(p.S.S) ;
          return (hash1^hash2)^hash3;
      }
};
struct pair_hash
{
        template <class T1, class T2>
        std::size_t operator () (std::pair<T1, T2> const &pair) const
        {
                std::size_t h1 = std::hash<T1>()(pair.first);
                std::size_t h2 = std::hash<T2>()(pair.second);

                return h1 ^ h2;
        }
};
*/