結果

問題 No.235 めぐるはめぐる (5)
ユーザー noshi91noshi91
提出日時 2018-04-11 19:07:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 790 ms / 10,000 ms
コード長 7,950 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 58,192 KB
実行使用メモリ 19,800 KB
最終ジャッジ日時 2023-09-09 04:07:46
合計ジャッジ時間 4,463 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 790 ms
19,800 KB
testcase_01 AC 448 ms
19,576 KB
testcase_02 AC 785 ms
19,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define NDEBUG
#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>

template <typename ValueMonoid, typename OperatorMonoid, class Modify>
class LinkCutTree {
public:
	using value_type = ValueMonoid;
	using reference = value_type &;
	using const_reference = const value_type &;
	using operator_type = OperatorMonoid;

private:
	struct node_t {
		node_t *left, *right, *par;
		value_type value, sum;
		operator_type lazy;
		bool isroot, reversed;
		node_t()
			: left(nil()), right(nil()), par(nullptr), value(), sum(), lazy(),
			isroot(1), reversed(0) {}
		bool isleft() const { return par->left == this; }
		void assign(const operator_type &data) { lazy = lazy + data; }
		void haulL(node_t *const t) { left = t, t->par = this; }
		void haulR(node_t *const t) { right = t, t->par = this; }
	};
	static node_t *nil() {
		static node_t *nilleaf = [] {
			node_t *ret = static_cast<node_t *>(malloc(sizeof(node_t)));
			ret->value = ret->sum = value_type();
			ret->lazy = operator_type();
			ret->isroot = 0;
			ret->reversed = 0;
			return ret;
		}();
		return nilleaf;
	}
	static void setnil() {
		nil()->sum = value_type();
		nil()->lazy = operator_type();
	}
	value_type reflect(const node_t *const t) const {
		return t->reversed ? m(~t->sum, t->lazy) : m(t->sum, t->lazy);
	}
	void recalc(node_t *const t) {
		t->sum = reflect(t->left) + t->value + reflect(t->right);
	}
	void splay(node_t *const t) {
		node_t *p, *pp = t, *x = t->par;
		while (!t->isroot) {
			(x->left == pp ? x->left : x->right) = t;
			p = t->par;
			if (p->isroot) {
				t->par = p->par;
				std::swap(t->isroot, p->isroot);
				if (p->left == t)
					p->haulL(t->right), t->haulR(p);
				else
					p->haulR(t->left), t->haulL(p);
				recalc(p);
				break;
			}
			pp = p->par;
			x = pp->par;
			std::swap(t->isroot, pp->isroot);
			if (t->isleft()) {
				if (p->isleft())
					pp->haulL(p->right), p->haulR(pp);
				else
					pp->haulR(t->left), t->haulL(pp);
				p->haulL(t->right);
				t->haulR(p);
			}
			else {
				if (p->isleft())
					pp->haulL(t->right), t->haulR(pp);
				else
					pp->haulR(p->left), p->haulL(pp);
				p->haulR(t->left);
				t->haulL(p);
			}
			recalc(pp);
			recalc(p);
			t->par = x;
		}
		recalc(t);
	}
	void expose(node_t *const t, node_t *const prev) {
		splay(t);
		//scan();
		t->right->isroot = 1;
		t->right = prev;
		prev->isroot = 0;
		recalc(t);
		if (t->par)
			expose(t->par, t);
	}
	void push(node_t *const t) {
		t->left->assign(t->lazy);
		t->right->assign(t->lazy);
		t->value = m(t->value, t->lazy);
		t->lazy = operator_type();
		if (!t->reversed)
			return;
		std::swap(t->left, t->right);
		t->left->reversed ^= 1;
		t->right->reversed ^= 1;
		t->value = ~t->value;
		t->reversed = 0;
	}
	void propagate(node_t*const t) {
		if (t->par)
			propagate(t->par);
		push(t);
	}
	using container_type = std::vector<node_t>;

public:
	using size_type = typename container_type::size_type;

private:
	container_type tree;
	const Modify m;
	void expose(node_t *const n) {
		propagate(n);
		//scan();
		setnil();
		expose(n, nil());
		//scan();
		splay(n);
		//scan();
	}

public:
	explicit LinkCutTree(const size_type size, const Modify &m = Modify())
		: tree(size), m(m) {}
	explicit LinkCutTree(const std::vector<value_type> &a,
		const Modify &m = Modify())
		: tree(a.size()), m(m) {
		for (size_type i = 0; i < a.size(); ++i) {
			tree[i].value = tree[i].sum = a[i];
		}
	}
	void link(const size_type child, const size_type parent) {
		assert(child < size());
		assert(parent < size());
		reroot(child);
		tree[child].par = &tree[parent];
	}
	void cut(const size_type child) {
		assert(child < size());
		node_t *const n = &tree[child];
		expose(n);
		n->left->isroot = 1;
		n->left->par = nullptr;
		n->left = nil();
		n->sum = n->value;
	}
	void update(const size_type u, const size_type v, const operator_type &data) {
		assert(u < size());
		assert(v < size());
		reroot(u);
		expose(&tree[v]);
		tree[v].assign(data);
	}
	value_type path(const size_type u, const size_type v) {
		assert(u < size());
		assert(v < size());
		reroot(u);
		//scan();
		expose(&tree[v]);
		//scan();
		return reflect(&tree[v]);
	}
	void reroot(const size_type v) {
		assert(v < size());
		expose(&tree[v]);
		tree[v].reversed ^= 1;
	}
	size_type size() const noexcept { return tree.size(); }
	bool empty() const noexcept { return tree.empty(); }
	//*
	struct vis {
		int l, r, p, rev;
	};
	std::vector<vis> v;
	int ch(node_t *n) {
		if (n == nil() || !n)
			return -9;
		return n - &tree[0];
	}
	void scan(void) {
		v = std::vector<vis>(size());
		for (int i = 0; i < size(); ++i) {
			v[i] = { ch(tree[i].left), ch(tree[i].right), ch(tree[i].par),
				tree[i].reversed };
		}
	}
	//*/
};
#include<cstdint>

