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>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-7
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int n;
vector<set<int>> vec;
vector<int> val;

ll res[200005];
set<int> now;

void dfs(int node, int par) {
  int cc = -1, ff = -1;
  if (now.insert(val[node]).second) {
    auto it = now.find(val[node]);
    it++;
    if (it != now.end()) {
      ff = *it;
      now.erase(it);
    }

    cc = val[node];
  }

  res[node] = now.size();

  for (auto &it : vec[node]) {
    if (par == it)
      continue;
    dfs(it, node);
  }

  if (cc != -1) {
    now.erase(now.find(cc));
    if (ff != -1)
      now.insert(ff);
  }
}

int32_t main() {
  fastio cin >> n;
  vec.assign(n, set<int>());
  val.assign(n, 0);
  for (auto &it : val)
    cin >> it;

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

  dfs(0, -1);

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-7
#define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int n;
vector<set<int>> vec;
vector<int> val;

vector<int> cur;
ll res[200005];

void dfs(int node, int par) {
  cur.push_back(val[node]);

  set<int> now;
  int ans = 1;
  for (int i = 0; i < cur.size(); ++i) {
    auto it = now.upper_bound(cur[i]);
    if (it == now.end()) {
      now.insert(cur[i]);
    } else {
      now.erase(it);
      now.insert(cur[i]);
    }
    ans = max(ans, int(now.size()));
  }
  res[node] = ans;

  for (auto &it : vec[node]) {
    if (par == it)
      continue;
    dfs(it, node);
  }
  cur.pop_back();
}

int32_t main() {
  fastio cin >> n;
  vec.assign(n, set<int>());
  val.assign(n, 0);
  for (auto &it : val)
    cin >> it;

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

  dfs(0, -1);

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