Language: C++

#include <algorithm>
#include <array>
#include <bitset>
#include <cstddef>
#include <tuple>
#include <utility>
#include <vector>

template <std::size_t BitLength, class Abelian> struct foldable_wavelet_matrix {
	using mapped_structure = Abelian;
	using mapped_type = typename mapped_structure::value_type;
	using key_type = std::bitset<BitLength>;

	struct bitvector {
		struct node_type;
		using container_type = std::vector<node_type>;
		using size_type = typename container_type::size_type;
		struct node_type {
			size_type count;
			mapped_type sum;
			node_type()
				: count(static_cast<size_type>(0)),
				sum(mapped_structure::identity()) {}
		};

		container_type vec;

		bitvector() : vec() {}

		void resize(const size_type size) {
			vec.resize(size + static_cast<size_type>(1));
		}
		void set(const size_type index) {
			vec[index].count = static_cast<size_type>(1);
		}
		template <class InputIterator>
		void build(const std::vector<InputIterator> &a) {
			for (size_type i = a.size(); i != static_cast<size_type>(0);) {
				--i;
				vec[i].count += vec[i + static_cast<size_type>(1)].count;
				vec[i].sum = mapped_structure::operation(
					a[i]->second, vec[i + static_cast<size_type>(1)].sum);
			}
		}

		size_type count(const size_type index) const { return vec[index].count; }
		size_type zeros() const { return vec.front().count; }
		mapped_type fold(const size_type first, const size_type last) const {
			return mapped_structure::operation(
				vec[first].sum, mapped_structure::inverse(vec[last].sum));
		}
	};

	using size_type = typename bitvector::size_type;

	std::array<bitvector, BitLength> matrix;

	foldable_wavelet_matrix():matrix(){}

	template <class InputIterator>
	explicit foldable_wavelet_matrix(InputIterator first,
		const InputIterator last) {
		using iterator = InputIterator;

		const size_type size = static_cast<size_type>(std::distance(first, last));
		std::vector<iterator> cur, pre;
		cur.reserve(size);
		for (; first != last; ++first) {
			cur.push_back(first);
		}
		pre = cur;
		std::size_t i = BitLength;
		while (i != static_cast<std::size_t>(0)) {
			--i;
			bitvector &vec = matrix[i];
			vec.resize(size);
			std::swap(cur, pre);
			typename std::vector<iterator>::iterator zero_itr = cur.begin(),
				one_itr = cur.end();
			for (size_type k = static_cast<size_type>(0); zero_itr != one_itr; ++k) {
				if (pre[k]->first.test(i)) {
					--one_itr;
					*one_itr = pre[k];
				}
				else {
					vec.set(k);
					*zero_itr = pre[k];
					++zero_itr;
				}
			}
			std::reverse(one_itr, cur.end());
			vec.build(cur);
		}
	}

	std::tuple<mapped_type, mapped_type, mapped_type>
		fold_leg(size_type first, size_type last, const key_type &key) const {
		mapped_type less = mapped_structure::identity(),
			greater = mapped_structure::identity();
		std::size_t i = BitLength;
		while (i != static_cast<std::size_t>(0)) {
			--i;
			const bitvector &vec = matrix[i];
			const size_type f = vec.count(first), l = vec.count(last),
				z = vec.zeros();
			if (key.test(i)) {
				less = mapped_structure::operation(std::move(less),
					vec.fold(z - f, z - l));
				first += f;
				last += l;
			}
			else {
				greater = mapped_structure::operation(vec.fold(first + f, last + l),
					std::move(greater));
				first = z - f;
				last = z - l;
			}
		}
		return std::forward_as_tuple(
			std::move(less), matrix.front().fold(first, last), std::move(greater));
	}

	std::pair<mapped_type, mapped_type>
		fold_quantile(size_type first, size_type last, size_type k) const {
		mapped_type less = mapped_structure::identity(),
			greater = mapped_structure::identity();
		std::size_t i = BitLength;
		while (i != static_cast<std::size_t>(0)) {
			--i;
			const bitvector &vec = matrix[i];
			const size_type f = vec.count(first), l = vec.count(last),
				z = vec.zeros();
			if (f - l <= k) {
				k -= f - l;
				less = mapped_structure::operation(std::move(less),
					vec.fold(z - f, z - l));
				first += f;
				last += l;
			}
			else {
				greater = mapped_structure::operation(vec.fold(first + f, last + l),
					std::move(greater));
				first = z - f;
				last = z - l;
			}
		}
		return std::make_pair(
			mapped_structure::operation(std::move(less),
				matrix.front().fold(first, first + k)),
			mapped_structure::operation(matrix.front().fold(first + k, last),
				std::move(greater)));
	}
};

template <class T> class plus_abelian {
public:
	using value_type = T;
	static value_type operation(const value_type &x, const value_type &y) {
		return x + y;
	}
	static value_type identity() { return static_cast<value_type>(0); }
	static value_type inverse(const value_type &x) { return -x; }
	static value_type reverse(const value_type &x) { return x; }
};

#include <algorithm>
#include <bitset>
#include <cstdint>
#include <iostream>
#include <limits>
#include <utility>
#include <vector>

#include<cstdio>

int main() {
	using i64 = std::int_fast64_t;
	static constexpr int BitLen = 30;

	int n;
	std::cin >> n;
	std::vector<i64> a(3 * n);
	for (auto &e : a) {
		scanf("%lld", &e);
	}

	foldable_wavelet_matrix<BitLen, plus_abelian<i64>> fwm;
	{
		std::vector<std::pair<std::bitset<BitLen>, i64>> b(3 * n);
		for (int i = 0; i < 3 * n; ++i) {
			b[i].first = a[i];
			b[i].second = a[i];
		}
		fwm = foldable_wavelet_matrix<BitLen, plus_abelian<i64>>(b.cbegin(),
			b.cend());
	}

	i64 max = std::numeric_limits<i64>::min();

	for (int i = n; i <= 2 * n; ++i) {
		max = std::max(max, fwm.fold_quantile(0, i, i - n).second -
			fwm.fold_quantile(i, 3 * n, n).first);
	}

	printf("%lld\n", ans);

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:205:26: error: 'ans' was not declared in this scope; did you mean 'abs'?
  205 |         printf("%lld\n", ans);
      |                          ^~~
      |                          abs