Problem p01564 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid, int)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

  int sz;
  vector<Monoid> data;
  vector<OperatorMonoid> lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;

  LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
                  const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) { data[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  void propagate(int k, int len) {
    if (lazy[k] != OM0) {
      if (k < sz) {
        lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      }
      data[k] = g(data[k], lazy[k], len);
      lazy[k] = OM0;
    }
  }

  Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
    propagate(k, r - l);
    if (r <= a || b <= l) {
      return data[k];
    } else if (a <= l && r <= b) {
      lazy[k] = h(lazy[k], x);
      propagate(k, r - l);
      return data[k];
    } else {
      return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
                         update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
    }
  }

  Monoid update(int a, int b, const OperatorMonoid &x) {
    return update(a, b, x, 1, 0, sz);
  }

  Monoid query(int a, int b, int k, int l, int r) {
    propagate(k, r - l);
    if (r <= a || b <= l) {
      return M1;
    } else if (a <= l && r <= b) {
      return data[k];
    } else {
      return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
               query(a, b, 2 * k + 1, (l + r) >> 1, r));
    }
  }

  Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }

  Monoid operator[](const int &k) { return query(k, k + 1); }
};

template <typename G> struct CentroidPathDecomposition {
  struct Centroid {
    int ParIndex, ParDepth, Deep;
    vector<int> node;

    Centroid(int idx, int dep, int deep)
        : ParIndex(idx), ParDepth(dep), Deep(deep) {}

    inline size_t size() { return (node.size()); }

    inline int &operator[](int k) { return (node[k]); }

    inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }
  };

  const G &graph;
  vector<int> SubTreeSize, NextPath;
  vector<int> TreeIndex, TreeDepth;
  vector<Centroid> Centroids;

  void BuildSubTreeSize() {
    stack<pair<int, int>> s;
    s.emplace(0, -1);
    while (!s.empty()) {
      auto p = s.top();
      s.pop();
      if (~SubTreeSize[p.first]) {
        NextPath[p.first] = -1;
        for (auto &to : graph[p.first]) {
          if (p.second == to)
            continue;
          SubTreeSize[p.first] += SubTreeSize[to];
          if (NextPath[p.first] == -1 ||
              SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
            NextPath[p.first] = to;
          }
        }
      } else {
        s.push(p);
        SubTreeSize[p.first] = 1;
        for (auto &to : graph[p.first]) {
          if (p.second != to)
            s.emplace(to, p.first);
        }
      }
    }
  }

  void BuildPath() {
    stack<pair<int, int>> s;
    Centroids.emplace_back(-1, -1, 0);
    s.emplace(0, -1);
    TreeIndex[0] = 0;
    while (!s.empty()) {
      auto p = s.top();
      s.pop();
      TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();
      for (auto &to : graph[p.first]) {
        if (p.second == to)
          continue;
        if (to == NextPath[p.first]) { // Centroid-Path
          TreeIndex[to] = TreeIndex[p.first];
        } else { // Not Centroid-Path
          TreeIndex[to] = (int)Centroids.size();
          Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],
                                 Centroids[TreeIndex[p.first]].Deep + 1);
        }
        s.emplace(to, p.first);
      }
      Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
    }
  }

  virtual void Build() {
    BuildSubTreeSize();
    BuildPath();
  }

  inline size_t size() { return (Centroids.size()); }

  inline pair<int, int> Information(int idx) {
    return (make_pair(TreeIndex[idx], TreeDepth[idx]));
  }

  inline Centroid &operator[](int k) { return (Centroids[k]); }

  inline int LCA(int a, int b) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while (TreeIdxA != TreeIdxB) {
      if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB);
    return (Centroids[TreeIdxA][TreeDepthA]);
  }

  inline virtual void query(int a, int b,
                            const function<void(int, int, int)> &f) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while (TreeIdxA != TreeIdxB) {
      if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        f(TreeIdxA, 0, TreeDepthA + 1);
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        f(TreeIdxB, 0, TreeDepthB + 1);
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB);
    f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
  }

  CentroidPathDecomposition(const G &g) : graph(g) {
    int SZ = g.size();
    SubTreeSize.assign(SZ, -1);
    NextPath.resize(SZ);
    TreeIndex.resize(SZ);
    TreeDepth.resize(SZ);
  }
};

