結果

問題 No.235 めぐるはめぐる (5)
ユーザー ふっぴーふっぴー
提出日時 2018-02-19 23:26:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,746 bytes
コンパイル時間 2,142 ms
コンパイル使用メモリ 190,732 KB
実行使用メモリ 39,284 KB
最終ジャッジ日時 2024-07-16 03:53:29
合計ジャッジ時間 8,785 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #


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

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
const int inf = 1000000001;
const ll INF = 2e18 * 2;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };

vl s, c, csum;
vl s2, c2;

struct HLDecomposition {
	int n, pos;
	vector<vector<int> > G;
	vector<int> vid, head, sub, hvy, par, dep, inv, type;

	HLDecomposition() {}
	HLDecomposition(int sz) :
		n(sz), pos(0), G(n),
		vid(n, -1), head(n), sub(n, 1), hvy(n, -1),
		par(n), dep(n), inv(n), type(n) {}

	void add_edge(int u, int v) {
		G[u].push_back(v);
		G[v].push_back(u);
	}

	void build(vector<int> rs = { 0 }) {
		int c = 0;
		for (int r : rs) {
			dfs(r);
			bfs(r, c++);
		}
	}

	void dfs(int rt) {
		using T = pair<int, int>;
		stack<T> st;
		par[rt] = -1;
		dep[rt] = 0;
		st.emplace(rt, 0);
		while (!st.empty()) {
			int v = st.top().first;
			int &i = st.top().second;
			if (i<(int)G[v].size()) {
				int u = G[v][i++];
				if (u == par[v]) continue;
				par[u] = v;
				dep[u] = dep[v] + 1;
				st.emplace(u, 0);
			}
			else {
				st.pop();
				int res = 0;
				for (int u : G[v]) {
					if (u == par[v]) continue;
					sub[v] += sub[u];
					if (res<sub[u]) res = sub[u], hvy[v] = u;
				}
			}
		}
	}

	void bfs(int r, int c) {
		int &k = pos;
		queue<int> q({ r });
		while (!q.empty()) {
			int h = q.front(); q.pop();
			for (int i = h; i != -1; i = hvy[i]) {
				type[i] = c;
				vid[i] = k++;
				inv[vid[i]] = i;
				head[i] = h;
				for (int j : G[i])
					if (j != par[i] && j != hvy[i]) q.push(j);
			}
		}
	}

	// for_each(vertex)
	// [l,r] <- attention!!
	void for_each(int u, int v, const function<void(int, int)>& f) {
		while (1) {
			if (vid[u]>vid[v]) swap(u, v);
			f(max(vid[head[v]], vid[u]), vid[v]);
			if (head[u] != head[v]) v = par[head[v]];
			else break;
		}
	}

	// for_each(edge)
	// [l,r] <- attention!!
	void for_each_edge(int u, int v, const function<void(int, int)>& f) {
		while (1) {
			if (vid[u]>vid[v]) swap(u, v);
			if (head[u] != head[v]) {
				f(vid[head[v]], vid[v]);
				v = par[head[v]];
			}
			else {
				if (u != v) f(vid[u] + 1, vid[v]);
				break;
			}
		}
	}

	int lca(int u, int v) {
		while (1) {
			if (vid[u]>vid[v]) swap(u, v);
			if (head[u] == head[v]) return u;
			v = par[head[v]];
		}
	}

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

class LazySegmentTree {
public:
	int n;
	vl node, lazy;

	LazySegmentTree(vl a) {
		int _n = a.size();
		n = 1; while (n < _n) n *= 2;
		node.resize(2 * n - 1);
		lazy.resize(2 * n - 1, 0);
		for (int i = 0; i < _n; i++) node[i + n - 1] = a[i];
		for (int i = n - 2; i >= 0; i--) {
			node[i] = node[i * 2 + 1] + node[i * 2 + 2];
		}
	}

	// k番目のノードについて遅延評価を行う
	inline void eval(int k, int l, int r) {
		if (lazy[k] != 0) {
			node[k] += (csum[r] - csum[l]) * lazy[k];
			if (r - l > 1) {
				lazy[2 * k + 1] += lazy[k]; /////
				lazy[2 * k + 2] += lazy[k]; /////
			}
			lazy[k] = 0;
		}
	}

	// [a, b)をxにする
	void add(int a, int b, ll x, int k, int l, int r) {
		// k 番目のノードに対して遅延評価を行う
		eval(k, l, r);
		if (b <= l || r <= a) return;
		if (a <= l && r <= b) {
			lazy[k] += x;
			eval(k, l, r);
		}
		else {
			add(a, b, x, 2 * k + 1, l, (l + r) / 2);
			add(a, b, x, 2 * k + 2, (l + r) / 2, r);
			node[k] = node[2 * k + 1] + node[2 * k + 2];
		}
	}

	ll getsum(int a, int b, int k, int l, int r) {
		eval(k, l, r);
		if (b <= l || r <= a) return 0;
		if (a <= l && r <= b) return node[k];
		ll resl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
		ll resr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
		return resl + resr;
	}
};


int main() {
	int n, i, j;
	cin >> n;

	s.resize(n); c.resize(n); csum.resize(n + 1);
	s2.resize(n); c2.resize(n);
	rep(i, n) {
		cin >> s[i];
	}
	rep(i, n) {
		cin >> c[i];
	}
	
	HLDecomposition hld(n);
	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		hld.add_edge(a, b);
	}
	hld.build();

	for (i = 0; i < n; i++) {
		s2[i] = s[hld.inv[i]];
		c2[i] = c[hld.inv[i]];
		csum[i + 1] = csum[i] + c2[i];
	}
	/*
	DEBUG_VEC(hld.vid);
	DEBUG_VEC(hld.head);
	DEBUG_VEC(hld.par);
	//*/
	LazySegmentTree lst(s2);
	
	int q;
	cin >> q;
	while (q--) {
		int t;
		cin >> t;
		if (t == 0) {
			int u, v, z;
			cin >> u >> v >> z;
			u--; v--;
			while (true) {
				//cout << hld.vid[u] << " " << hld.vid[v] << endl;
				if (hld.vid[u] > hld.vid[v]) swap(u, v);
				lst.add(max(hld.vid[hld.head[v]], hld.vid[u]), hld.vid[v] + 1, z, 0, 0, hld.n);
				if (hld.head[u] != hld.head[v]) v = hld.par[hld.head[v]];
				else break;
			}
			// cout << endl;
		}
		else {
			int u, v;
			cin >> u >> v;
			u--; v--;
			ll ans = 0;
			while (true) {
				//cout << hld.vid[u] << " " << hld.vid[v] << endl;
				if (hld.vid[u] > hld.vid[v]) swap(u, v);
				ans += lst.getsum(max(hld.vid[hld.head[v]], hld.vid[u]), hld.vid[v] + 1, 0, 0, hld.n);
				if (hld.head[u] != hld.head[v]) v = hld.par[hld.head[v]];
				else break;
			}
			cout << ans << endl;
			//DEBUG(ans); cout << endl;
		}
	}
}
0