結果

問題 No.900 aδδitivee
ユーザー QCFiumQCFium
提出日時 2019-10-05 13:43:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,875 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 176,716 KB
実行使用メモリ 812,480 KB
最終ジャッジ日時 2024-04-15 17:34:40
合計ジャッジ時間 7,474 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 183 ms
21,544 KB
testcase_09 WA -
testcase_10 AC 173 ms
21,548 KB
testcase_11 AC 184 ms
21,544 KB
testcase_12 AC 176 ms
21,540 KB
testcase_13 AC 178 ms
21,480 KB
testcase_14 WA -
testcase_15 AC 175 ms
21,668 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 MLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

std::vector<std::vector<std::pair<int, int> > > hen;
std::vector<int64_t> toured;
std::vector<int64_t> dist;
std::vector<int64_t> depth;

void dfs(int i) {
	toured.push_back(i);
	for (auto j : hen[i]) {
		if (!j.first || dist[j.first]) continue;
		dist[j.first] = dist[i] + j.second;
		depth[j.first] = depth[i] + 1;
		dfs(j.first);
		toured.push_back(i);
	}
}

struct SegTree {
	int n;
	std::vector<std::pair<int64_t, int64_t> > data; // depth * first + second
	SegTree(int n_) {
		for (n = 1; n < n_; n <<= 1);
		data.resize(2 * n);
	}
	void add(int l, int r, int64_t first, int64_t second) {
		for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if (r & 1) data[--r].first += first, data[r].second += second;
			if (l & 1) data[l].first += first, data[l++].second += second;
		}
	}
	std::pair<int64_t, int64_t> get(int i) {
		std::pair<int64_t, int64_t> res = {0, 0};
		for (i += n; i; i >>= 1) res.first += data[i].first, res.second += data[i].second;
		return res;
	}
};

int main() {
	int n = ri();
	hen.resize(n);
	for (int i = 0; i + 1 < n; i++) {
		int a = ri();
		int b = ri();
		int c = ri();
		hen[a].push_back({b, c});
		hen[b].push_back({a, c});
	}
	dist.resize(n);
	depth.resize(n);
	dfs(0);
	std::vector<std::pair<int, int> > lr(n, {1000000000, -1000000000});
	for (int i = 0; i < (int) toured.size(); i++) {
		int j = toured[i];
		lr[j].first = std::min(lr[j].first, i);
		lr[j].second = std::max(lr[j].second, i);
	}
	SegTree tree(toured.size());
	int q = ri();
	for (int i = 0; i < q; i++) {
		if (ri() == 1) {
			int a = ri();
			int x = ri();
			tree.add(lr[a].first, lr[a].second + 1, x, (int64_t) -x * depth[a]);
		} else {
			int b = ri();
			auto res = tree.get(lr[b].first);
			std::cout << dist[b] + depth[b] * res.first + res.second << std::endl;
		}
	}
	
	
	return 0;
}
0