:heavy_check_mark: test/tree/euler_tour/aoj_grl_5_d.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/5/GRL_5_D

#include "src/datastructure/segment_tree.hpp"
#include "src/tree/euler_tour.hpp"

#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> p(n);
	EulerTour et(n);
	for (int i = 0; i < n; ++i) {
		int k;
		cin >> k;
		for (int j = 0; j < k; ++j) {
			int c;
			cin >> c;
			p[c] = i;
			et.add_edge(i, c);
		}
	}
	et.build();
	SegmentTree st([](int a, int b) { return a + b; }, 0);
	st.init(2 * n);

	int q;
	cin >> q;
	while (q--) {
		int c;
		cin >> c;
		if (c == 0) {
			int v, w;
			cin >> v >> w;
			auto update = [&](int i, int x) { st.update(i, st.at(i) + x); };
			et.update_edge(v, p[v], w, update);
		} else {
			int u;
			cin >> u;
			int res = 0;
			auto query = [&](int l, int r) { res += st.find(l, r); };
			et.query_edge(u, 0, query);
			cout << res << endl;
		}
	}

	return 0;
}
#line 1 "test/tree/euler_tour/aoj_grl_5_d.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/5/GRL_5_D

#line 1 "src/datastructure/segment_tree.hpp"



#include <vector>

template<class T, class F>
class SegmentTree { // 0-indexed
private:
	int n_{};
	std::vector<T> tree;
	F f; // function<T(T, T)>
	T ti;

public:
	SegmentTree(F f, T ti) : f(f), ti(ti) {}

	void init(int n) {
		n_ = 1;
		while (n_ < n) n_ *= 2;
		tree.assign(2 * n_, ti);
	}

	void build(std::vector<T> const &v) {
		int const N = v.size();
		init(N);
		for (int i = 0; i < N; ++i) tree[n_ + i] = v[i];
		for (int i = n_ - 1; i > 0; --i) tree[i] = f(tree[2 * i], tree[2 * i + 1]);
	}

	void update(int i, T const &x) {
		i += n_;
		tree[i] = x;
		while (i >>= 1) tree[i] = f(tree[2 * i], tree[2 * i + 1]);
	}

	T find(int l, int r) { // [l, r)
		l += n_, r += n_;
		T ll = ti, rr = ti;
		while (l < r) {
			if (l & 1) ll = f(ll, tree[l++]);
			if (r & 1) rr = f(tree[--r], rr);
			l >>= 1, r >>= 1;
		}
		return f(ll, rr);
	}

	T at(int i) { return tree[i + n_]; }
};


#line 1 "src/tree/euler_tour.hpp"



#line 5 "src/tree/euler_tour.hpp"

#include <algorithm>
#include <utility>
#line 9 "src/tree/euler_tour.hpp"

class EulerTour {
private:
	static std::pair<int, int> min(std::pair<int, int> &a, std::pair<int, int> &b) {
		return std::min(a, b);
	}

	std::vector<int> down, up, depth, terminal;
	SegmentTree<std::pair<int, int>, decltype(&min)> st;
	std::vector<std::vector<int>> G;

	void dfs(int v, int p, int d) {
		depth[terminal.size()] = d;
		down[v] = terminal.size();
		terminal.emplace_back(v);
		for (int u : G[v]) {
			if (u == p) continue;
			dfs(u, v, d + 1);
		}
		depth[terminal.size()] = d - 1;
		up[v] = terminal.size();
		terminal.emplace_back(p);
	}

public:
	EulerTour(int n) : down(n), up(n), depth(n << 1), st(min, {1 << 30, -1}), G(n) {}

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

	void build(int root = 0) {
		terminal.clear();
		dfs(root, -1, 0);
		std::vector<std::pair<int, int>> dep(terminal.size());
		for (int i = 0; i < (int)terminal.size(); ++i) dep[i] = {depth[i], terminal[i]};
		st.build(dep);
	}

	std::pair<int, int> index(int v) { return {down[v], up[v]}; };

	int parent(int u, int v) { return depth[down[u]] < depth[down[v]] ? u : v; }

	int child(int u, int v) { return depth[down[u]] < depth[down[v]] ? v : u; }

