結果

問題 No.235 めぐるはめぐる (5)
ユーザー antaanta
提出日時 2015-06-27 14:55:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,277 ms / 10,000 ms
コード長 10,849 bytes
コンパイル時間 2,904 ms
コンパイル使用メモリ 115,956 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2023-09-22 03:13:01
合計ジャッジ時間 8,078 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,277 ms
18,940 KB
testcase_01 AC 486 ms
18,860 KB
testcase_02 AC 1,073 ms
18,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

struct CentroidPathDecomposition {
	vector<int> colors, positions;	//Vertex -> Color, Vertex -> Offset
	vector<int> lengths, parents, branches;	//Color -> Int, Color -> Color, Color -> Offset
	vector<int> parentnodes, depths;	//Vertex -> Vertex, Vertex -> Int
	//vector<FenwickTree>とかを避けて1次元にしたい時に使う
	//sortednodesの[lefts[v], rights[v])はvのsubtreeとなっている
	vector<int> sortednodes, offsets;	//Index -> Vertex, Color -> Index
	vector<int> lefts, rights;	//Vertex -> Index

	struct BuildDFSState {
		int i, len, parent;
		BuildDFSState() { }
		BuildDFSState(int i_, int l, int p): i(i_), len(l), parent(p) { }
	};

	//両方の辺があってもいいし、親から子への辺だけでもよい
	void build(const vector<vi> &g, int root) {
		int n = g.size();

		colors.assign(n, -1); positions.assign(n, -1);
		lengths.clear(); parents.clear(); branches.clear();
		parentnodes.assign(n, -1); depths.assign(n, -1);

		sortednodes.clear(); offsets.clear();
		lefts.assign(n, -1); rights.assign(n, -1);

		vector<int> subtreesizes;
		measure(g, root, subtreesizes);

		typedef BuildDFSState State;
		depths[root] = 0;
		vector<State> s;
		s.push_back(State(root, 0, -1));
		while(!s.empty()) {
			State t = s.back(); s.pop_back();
			int i = t.i, len = t.len;
			int index = sortednodes.size();
			int color = lengths.size();

			if(t.parent == -3) {
				rights[i] = index;
				continue;
			}

			if(t.parent != -2) {
				assert(parents.size() == color);
				parents.push_back(t.parent);
				branches.push_back(len);
				offsets.push_back(index);
				len = 0;
			}
			colors[i] = color;
			positions[i] = len;

			lefts[i] = index;
			sortednodes.push_back(i);

			int maxsize = -1, maxj = -1;
			each(j, g[i]) if(colors[*j] == -1) {
				if(maxsize < subtreesizes[*j]) {
					maxsize = subtreesizes[*j];
					maxj = *j;
				}
				parentnodes[*j] = i;
				depths[*j] = depths[i] + 1;
			}
			s.push_back(State(i, -1, -3));
			if(maxj == -1) {
				lengths.push_back(len + 1);
			}else {
				each(j, g[i]) if(colors[*j] == -1 && *j != maxj)
					s.push_back(State(*j, len, color));
				s.push_back(State(maxj, len + 1, -2));
			}
		}
	}
	
	void get(int v, int &c, int &p) const {
		c = colors[v]; p = positions[v];
	}
	bool go_up(int &c, int &p) const {
		p = branches[c]; c = parents[c];
		return c != -1;
	}

	inline const int *nodesBegin(int c) const { return &sortednodes[0] + offsets[c]; }
	inline const int *nodesEnd(int c) const { return &sortednodes[0] + (c+1 == offsets.size() ? sortednodes.size() : offsets[c+1]); }

private:
	void measure(const vector<vi> &g, int root, vector<int> &out_subtreesizes) const {
		out_subtreesizes.assign(g.size(), -1);
		vector<int> s;
		s.push_back(root);
		while(!s.empty()) {
			int i = s.back(); s.pop_back();
			if(out_subtreesizes[i] == -2) {
				int s = 1;
				each(j, g[i]) if(out_subtreesizes[*j] != -2)
					s += out_subtreesizes[*j];
				out_subtreesizes[i] = s;
			}else {
				s.push_back(i);
				each(j, g[i]) if(out_subtreesizes[*j] == -1)
					s.push_back(*j);
				out_subtreesizes[i] = -2;
			}
		}
	}
};

