compro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub siro53/compro_library

:heavy_check_mark: test/library-checker/data-structure/vertex-set-path-composite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include "../../../template/template.cpp"
#include "../../../data-structure/monoid/affine.hpp"
#include "../../../data-structure/segtree/segtree.hpp"
#include "../../../graph/tree/hld.hpp"
#include "../../../modint/modint.hpp"

using mint = ModInt<MOD2>;
using AffineLeft = MonoidAffine<mint, false>;
using AffineRight = MonoidAffine<mint, true>;

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<pair<mint, mint>> af(N);
    REP(i, N) {
        mint a, b;
        cin >> a >> b;
        af[i] = {a, b};
    }
    HeavyLightDecomposition hld(N);
    REP(_, N-1) {
        int u, v;
        cin >> u >> v;
        hld.add_edge(u, v);
    }
    hld.build();
    Segtree<AffineLeft> seg(N);
    Segtree<AffineRight> seg_rev(N);
    REP(i, N) {
        seg.set(hld[i], af[i]);
        seg_rev.set(hld[i], af[i]);
    }

    while(Q--) {
        int type;
        cin >> type;
        if(type == 0) {
            int p;
            mint c, d;
            cin >> p >> c >> d;
            seg.set(hld[p], make_pair(c, d));
            seg_rev.set(hld[p], make_pair(c, d));
        } else {
            int u, v;
            mint x;
            cin >> u >> v >> x;
            pair<mint, mint> res_l = AffineLeft::e(), res_r = AffineRight::e();
            auto f = [&](int l, int r) {
                res_l = AffineLeft::op(seg.prod(l, r), res_l);
            };
            auto f_rev = [&](int l, int r) {
                res_r = AffineRight::op(seg_rev.prod(l, r), res_r);
            };
            hld.path_query<decltype(f), decltype(f_rev)>(u, v, f, f_rev);
            auto [a, b] = AffineLeft::op(res_r, res_l);
            mint ans = a * x + b;
            cout << ans << '\n';
        }
    }
}
#line 1 "test/library-checker/data-structure/vertex-set-path-composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#line 1 "template/template.cpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
    // https://maspypy.github.io/library/other/io_old.hpp
    struct has_val_impl {
        template <class T>
        static auto check(T &&x) -> decltype(x.val(), std::true_type{});

        template <class T> static auto check(...) -> std::false_type;
    };

    template <class T>
    class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
    };

    // debug
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void dump(const T &t) {
        cerr << t.val();
    }
    void dump(__int128_t n) {
        if(n == 0) {
            cerr << '0';
            return;
        } else if(n < 0) {
            cerr << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
    void dump(const string &s) { cerr << s; }
    void dump(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cerr << s[i];
    }
    template <class T1, class T2> void dump(const pair<T1, T2> &p) {
        cerr << '(';
        dump(p.first);
        cerr << ',';
        dump(p.second);
        cerr << ')';
    }
    template <class T> void dump(const vector<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const set<T> &s) {
        cerr << '{';
        for(auto it = s.begin(); it != s.end(); it++) {
            dump(*it);
            if(next(it) != s.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value> void dump(const map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value>
    void dump(const unordered_map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const deque<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(queue<T> q) {
        cerr << '{';
        while(!q.empty()) {
            dump(q.front());
            if((int)q.size() > 1) cerr << ',';
            q.pop();
        }
        cerr << '}';
    }

    void debug_print() { cerr << endl; }
    template <class Head, class... Tail>
    void debug_print(const Head &h, const Tail &...t) {
        dump(h);
        if(sizeof...(Tail)) dump(' ');
        debug_print(t...);
    }
    // print
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void print_single(const T t) {
        cout << t.val();
    }
    void print_single(__int128_t n) {
        if(n == 0) {
            cout << '0';
            return;
        } else if(n < 0) {
            cout << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cout << s;
    }
    void print_single(const string &s) { cout << s; }
    void print_single(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cout << s[i];
    }
    template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
        print_single(p.first);
        cout << ' ';
        print_single(p.second);
    }
    template <class T> void print_single(const vector<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(const set<T> &s) {
        for(auto it = s.begin(); it != s.end(); it++) {
            print_single(*it);
            if(next(it) != s.end()) cout << ' ';
        }
    }
    template <class T> void print_single(const deque<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(queue<T> q) {
        while(!q.empty()) {
            print_single(q.front());
            if((int)q.size() > 1) cout << ' ';
            q.pop();
        }
    }

    void print() { cout << '\n'; }
    template <class Head, class... Tail>
    void print(const Head &h, const Tail &...t) {
        print_single(h);
        if(sizeof...(Tail)) print_single(' ');
        print(t...);
    }

    // input
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void input_single(T &t) {
        cin >> t;
    }
    void input_single(__int128_t &n) {
        string s;
        cin >> s;
        if(s == "0") {
            n = 0;
            return;
        }
        bool is_minus = false;
        if(s[0] == '-') {
            s = s.substr(1);
            is_minus = true;
        }
        n = 0;
        for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
        if(is_minus) n = -n;
    }
    void input_single(string &s) { cin >> s; }
    template <class T1, class T2> void input_single(pair<T1, T2> &p) {
        input_single(p.first);
        input_single(p.second);
    }
    template <class T> void input_single(vector<T> &v) {
        for(auto &e : v) input_single(e);
    }
    void input() {}
    template <class Head, class... Tail> void input(Head &h, Tail &...t) {
        input_single(h);
        input(t...);
    }
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    input(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    input(__VA_ARGS__)
#define DBL(...)                                                               \
    double __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define LD(...)                                                                \
    long double __VA_ARGS__;                                                   \
    input(__VA_ARGS__)
#define UINT(...)                                                              \
    unsigned int __VA_ARGS__;                                                  \
    input(__VA_ARGS__)
#define ULL(...)                                                               \
    unsigned long long __VA_ARGS__;                                            \
    input(__VA_ARGS__)
#define VEC(name, type, len)                                                   \
    vector<type> name(len);                                                    \
    input(name);
#define VEC2(name, type, len1, len2)                                           \
    vector name(len1, vector<type>(len2));                                     \
    input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v)                                                              \
    sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
    return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
    return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
    if constexpr(dim == 1) {
        return vector(sizes[0], e);
    } else {
        int n = sizes[dim - 1];
        sizes.pop_back();
        return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
    }
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
    vector<int> s(dim);
    for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
    return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), start);
    return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
    auto ord = iota_gen((int)v.size());
    sort(ALL(ord), [&](int i, int j) {
        if(greater) return v[i] > v[j];
        return v[i] < v[j];
    });
    return ord;
}
#pragma endregion Macros
#line 2 "data-structure/monoid/affine.hpp"

#line 4 "data-structure/monoid/affine.hpp"

template <typename T, bool rev = false> struct MonoidAffine {
    using S = std::pair<T, T>;
    using value_type = S;
    inline static S op(const S &l, const S &r) {
        if(rev) return S(r.first * l.first, r.first * l.second + r.second);
        return S(l.first * r.first, l.first * r.second + l.second);
    }
    inline static S e() { return S(T(1), T(0)); }
};
#line 2 "data-structure/segtree/segtree.hpp"

#line 5 "data-structure/segtree/segtree.hpp"

template <class Monoid> class Segtree {
  public:
    using T = typename Monoid::value_type;

    Segtree() : Segtree(0) {}
    explicit Segtree(int n) : Segtree(std::vector<T>(n, Monoid::e())) {}
    explicit Segtree(const std::vector<T> &v) : N((int)v.size()), sz(1) {
        while(sz < N) sz <<= 1;
        node.resize(sz * 2, Monoid::e());
        for(int i = 0; i < N; i++) node[i + sz] = v[i];
        for(int i = sz - 1; i >= 1; i--) {
            node[i] = Monoid::op(node[i << 1], node[i << 1 | 1]);
        }
    }
    void set(int pos, T val) {
        assert(0 <= pos && pos < N);
        pos += sz;
        node[pos] = val;
        while(pos > 1) {
            pos >>= 1;
            node[pos] = Monoid::op(node[pos << 1], node[pos << 1 | 1]);
        }
    }
    T get(int pos) const {
        assert(0 <= pos && pos < N);
        return node[pos + sz];
    }
    void apply(int pos, T val) {
        this->set(pos, Monoid::op(this->get(pos), val));
    }
    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= N);
        T value_l = Monoid::e(), value_r = Monoid::e();
        l += sz;
        r += sz;
        while(l < r) {
            if(l & 1) value_l = Monoid::op(value_l, node[l++]);
            if(r & 1) value_r = Monoid::op(node[--r], value_r);
            l >>= 1;
            r >>= 1;
        }
        return Monoid::op(value_l, value_r);
    }
    T all_prod() const { return node[1]; }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= N);
        assert(f(Monoid::e()));
        if(l == N) return N;
        l += sz;
        T value_now = Monoid::e();
        do {
            while((l & 1) == 0) l >>= 1;
            if(!f(Monoid::op(value_now, node[l]))) {
                while(l < sz) {
                    l = 2 * l;
                    if(f(Monoid::op(value_now, node[l]))) {
                        value_now = Monoid::op(value_now, node[l]);
                        l++;
                    }
                }
                return (l - sz);
            }
            value_now = Monoid::op(value_now, node[l]);
            l++;
        } while((l & -l) != l);
        return N;
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= N);
        assert(f(Monoid::e()));
        if(r == 0) return 0;
        r += sz;
        T value_now = Monoid::e();
        do {
            r--;
            while(r > 1 && (r & 1)) r >>= 1;
            if(!f(Monoid::op(node[r], value_now))) {
                while(r < sz) {
                    r = 2 * r + 1;
                    if(f(Monoid::op(node[r], value_now))) {
                        value_now = Monoid::op(node[r], value_now);
                        r--;
                    }
                }
                return ((r + 1) - sz);
            }
            value_now = Monoid::op(node[r], value_now);
        } while((r & -r) != r);
        return 0;
    }

  private:
    int N, sz;
    std::vector<T> node;
};
#line 2 "graph/tree/hld.hpp"

#line 4 "graph/tree/hld.hpp"

#line 2 "graph/graph_template.hpp"

#line 5 "graph/graph_template.hpp"

template <typename Cost = int> struct Edge {
    int from, to;
    Cost cost;
    int id;
    Edge() = default;
    explicit Edge(int from, int to, Cost cost = 1, int id = -1)
        : from(from), to(to), cost(cost), id(id) {}
    operator int() const { return to; }
};

template <typename Cost = int> class Graph {
  public:
    Graph() = default;
    explicit Graph(int N) : N(N), M(0), G(N) {}

    inline void add_directed_edge(int from, int to, Cost cost = 1) {
        assert(0 <= from && from < N);
        assert(0 <= to && to < N);
        G[from].emplace_back(from, to, cost, M++);
    }

    inline void add_undirected_edge(int from, int to, Cost cost = 1) {
        assert(0 <= from && from < N);
        assert(0 <= to && to < N);
        G[from].emplace_back(from, to, cost, M);
        G[to].emplace_back(to, from, cost, M++);
    }

    inline size_t size() const { return G.size(); }
    inline std::vector<Edge<Cost>> &operator[](const int &i) { return G[i]; }
    inline const std::vector<Edge<Cost>> &operator[](const int &i) const {
        return G[i];
    }

  protected:
    int N, M;
    std::vector<std::vector<Edge<Cost>>> G;
};

template <class Cost = int> using Edges = std::vector<Edge<Cost>>;
#line 6 "graph/tree/hld.hpp"

template <typename Cost = int> class HeavyLightDecomposition {
  public:
    explicit HeavyLightDecomposition(int N)
        : G(N), in(N), out(N), sz(N), head(N), par(N), dep(N), rev(N),
          isBuilt(false) {}
    explicit HeavyLightDecomposition(const Graph<Cost> &g)
        : G(g), in(g.size()), out(g.size()), sz(g.size()), head(g.size()),
          par(g.size()), dep(g.size()), rev(g.size()), isBuilt(false) {
        build();
    }
    void add_edge(int from, int to, Cost cost = 1) {
        G.add_undirected_edge(from, to, cost);
    }
    void build(int root = 0) {
        dfs1(root, -1, 0);
        head[root] = root;
        int idx = 0;
        dfs2(root, -1, idx);
        isBuilt = true;
    }
	int lca(int u, int v) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		assert(0 <= v && v < (int)G.size());
		while(1) {
			if(in[u] > in[v]) std::swap(u, v);
			if(head[u] == head[v]) return u;
			v = par[head[v]];
		}
	}
	int dist(int u, int v) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		assert(0 <= v && v < (int)G.size());
		return (dep[u] + dep[v] - 2 * dep[lca(u, v)]);
	}
	int la(int u, int k) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		if(dep[u] < k) return -1;
		while(1) {
			if(in[u] - k >= in[head[u]]) {
				return rev[in[u] - k];
			}
			k -= (in[u] - in[head[u]] + 1);
			u = par[head[u]];
		}
	}
	template<class F>
	void path_query(int u, int v, const F& f, bool edge = false) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		assert(0 <= v && v < (int)G.size());
		while(1) {
			if(in[u] > in[v]) std::swap(u, v);
			if(head[u] == head[v]) {
				f(in[u] + (int)(edge), in[v] + 1);
				break;
			} else {
				f(in[head[v]], in[v] + 1);
			}
			v = par[head[v]];
		}
	}
	template<class F, class F_rev>
	void path_query(int u, int v, const F& f, const F_rev& f_rev, bool edge = false) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		assert(0 <= v && v < (int)G.size());
		int L = lca(u, v);
		path_query<F>(u, L, f, edge);
		path_query<F_rev>(L, v, f_rev, true);
	}
	template<class F>
	void subtree_query(int u, const F& f) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		f(in[u], out[u]);
	}
	int operator[](int u) const {
		assert(isBuilt);
		assert(0 <= u && u < (int)G.size());
		return in[u];
	}

  private:
    Graph<Cost> G;
    std::vector<int> in, out, sz, head, par, dep, rev;
    bool isBuilt;

    void dfs1(int u, int p, int d) {
        dep[u] = d;
        sz[u] = 1;
        if(!G[u].empty() && G[u].front().to == p) {
            std::swap(G[u].front(), G[u].back());
        }
        for(auto &e : G[u]) {
            if(e.to == p) continue;
            dfs1(e.to, u, d + 1);
            sz[u] += sz[e.to];
            if(sz[e.to] > sz[G[u].front().to]) { std::swap(G[u].front(), e); }
        }
    }

    void dfs2(int u, int p, int &idx) {
        par[u] = p;
        in[u] = idx++;
        rev[in[u]] = u;
        for(const auto &e : G[u]) {
            if(e.to == p) continue;
            head[e.to] = (e.to == G[u].front().to ? head[u] : e.to);
            dfs2(e.to, u, idx);
        }
        out[u] = idx;
    }
};
#line 2 "modint/modint.hpp"