	int lca(int u, int v) {
		if (down[u] > down[v]) std::swap(u, v);
		return st.find(down[u], down[v] + 1).second;
	}

	template<class F>
	void query_vertex(int u, int v, F const &f) {
		int a = lca(u, v);
		f(down[a], down[u] + 1);
		f(down[a] + 1, down[v] + 1);
	}

	template<class F>
	void query_edge(int u, int v, F const &f) {
		int a = lca(u, v);
		f(down[a] + 1, down[u] + 1);
		f(down[a] + 1, down[v] + 1);
	}

	template<class T, class F>
	void update_vertex(int v, T x, F const &f) {
		f(down[v], x);
		f(up[v], -x);
	}

	template<class T, class F>
	void update_edge(int u, int v, T x, F const &f) {
		update_vertex(child(u, v), x, f);
	}
};


#line 5 "test/tree/euler_tour/aoj_grl_5_d.test.cpp"

#include <iostream>
#line 8 "test/tree/euler_tour/aoj_grl_5_d.test.cpp"

using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> p(n);
	EulerTour et(n);
	for (int i = 0; i < n; ++i) {
		int k;
		cin >> k;
		for (int j = 0; j < k; ++j) {
			int c;
			cin >> c;
			p[c] = i;
			et.add_edge(i, c);
		}
	}
	et.build();
	SegmentTree st([](int a, int b) { return a + b; }, 0);
	st.init(2 * n);

	int q;
	cin >> q;
	while (q--) {
		int c;
		cin >> c;
		if (c == 0) {
			int v, w;
			cin >> v >> w;
			auto update = [&](int i, int x) { st.update(i, st.at(i) + x); };
			et.update_edge(v, p[v], w, update);
		} else {
			int u;
			cin >> u;
			int res = 0;
			auto query = [&](int l, int r) { res += st.find(l, r); };
			et.query_edge(u, 0, query);
			cout << res << endl;
		}
	}

	return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample_00.in :heavy_check_mark: AC 14 ms 8 MB
