結果

問題 No.235 めぐるはめぐる (5)
ユーザー pekempeypekempey
提出日時 2016-05-25 16:31:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,192 ms / 10,000 ms
コード長 3,766 bytes
コンパイル時間 1,474 ms
コンパイル使用メモリ 165,244 KB
実行使用メモリ 33,988 KB
最終ジャッジ日時 2024-10-07 14:13:20
合計ジャッジ時間 6,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,192 ms
33,348 KB
testcase_01 AC 551 ms
33,988 KB
testcase_02 AC 994 ms
33,344 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:169:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  169 |         for (int i = 0; i < n; i++) scanf("%d", &S[i]);
      |                                     ~~~~~^~~~~~~~~~~~~
main.cpp:170:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  170 |         for (int i = 0; i < n; i++) scanf("%d", &C[i]);
      |                                     ~~~~~^~~~~~~~~~~~~
main.cpp:175:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  175 |                 scanf("%d %d", &u, &v);
      |                 ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:188:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  188 |                 scanf("%d", &k);
      |                 ~~~~~^~~~~~~~~~
main.cpp:192:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  192 |                         scanf("%d %d %d", &x, &y, &z);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:201:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  201 |                         scanf("%d %d", &x, &y);
      |                         ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;

enum { LEFT, RIGHT };

struct node {
	node *parent = nullptr;
	node *lch = nullptr;
	node *rch = nullptr;
	bool rev = false;
	long long lazy = 0;
	long long coef = 0;
	long long value = 0;
	long long sum_coef = 0;
	long long sum = 0;

	node(long long value, long long coef) : value(value), coef(coef) {
		sum_coef = coef;
		sum = value;
	}

	int direction() {
		if (parent == nullptr) return -1;
		return parent->lch == this ? LEFT : RIGHT;
	}

	bool is_root() {
		return parent == nullptr || (parent->lch != this && parent->rch != this);
	}
};

void connect(node *child, node *parent, int dir) {
	if (parent != nullptr) (dir == LEFT ? parent->lch : parent->rch) = child;
	if (child != nullptr) child->parent = parent;
}

long long sum_coef(node *x) {
	return x == nullptr ? 0 : x->sum_coef;
}

long long sum(node *x) {
	return x == nullptr ? 0 : (x->sum + x->lazy * sum_coef(x)) % mod;
}

void update(node *x) {
	if (x == nullptr) return;
	x->sum_coef = (x->coef + sum_coef(x->lch) + sum_coef(x->rch)) % mod;
	x->sum = (x->value + sum(x->lch) + sum(x->rch)) % mod;
}

void push(node *x) {
	if (x == nullptr) return;
	for (node *y : { x->lch, x->rch }) if (y != nullptr) {
		(y->lazy += x->lazy) %= mod;
		y->rev ^= x->rev;
	}
	if (x->rev) swap(x->lch, x->rch);

	(x->value += x->lazy * x->coef) %= mod;
	x->lazy = 0;
	x->rev = false;

	update(x);
}

void raise(node *x) {
	node *y = x->parent;
	node *z = y->parent;
	int ydir = y->direction();
	bool is_root = y->is_root();
	if (x->direction() == LEFT) {
		connect(x->rch, y, LEFT);
		connect(y, x, RIGHT);
	} else {
		connect(x->lch, y, RIGHT);
		connect(y, x, LEFT);
	}
	if (is_root) {
		x->parent = z;
	} else {
		connect(x, z, ydir);
	}
	update(y);
	update(x);
	update(z);
}

void splay(node *x) {
	while (!x->is_root()) {
		if (x->parent->parent != nullptr) push(x->parent->parent);
		push(x->parent);
		push(x);
		if (x->parent->is_root()) {
			raise(x);
		} else {
			if (x->direction() == x->parent->direction()) {
				raise(x->parent);
				raise(x);
			} else {
				raise(x);
				raise(x);
			}
		}
	}
	push(x);
}

void expose(node *x) {
	node *rch = nullptr;
	node *y = x;
	while (y != nullptr) {
		splay(y);
		assert(y->lazy == 0);
		y->rch = rch;
		rch = y;
		update(y);
		y = y->parent;
	}
	splay(x);
}

void cut(node *x) {
	expose(x);
	x->lch->parent = nullptr;
	x->lch = nullptr;
	update(x);
}

void link(node *x, node *p) {
	expose(x);
	expose(p);
	x->parent = p;
	p->rch = x;
	update(x);
	update(p);
}

void evert(node *x) {
	expose(x);
	x->rch = x->lch;
	x->lch = nullptr;
	if (x->rch != nullptr) x->rch->rev ^= 1;
	update(x);
}

void add(node *x, long long value) {
	(x->value += value * x->coef) %= mod;
	if (x->lch != nullptr) (x->lch->lazy += value) %= mod;
	update(x);
}

vector<int> g[202020];
node *tr[202020];

void dfs(int curr, int prev) {
	for (int next : g[curr]) if (next != prev) {
		link(tr[next], tr[curr]);
		dfs(next, curr);
	}
}

int main() {
	int n;
	cin >> n;

	vector<int> S(n), C(n);
	for (int i = 0; i < n; i++) scanf("%d", &S[i]);
	for (int i = 0; i < n; i++) scanf("%d", &C[i]);
	for (int i = 0; i < n; i++) tr[i] = new node(S[i], C[i]);

	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--; v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	dfs(0, -1);

	int q;
	cin >> q;

	for (int i = 0; i < q; i++) {
		int k;
		scanf("%d", &k);

		if (k == 0) {
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);
			x--; y--;

			evert(tr[x]);
			expose(tr[y]);

			add(tr[y], z);
		} else {
			int x, y;
			scanf("%d %d", &x, &y);
			x--; y--;

			evert(tr[x]);
			expose(tr[y]);

			long long ans = (tr[y]->value + sum(tr[y]->lch)) % mod;
			printf("%lld\n", ans);
		}
	}
}
0