template <typename G> struct TreeArray : CentroidPathDecomposition<G> {
  using CPD = CentroidPathDecomposition<G>;

  TreeArray(const G &g) : CPD(g) {}

  vector<int> index;

  void Build() {
    CPD::Build();
    int ptr = 0;
    for (auto &centroid : CPD::Centroids) {
      index.emplace_back(ptr);
      ptr += centroid.size();
    }
  }

  inline int get(int a) {
    auto p = CPD::Information(a);
    return (index[p.first] + p.second);
  }

  inline void query(int a, int b, const function<void(int, int)> &f) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
    tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
    while (TreeIdxA != TreeIdxB) {
      if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
        f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
        tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
      } else {
        f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
        tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
      }
    }
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB);
    f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
  }

  inline vector<tuple<int, int, bool>> get_path(int a, int b) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
    tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
    vector<tuple<int, int, bool>> vs, ws;
    while (TreeIdxA != TreeIdxB) {
      if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
        vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1,
                        true);
        tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
      } else {
        ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1,
                        false);
        tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
      }
    }
    bool rev = true;
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB), rev = false;
    vs.emplace_back(index[TreeIdxA] + TreeDepthA,
                    index[TreeIdxA] + TreeDepthB + 1, !rev);
    reverse(begin(ws), end(ws));
    copy(begin(ws), end(ws), back_inserter(vs));
    return (vs);
  }
};

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

using int64 = long long;
const int64 INF = 1LL << 60;
struct node {
  int64 left, right, ans, all;
};

int main() {
  int N, Q, S[100000];
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &S[i]);
  }
  UnWeightedGraph g(N);
  for (int i = 1; i < N; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x, --y;
    g[x].emplace_back(y);
    g[y].emplace_back(x);
  }
  TreeArray<UnWeightedGraph> cpd(g);
  cpd.Build();

  node np = (node){-INF, -INF, -INF, 0};
  auto f = [](const node &x, const node &y) {
    node z;
    z.all = x.all + y.all;
    z.left = max(x.left, x.all + y.left);
    z.right = max(y.right, y.all + x.right);
    z.ans = max(x.right + y.left, max(x.ans, y.ans));
    return z;
  };

  auto gg = [](const node &x, int64 y, int len) {
    node z;
    z.all = y * len;
    z.ans = z.left = z.right = max(y, z.all);
    return z;
  };

  auto h = [&](int64 x, int64 y) { return y; };

  LazySegmentTree<node, int64> seg(N, f, gg, h, np, -INF);
  for (int i = 0; i < N; i++) {
    seg.set(cpd.get(i), (node){S[i], S[i], S[i], S[i]});
  }
  seg.build();

  for (int i = 0; i < Q; i++) {
    int t, x, y, z;
    scanf("%d %d %d %d", &t, &x, &y, &z);
    --x, --y;
    if (t == 1) {
      cpd.query(x, y, [&](int a, int b) { seg.update(a, b, z); });
    } else {
      auto l = np, r = np;
      for (auto &p : cpd.get_path(x, y)) {
        int a, b;
        bool c;
        tie(a, b, c) = p;
        if (c)
          r = f(seg.query(a, b), r);
        else
          l = f(l, seg.query(a, b));
      }
      swap(l.left, l.right);
      printf("%lld\n", f(l, r).ans);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid, int)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

  int sz;
  vector<Monoid> data;
  vector<OperatorMonoid> lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;

  LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
                  const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) { data[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  void propagate(int k, int len) {
    if (lazy[k] != OM0) {
      if (k < sz) {
        lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      }
      data[k] = g(data[k], lazy[k], len);
      lazy[k] = OM0;
    }
  }

  Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
    propagate(k, r - l);
    if (r <= a || b <= l) {
      return data[k];
    } else if (a <= l && r <= b) {
      lazy[k] = h(lazy[k], x);
      propagate(k, r - l);
      return data[k];
    } else {
      return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
                         update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
    }
  }

  Monoid update(int a, int b, const OperatorMonoid &x) {
    return update(a, b, x, 1, 0, sz);
  }

  Monoid query(int a, int b, int k, int l, int r) {
    propagate(k, r - l);
    if (r <= a || b <= l) {
      return M1;
    } else if (a <= l && r <= b) {
      return data[k];
    } else {
      return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
               query(a, b, 2 * k + 1, (l + r) >> 1, r));
    }
  }

  Monoid query(int a, int b) { return query(a, b, 1, 0, sz); }

  Monoid operator[](const int &k) { return query(k, k + 1); }
};

