結果

問題 No.1094 木登り / Climbing tree
ユーザー FF256grhyFF256grhy
提出日時 2020-06-25 20:04:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 844 ms / 2,000 ms
コード長 3,695 bytes
コンパイル時間 2,590 ms
コンパイル使用メモリ 207,936 KB
実行使用メモリ 42,672 KB
最終ジャッジ日時 2024-04-25 18:29:09
合計ジャッジ時間 23,298 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 801 ms
25,052 KB
testcase_02 AC 229 ms
42,672 KB
testcase_03 AC 229 ms
6,940 KB
testcase_04 AC 215 ms
13,352 KB
testcase_05 AC 312 ms
23,256 KB
testcase_06 AC 475 ms
9,600 KB
testcase_07 AC 841 ms
25,032 KB
testcase_08 AC 834 ms
25,040 KB
testcase_09 AC 835 ms
24,976 KB
testcase_10 AC 834 ms
25,032 KB
testcase_11 AC 830 ms
24,916 KB
testcase_12 AC 814 ms
24,976 KB
testcase_13 AC 826 ms
25,048 KB
testcase_14 AC 830 ms
25,084 KB
testcase_15 AC 497 ms
12,288 KB
testcase_16 AC 656 ms
35,272 KB
testcase_17 AC 562 ms
21,464 KB
testcase_18 AC 532 ms
17,884 KB
testcase_19 AC 613 ms
27,136 KB
testcase_20 AC 834 ms
25,044 KB
testcase_21 AC 564 ms
22,576 KB
testcase_22 AC 844 ms
25,016 KB
testcase_23 AC 805 ms
25,016 KB
testcase_24 AC 805 ms
25,044 KB
testcase_25 AC 799 ms
25,136 KB
testcase_26 AC 795 ms
24,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
#define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__);
void IN_() { };
template<typename T, typename ... U> void IN_(T &  a, U &  ... b) { cin >> a; IN_(b ...); };
template<typename T                > void OUT(T && a            ) { cout << a << endl; }
template<typename T, typename ... U> void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); }

// ---- ----

template<typename T> class SegmentTree {
private:
	int n, s;
	vector<T> a;
	function<T(T &, T &)> f;
	T e;
	bool ok;
public:
	SegmentTree() { n = 0; }
	SegmentTree(int nn, function<T(T &, T &)> ff, T ee) { init(nn, ff, ee); }
	void init(int nn, function<T(T &, T &)> ff, T ee) {
		n = nn;
		f = ff;
		e = ee;
		s = 1;
		while(s < n) { s *= 2; }
		a = vector<T>(2 * s, e);
		ok = true;
	}
	void shift(int & p) {
		assert(inID(p, 0, n));
		p += s;
	}
	void apply(int p, function<void(T &)> g) {
		shift(p);
		g(a[p]);
		while(p > 1) {
			p /= 2;
			a[p] = f(a[2 * p], a[2 * p + 1]);
		}
	}
	T fold_ID(int l, int r) {
		assert(ok);
		assert(inII(l, 0, n)); l += s;
		assert(inII(r, 0, n)); r += s; r--;
		T x = e, y = e;
		while(l <= r) {
			if(l % 2 == 1) { x = f(x, a[l]); l++; }
			if(r % 2 == 0) { y = f(a[r], y); r--; }
			l /= 2;
			r /= 2;
		}
		return f(x, y);
	}
	T fold_II(int l, int r) { return fold_ID(l + 0, r + 1); }
	T fold_CI(int l, int r) { return fold_ID(l + 1, r + 1); }
	T fold_CD(int l, int r) { return fold_ID(l + 1, r + 0); }
	const T & operator[](int p) {
		shift(p);
		return a[p];
	}
	T & ref(int p) {
		shift(p);
		ok = false;
		return a[p];
	}
	void update() {
		dec(i, s) { a[i] = f(a[2 * i], a[2 * i + 1]); }
		ok = true;
	}
};
#define OP(s) [&](auto & A, auto & B) { return s; }
#define AP(s) [&](auto & A) { s; }

// ----

int main() {
	IN(int, n);
	vector<vector<pair<int, int>>> g(n);
	inc(i, n - 1) {
		IN(int, a, b, c);
		a--; b--;
		g[a].EB(b, c);
		g[b].EB(a, c);
	}
	
	vector<int> f(n), s(n);
	SegmentTree<pair<int, int>> st(2 * n - 1, OP(min(A, B)), { n, -1 });
	int c = 0;
	function<void(int, int, int, int)> dfs = [&](int v, int p, int d, int w) {
		f[v] = c;
		s[v] = w;
		st.ref(c++) = MP(d, v);
		RF(e, g[v]) {
			if(e.FI == p) { continue; }
			dfs(e.FI, v, d + 1, w + e.SE);
			st.ref(c++) = MP(d, v);
		}
	};
	dfs(0, -1, 0, 0);
	st.update();
	
	IN(int, q);
	inc(qq, q) {
		IN(int, a, b);
		a--; b--;
		if_not(f[a] <= f[b]) { swap(a, b); }
		int c = st.fold_II(f[a], f[b]).SE;
		OUT(s[a] + s[b] - 2 * s[c]);
	}
}
0