template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt(): x(0) { }
	ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }
	
	ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
	
	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
	ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
	
	ModInt inverse() const {
		signed a = x, b = MOD, u = 1, v = 0;
		while(b) {
			signed t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		if(u < 0) u += Mod;
		ModInt res; res.x = (unsigned)u;
		return res;
	}
	
	bool operator==(ModInt that) const { return x == that.x; }
	bool operator!=(ModInt that) const { return x != that.x; }
	ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
typedef ModInt<1000000007> mint;

vector<mint> coefs, coefsum;
struct Val {
	mint val;
	mint coef;
	Val() { }
};
struct Sum {
	mint sum, coefsum;
	Sum() { }
	Sum(const Val &val): sum(val.val), coefsum(val.coef) { }
	Sum &operator+=(const Sum &that) { sum += that.sum; coefsum += that.coefsum; return *this; }
	Sum operator+(const Sum &that) const { return Sum(*this) += that; }
};
struct Add {
	mint add;
	Add() { }
	Add &operator+=(const Add &that) { add += that.add; return *this; }
	void addToVal(Val &val) const { val.val += add * val.coef; }
	void addToSum(Sum &sum) const { sum.sum += add * sum.coefsum; }
	operator bool() const { return add.x != 0; }
};

struct Node {
private:
	Node *parent, *left, *right;
	Val val; Sum sum; Add add;
	bool rev;
 
public:
	Node(): parent(NULL), left(NULL), right(NULL),
		val(), sum(Val()), add(), rev(false) { }

private:
	bool is_root() const {
		return !parent || (parent->left != this && parent->right != this);
	}
	void update() {
		sum = Sum(val);
		if(left) {
			left->propagate();
			sum += left->sum;
		}
		if(right) {
			right->propagate();
			sum += right->sum;
		}
	}
	void propagate() {
		if(rev) {
			if(left) left->rev = !left->rev;
			if(right) right->rev = !right->rev;
			swap(left, right);
			rev = false;
		}
		if(add) {
			add.addToVal(val);
			add.addToSum(sum);
			if(left) left->add += add;
			if(right) right->add += add;
			add = Add();
		}
	}

public:
	bool debugCheckUpdated() const {
		Node tmp = *this;
		tmp.update();
		return memcmp(this, &tmp, sizeof(Node)) == 0;
	}
	bool debugCheckPropagated() const {
		return !rev && !add;
	}

private:
	void rotateR() {
		propagate();
		Node *q = parent, *r = q->parent;
		if(q->left = right) right->parent = q;
		right = q; q->parent = this;
		if(parent = r) {
			if(r->left == q) r->left = this;
			if(r->right == q) r->right = this;
		}
		q->update(); update();
	}
	void rotateL() {
		propagate();
		Node *q = parent, *r = q->parent;
		if(q->right = left) left->parent = q;
		left = q; q->parent = this;
		if(parent = r) {
			if(r->left == q) r->left = this;
			if(r->right == q) r->right = this;
		}
		q->update(); update();
	}
	void topdown() {
		static vector<Node*> route;
		for(Node *q = this; ; q = q->parent) {
			route.push_back(q);
			if(q->is_root()) break;
		}
		for(int i = route.size()-1; i >= 0; i --) {
			Node *q = route[i];
			q->propagate();
		}
		route.clear();
	}
	void splay() {
		topdown();
		while(!is_root()) {
			Node *q = parent;
			if(q->is_root()) {
				if(q->left == this) rotateR();
				else rotateL();
			}else {
				Node *r = q->parent;
				if(r->left == q) {
					if(q->left == this) q->rotateR(), rotateR();
					else rotateL(), rotateR();
				}else {
					if(q->right == this) q->rotateL(), rotateL();
					else rotateR(), rotateL();
				}
			}
		}
	}

public:
	void setVal(const Val &val_) {
		assert(is_root());
		val = val_;
		update();
	}
	void addAdd(const Add &add_) {
		assert(is_root());
		add += add_;
	}
	Val getVal() const {
		assert(is_root());
		Val t = val;
		add.addToVal(t);
		return t;
	}
	Sum getSum() const {
		assert(is_root() && debugCheckUpdated());
		Sum t = sum;
		add.addToSum(t);
		return t;
	}

private:
	static Node *pathHead(Node *a) {
		assert(a->is_root() && a->debugCheckPropagated());
		Node *h = a;
		while(1) {
			Node *c = h->left;
			if(!c) break;
			c->propagate();
			h = c;
		}
		h->splay();
		return h;
	}

	static void splitPath(Node *a) {
		assert(a->is_root() && a->debugCheckPropagated());
		Node *r = a->right;
		if(r != NULL) {
			a->right = NULL;
			a->update();
		}
	}

public:
	static void expose(Node *x) {
		Node *rp = NULL;
		for(Node *p = x; p != NULL; p = p->parent) {
			p->splay();
			p->right = rp;
			p->update();
			rp = p;
		}
		x->splay();
	}

	static void exposePath(Node *a, Node *b) {
		evert(a);
		a->propagate();
		expose(b);
		assert(a == b || a->parent != NULL);
		splitPath(b);
	}

	static void evert(Node *x) {
		expose(x);
		splitPath(x);
		assert(x->debugCheckPropagated());
		x->rev = true;
	}
	static void connect(Node *x, Node *y) {
		evert(x);
		x->parent = y;
	}
};

bool naivegetpath(int i, int p, int t, const vector<vi> &g, vi &path) {
	bool r = false;
	if(i == t) {
		r = true;
	}else {
		each(j, g[i]) if(*j != p)
			r = r || naivegetpath(*j, i, t, g, path);
	}
	if(r)
		path.push_back(i);
	return r;
}

int main() {
	int N;
	while(1) {
		if(!~scanf("%d", &N)) break;
		vector<int> S(N), C(N);
		rep(i, N) scanf("%d", &S[i]);
		rep(i, N) scanf("%d", &C[i]);
		vector<Node> nodes(N);
		rep(i, N) {
			Val val;
			val.val = S[i];
			val.coef = C[i];
			nodes[i].setVal(val);
		}
		vector<vi> g(N);
		rep(i, N-1) {
			int A, B;
			scanf("%d%d", &A, &B), -- A, -- B;
			Node::connect(&nodes[A], &nodes[B]);
		}
		int Q;
		scanf("%d", &Q);
		rep(ii, Q) {
			int ty;
			scanf("%d", &ty);
			if(ty == 0) {
				int X, Y, Z;
				scanf("%d%d%d", &X, &Y, &Z), -- X, -- Y;
				Add add; add.add = Z;
				Node::exposePath(&nodes[X], &nodes[Y]);
				nodes[Y].addAdd(add);
			}else {
				int X, Y;
				scanf("%d%d", &X, &Y), -- X, -- Y;
				Node::exposePath(&nodes[X], &nodes[Y]);
				mint ans = nodes[Y].getSum().sum;
				printf("%d\n", ans.get());
			}
		}
	}
	return 0;
}
0