template <typename G> struct CentroidPathDecomposition {
  struct Centroid {
    int ParIndex, ParDepth, Deep;
    vector<int> node;

    Centroid(int idx, int dep, int deep)
        : ParIndex(idx), ParDepth(dep), Deep(deep) {}

    inline size_t size() { return (node.size()); }

    inline int &operator[](int k) { return (node[k]); }

    inline pair<int, int> Up() { return (make_pair(ParIndex, ParDepth)); }
  };

  const G &graph;
  vector<int> SubTreeSize, NextPath;
  vector<int> TreeIndex, TreeDepth;
  vector<Centroid> Centroids;

  void BuildSubTreeSize() {
    stack<pair<int, int>> s;
    s.emplace(0, -1);
    while (!s.empty()) {
      auto p = s.top();
      s.pop();
      if (~SubTreeSize[p.first]) {
        NextPath[p.first] = -1;
        for (auto &to : graph[p.first]) {
          if (p.second == to)
            continue;
          SubTreeSize[p.first] += SubTreeSize[to];
          if (NextPath[p.first] == -1 ||
              SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
            NextPath[p.first] = to;
          }
        }
      } else {
        s.push(p);
        SubTreeSize[p.first] = 1;
        for (auto &to : graph[p.first]) {
          if (p.second != to)
            s.emplace(to, p.first);
        }
      }
    }
  }

  void BuildPath() {
    stack<pair<int, int>> s;
    Centroids.emplace_back(-1, -1, 0);
    s.emplace(0, -1);
    TreeIndex[0] = 0;
    while (!s.empty()) {
      auto p = s.top();
      s.pop();
      TreeDepth[p.first] = (int)Centroids[TreeIndex[p.first]].size();
      for (auto &to : graph[p.first]) {
        if (p.second == to)
          continue;
        if (to == NextPath[p.first]) { // Centroid-Path
          TreeIndex[to] = TreeIndex[p.first];
        } else { // Not Centroid-Path
          TreeIndex[to] = (int)Centroids.size();
          Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first],
                                 Centroids[TreeIndex[p.first]].Deep + 1);
        }
        s.emplace(to, p.first);
      }
      Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
    }
  }

  virtual void Build() {
    BuildSubTreeSize();
    BuildPath();
  }

  inline size_t size() { return (Centroids.size()); }

  inline pair<int, int> Information(int idx) {
    return (make_pair(TreeIndex[idx], TreeDepth[idx]));
  }

  inline Centroid &operator[](int k) { return (Centroids[k]); }

  inline int LCA(int a, int b) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while (TreeIdxA != TreeIdxB) {
      if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB);
    return (Centroids[TreeIdxA][TreeDepthA]);
  }

  inline virtual void query(int a, int b,
                            const function<void(int, int, int)> &f) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = Information(a);
    tie(TreeIdxB, TreeDepthB) = Information(b);
    while (TreeIdxA != TreeIdxB) {
      if (Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
        f(TreeIdxA, 0, TreeDepthA + 1);
        tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
      } else {
        f(TreeIdxB, 0, TreeDepthB + 1);
        tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
      }
    }
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB);
    f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
  }

  CentroidPathDecomposition(const G &g) : graph(g) {
    int SZ = g.size();
    SubTreeSize.assign(SZ, -1);
    NextPath.resize(SZ);
    TreeIndex.resize(SZ);
    TreeDepth.resize(SZ);
  }
};

