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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int N;
int A[200001];
int E[400001], ne[400001], he[200001];
int V[200001], v;
void dfs(int p, int mae) {
  if (V[v - 1] < A[p]) {
    V[v++] = A[p];
    for (auto to = he[p]; to; to = ne[to]) {
      if (E[to] != mae) {
        dfs(E[to], p);
      }
    }
    he[p] = v--;
  } else {
    int itr = lower_bound(V, V + v, A[p]) - V;
    swap(V[itr], A[p]);
    for (auto to = he[p]; to; to = ne[to]) {
      if (E[to] != mae) {
        dfs(E[to], p);
      }
    }
    he[p] = v;
    V[itr] = A[p];
  }
}

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], *ci = cn + CM + CL, *owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
  if (ci - owa > 0) {
    memcpy(cn, owa, CL);
    ci -= CM;
    fread(cn + CL, 1, CM, stdin);
  }
  ll tmp = *(ll *)ci;
  int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
  tmp = tmp << dig & ma1;
  tmp = tmp * 10 + (tmp >> 8) & ma2;
  tmp = tmp * 100 + (tmp >> 16) & ma3;
  tmp = tmp * 10000 + (tmp >> 32) & ma4;
  ci += 72 - dig >> 3;
  return tmp;
}
inline int getintA() {
  if (ci - owa > 0) {
    memcpy(cn, owa, CL);
    ci -= CM;
    fread(cn + CL, 1, CM, stdin);
  }
  ll tmp = *(ll *)ci;
  int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
  tmp = tmp << dig & ma1;
  tmp = tmp * 10 + (tmp >> 8) & ma2;
  tmp = tmp * 100 + (tmp >> 16) & ma3;
  tmp = tmp * 10000 + (tmp >> 32) & ma4;
  ci += (64 - dig >> 3);
  while ((ct = *ci++) >= '0')
    tmp = tmp * 10 + ct - '0';
  return tmp;
}
char dn[1300000], *di = dn;
inline void putint(int X) {
  int keta = 0;
  char C[10];
  while (X) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--)
    *di++ = (*(C + i));
  *di++ = '\\n';
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  N = getint();
  rep1(i, N) A[i] = getintA();
  int k = 1;
  rep(i, N - 1) {
    int u = getint(), v = getint();
    E[k] = v;
    ne[k] = he[u];
    he[u] = k++;
    E[k] = u;
    ne[k] = he[v];
    he[v] = k++;
  }

  V[v++] = 2000000000;
  dfs(1, -1);

  rep1(i, N) putint(he[i]);
  fwrite(dn, 1, di - dn, stdout);

  Would you please return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize(3)
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n;
int a[200005];
int ans[200005];
int fa[200005];
vector<int> g[200005];
vector<int> tre[200005];
int top = 0;
priority_queue<int, vector<int>, greater<int>> q;
vector<int> s;
bool vis[200005];
void read(int &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while ('0' > c || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  x *= f;
}
void write(int x) {
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
void dfs(int u, int fat) {
  while (!q.empty()) {
    int t = q.top();
    if (t < u) {
      tre[t].push_back(u);
      fa[u] = t;
      q.pop();
    } else
      break;
  }
  priority_queue<int, vector<int>, greater<int>> t = q;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v != fat) {
      dfs(v, u);
      q = t;
    }
  }
}
void lis(int u, int fa) {
  vector<int> f = s;
  vis[u] = 1;
  if (fa == 0) {
    s.push_back(a[u]);
    ans[u] = 1;
  } else {
    if (a[u] > s[s.size() - 1]) {
      ans[u] = s.size() + 1;
      s.push_back(a[u]);
    } else {
      ans[u] = s.size();
      int t = lower_bound(s.begin(), s.end(), a[u]) - s.begin();
      s[t] = a[u];
    }
  }
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v != fa) {
      lis(v, u);
    }
  }
  s = f;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1, u, v; i < n; i++) {
    read(u);
    read(v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      lis(i, 0);
    }
  }
  for (int i = 1; i <= n; i++) {
    write(ans[i]);
    putchar('\\n');
  }
  return 0;
}