結果

問題 No.650 行列木クエリ
コンテスト
ユーザー _yes
提出日時 2025-11-07 16:16:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 7,870 bytes
コンパイル時間 3,932 ms
コンパイル使用メモリ 304,756 KB
実行使用メモリ 34,856 KB
最終ジャッジ日時 2025-11-07 16:16:22
合計ジャッジ時間 7,666 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other AC * 1 RE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i< (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define fore(i, a) for(auto &i:a)
using ll = long long;
using int64 = long long;
#define DEBUG(x) cerr << #x << ": "; for (auto _ : x) cerr << _ << " "; cerr << endl;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
	IoSetup() {
		cin.tie(nullptr);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(10);
		cerr << fixed << setprecision(10);
	}
} iosetup;

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
	os << p.first << " " << p.second;
	return os;
}

template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
	is >> p.first >> p.second;
	return is;
}

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 != v.size() ? " " : "");
	}
	return os;
}

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

template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
	return a < b && (a = b, true);
}

template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
	return a > b && (a = b, true);
}

template <typename T = int64>
vector<T> make_v(size_t a) {
	return vector<T>(a);
}

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
	t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
	for (auto& e : t) fill_v(e, v);
}

template <typename F>
struct FixPoint : F {
	explicit FixPoint(F&& f) : F(std::forward<F>(f)) {}

	template <typename... Args>
		decltype(auto) operator()(Args&&... args) const {
			return F::operator()(*this, std::forward<Args>(args)...);
		}
};

template <typename F>
inline decltype(auto) MFP(F&& f) {
	return FixPoint<F>{std::forward<F>(f)};
}
template <typename T = int>
struct Edge {
	int from, to;
	T cost;
	int idx;

	Edge() = default;

	Edge(int from, int to, T cost = 1, int idx = -1)
		: from(from), to(to), cost(cost), idx(idx) {}

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

template <typename T = int>
struct Graph {
	vector<vector<Edge<T> > > g;
	int es;

	Graph() = default;

	explicit Graph(int n) : g(n), es(0) {}

	size_t size() const { return g.size(); }

	void add_directed_edge(int from, int to, T cost = 1) {
		g[from].emplace_back(from, to, cost, es++);
	}

	void add_edge(int from, int to, T cost = 1) {
		g[from].emplace_back(from, to, cost, es);
		g[to].emplace_back(to, from, cost, es++);
	}

	void read(int M, int padding = -1, bool weighted = false,
			bool directed = false) {
		for (int i = 0; i < M; i++) {
			int a, b;
			cin >> a >> b;
			a += padding;
			b += padding;
			T c = T(1);
			if (weighted) cin >> c;
			if (directed)
				add_directed_edge(a, b, c);
			else
				add_edge(a, b, c);
		}
	}

	inline vector<Edge<T> >& operator[](const int& k) { return g[k]; }

	inline const vector<Edge<T> >& operator[](const int& k) const { return g[k]; }
};

template <typename T = int>
using Edges = vector<Edge<T> >;
//depend on graph-template
template <typename T = int>
struct HeavyLightDecomposition : Graph<T> {
	public:
		using Graph<T>::Graph;
		using Graph<T>::g;
		vector<int> sz, in, out, head, rev, par, dep;

		void build(int root = 0) {
			sz.assign(g.size(), 0);
			in.assign(g.size(), 0);
			out.assign(g.size(), 0);
			head.assign(g.size(), 0);
			rev.assign(g.size(), 0);
			par.assign(g.size(), 0);
			dep.assign(g.size(), 0);
			dfs_sz(root, -1, 0);
			int t = 0;
			head[root] = root;
			dfs_hld(root, -1, t);
		}

		/* k: 0-indexed */
		int la(int v, int k) {
			while (1) {
				int u = head[v];
				if (in[v] - k >= in[u]) return rev[in[v] - k];
				k -= in[v] - in[u] + 1;
				v = par[u];
			}
		}

		int lca(int u, int v) const {
			for (;; v = par[head[v]]) {
				if (in[u] > in[v]) swap(u, v);
				if (head[u] == head[v]) return u;
			}
		}

		int dist(int u, int v) const { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }

