lmori's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub lmorinn/library

:heavy_check_mark: verify/AizuOnlineJudge/graph/dynamic-tree/AOJ_2450.test.cpp

Depends on

Code

#include "../../../../template/template.hpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2450"
#include "../../../../graph/dynamic-tree/LinkCutTreeVertex.hpp"

struct S {
    lint val;
    lint siz;
    lint lmx;
    lint rmx;
    lint mx;
};

using T = lint;
using F = lint;
F ID = -1e17;

S op(S a, S b) {
    if (a.lmx == ID) return b;
    if (b.lmx == ID) return a;
    S ret;
    ret.val = a.val + b.val;
    ret.siz = a.siz + b.siz;
    ret.lmx = max(a.val + b.lmx, a.lmx);
    ret.rmx = max(b.val + a.rmx, b.rmx);
    ret.mx = max({a.mx, b.mx, a.rmx + b.lmx, ret.rmx, ret.lmx});
    return ret;
}

S e() {
    return {0, 0, ID, ID, ID};
}

S mapping(F f, S x) {
    if (f != ID) {
        x.val = f * x.siz;
        x.mx = x.lmx = x.rmx = max(f, f * x.siz);
    }
    return x;
}

F composition(F f, F g) {
    return (f == ID ? g : f);
}

F id() {
    return ID;
}

void reverseprod(S &node) {
    swap(node.lmx, node.rmx);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, q;
    in(n, q);
    vector<S> nodew(n);
    rep(i, n) {
        lint c;
        in(c);
        nodew[i] = {c, 1, c, c, c};
    }
    LinkCutTree<S, op, e, F, mapping, composition, id, reverseprod> t(nodew);

    rep(i, n - 1) {
        int u, v;
        in(u, v);
        u--;
        v--;
        t.add(u, v);
    }

    rep(i, q) {
        lint c;
        int com, u, v;
        in(com, u, v, c);
        u--;
        v--;
        if (com == 1) {
            t.apply(u, v, c);
        } else {
            out(t.prod(u, v).mx);
        }
    }
}
#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
  cout << fixed << setprecision(14) << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion
#line 2 "verify/AizuOnlineJudge/graph/dynamic-tree/AOJ_2450.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2450"
#line 1 "graph/dynamic-tree/LinkCutTreeVertex.hpp"
// ReverseProd: 反転の処理
// 入力時にb2の入力を忘れない
// 一次関数の合成なら,b1とb2をswap
template <class S, auto op, auto e, class F, auto mapping, auto composition, auto id, auto reverseprod>
struct LinkCutTree {
   private:
    struct Node {
        Node *l = 0;
        Node *r = 0;
        Node *p = 0;
        Node *pp = 0;
        // 値、集約値、作用値
        S val = e();
        S acc = e();
        F lazy = id();
        int idx = -1;
        int z = 0;
        int sumz = 0;
        bool rev = 0;
    };

    using pNode = shared_ptr<Node>;
    pNode pNIL;
    Node *NIL = nullptr;
    vector<pNode> A;

    void push(Node *v) {
        if (v->l != NIL) {
            v->l->val = mapping(v->lazy, v->l->val);
            v->l->acc = mapping(v->lazy, v->l->acc);
            v->l->lazy = composition(v->lazy, v->l->lazy);
        }
        if (v->r != NIL) {
            v->r->val = mapping(v->lazy, v->r->val);
            v->r->acc = mapping(v->lazy, v->r->acc);
            v->r->lazy = composition(v->lazy, v->r->lazy);
        }
        if (v->rev) {
            swap(v->l, v->r);
            if (v->l != NIL) {
                v->l->rev ^= 1;
                reverseprod(v->l->acc);
            }
            if (v->r != NIL) {
                v->r->rev ^= 1;
                reverseprod(v->r->acc);
            }
            v->rev = 0;
        }
        v->lazy = id();
    }

    void update(Node *v) {
        v->sumz = v->l->sumz + v->r->sumz + 1;
        v->acc = op(op(v->l->acc, v->val), v->r->acc);
    }

    Node *&parentchild(Node *v) {
        if (v->p == NIL) return NIL;
        if (v->p->l == v) {
            return v->p->l;
        } else {
            return v->p->r;
        }
    }

    Node *at(int idx) {
        return A[idx].get();
    }

    void rotL(Node *v) {
        Node *p = v->p;
        if (p->p == NIL) {
            swap(p->pp, v->pp);
        } else {
            parentchild(p) = v;
        }
        v->p = p->p;
        p->p = v;
        if (v->l != NIL) v->l->p = p;
        p->r = v->l;
        v->l = p;
    }

    void rotR(Node *v) {
        Node *p = v->p;
        if (p->p == NIL) {
            swap(p->pp, v->pp);
        } else {
            parentchild(p) = v;
        }
        v->p = p->p;
        p->p = v;
        if (v->r != NIL) v->r->p = p;
        p->l = v->r;
        v->r = p;
    }