template <typename G> struct TreeArray : CentroidPathDecomposition<G> {
  using CPD = CentroidPathDecomposition<G>;

  TreeArray(const G &g) : CPD(g) {}

  vector<int> index;

  void Build() {
    CPD::Build();
    int ptr = 0;
    for (auto &centroid : CPD::Centroids) {
      index.emplace_back(ptr);
      ptr += centroid.size();
    }
  }

  inline int get(int a) {
    auto p = CPD::Information(a);
    return (index[p.first] + p.second);
  }

  inline void query(int a, int b, const function<void(int, int)> &f) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
    tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
    while (TreeIdxA != TreeIdxB) {
      if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
        f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
        tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
      } else {
        f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
        tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
      }
    }
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB);
    f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
  }

  inline vector<tuple<int, int, bool>> get_path(int a, int b) {
    int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
    tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
    tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
    vector<tuple<int, int, bool>> vs, ws;
    while (TreeIdxA != TreeIdxB) {
      if (CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
        vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1,
                        true);
        tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
      } else {
        ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1,
                        false);
        tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
      }
    }
    bool rev = true;
    if (TreeDepthA > TreeDepthB)
      swap(TreeDepthA, TreeDepthB), rev = false;
    vs.emplace_back(index[TreeIdxA] + TreeDepthA,
                    index[TreeIdxA] + TreeDepthB + 1, !rev);
    reverse(begin(ws), end(ws));
    copy(begin(ws), end(ws), back_inserter(vs));
    return (vs);
  }
};

template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

using int64 = long long;
const int64 INF = 1LL << 60;
struct node {
  int64 left, right, ans, all;
};

int main() {
  int N, Q, S[200000];
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < N; i++) {
    scanf("%d", &S[i]);
  }
  UnWeightedGraph g(N);
  for (int i = 1; i < N; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    --x, --y;
    g[x].emplace_back(y);
    g[y].emplace_back(x);
  }
  TreeArray<UnWeightedGraph> cpd(g);
  cpd.Build();

  node np = (node){-INF, -INF, -INF, 0};
  auto f = [](const node &x, const node &y) {
    node z;
    z.all = x.all + y.all;
    z.left = max(x.left, x.all + y.left);
    z.right = max(y.right, y.all + x.right);
    z.ans = max(x.right + y.left, max(x.ans, y.ans));
    return z;
  };

  auto gg = [](const node &x, int64 y, int len) {
    node z;
    z.all = y * len;
    z.ans = z.left = z.right = max(y, z.all);
    return z;
  };

  auto h = [&](int64 x, int64 y) { return y; };

  LazySegmentTree<node, int64> seg(N, f, gg, h, np, -INF);
  for (int i = 0; i < N; i++) {
    seg.set(cpd.get(i), (node){S[i], S[i], S[i], S[i]});
  }
  seg.build();

  for (int i = 0; i < Q; i++) {
    int t, x, y, z;
    scanf("%d %d %d %d", &t, &x, &y, &z);
    --x, --y;
    if (t == 1) {
      cpd.query(x, y, [&](int a, int b) { seg.update(a, b, z); });
    } else {
      auto l = np, r = np;
      for (auto &p : cpd.get_path(x, y)) {
        int a, b;
        bool c;
        tie(a, b, c) = p;
        if (c)
          r = f(seg.query(a, b), r);
        else
          l = f(l, seg.query(a, b));
      }
      swap(l.left, l.right);
      printf("%lld\n", f(l, r).ans);
    }
  }
}