g++ 00_sample_01.in :heavy_check_mark: AC 14 ms 7 MB
g++ 00_sample_02.in :heavy_check_mark: AC 13 ms 7 MB
g++ 01_small_00.in :heavy_check_mark: AC 14 ms 8 MB
g++ 01_small_01.in :heavy_check_mark: AC 13 ms 8 MB
g++ 02_medium_00.in :heavy_check_mark: AC 14 ms 8 MB
g++ 03_corner_00.in :heavy_check_mark: AC 13 ms 7 MB
g++ 03_corner_01.in :heavy_check_mark: AC 13 ms 7 MB
g++ 03_corner_02.in :heavy_check_mark: AC 14 ms 7 MB
g++ 04_rand_00.in :heavy_check_mark: AC 13 ms 7 MB
g++ 04_rand_01.in :heavy_check_mark: AC 13 ms 8 MB
g++ 04_rand_02.in :heavy_check_mark: AC 14 ms 8 MB
g++ 04_rand_03.in :heavy_check_mark: AC 14 ms 8 MB
g++ 04_rand_04.in :heavy_check_mark: AC 15 ms 8 MB
g++ 04_rand_05.in :heavy_check_mark: AC 17 ms 8 MB
g++ 04_rand_06.in :heavy_check_mark: AC 20 ms 8 MB
g++ 04_rand_07.in :heavy_check_mark: AC 38 ms 8 MB
g++ 05_grape_100.in :heavy_check_mark: AC 14 ms 8 MB
g++ 05_grape_1000.in :heavy_check_mark: AC 16 ms 8 MB
g++ 05_grape_100000.in :heavy_check_mark: AC 465 ms 45 MB
g++ 05_grape_50000.in :heavy_check_mark: AC 368 ms 26 MB
g++ 06_linear_10000.in :heavy_check_mark: AC 51 ms 13 MB
g++ 06_linear_100000.in :heavy_check_mark: AC 490 ms 57 MB
g++ 07_stair_100.in :heavy_check_mark: AC 17 ms 8 MB
g++ 07_stair_1000.in :heavy_check_mark: AC 39 ms 8 MB
g++ 07_stair_10000.in :heavy_check_mark: AC 503 ms 10 MB
g++ 07_stair_100000.in :heavy_check_mark: AC 495 ms 32 MB
g++ 08_star_100000_1.in :heavy_check_mark: AC 453 ms 57 MB
g++ 08_star_100000_5.in :heavy_check_mark: AC 416 ms 32 MB
g++ 08_star_10000_1.in :heavy_check_mark: AC 233 ms 13 MB
g++ 08_star_10000_4.in :heavy_check_mark: AC 153 ms 10 MB
g++ 09_nstar_100000_100_10.in :heavy_check_mark: AC 462 ms 32 MB
g++ 09_nstar_100000_10_100.in :heavy_check_mark: AC 437 ms 32 MB
g++ 09_nstar_10000_10_10.in :heavy_check_mark: AC 281 ms 10 MB
g++ 09_nstar_10000_20_5.in :heavy_check_mark: AC 308 ms 10 MB
g++ 10_extreme_00.in :heavy_check_mark: AC 612 ms 57 MB
clang++ 00_sample_00.in :heavy_check_mark: AC 13 ms 15 MB
clang++ 00_sample_01.in :heavy_check_mark: AC 12 ms 11 MB
clang++ 00_sample_02.in :heavy_check_mark: AC 12 ms 15 MB
clang++ 01_small_00.in :heavy_check_mark: AC 13 ms 11 MB
clang++ 01_small_01.in :heavy_check_mark: AC 17 ms 14 MB
clang++ 02_medium_00.in :heavy_check_mark: AC 17 ms 8 MB
clang++ 03_corner_00.in :heavy_check_mark: AC 13 ms 15 MB
clang++ 03_corner_01.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 03_corner_02.in :heavy_check_mark: AC 13 ms 15 MB
clang++ 04_rand_00.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 04_rand_01.in :heavy_check_mark: AC 13 ms 13 MB
clang++ 04_rand_02.in :heavy_check_mark: AC 13 ms 13 MB
clang++ 04_rand_03.in :heavy_check_mark: AC 13 ms 8 MB
clang++ 04_rand_04.in :heavy_check_mark: AC 14 ms 12 MB
clang++ 04_rand_05.in :heavy_check_mark: AC 24 ms 8 MB
clang++ 04_rand_06.in :heavy_check_mark: AC 23 ms 10 MB
clang++ 04_rand_07.in :heavy_check_mark: AC 40 ms 16 MB
clang++ 05_grape_100.in :heavy_check_mark: AC 13 ms 11 MB
clang++ 05_grape_1000.in :heavy_check_mark: AC 16 ms 12 MB
clang++ 05_grape_100000.in :heavy_check_mark: AC 697 ms 52 MB
clang++ 05_grape_50000.in :heavy_check_mark: AC 435 ms 34 MB
clang++ 06_linear_10000.in :heavy_check_mark: AC 42 ms 19 MB
clang++ 06_linear_100000.in :heavy_check_mark: AC 454 ms 66 MB
clang++ 07_stair_100.in :heavy_check_mark: AC 14 ms 12 MB
clang++ 07_stair_1000.in :heavy_check_mark: AC 28 ms 10 MB
clang++ 07_stair_10000.in :heavy_check_mark: AC 321 ms 12 MB
clang++ 07_stair_100000.in :heavy_check_mark: AC 437 ms 34 MB
clang++ 08_star_100000_1.in :heavy_check_mark: AC 475 ms 70 MB
clang++ 08_star_100000_5.in :heavy_check_mark: AC 494 ms 38 MB
clang++ 08_star_10000_1.in :heavy_check_mark: AC 162 ms 13 MB
clang++ 08_star_10000_4.in :heavy_check_mark: AC 206 ms 18 MB
clang++ 09_nstar_100000_100_10.in :heavy_check_mark: AC 431 ms 38 MB
clang++ 09_nstar_100000_10_100.in :heavy_check_mark: AC 462 ms 36 MB
clang++ 09_nstar_10000_10_10.in :heavy_check_mark: AC 360 ms 14 MB
clang++ 09_nstar_10000_20_5.in :heavy_check_mark: AC 340 ms 12 MB
clang++ 10_extreme_00.in :heavy_check_mark: AC 505 ms 68 MB
Back to top page