Problem p02698 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define _sz(v) (int)(v).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

const int N = (int)2e5 + 5;

struct SegmentTree {
  struct Node {
    Node *l, *r;
    int sz;
    int mx;

    Node() : sz(0), mx(0){};

    Node(int _sz) : sz(_sz), mx(0) {
      if (sz != 1) {
        l = new Node(sz / 2);
        r = new Node(sz - sz / 2);
      }
    }

    void recalc() {
      if (sz != 1)
        mx = max(l->mx, r->mx);
    }

    void modify(int pos, int val) {
      if (sz == 1) {
        mx = val;
      } else {
        if (pos < sz / 2)
          l->modify(pos, val);
        else
          r->modify(pos - sz / 2, val);
        recalc();
      }
    }
    int get(int i) {
      if (i >= sz)
        return mx;
      if (i <= 0)
        return 0;
      return max(l->get(i), r->get(i - sz / 2));
    }
  };

  int n;
  Node *root;
  vector<int> memory[N];

  SegmentTree(){};
  SegmentTree(int _n) : n(_n) {
    root = new Node(n);
    for (int i = 0; i < n; ++i)
      memory[i] = {0};
  }

  int get(int r) { return root->get(r); }
  void modify(int pos, int val) {
    memory[pos].push_back(val);
    root->modify(pos, val);
  }
  void rollback(int pos) {
    memory[pos].pop_back();
    root->modify(pos, memory[pos].back());
  }
};

int a[N];
vector<int> g[N];
SegmentTree tree;
int ans[N];

void dfs(int v, int p) {
  int val = 1 + tree.get(a[v]);
  ans[v] = max(ans[v], val);
  tree.modify(a[v], val);
  for (int u : g[v]) {
    if (u != p) {
      ans[u] = ans[v];
      dfs(u, v);
    }
  }
  tree.rollback(a[v]);
}

int main() {
  // freopen("input.txt", "r", stdin);

  int n;
  scanf("%d", &n);

  vector<int> xs;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    xs.push_back(a[i]);
  }
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());
  for (int i = 0; i < n; ++i)
    a[i] = lower_bound(all(xs), a[i]) - xs.begin();

  /*
  for (int i = 0; i < n; ++i)
      printf("%d ", a[i]);
  printf("\\n");
  */

  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  tree = SegmentTree(n);

  dfs(0, -1);

  for (int i = 0; i < n; ++i) {
    printf("%d\\n", ans[i]);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define _sz(v) (int)(v).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

const int N = (int)105 + 5;

struct SegmentTree {
  struct Node {
    Node *l, *r;
    int sz;
    int mx;

    Node() : sz(0), mx(0){};

    Node(int _sz) : sz(_sz), mx(0) {
      if (sz != 1) {
        l = new Node(sz / 2);
        r = new Node(sz - sz / 2);
      }
    }

    void recalc() {
      if (sz != 1)
        mx = max(l->mx, r->mx);
    }

    void modify(int pos, int val) {
      if (sz == 1) {
        mx = val;
      } else {
        if (pos < sz / 2)
          l->modify(pos, val);
        else
          r->modify(pos - sz / 2, val);
        recalc();
      }
    }
    int get(int i) {
      if (i >= sz)
        return mx;
      if (i <= 0)
        return 0;
      return max(l->get(i), r->get(i - sz / 2));
    }
  };

  int n;
  Node *root;
  vector<int> st[N];

  SegmentTree(){};
  SegmentTree(int _n) : n(_n) {
    root = new Node(n);
    for (int i = 0; i < n; ++i)
      st[i] = {0};
  }

  int get(int r) { return root->get(r); }
  void modify(int pos, int val) {
    st[pos].push_back(val);
    root->modify(pos, val);
  }
  void rollback(int pos) {
    st[pos].pop_back();
    root->modify(pos, st[pos].back());
  }
};

int a[N];
vector<int> g[N];
SegmentTree tree;
int ans[N];

void dfs(int v, int p) {
  // int val;
  int val = 1 + tree.get(a[v]);
  tree.modify(a[v], val);
  ans[v] = max(ans[v], val);
  // printf("%d %d %d\\n", v + 1, p + 1, ans[v]);
  for (int u : g[v]) {
    if (u != p) {
      ans[u] = ans[v];
      dfs(u, v);
    }
  }
  tree.rollback(v);
}

int main() {
  freopen("input.txt", "r", stdin);

  int n;
  scanf("%d", &n);

  vector<int> xs;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    xs.push_back(a[i]);
  }
  sort(all(xs));
  xs.erase(unique(all(xs)), xs.end());
  for (int i = 0; i < n; ++i)
    a[i] = lower_bound(all(xs), a[i]) - xs.begin();

  /*
  for (int i = 0; i < n; ++i)
      printf("%d ", a[i]);
  printf("\\n");
  */

  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  tree = SegmentTree(n);

  dfs(0, -1);

  for (int i = 0; i < n; ++i) {
    printf("%d\\n", ans[i]);
  }
}