Language: C++

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

#ifndef INCLUDED_SEGMENT_TREE_HPP
#define INCLUDED_SEGMENT_TREE_HPP

#include <cstdint>
#include <vector>
#include <functional>

template<class Monoid>
class segment_tree {
public:
	using T = typename Monoid::value_type;
	using size_type = std::uint_fast32_t;

	using updator = std::function<T(T)>;
	using checker = std::function<bool(T)>;
	
private:
	const size_type size_;
	size_type height_;

public:
	std::vector<T> data;
	
private:
	const size_type get_height(size_type size) const {
		size_type height = 1;
		while(1 << height <= size) height++;
		return height;
	}
	const size_type base_size() const { return 1 << height_; }

	void meld(size_type index) {
		data[index] = Monoid::operation(data[index << 1 ^ 0], data[index << 1 ^ 1]);
	}

public:
	segment_tree() = default;
	segment_tree(segment_tree&&) = default;
	segment_tree(const segment_tree&) = default;
	
	segment_tree(size_type size)
		: size_(size) {
		height_ = get_height(size);
		data.assign(base_size() << 1, T{});
	}
	T fold(size_type left, size_type right) {
		T l_value = T{};
		T r_value = T{};

		for(left += base_size(), right += base_size();
			left < right;
			left >>= 1, right >>= 1) {
			if(left & 1)  l_value = Monoid::operation(l_value, data[left++]);
			if(right & 1) r_value = Monoid::operation(data[--right], r_value); 
		}
		return Monoid::operation(std::move(l_value), std::move(r_value));
	}

	void update(size_type index, const updator& update) {
		index += base_size();
		data[index] = update(data[index]);

		while(index >>= 1) meld(index);
	}
	void update(size_type index, const T& value) { update(index, [&value](const T& x) { return Monoid::operation(x, value); }); }
	void change(size_type index, const T& value) { update(index, [&value](const T& x) { return value; }); }
	
	const size_type search(size_type left, const checker& check) {
		T val = T{};
		size_type k = left + base_size();
		while(true) {
			if(check(Monoid::operation(val, data[k]))) {
				val = Monoid::operation(val, data[k]);
				if(k & 1) {
					if((k + 1) & k) k = (k + 1) >> 1;
					else return size();
				} else {
					k = k + 1;
				}
			} else {
				if(k < base_size()) {
					k = k << 1 ^ 0;
				} else {
					return k - base_size();
				}
			}
		}
	}
	
	const T operator[](size_type index) const { return data[index + base_size()]; }
	const size_type size() const { return size_; }
	const bool empty() const { return data.empty(); }
};

template<class T>
struct monoid {
	using value_type = T;

	static value_type operation(const value_type& a, const value_type& b) { return a + b; };
};

// @docs docs/segment_tree.md

#endif

struct max {
	int x;
	
	max(int x = 0): x(x) {}
	max operator+(max r) const { return max(std::max(x, r.x)); }
}

int main() {
	int n; scanf("%d", &n); std::vector<int> v(n), w(n);
	for(int i = 0; i < n; i++) scanf("%d%d", &v[i], &w[i]);

	int q; scanf("%d", &q); std::vector<int> a(q), b(q);
	for(int i = 0; i < q; i++) scanf("%d%d", &a[i], &b[i]);

	std::vector<std::vector<int>> query(n);
	for(int i = 0; i < q; i++) query[--a[i]].push_back(i);

	segment_tree<monoid<max>> seg(n);

	std::vector<int> ans(q, -1);
	std::vector<std::pair<int, int>> vec(1, {0, 0});
	auto dfs = [&](auto&& dfs, int now, int par) -> void {
		if(now >= n) return;
		std::vector<std::pair<int, int>> nxt(vec);
		std::vector<std::pair<int, int>> back;
		for(auto e: vec) if(e.first + w[now] <= (int)1e5) {
			nxt.push_back({e.first + w[now], e.second + v[now]});

			back.push_back({e.first + w[now], seg[e.first + w[now]});
			seg.change(e.first + w[now], e.second + v[now]);
		}
		
		nxt.swap(vec);
		for(int id: {2 * now + 1, 2 * now + 2}) {
			int x = v[id], y = w[id], to = id;
			if(to == par) continue;
			
			dfs(dfs, to, now);
		}
		vec.swap(nxt);

		for(auto e: back) seg.change(e.first, e.second);

		for(auto id: query[now]) ans[id] = seg.fold(0, b[id] + 1).x;
	};
	dfs(dfs, 0, -1);
	
	for(auto v: ans) cout << v << " "; cout << endl;
	return 0;
}


Compiler output:
a.cc:127:2: error: expected ';' after struct definition
  127 | }
      |  ^
      |  ;
a.cc: In lambda function:
a.cc:150:79: error: expected ']' before '}' token
  150 |                         back.push_back({e.first + w[now], seg[e.first + w[now]});
      |                                                                               ^
      |                                                                               ]
a.cc:150:39: error: no matching function for call to 'std::vector<std::pair<int, int> >::push_back(<brace-enclosed initializer list>)'
  150 |                         back.push_back({e.first + w[now], seg[e.first + w[now]});
      |                         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; value_type = std::pair<int, int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::vector<std::pair<int, int> >::value_type&' {aka 'const std::pair<int, int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, int>; _Alloc = std::allocator<std::pair<int, int> >; value_type = std::pair<int, int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::vector<std::pair<int, int> >::value_type&&' {aka 'std::pair<int, int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~