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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map>
// #include <unordered_set>
#define lowbit(x) (x & (-x))
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 2e5 + 7;
int N, head[maxN], cnt, a[maxN], Lsan[maxN], _UP;
struct Eddge {
  int nex, to;
  Eddge(int a = -1, int b = 0) : nex(a), to(b) {}
} edge[maxN << 1];
inline void addEddge(int u, int v) {
  edge[cnt] = Eddge(head[u], v);
  head[u] = cnt++;
}
inline void _add(int u, int v) {
  addEddge(u, v);
  addEddge(v, u);
}
int ans[maxN], dp[maxN];
int tree[maxN << 2] = {0};
struct heap {
  priority_queue<int> Que, Del;
  inline bool empty() {
    while (!Que.empty() && !Del.empty() && Que.top() == Del.top()) {
      Que.pop();
      Del.pop();
    }
    return Que.empty();
  }
  inline void push(int val) { Que.push(val); }
  inline void clear(int val) { Del.push(val); }
  inline int size() { return (int)(Que.size() - Del.size()); }
  inline int top() {
    while (!Que.empty() && !Del.empty() && Que.top() == Del.top()) {
      Que.pop();
      Del.pop();
    }
    return Que.top();
  }
  inline void pop() {
    while (!Que.empty() && !Del.empty() && Que.top() == Del.top()) {
      Que.pop();
      Del.pop();
    }
    Que.pop();
  }
} st[maxN];
inline void pushup(int rt) { tree[rt] = max(tree[lsn], tree[rsn]); }
void Add(int rt, int l, int r, int qx, int val) {
  if (l == r) {
    st[l].push(val);
    tree[rt] = st[l].top();
    return;
  }
  int mid = HalF;
  if (qx <= mid)
    Add(Lson, qx, val);
  else
    Add(Rson, qx, val);
  pushup(rt);
}
void Del(int rt, int l, int r, int qx, int val) {
  if (l == r) {
    st[l].clear(val);
    if (st[l].empty())
      tree[rt] = 0;
    else
      tree[rt] = st[l].top();
    return;
  }
  int mid = HalF;
  if (qx <= mid)
    Del(Lson, qx, val);
  else
    Del(Rson, qx, val);
  pushup(rt);
}
int query(int rt, int l, int r, int ql, int qr) {
  if (ql <= l && qr >= r)
    return tree[rt];
  int mid = HalF;
  if (qr <= mid)
    return query(QL);
  else if (ql > mid)
    return query(QR);
  else
    return max(query(QL), query(QR));
}
void dfs(int u, int fa) {
  Add(1, 1, _UP, a[u], dp[u]);
  ans[u] = max(dp[u], ans[fa]);
  for (int i = head[u], v; ~i; i = edge[i].nex) {
    v = edge[i].to;
    if (v == fa)
      continue;
    dp[v] = ((a[v] - 1) > 0 ? query(1, 1, _UP, 1, a[v] - 1) : 0) + 1;
    dfs(v, u);
  }
  Del(1, 1, _UP, a[u], dp[u]);
}
inline void init() {
  cnt = 0;
  for (int i = 1; i <= N; i++)
    head[i] = -1;
}
int main() {
  scanf("%d", &N);
  init();
  for (int i = 1; i <= N; i++) {
    scanf("%d", &a[i]);
    Lsan[i] = a[i];
  }
  sort(Lsan + 1, Lsan + N + 1);
  _UP = (int)(unique(Lsan + 1, Lsan + N + 1) - Lsan - 1);
  for (int i = 1; i <= N; i++)
    a[i] = (int)(lower_bound(Lsan + 1, Lsan + _UP + 1, a[i]) - Lsan);
  for (int i = 1, u, v; i < N; i++) {
    scanf("%d%d", &u, &v);
    _add(u, v);
  }
  dp[1] = 1;
  dfs(1, 0);
  for (int i = 1; i <= N; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map>
// #include <unordered_set>
#define lowbit(x) (x & (-x))
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x;
}
const int maxN = 2e5 + 7;
int N, head[maxN], cnt, a[maxN], Lsan[maxN], _UP;
struct Eddge {
  int nex, to;
  Eddge(int a = -1, int b = 0) : nex(a), to(b) {}
} edge[maxN << 1];
inline void addEddge(int u, int v) {
  edge[cnt] = Eddge(head[u], v);
  head[u] = cnt++;
}
inline void _add(int u, int v) {
  addEddge(u, v);
  addEddge(v, u);
}
int ans[maxN];
int tree[maxN << 2] = {0};
struct heap {
  priority_queue<int> Que, Del;
  inline bool empty() {
    while (!Que.empty() && !Del.empty() && Que.top() == Del.top()) {
      Que.pop();
      Del.pop();
    }
    return Que.empty();
  }
  inline void push(int val) { Que.push(val); }
  inline void clear(int val) { Del.push(val); }
  inline int size() { return (int)(Que.size() - Del.size()); }
  inline int top() {
    while (!Que.empty() && !Del.empty() && Que.top() == Del.top()) {
      Que.pop();
      Del.pop();
    }
    return Que.top();
  }
  inline void pop() {
    while (!Que.empty() && !Del.empty() && Que.top() == Del.top()) {
      Que.pop();
      Del.pop();
    }
    Que.pop();
  }
} st[maxN];
inline void pushup(int rt) { tree[rt] = max(tree[lsn], tree[rsn]); }
void Add(int rt, int l, int r, int qx, int val) {
  if (l == r) {
    st[l].push(val);
    tree[rt] = st[l].top();
    return;
  }
  int mid = HalF;
  if (qx <= mid)
    Add(Lson, qx, val);
  else
    Add(Rson, qx, val);
  pushup(rt);
}
void Del(int rt, int l, int r, int qx, int val) {
  if (l == r) {
    st[l].clear(val);
    if (st[l].empty())
      tree[rt] = 0;
    else
      tree[rt] = st[l].top();
    return;
  }
  int mid = HalF;
  if (qx <= mid)
    Del(Lson, qx, val);
  else
    Del(Rson, qx, val);
  pushup(rt);
}
int query(int rt, int l, int r, int ql, int qr) {
  if (ql <= l && qr >= r)
    return tree[rt];
  int mid = HalF;
  if (qr <= mid)
    return query(QL);
  else if (ql > mid)
    return query(QR);
  else
    return max(query(QL), query(QR));
}
void dfs(int u, int fa) {
  Add(1, 1, _UP, a[u], ans[u]);
  for (int i = head[u], v; ~i; i = edge[i].nex) {
    v = edge[i].to;
    if (v == fa)
      continue;
    ans[v] = query(1, 1, _UP, 1, a[v] - 1) + 1;
    dfs(v, u);
  }
  Del(1, 1, _UP, a[u], ans[u]);
}
inline void init() {
  cnt = 0;
  for (int i = 1; i <= N; i++)
    head[i] = -1;
}
int main() {
  //    scanf("%d", &N);
  N = read();
  init();
  for (int i = 1; i <= N; i++) { /*scanf("%d", &a[i]);*/
    a[i] = read();
    Lsan[i] = a[i];
  }
  sort(Lsan + 1, Lsan + N + 1);
  _UP = (int)(unique(Lsan + 1, Lsan + N + 1) - Lsan - 1);
  for (int i = 1; i <= N; i++)
    a[i] = (int)(lower_bound(Lsan + 1, Lsan + _UP + 1, a[i]) - Lsan);
  for (int i = 1, u, v; i < N; i++) {
    //        scanf("%d%d", &u, &v);
    u = read();
    v = read();
    _add(u, v);
  }
  ans[1] = 1;
  dfs(1, 0);
  for (int i = 1; i <= N; i++)
    printf("%d\\n", ans[i]);
  return 0;
}