template<std::uint_fast32_t MOD>
struct modint {
	using uint32 = std::uint_fast32_t;
	using uint64 = std::uint_fast64_t;
	uint32 a;
	modint() :a(0) {}
	modint(std::int_fast64_t x) :a(norms(x%MOD + MOD)) {}
	static uint32 norms(const uint32 &x) { return(x<MOD) ? x : x - MOD; }
	static modint make(const uint32 &x) { modint ret;ret.a = x;return ret; }
	modint operator+(const modint &o)const { return make(norms(a + o.a)); }
	modint operator-(const modint &o)const { return make(norms(a + MOD - o.a)); }
	modint operator*(const modint &o)const { return make((uint64)a*o.a%MOD); }
	modint operator/(const modint &o)const { return make((uint64)a*~o%MOD); }
	modint &operator+=(const modint &o) { return *this = *this + o; }
	modint &operator-=(const modint &o) { return *this = *this - o; }
	modint &operator*=(const modint &o) { return *this = *this * o; }
	modint &operator/=(const modint &o) { return *this = *this / o; }
	modint &operator^=(const uint32 &o) { return *this = *this^o; }
	modint operator~ ()const { return *this ^ (MOD - 2); }
	modint operator- ()const { return make(norms(MOD - a)); }
	modint operator++() { return *this = make(norms(a + 1)); }
	modint operator--() { return *this = make(norms(a + MOD - 1)); }
	bool operator==(const modint &o)const { return a == o.a; }
	bool operator!=(const modint &o)const { return a != o.a; }
	bool operator< (const modint &o)const { return a <  o.a; }
	bool operator<=(const modint &o)const { return a <= o.a; }
	bool operator> (const modint &o)const { return a >  o.a; }
	bool operator>=(const modint &o)const { return a >= o.a; }
	explicit operator bool()const { return a; }
	explicit operator uint32()const { return a; }
	modint operator^(uint32 x)const {
		uint64 t = (uint64)a;uint64 u = 1;
		while (x) { if (x & 1) u = u*t%MOD;t = (t*t) % MOD;x >>= 1; }
		return make((uint32)u);
	}
	/*
	friend std::istream &operator>>(std::istream &is, modint<MOD> &o) {
	std::int_fast64_t x;is >> x;o = modint<MOD>(x);return(is);
	}
	friend std::ostream &operator<<(std::ostream &os, const modint<MOD> &o) { return os << o.a; }
	*/
};
//using mint = modint<1000000007>;
using uint64 = std::uint_fast64_t;
constexpr uint64 MOD = 1000000007;
struct p {
	uint64 z;
	p(uint64 x = 0) :z(x) {}
	p operator+(const p &o)const {
		return p((z + o.z)%MOD);
	}
};
struct m {
	uint64 s, c;
	m(uint64 x = 0, uint64 y = 0) :s(x), c(y) {}
	m operator~()const { return *this; }
	m operator+(const m &o)const { return m((s + o.s)%MOD, (c + o.c)%MOD); }
	m operator*(const p &o)const { return m((s + (c*o.z)%MOD)%MOD, c); }
};
struct hoge {
	m operator()(const m &x, const p &y)const {
		return x*y;
	}
};
#include<cstdio>
#include<vector>
int main(void) {
	int n;
	scanf("%d", &n);
	std::vector<m> d(n);
	for (auto &e : d)scanf("%llu", &e.s);
	for (auto &e : d)scanf("%llu", &e.c);
	LinkCutTree<m, p, hoge> T(d);
	int a, b, c;
	while (--n) {
		scanf("%d %d", &a, &b);
		T.link(a - 1, b - 1);
		//T.scan();
	}
	scanf("%d", &n);
	uint64 z;
	while (n--) {
		scanf("%d %d %d", &c, &a, &b);
		if (c) {
			printf("%llu\n", T.path(a - 1, b - 1).s);
		}
		else {
			scanf("%llu", &z);
			T.update(a - 1, b - 1, p(z));
		}
		//T.scan();
	}
	return 0;
}
0