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

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

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ld long double
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define all(v) v.begin(), v.end()
#define decimal_digits cout << fixed << setprecision(15);

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define GOOGLE 0
#define MULTIPLE_TC 0

const int inf = 1e9;
const long long INF = 1e18;
const int mod = 1e9 + 7;
const int bit32 = log2(inf) + 3;
const int bit64 = log2(INF) + 3;

inline int add(long long a, long long b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
inline int sub(long long a, long long b) {
  a -= b;
  if (a < 0)
    a += mod;
  return a;
}
inline int mul(long long a, long long b) {
  a %= mod;
  b %= mod;
  return (int)((long long)a * b % mod);
}
inline int modexpo(long long a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}
inline int divide(long long a, long long b) {
  return mul(a, modexpo(b, mod - 2));
}
inline int inv(long long a) { return modexpo(a, mod - 2); }

clock_t time_p = clock();

void ktj() {
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\\n";
}

void pre() { // Reset each global variable (esp. for graphs)
}

const int N = 2e5 + 5;
vector<int> g[N];
int a[N], ans[N];

int t[4 * N], lim, cnt[N];

void update(int v, int L, int R, int pos, int val) {
  if (L == R) {
    t[v] = val;
    return;
  }
  int mid = (L + R) >> 1;
  if (pos <= mid)
    update(2 * v, L, mid, pos, val);
  else
    update(2 * v + 1, mid + 1, R, pos, val);
  t[v] = max(t[2 * v], t[2 * v + 1]);
}

int query(int v, int L, int R, int l, int r) {
  if (L > r or R < l or R < L)
    return 0;
  if (l <= L and R <= r)
    return t[v];
  int mid = (L + R) >> 1;
  return max(query(2 * v, L, mid, l, r), query(2 * v + 1, mid + 1, R, l, r));
}

void dfs(int u, int p, int level) {

  int prev = query(1, 1, lim, a[u], a[u]);
  int q = query(1, 1, lim, 1, a[u] - 1);
  ans[u] = max(ans[u], ans[p]);
  ans[u] = max(ans[u], q + 1);

  update(1, 1, lim, a[u], q + 1);

  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u, level + 1);
    }
  }

  update(1, 1, lim, a[u], prev);
}

void solve() {
  int n, i, j, k;
  cin >> n;
  set<int> s;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }
  map<int, int> compress;
  i = 1;
  for (auto p : s) {
    compress[p] = i++;
  }
  lim = i - 1;
  for (i = 1; i <= n; i++) {
    a[i] = compress[a[i]];
  }
  for (i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1, 0, 1);
  for (i = 1; i <= n; i++) {
    cout << ans[i] << '\\n';
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  pre();

  int t = 1, T;

  if (MULTIPLE_TC)
    cin >> t;

  for (T = 1; T <= t; T++) {
    if (GOOGLE)
      cout << "Case #" << T << ": ";
    solve();
  }

  ktj();
}


Error: TLE

Fixed submission:

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

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ld long double
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define all(v) v.begin(), v.end()
#define decimal_digits cout << fixed << setprecision(15);

string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A> string to_string(A);
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f)
      r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define GOOGLE 0
#define MULTIPLE_TC 0

const int inf = 1e9;
const long long INF = 1e18;
const int mod = 1e9 + 7;
const int bit32 = log2(inf) + 3;
const int bit64 = log2(INF) + 3;

inline int add(long long a, long long b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}
inline int sub(long long a, long long b) {
  a -= b;
  if (a < 0)
    a += mod;
  return a;
}
inline int mul(long long a, long long b) {
  a %= mod;
  b %= mod;
  return (int)((long long)a * b % mod);
}
inline int modexpo(long long a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b /= 2;
  }
  return res;
}
inline int divide(long long a, long long b) {
  return mul(a, modexpo(b, mod - 2));
}
inline int inv(long long a) { return modexpo(a, mod - 2); }

clock_t time_p = clock();

void ktj() {
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\\n";
}

void pre() { // Reset each global variable (esp. for graphs)
}

const int N = 2e5 + 5;
vector<int> g[N];
int a[N], ans[N];

int t[4 * N], lim, cnt[N];

void update(int v, int L, int R, int pos, int val) {
  if (L == R) {
    t[v] = val;
    return;
  }
  int mid = (L + R) >> 1;
  if (pos <= mid)
    update(2 * v, L, mid, pos, val);
  else
    update(2 * v + 1, mid + 1, R, pos, val);
  t[v] = max(t[2 * v], t[2 * v + 1]);
}

int query(int v, int L, int R, int l, int r) {
  if (L > r or R < l or R < L)
    return 0;
  if (l <= L and R <= r)
    return t[v];
  int mid = (L + R) >> 1;
  return max(query(2 * v, L, mid, l, r), query(2 * v + 1, mid + 1, R, l, r));
}

void dfs(int u, int p, int level) {

  int prev = cnt[a[u]];
  int q = query(1, 1, lim, 1, a[u] - 1);
  ans[u] = max(ans[u], prev);
  ans[u] = max(ans[u], ans[p]);
  ans[u] = max(ans[u], q + 1);
  pr(u, prev, ans[p], q);

  update(1, 1, lim, a[u], ans[u]);

  for (auto v : g[u]) {
    if (v != p) {
      dfs(v, u, level + 1);
    }
  }

  update(1, 1, lim, a[u], prev);
}

void solve() {
  int n, i, j, k;
  cin >> n;
  set<int> s;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }
  map<int, int> compress;
  i = 1;
  for (auto p : s) {
    compress[p] = i++;
  }
  lim = i - 1;
  for (i = 1; i <= n; i++) {
    a[i] = compress[a[i]];
  }
  for (i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(1, 0, 1);
  for (i = 1; i <= n; i++) {
    cout << ans[i] << '\\n';
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  pre();

  int t = 1, T;

  if (MULTIPLE_TC)
    cin >> t;

  for (T = 1; T <= t; T++) {
    if (GOOGLE)
      cout << "Case #" << T << ": ";
    solve();
  }

  ktj();
}