		template <typename E, typename Q, typename F, typename S>
			E query(int u, int v, const E& ti, const Q& q, const F& f, const S& s,
					bool edge = false) {
				E l = ti, r = ti;
				for (;; v = par[head[v]]) {
					if (in[u] > in[v]) swap(u, v), swap(l, r);
					if (head[u] == head[v]) break;
					l = f(q(in[head[v]], in[v] + 1), l);
				}
				return s(f(q(in[u] + edge, in[v] + 1), l), r);
			}

		template <typename E, typename Q, typename F>
			E query(int u, int v, const E& ti, const Q& q, const F& f,
					bool edge = false) {
				return query(u, v, ti, q, f, f, edge);
			}

		template <typename Q>
			void add(int u, int v, const Q& q, bool edge = false) {
				for (;; v = par[head[v]]) {
					if (in[u] > in[v]) swap(u, v);
					if (head[u] == head[v]) break;
					q(in[head[v]], in[v] + 1);
				}
				q(in[u] + edge, in[v] + 1);
			}

		/* {parent, child} */
		vector<pair<int, int> > compress(vector<int>& remark) {
			auto cmp = [&](int a, int b) { return in[a] < in[b]; };
			sort(begin(remark), end(remark), cmp);
			remark.erase(unique(begin(remark), end(remark)), end(remark));
			int K = (int)remark.size();
			for (int k = 1; k < K; k++)
				remark.emplace_back(lca(remark[k - 1], remark[k]));
			sort(begin(remark), end(remark), cmp);
			remark.erase(unique(begin(remark), end(remark)), end(remark));
			vector<pair<int, int> > es;
			stack<int> st;
			for (auto& k : remark) {
				while (!st.empty() && out[st.top()] <= in[k]) st.pop();
				if (!st.empty()) es.emplace_back(st.top(), k);
				st.emplace(k);
			}
			return es;
		}

		explicit HeavyLightDecomposition(const Graph<T>& g) : Graph<T>(g) {}

	private:
		void dfs_sz(int idx, int p, int d) {
			dep[idx] = d;
			par[idx] = p;
			sz[idx] = 1;
			if (g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
			for (auto& to : g[idx]) {
				if (to == p) continue;
				dfs_sz(to, idx, d + 1);
				sz[idx] += sz[to];
				if (sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
			}
		}

		void dfs_hld(int idx, int p, int& times) {
			in[idx] = times++;
			rev[in[idx]] = idx;
			for (auto& to : g[idx]) {
				if (to == p) continue;
				head[to] = (g[idx][0] == to ? head[idx] : to);
				dfs_hld(to, idx, times);
			}
			out[idx] = times;
		}
};
#include<atcoder/modint>
#include<atcoder/lazysegtree>
using namespace atcoder;
using mint = modint1000000007;
struct S{
	mint x00, x01, x10, x11;
};
S op(S a, S b){
  S n;
	n.x00 = a.x00*b.x00+a.x01*b.x10;
	n.x01 = a.x00*b.x01+a.x01*b.x11;
	n.x10 = a.x10*b.x00+a.x11*b.x10;
	n.x11 = a.x10*b.x01+a.x11*b.x11;
	return n;
}
S e(){
	return {1,0,0,1};
}
struct F{
	S mat;
	bool flag;
};
S mapping(F f, S x){
	if(f.flag){
		return f.mat;
	}
	else{
		return x;
	}
}
F composition(F f, F g){
	if(g.flag)return g;
	else return f;
}
F id(){
	return {{0,0,0,0},false};
}
int main(){
	ll n;cin >> n;
	Graph g(n);
	vector<ll> A(n-1), B(n-1);
	rep(i, n-1){
		cin >> A[i] >> B[i];
		g.add_edge(A[i],B[i]);
	}
	HeavyLightDecomposition HLD(g);
	HLD.build();
  ll q;cin >> q;
	lazy_segtree<S,op,e,F,mapping,composition,id> seg(n);
	while(q--){
		char t;
		cin >> t;
		if(t == 'x'){
			ll i, a, b, c, d;
			cin >> i >> a >> b >> c >> d;
			auto Q = [&](ll l, ll r){
				seg.apply(A[i], B[i], F{{a, b, c, d}, true});
			};
			HLD.add(i, i, Q, true);
		}
		else{
			ll i, j;
			cin >> i >> j;
			auto Q = [&](ll l, ll r){
				return seg.prod(l, r);
			};
			S res = HLD.query(i, j, e(), Q, op, true);
			cout << res.x00.val() << " " << res.x01.val() << " " << res.x10.val() << " " << res.x11.val() << "\n";
		}
	}




}
0