    void splay(Node *v) {
        push(v);
        while (v->p != NIL) {
            Node *p = v->p;
            Node *pp = p->p;
            if (pp != NIL) push(pp);
            push(p);
            push(v);

            // zig zag
            if (p->l == v) {
                if (pp == NIL) {
                    rotR(v);
                } else if (pp->l == p) {
                    rotR(p);
                    rotR(v);
                } else if (pp->r == p) {
                    rotR(v);
                    rotL(v);
                }
            } else {
                if (pp == NIL) {
                    rotL(v);
                } else if (pp->r == p) {
                    rotL(p);
                    rotL(v);
                } else if (pp->l == p) {
                    rotL(v);
                    rotR(v);
                }
            }

            if (pp != NIL) update(pp);
            update(p);
        }
        update(v);
    }

    void expose(Node *v) {
        auto p = v;
        while (p != NIL) {
            splay(p);
            p = p->pp;
        }
        p = v;
        while (p->pp != NIL) {
            auto prev = p->pp->r;
            if (prev != NIL) swap(prev->pp, prev->p);
            swap(p->p, p->pp);
            p->p->r = p;
            p = p->p;
        }
        splay(v);
    }

    void evert(Node *v) {
        expose(v);
        if (v->r != NIL) {
            v->r->pp = v->r->p;
            v->r->p = NIL;
            v->r = NIL;
        }
        v->rev ^= 1;
        reverseprod(v->acc);
        push(v);
    }

    void link(Node *u, Node *v) {
        evert(u);
        evert(v);
        if (u->p != NIL or u->pp != NIL) return;
        u->pp = v;
    }

    void cut(Node *v) {
        expose(v);
        if (v->l == NIL) return;
        v->l->p = NIL;
        v->l = NIL;
    }

    Node *between(Node *u, Node *v) {
        evert(u);
        expose(v);
        push(v->l);
        return v->l;
    }

    S prod(Node *u, Node *v) {
        S res = between(u, v)->acc;
        res = op(res, v->val);
        return res;
    }

    S get(Node *v) {
        expose(v);
        return v->val;
    }

    void set(Node *v, S x) {
        expose(v);
        v->val = x;
        update(v);
    }

   public:
    // コンストラクタ
    LinkCutTree(vector<S> &w) {
        if (!pNIL) {
            pNIL = make_shared<Node>();
            NIL = pNIL.get();
            NIL->l = NIL->r = NIL->p = NIL->pp = NIL;
        }
        int n = w.size();
        A.resize(n);
        for (int i = 0; i < n; i++) {
            A[i] = make_shared<Node>(*NIL);
            A[i]->z = A[i]->sumz = 1;
            A[i]->idx = i;
            A[i]->val = A[i]->acc = w[i];
        }
    }

    // u,v間のパス上の頂点に書かれた総積
    S prod(int u, int v) {
        return prod(at(u), at(v));
    }

    // 頂点idxにxを代入
    void set(int idx, S x) {
        set(at(idx), x);
    }

    // 頂点idxの値を取得
    S get(int idx) {
        return get(at(idx));
    }

    // uとvを結ぶ辺を追加
    void add(int u, int v) {
        link(at(u), at(v));
    }

    void apply(int u, int v, F f) {
        evert(at(u));
        expose(at(v));
        Node *path = between(at(u), at(v));
        if (path != NIL) {
            path->val = mapping(f, path->val);
            path->acc = mapping(f, path->acc);
            path->lazy = composition(f, path->lazy);
        }
        update(path);
        Node *nd = at(v);
        nd->val = mapping(f, nd->val);
        nd->acc = mapping(f, nd->acc);
        update(nd);
    }

    // uとvを結ぶ辺を削除
    void erase(int u, int v) {
        evert(at(u));
        cut(at(v));
    }
};
#line 4 "verify/AizuOnlineJudge/graph/dynamic-tree/AOJ_2450.test.cpp"

struct S {
    lint val;
    lint siz;
    lint lmx;
    lint rmx;
    lint mx;
};

using T = lint;
using F = lint;
F ID = -1e17;

S op(S a, S b) {
    if (a.lmx == ID) return b;
    if (b.lmx == ID) return a;
    S ret;
    ret.val = a.val + b.val;
    ret.siz = a.siz + b.siz;
    ret.lmx = max(a.val + b.lmx, a.lmx);
    ret.rmx = max(b.val + a.rmx, b.rmx);
    ret.mx = max({a.mx, b.mx, a.rmx + b.lmx, ret.rmx, ret.lmx});
    return ret;
}

S e() {
    return {0, 0, ID, ID, ID};
}

S mapping(F f, S x) {
    if (f != ID) {
        x.val = f * x.siz;
        x.mx = x.lmx = x.rmx = max(f, f * x.siz);
    }
    return x;
}

F composition(F f, F g) {
    return (f == ID ? g : f);
}

F id() {
    return ID;
}

void reverseprod(S &node) {
    swap(node.lmx, node.rmx);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, q;
    in(n, q);
    vector<S> nodew(n);
    rep(i, n) {
        lint c;
        in(c);
        nodew[i] = {c, 1, c, c, c};
    }
    LinkCutTree<S, op, e, F, mapping, composition, id, reverseprod> t(nodew);

    rep(i, n - 1) {
        int u, v;
        in(u, v);
        u--;
        v--;
        t.add(u, v);
    }

    rep(i, q) {
        lint c;
        int com, u, v;
        in(com, u, v, c);
        u--;
        v--;
        if (com == 1) {
            t.apply(u, v, c);
        } else {
            out(t.prod(u, v).mx);
        }
    }
}
Back to top page