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

#include <bits/stdc++.h>

#define FOR(i, a, b) for (auto i = a; i <= b; ++i)
#define REP(i, a, b) for (auto i = a; i < b; ++i)
#define FORI(i, a, b)                                                          \\
  for (auto i = a; i != b + 1 - 2 * (a > b); i += 1 - 2 * (a > b))
#define REPI(i, a, b)                                                          \\
  for (auto i = a - (a > b); i != b - (a > b); i += 1 - 2 * (a > b))
#define ALL(v) v.begin(), v.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define eb(a, b) emplace_back(a, b)
#define fir first
#define sec second
#define what_is(x) cout << #x << " is " << x << endl;
#define type(x) typeid(x).name()
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define PI acos(-1)
#define open_read freopen("christmas.in", "r", stdin)
#define open_write freopen("christmas.out", "w", stdout)

using namespace std;

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, PII> PIPII;
typedef pair<PII, PII> PPIIPII;
typedef pair<LL, LL> PLL;
typedef pair<ULL, ULL> PUU;
typedef pair<LL, PLL> PLPLL;
typedef pair<PLL, PLL> PPLLPLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;

template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
  out << "(" << obj.first << ", " << obj.second << ")";
  return out;
}

template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
  typename map<T, T1>::const_iterator itr = cont.begin();
  typename map<T, T1>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T> ostream &operator<<(ostream &out, set<T> cont) {
  typename set<T>::const_iterator itr = cont.begin();
  typename set<T>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T> ostream &operator<<(ostream &out, multiset<T> cont) {
  typename multiset<T>::const_iterator itr = cont.begin();
  typename multiset<T>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T,
          template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
  typename CONT<T>::const_iterator itr = cont.begin();
  typename CONT<T>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
  REP(i, 0, N) { out << arr[i] << " "; }

  out << endl;
  return out;
}

template <typename T> T GCD(T a, T b) {
  T min_v = min(a, b);
  T max_v = max(a, b);

  while (min_v) {
    T temp = max_v % min_v;
    max_v = min_v;
    min_v = temp;
  }

  return max_v;
}

template <typename T> T LCM(T a, T b) { return (a * b) / gcd(a, b); }

template <typename T> T fastExpPow(T base, T exp, T mod) {
  T res = 1;

  while (exp) {
    if (exp & 1) {
      res *= base;
      res %= mod;
    }

    exp >>= 1;
    base *= base;
    base %= mod;
  }

  return res % mod;
}

/*#################################################################################################################
###################################################################################################################
###################################################################################################################
#################################################################################################################*/

#define SIZE 200010

int N;
int numbers[SIZE], segTree[4 * SIZE], lis[SIZE];
vector<int> tree[SIZE], order[SIZE];

int getMax(int Qbeg, int Qend, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
  if (Tbeg > Qend - 1 || Tend < Qbeg - 1) {
    return 0;
  }

  if (Qbeg - 1 <= Tbeg && Tend <= Qend - 1) {
    return segTree[ind];
  }

  int Tmid = (Tbeg + Tend) >> 1;

  int max1 = getMax(Qbeg, Qend, Tbeg, Tmid, 2 * ind + 1);
  int max2 = getMax(Qbeg, Qend, Tmid + 1, Tend, 2 * ind + 2);

  return max(max1, max2);
}

void update(int pos, int val, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
  if (Tbeg > pos - 1 || Tend < pos - 1) {
    return;
  }

  if (Tbeg == pos - 1 && Tbeg == Tend) {
    segTree[ind] = val;
    return;
  }

  int Tmid = (Tbeg + Tend) >> 1;

  update(pos, val, Tbeg, Tmid, 2 * ind + 1);
  update(pos, val, Tmid + 1, Tend, 2 * ind + 2);

  segTree[ind] = max(segTree[2 * ind + 1], segTree[2 * ind + 2]);
}

void dfs(int node = 1, int par = 0) {
  int num = numbers[node];
  int val = getMax(0, num - 1) + 1;

  order[num].pb(val);
  update(num, order[num].back());
  lis[node] = getMax(1, N);

  for (auto connNode : tree[node]) {
    if (connNode != par) {
      dfs(connNode, node);
    }
  }

  order[num].pop_back();

  if (order[num].empty()) {
    update(num, 0);
  } else {
    update(num, order[num].back());
  }
}

int main() {
  scanf("%d", &N);
  vector<PII> sorted;

  FOR(i, 1, N) {
    scanf("%d", numbers + i);
    sorted.eb(numbers[i], i);
  }

  sort(ALL(sorted));

  int cnt = 1;
  numbers[sorted[0].sec] = cnt;

  REP(i, 1, N) {
    if (sorted[i].fir > sorted[i - 1].fir) {
      ++cnt;
    }

    numbers[sorted[i].sec] = cnt;
  }

  FOR(i, 1, N - 1) {
    int U, V;
    scanf("%d%d", &U, &V);

    tree[U].pb(V);
    tree[V].pb(U);
  }

  dfs();

  FOR(i, 1, N) { printf("%d\\n", lis[i]); }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (auto i = a; i <= b; ++i)
#define REP(i, a, b) for (auto i = a; i < b; ++i)
#define FORI(i, a, b)                                                          \\
  for (auto i = a; i != b + 1 - 2 * (a > b); i += 1 - 2 * (a > b))
#define REPI(i, a, b)                                                          \\
  for (auto i = a - (a > b); i != b - (a > b); i += 1 - 2 * (a > b))
#define ALL(v) v.begin(), v.end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define eb(a, b) emplace_back(a, b)
#define fir first
#define sec second
#define what_is(x) cout << #x << " is " << x << endl;
#define type(x) typeid(x).name()
#define ms(arr, val) memset(arr, val, sizeof(arr))
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define PI acos(-1)
#define open_read freopen("christmas.in", "r", stdin)
#define open_write freopen("christmas.out", "w", stdout)

using namespace std;

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef pair<int, int> PII;
typedef pair<int, PII> PIPII;
typedef pair<PII, PII> PPIIPII;
typedef pair<LL, LL> PLL;
typedef pair<ULL, ULL> PUU;
typedef pair<LL, PLL> PLPLL;
typedef pair<PLL, PLL> PPLLPLL;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;

template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
  out << "(" << obj.first << ", " << obj.second << ")";
  return out;
}

template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
  typename map<T, T1>::const_iterator itr = cont.begin();
  typename map<T, T1>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T> ostream &operator<<(ostream &out, set<T> cont) {
  typename set<T>::const_iterator itr = cont.begin();
  typename set<T>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T> ostream &operator<<(ostream &out, multiset<T> cont) {
  typename multiset<T>::const_iterator itr = cont.begin();
  typename multiset<T>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T,
          template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
  typename CONT<T>::const_iterator itr = cont.begin();
  typename CONT<T>::const_iterator ends = cont.end();

  for (; itr != ends; ++itr) {
    out << *itr << " ";
  }

  out << endl;
  return out;
}

template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
  REP(i, 0, N) { out << arr[i] << " "; }

  out << endl;
  return out;
}

template <typename T> T GCD(T a, T b) {
  T min_v = min(a, b);
  T max_v = max(a, b);

  while (min_v) {
    T temp = max_v % min_v;
    max_v = min_v;
    min_v = temp;
  }

  return max_v;
}

template <typename T> T LCM(T a, T b) { return (a * b) / gcd(a, b); }

template <typename T> T fastExpPow(T base, T exp, T mod) {
  T res = 1;

  while (exp) {
    if (exp & 1) {
      res *= base;
      res %= mod;
    }

    exp >>= 1;
    base *= base;
    base %= mod;
  }

  return res % mod;
}

/*#################################################################################################################
###################################################################################################################
###################################################################################################################
#################################################################################################################*/

#define SIZE 200010

int N;
int numbers[SIZE], segTree[4 * SIZE], lis[SIZE];
vector<int> tree[SIZE], order[SIZE];

int getMax(int Qbeg, int Qend, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
  if (Tbeg > Qend - 1 || Tend < Qbeg - 1) {
    return 0;
  }

  if (Qbeg - 1 <= Tbeg && Tend <= Qend - 1) {
    return segTree[ind];
  }

  int Tmid = (Tbeg + Tend) >> 1;

  int max1 = getMax(Qbeg, Qend, Tbeg, Tmid, 2 * ind + 1);
  int max2 = getMax(Qbeg, Qend, Tmid + 1, Tend, 2 * ind + 2);

  return max(max1, max2);
}

void update(int pos, int val, int Tbeg = 0, int Tend = N - 1, int ind = 0) {
  if (Tbeg >= Tend) {
    if (Tbeg == pos - 1) {
      segTree[ind] = val;
    }
    return;
  }

  int Tmid = (Tbeg + Tend) >> 1;

  update(pos, val, Tbeg, Tmid, 2 * ind + 1);
  update(pos, val, Tmid + 1, Tend, 2 * ind + 2);

  segTree[ind] = max(segTree[2 * ind + 1], segTree[2 * ind + 2]);
}

void dfs(int node = 1, int par = 0) {
  int num = numbers[node];
  int val = getMax(0, num - 1) + 1;

  order[num].pb(val);
  update(num, order[num].back());
  lis[node] = getMax(1, N);

  for (auto connNode : tree[node]) {
    if (connNode != par) {
      dfs(connNode, node);
    }
  }

  order[num].pop_back();

  if (order[num].empty()) {
    update(num, 0);
  } else {
    update(num, order[num].back());
  }
}

int main() {
  scanf("%d", &N);
  vector<PII> sorted;

  FOR(i, 1, N) {
    scanf("%d", numbers + i);
    sorted.eb(numbers[i], i);
  }

  sort(ALL(sorted));

  int cnt = 1;
  numbers[sorted[0].sec] = cnt;

  REP(i, 1, N) {
    if (sorted[i].fir > sorted[i - 1].fir) {
      ++cnt;
    }

    numbers[sorted[i].sec] = cnt;
  }

  FOR(i, 1, N - 1) {
    int U, V;
    scanf("%d%d", &U, &V);

    tree[U].pb(V);
    tree[V].pb(U);
  }

  dfs();

  FOR(i, 1, N) { printf("%d\\n", lis[i]); }

  return 0;
}