結果

問題 No.900 aδδitivee
ユーザー 👑 jupirojupiro
提出日時 2020-03-06 01:26:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 477 ms / 2,000 ms
コード長 4,917 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 143,328 KB
実行使用メモリ 36,444 KB
最終ジャッジ日時 2023-08-04 05:23:06
合計ジャッジ時間 16,074 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,880 KB
testcase_01 AC 3 ms
5,796 KB
testcase_02 AC 3 ms
5,936 KB
testcase_03 AC 4 ms
5,912 KB
testcase_04 AC 3 ms
5,844 KB
testcase_05 AC 3 ms
5,864 KB
testcase_06 AC 3 ms
5,844 KB
testcase_07 AC 431 ms
31,528 KB
testcase_08 AC 424 ms
31,444 KB
testcase_09 AC 452 ms
31,712 KB
testcase_10 AC 429 ms
31,460 KB
testcase_11 AC 425 ms
31,648 KB
testcase_12 AC 426 ms
31,480 KB
testcase_13 AC 440 ms
31,472 KB
testcase_14 AC 423 ms
31,472 KB
testcase_15 AC 426 ms
31,476 KB
testcase_16 AC 435 ms
31,532 KB
testcase_17 AC 426 ms
31,540 KB
testcase_18 AC 425 ms
31,552 KB
testcase_19 AC 445 ms
31,540 KB
testcase_20 AC 436 ms
31,396 KB
testcase_21 AC 426 ms
31,632 KB
testcase_22 AC 474 ms
36,444 KB
testcase_23 AC 469 ms
36,296 KB
testcase_24 AC 477 ms
36,208 KB
testcase_25 AC 469 ms
36,196 KB
testcase_26 AC 464 ms
36,296 KB
testcase_27 AC 466 ms
36,204 KB
testcase_28 AC 459 ms
36,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

vector<pair<int, ll>> g[100001];

void dfs(int cur, int pre, vector<pair<int, ll>> g[], vector<pair<int, int>>& eul, vector<int>& dep, int& idx) {
	for (auto next : g[cur]) {
		if (next.first == pre) continue;
		eul[next.first].first = idx++;
		dep[next.first] = dep[cur] + 1;
		dfs(next.first, cur, g, eul, dep, idx);
		eul[next.first].second = idx++;
	}
}

template <typename T, typename E>
struct lazySegTree {
	using F = function<T(T, T)>;
	using G = function<T(T, E)>;
	using H = function<E(E, E)>;
	using P = function<E(E, int)>;
	F f; G g; H h; P p; T d1; E d0;
	int n;
	vector<T> dat;
	vector<E> lazy;

	lazySegTree() {}
	lazySegTree(int n_, F f_, G g_, H h_,
		T d1_, E d0_, P p_ = [](E a, int b) {return a; })
		: f(f_), g(g_), h(h_), p(p_), d1(d1_), d0(d0_)
	{
		n = 1; while (n < n_) n *= 2;
		dat.assign(n * 2 - 1, d1);
		lazy.assign(n * 2 - 1, d0);
	}
	void build(vector<T> v) {
		for (ll i = 0; i < v.size(); i++) dat[i + n - 1] = v[i];
		for (int i = n - 2; i >= 0; --i) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
	}

	// 区間の幅がlenの節点kについて遅延評価
	inline void eval(int len, int k) {
		if (lazy[k] == d0) return;
		if (k * 2 + 1 < n * 2 - 1) {
			lazy[2 * k + 1] = h(lazy[k * 2 + 1], lazy[k]);
			lazy[2 * k + 2] = h(lazy[k * 2 + 2], lazy[k]);
		}
		dat[k] = g(dat[k], p(lazy[k], len));
		lazy[k] = d0;
	}
	// [a, b)
	T update(int a, int b, E x, int k, int l, int r) {
		eval(r - l, k);
		if (b <= l || r <= a) return dat[k];
		if (a <= l && r <= b) {
			lazy[k] = h(lazy[k], x);
			return g(dat[k], p(lazy[k], r - l));
		}
		return dat[k] = f(update(a, b, x, 2 * k + 1, l, (l + r) / 2),
			update(a, b, x, 2 * k + 2, (l + r) / 2, r));
	}
	T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); }
	// [a, b)
	T query(int a, int b, int k, int l, int r) {
		eval(r - l, k);
		if (a <= l && r <= b) return dat[k];
		bool left = !((l + r) / 2 <= a || b <= l), right = !(r <= 1 || b <= (l + r) / 2);
		if (left && right) return f(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
		if (left) return query(a, b, 2 * k + 1, l, (l + r) / 2);
		return query(a, b, 2 * k + 2, (l + r) / 2, r);
	}
	T query(int a, int b) { return query(a, b, 0, 0, n); }

};
/**
* 区間更新区間max d1=d0=INT_MAX f=max(a,b) g=h=(b==INT_MAX?a:b)\n
* 区間加算区間和  d1=d0=0 f=g=h=a+b p=a*b\n
* 区間加算区間min d1=d0=0 f=min(a,b) g=h=a+b\n
* 区間更新区間和  d1=d0=0 f=a+b g=h=(b==0?a:b) p=a*b\n
* 区間xor区間和   d1=d0=0 f=a+b g=(b>=1?b-a:a) h=a^b p=a*b
*/

pair<ll, ll> f1(pair<ll, ll> a, pair<ll, ll> b) {
	return { a.first + b.first,a.second + b.second };
}

pair<ll, ll> f2(pair<ll, ll> a, ll b) {
	return { a.first + b * a.second, a.second };
}

ll f3(ll a, ll b) {
	return a + b;
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	int n; scanf("%d", &n);
	vector<tuple<int, int, int>> vt(n - 1);
	for (int i = 0; i < n - 1; i++) {
		int u, v, w; scanf("%d %d %d", &u, &v, &w);
		vt[i] = make_tuple(u, v, w);
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	vector<pair<int, int>> eul(n);
	vector<int> dep(n);
	{
		int idx = 1;
		dfs(0, -1, g, eul, dep, idx);
		eul[0].second = idx;
	}
	lazySegTree<pair<ll, ll>, ll> lsg(n * 2 + 1, f1, f2, f3, { 0, 0 }, 0);
	vector<pair<ll, ll>> vp(n * 2 + 1);
	for (int i = 0; i < n; i++) {
		vp[eul[i].first].second = 1;
		vp[eul[i].second ].second = -1;
	}
	lsg.build(vp);
	for (int i = 0; i < n - 1; i++) {
		int u, v, w; tie(u, v, w) = vt[i];
		if (dep[u] > dep[v]) swap(u, v);
		lsg.update(eul[v].first, eul[v].first + 1, w);
		lsg.update(eul[v].second, eul[v].second + 1, w);
	}
	int q; scanf("%d", &q);
	while (q--) {
		int t; scanf("%d", &t);
		if (t == 1) {
			int a, x; scanf("%d %d", &a, &x);
			lsg.update(eul[a].first + 1, eul[a].second, x);
		}
		else {
			int b; scanf("%d", &b);
			printf("%lld\n", lsg.query(0, eul[b].first + 1).first);
		}
	}
	return 0;
	/*
		おまじないを使ったらscanfとprintf関連注意!!!!!!!!!!!!
	*/
}
0