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

// #pragma GCC optimize("O3", "unroll-loops")
// #pragma GCC target("avx2")

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ost tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>

#define int long long int
#define ld long double
#define f(i, j, n) for (int i = j; i <= n; i++)
#define r(i, n, j) for (int i = n; i >= j; i--)
#define mod 1000000007
#define speed ios_base::sync_with_stdio(false);
#define upp cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define T                                                                      \\
  int ttt;                                                                     \\
  cin >> ttt;                                                                  \\
  while (ttt--)
#define ff first
#define ss second
#define all(container) container.begin(), container.end()
#define sz(container) (int)container.size()
#define pii pair<int, int>
#define sp(x) setprecision(x)
#define bp(x) __builtin_popcount(x)
#define endl "\\n"
int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<int> v[200005];
int a[200005], ans[200005];
bool vis[200005];
set<int> s;
void dfs(int r) {
  vis[r] = true;
  int cc = -1, ff = -1;
  set<int>::iterator it;
  if (s.insert(a[r]).second) {
    it = s.find(a[r]);
    it++;
    if (it != s.end()) {
      ff = *it;
      s.erase(it);
    }
    cc = a[r];
  }
  ans[r] = sz(s);
  for (auto i : v[r])
    if (!vis[i]) {
      dfs(i);
    }
  if (cc != -1) {
    s.erase(s.find(cc));
    if (ff != -1)
      s.insert(ff);
  }
}
int32_t main() {
  speed upp int n, x, y;
  cin >> n;
  f(i, 1, n) cin >> a[i];
  f(i, 1, n - 1) {
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }

  dfs(1);
  f(i, 1, n) {
    cout << ans[i];
    cout << endl;
  }
}


Error: TLE

Fixed submission:

// #pragma GCC optimize("O3", "unroll-loops")
// #pragma GCC target("avx2")

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ost tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>

#define int long long int
#define ld long double
#define f(i, j, n) for (int i = j; i <= n; i++)
#define r(i, n, j) for (int i = n; i >= j; i--)
#define mod 1000000007
#define speed ios_base::sync_with_stdio(false);
#define upp cin.tie(NULL), cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define T                                                                      \\
  int ttt;                                                                     \\
  cin >> ttt;                                                                  \\
  while (ttt--)
#define ff first
#define ss second
#define all(container) container.begin(), container.end()
#define sz(container) (int)container.size()
#define pii pair<int, int>
#define sp(x) setprecision(x)
#define bp(x) __builtin_popcount(x)
#define endl "\\n"
int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<int> v[200005];
int a[200005], ans[200005];
bool vis[200005];

void dfs(int r, set<int> s) {
  vis[r] = true;
  set<int>::iterator it;
  if (s.insert(a[r]).second) {
    it = s.find(a[r]);
    it++;
    if (it != s.end())
      s.erase(it);
  }
  ans[r] = sz(s);
  for (auto i : v[r])
    if (!vis[i]) {
      dfs(i, s);
    }
}
int32_t main() {
  speed upp int n, x, y;
  cin >> n;
  f(i, 1, n) cin >> a[i];
  f(i, 1, n - 1) {
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  set<int> s;
  dfs(1, s);
  f(i, 1, n) {
    cout << ans[i];
    cout << endl;
  }
}