#line 6 "modint/modint.hpp"

template <int mod> class ModInt {
  public:
    ModInt() : x(0) {}
    ModInt(long long y)
        : x(y >= 0 ? y % umod() : (umod() - (-y) % umod()) % umod()) {}
    unsigned int val() const { return x; }
    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= umod()) x -= umod();
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if((x += umod() - p.x) >= umod()) x -= umod();
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        x = (unsigned int)(1ULL * x * p.x % umod());
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inv();
        return *this;
    }
    ModInt operator-() const { return ModInt(-(long long)x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
    ModInt inv() const {
        long long a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return ModInt(u);
    }
    ModInt pow(unsigned long long n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &p) {
        return os << p.x;
    }
    friend std::istream &operator>>(std::istream &is, ModInt &a) {
        long long t;
        is >> t;
        a = ModInt<mod>(t);
        return (is);
    }
    static constexpr int get_mod() { return mod; }

  private:
    unsigned int x;
    static constexpr unsigned int umod() { return mod; }
};
#line 7 "test/library-checker/data-structure/vertex-set-path-composite.test.cpp"

using mint = ModInt<MOD2>;
using AffineLeft = MonoidAffine<mint, false>;
using AffineRight = MonoidAffine<mint, true>;

int main() {
    int N, Q;
    cin >> N >> Q;
    vector<pair<mint, mint>> af(N);
    REP(i, N) {
        mint a, b;
        cin >> a >> b;
        af[i] = {a, b};
    }
    HeavyLightDecomposition hld(N);
    REP(_, N-1) {
        int u, v;
        cin >> u >> v;
        hld.add_edge(u, v);
    }
    hld.build();
    Segtree<AffineLeft> seg(N);
    Segtree<AffineRight> seg_rev(N);
    REP(i, N) {
        seg.set(hld[i], af[i]);
        seg_rev.set(hld[i], af[i]);
    }

    while(Q--) {
        int type;
        cin >> type;
        if(type == 0) {
            int p;
            mint c, d;
            cin >> p >> c >> d;
            seg.set(hld[p], make_pair(c, d));
            seg_rev.set(hld[p], make_pair(c, d));
        } else {
            int u, v;
            mint x;
            cin >> u >> v >> x;
            pair<mint, mint> res_l = AffineLeft::e(), res_r = AffineRight::e();
            auto f = [&](int l, int r) {
                res_l = AffineLeft::op(seg.prod(l, r), res_l);
            };
            auto f_rev = [&](int l, int r) {
                res_r = AffineRight::op(seg_rev.prod(l, r), res_r);
            };
            hld.path_query<decltype(f), decltype(f_rev)>(u, v, f, f_rev);
            auto [a, b] = AffineLeft::op(res_r, res_l);
            mint ans = a * x + b;
            cout << ans << '\n';
        }
    }
}
Back to top page