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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) {
  if (x > y)
    x = y;
}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) {
  if (x < y)
    x = y;
}

const int MAXN = 2e5 + 10;
int n;
vector<int> a;
vector<int> g[MAXN];

const int MAXMEM = 5e7 + 10;

struct node {
  int l, r;
  int val;
  node() { l = -1, r = -1, val = 0; }
};

node tree[MAXMEM];
int topQ = 0;

int clone(int root) {
  tree[topQ] = tree[root];
  topQ++;
  assert(topQ < MAXMEM);
  return topQ - 1;
}

int build(int l, int r) {
  if (l == r - 1) {
    tree[topQ] = node();
    topQ++;
    // cout << l << " " << r << " " << topQ << endl;
    return topQ - 1;
  }
  tree[topQ] = node();
  int root = topQ;
  topQ++;
  int m = (l + r) / 2;
  tree[root].l = build(l, m);
  tree[root].r = build(m, r);
  // cout << l << " " << r << " " << root << " " << tree[root].l << " " <<
  // tree[root].r << endl;
  return root;
}

int upd(int root, int tl, int tr, int pos, int val) {
  if (tl >= pos + 1 || tr <= pos)
    return root;
  if (tl == tr - 1) {
    root = clone(root);
    chkmax(tree[root].val, val);
    return root;
  }
  int tm = (tl + tr) / 2;
  root = clone(root);
  tree[root].l = upd(tree[root].l, tl, tm, pos, val);
  tree[root].r = upd(tree[root].r, tm, tr, pos, val);
  tree[root].val = max(tree[tree[root].l].val, tree[tree[root].r].val);
  return root;
}

int upd(int root, int pos, int val) { return upd(root, 0, n, pos, val); }

int get(int root, int tl, int tr, int l, int r) {
  assert(root != -1);
  // cout << root << " " << tl << " " << tr << " " << l << " " << r << endl;
  if (tl >= r || tr <= l)
    return 0;
  if (tl >= l && tr <= r)
    return tree[root].val;
  int tm = (tl + tr) / 2;
  return max(get(tree[root].l, tl, tm, l, r), get(tree[root].r, tm, tr, l, r));
}

int get(int root, int l, int r) { return get(root, 0, n, l, r + 1); }

void read() {
  cin >> n;
  a.resize(n);
  for (auto &i : a) {
    cin >> i;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
}

void make() {
  auto have = a;
  sort(all(have));
  have.resize(unique(all(have)) - have.begin());
  for (auto &i : a) {
    i = lower_bound(all(have), i) - have.begin();
  }
}

vector<int> ans;

void dfs(int v, int p, int root) {
  // cout << v << " " << p << " " << root << endl;
  int have = get(root, 0, a[v] - 1);
  // cout << "puhh" << endl;
  ans[v] = max(get(root, 0, n - 1), have + 1);
  root = upd(root, a[v], have + 1);
  for (auto i : g[v]) {
    if (i == p)
      continue;
    dfs(i, v, root);
  }
}

void solve() {
  ans.resize(n);
  int root = build(0, n);
  /*cout << "build = " << endl;
  for (int i = 0; i < topQ; i++) {
      cout << tree[i].l << " " << tree[i].r << endl;
  }
  cout << endl;*/
  dfs(0, 0, root);
}

void run() {
  make();
  solve();
}

void write() {
  for (auto i : ans) {
    cout << i << "\\n";
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read();
  run();
  write();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) {
  if (x > y)
    x = y;
}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) {
  if (x < y)
    x = y;
}

const int MAXN = 2e5 + 10;
int n;
vector<int> a;
vector<int> g[MAXN];

const int MAXMEM = 1e8 + 10;

struct node {
  int l, r;
  int val;
  node() { l = -1, r = -1, val = 0; }
};

node tree[MAXMEM];
int topQ = 0;

int clone(int root) {
  tree[topQ] = tree[root];
  topQ++;
  assert(topQ < MAXMEM);
  return topQ - 1;
}

int build(int l, int r) {
  if (l == r - 1) {
    tree[topQ] = node();
    topQ++;
    // cout << l << " " << r << " " << topQ << endl;
    return topQ - 1;
  }
  tree[topQ] = node();
  int root = topQ;
  topQ++;
  int m = (l + r) / 2;
  tree[root].l = build(l, m);
  tree[root].r = build(m, r);
  // cout << l << " " << r << " " << root << " " << tree[root].l << " " <<
  // tree[root].r << endl;
  return root;
}

int upd(int root, int tl, int tr, int pos, int val) {
  if (tl >= pos + 1 || tr <= pos)
    return root;
  if (tl == tr - 1) {
    root = clone(root);
    chkmax(tree[root].val, val);
    return root;
  }
  int tm = (tl + tr) / 2;
  root = clone(root);
  tree[root].l = upd(tree[root].l, tl, tm, pos, val);
  tree[root].r = upd(tree[root].r, tm, tr, pos, val);
  tree[root].val = max(tree[tree[root].l].val, tree[tree[root].r].val);
  return root;
}

int upd(int root, int pos, int val) { return upd(root, 0, n, pos, val); }

int get(int root, int tl, int tr, int l, int r) {
  assert(root != -1);
  // cout << root << " " << tl << " " << tr << " " << l << " " << r << endl;
  if (tl >= r || tr <= l)
    return 0;
  if (tl >= l && tr <= r)
    return tree[root].val;
  int tm = (tl + tr) / 2;
  return max(get(tree[root].l, tl, tm, l, r), get(tree[root].r, tm, tr, l, r));
}

int get(int root, int l, int r) { return get(root, 0, n, l, r + 1); }

void read() {
  cin >> n;
  a.resize(n);
  for (auto &i : a) {
    cin >> i;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
}

void make() {
  auto have = a;
  sort(all(have));
  for (auto &i : a) {
    i = lower_bound(all(have), i) - have.begin();
  }
}

vector<int> ans;

void dfs(int v, int p, int root) {
  // cout << v << " " << p << " " << root << endl;
  int have = get(root, 0, a[v] - 1);
  // cout << "puhh" << endl;
  ans[v] = have + 1;
  root = upd(root, a[v], have + 1);
  for (auto i : g[v]) {
    if (i == p)
      continue;
    dfs(i, v, root);
  }
}

void solve() {
  ans.resize(n);
  int root = build(0, n);
  /*cout << "build = " << endl;
  for (int i = 0; i < topQ; i++) {
      cout << tree[i].l << " " << tree[i].r << endl;
  }
  cout << endl;*/
  dfs(0, 0, root);
}

void run() {
  make();
  solve();
}

void write() {
  for (auto i : ans) {
    cout << i << "\\n";
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read();
  run();
  write();
  return 0;
}