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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

struct segment_tree {
  static const int N = 2e5 + 10;
  int t[4 * N];
  int merge(int x, int y) { return max(x, y); }
  void update(int pos, int val, int v = 1, int tl = 1, int tr = N - 1) {
    if (tl > tr || tl > pos || pos > tr)
      return;
    if (tl == tr) {
      t[v] = val;
      return;
    }
    int tm = (tl + tr) / 2;
    update(pos, val, v + v, tl, tm);
    update(pos, val, v + v + 1, tm + 1, tr);
    t[v] = merge(t[v + v], t[v + v + 1]);
  }
  int get(int l, int r, int v = 1, int tl = 1, int tr = N - 1) {
    if (tl > tr || l > r || tl > r || l > tr)
      return 0;
    if (l <= tl && tr <= r) {
      return t[v];
    }
    int tm = (tl + tr) / 2;
    return merge(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
  }
} t;

void delete_copies(vector<int> &a) {
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
}

const int N = 2e5 + 10;
int a[N];
vector<int> all;
map<int, int> mp;
vector<int> g[N];
int ans[N];

void dfs(int v, int p) {
  int pos = mp[a[v]];
  int rem = t.get(pos, pos);
  t.update(pos, t.get(1, pos - 1) + 1);
  ans[v] = t.get(1, N - 1);
  for (int to : g[v]) {
    if (to != p) {
      dfs(to, v);
    }
  }
  t.update(pos, rem);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    all.pb(a[i]);
  }
  delete_copies(all);
  for (int i = 0; i < all.size(); i++) {
    mp[all[i]] = i + 1;
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << "\\n";
  }

#ifdef LOCAL
  cerr << "\\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

struct segment_tree {
  static const int N = 2e5 + 10;
  multiset<int> t[4 * N];
  int merge(int x, int y) { return max(x, y); }
  void init() {
    for (int i = 0; i <= 4 * N - 1; i++) {
      t[i] = {0};
    }
  }
  void add(int pos, int val, int v = 1, int tl = 1, int tr = N - 1) {
    if (tl > tr || tl > pos || pos > tr)
      return;
    if (tl == tr) {
      t[v].insert(val);
      return;
    }
    int tm = (tl + tr) / 2;
    add(pos, val, v + v, tl, tm);
    add(pos, val, v + v + 1, tm + 1, tr);
    t[v].insert(val);
  }
  void del(int pos, int val, int v = 1, int tl = 1, int tr = N - 1) {
    if (tl > tr || tl > pos || pos > tr)
      return;
    if (tl == tr) {
      t[v].erase(t[v].find(val));
      return;
    }
    int tm = (tl + tr) / 2;
    del(pos, val, v + v, tl, tm);
    del(pos, val, v + v + 1, tm + 1, tr);
    t[v].erase(t[v].find(val));
  }
  int get(int l, int r, int v = 1, int tl = 1, int tr = N - 1) {
    if (tl > tr || l > r || tl > r || l > tr)
      return 0;
    if (l <= tl && tr <= r) {
      return *t[v].rbegin();
    }
    int tm = (tl + tr) / 2;
    return merge(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
  }
} t;

void delete_copies(vector<int> &a) {
  sort(a.begin(), a.end());
  a.erase(unique(a.begin(), a.end()), a.end());
}

const int N = 2e5 + 10;
int a[N];
vector<int> all;
map<int, int> mp;
vector<int> g[N];
int ans[N];

void dfs(int v, int p) {
  int pos = mp[a[v]];
  int val = t.get(1, pos - 1) + 1;
  t.add(pos, val);
  ans[v] = t.get(1, N - 1);
  for (int to : g[v]) {
    if (to != p) {
      dfs(to, v);
    }
  }
  t.del(pos, val);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  t.init();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    all.pb(a[i]);
  }
  delete_copies(all);
  for (int i = 0; i < int(all.size()); i++) {
    mp[all[i]] = i + 1;
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << "\\n";
  }

#ifdef LOCAL
  cerr << "\\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}