Submission #39110114


Source Code Expand

#line 2 "/home/zawatin/compro/library/src/dataStructure/monotone_CHT.hpp"

#include <deque>
#include <cassert>

namespace zawa {

template <class T, bool min>
class monotone_CHT {
private:
	struct line {
		T coeff, intercept;
		line(T _coeff, T _intercept) : coeff(_coeff), intercept(_intercept) {}
		T map(T x) {
			return coeff * x + intercept;
		}
	};

	std::deque<line> dat;

	bool is_need(const line& l1, const line& l2, const line& l3) {
		assert(l1.coeff >= l2.coeff and l2.coeff >= l3.coeff);
		return (l1.coeff - l2.coeff) * (l2.intercept - l3.intercept) < (l2.coeff - l3.coeff) * (l1.intercept - l2.intercept);
	}

	bool manage_front(const line& l) {
		if (l.coeff == dat.front().coeff and l.intercept >= dat.front().intercept) {
			return false;
		}
		while (size() >= 2 and !is_need(l, dat.front(), dat[1])) {
			dat.pop_front();
		}
		return true;
	}

	bool manage_back(const line& l) {
		if (l.coeff == dat.back().coeff and l.intercept >= dat.back().intercept) {
			return false;
		}
		while (size() >= 2 and !is_need(dat[size() - 2], dat.back(), l)) {
			dat.pop_back();
		}
		return true;
	}

	void add(const line& l) {
		if (empty()) {
			dat.push_back(l);
			return;
		}
		if (l.coeff >= dat.front().coeff) {
			if (manage_front(l)) {
				dat.push_front(l);
			}
		}
		else if (dat.back().coeff >= l.coeff) {
			if (manage_back(l)) {
				dat.push_back(l);
			}
		}
		else {
			assert(false);
		}
	}

public:

	monotone_CHT() {}

	inline bool empty() {
		return dat.empty();
	}

	inline std::size_t size() {
		return dat.size();
	}

	inline std::deque<line> _dat() {
		return dat;
	}

	void add(T coeff, T intercept) {
		if (!min) {
			coeff *= -1;
			intercept *= -1;
		}
		add(line(coeff, intercept));
	}

	T incremental_query(T x) {
		assert(!empty());
		while (dat.size() >= 2 and dat.front().map(x) >= dat[1].map(x)) {
			dat.pop_front();
		}
		return (min ? 1 : -1) * dat.front().map(x);
	}

	T decremental_query(T x) {
		assert(!empty());
		while (dat.size() >= 2 and dat.back().map(x) >= dat[size() - 2].map(x)) {
			dat.pop_back();
		}
		return (min ? 1 : -1) * dat.back().map(x);
	}

	T query(T x) {
		assert(!empty());
		int left = -1, right = size() - 1;
		while (right - left > 1) {
			int mid = (left + right) >> 1;
			if (dat[mid].map(x) >= dat[mid + 1].map(x)) {
				left = mid;
			}
			else {
				right = mid;
			}
		}
		return (min ? 1 : -1) * dat[right].map(x);
	}
	
};

} // namespace zawa
#line 2 "ABC289-G.test.cpp"

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

int main() {
	int N, M; std::cin >> N >> M;
	std::vector B(N, 0); 
	for (auto& b : B) {
		std::cin >> b;
	}
	std::sort(B.begin(), B.end(), std::greater<int>());
	std::vector C(M, 0);
	for (auto& c : C) {
		std::cin >> c;
	}
	zawa::monotone_CHT<long long, false> cht;
	for (int i = 1 ; i <= N ; i++) {
		cht.add(i, (long long)i * B[i - 1]);
	}
	std::vector X(M, std::pair(0, 0));
	for (int i = 0 ; i < M ; i++) {
		X[i] = { C[i], i };
	}
	std::sort(X.begin(), X.end());
	std::vector A(M, 0LL);
	for (auto [x, index] : X) {
		A[index] = cht.incremental_query(x);
	}
	for (int i = 0 ; i < M ; i++) {
		std::cout << A[i] << (i + 1 == M ? '\n' : ' ');
	}
}

Submission Info

Submission Time
Task G - Shopping in AtCoder store
User zawatin
Language C++ (GCC 9.2.1)
Score 600
Code Size 3345 Byte
Status AC
Exec Time 164 ms
Memory 11468 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 37
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 02_handmade_23.txt, 02_handmade_24.txt, 02_handmade_25.txt, 02_handmade_26.txt, 02_handmade_27.txt, 02_handmade_28.txt, 02_handmade_29.txt, 02_handmade_30.txt, 02_handmade_31.txt, 02_handmade_32.txt, 02_handmade_33.txt, 02_handmade_34.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 4 ms 3480 KiB
00_sample_01.txt AC 2 ms 3600 KiB
00_sample_02.txt AC 2 ms 3548 KiB
00_sample_03.txt AC 3 ms 3556 KiB
01_random_03.txt AC 163 ms 8272 KiB
01_random_04.txt AC 162 ms 8268 KiB
01_random_05.txt AC 160 ms 8268 KiB
01_random_06.txt AC 162 ms 8164 KiB
01_random_07.txt AC 162 ms 8272 KiB
01_random_08.txt AC 164 ms 8184 KiB
01_random_09.txt AC 160 ms 8264 KiB
01_random_10.txt AC 162 ms 8268 KiB
01_random_11.txt AC 160 ms 8264 KiB
01_random_12.txt AC 160 ms 8196 KiB
01_random_13.txt AC 162 ms 8268 KiB
01_random_14.txt AC 51 ms 3656 KiB
01_random_15.txt AC 73 ms 5012 KiB
01_random_16.txt AC 98 ms 5660 KiB
01_random_17.txt AC 131 ms 7580 KiB
01_random_18.txt AC 63 ms 5628 KiB
01_random_19.txt AC 58 ms 4212 KiB
01_random_20.txt AC 71 ms 4612 KiB
01_random_21.txt AC 59 ms 4576 KiB
01_random_22.txt AC 113 ms 7460 KiB
01_random_23.txt AC 86 ms 6188 KiB
02_handmade_23.txt AC 54 ms 7068 KiB
02_handmade_24.txt AC 56 ms 7116 KiB
02_handmade_25.txt AC 78 ms 7840 KiB
02_handmade_26.txt AC 90 ms 7836 KiB
02_handmade_27.txt AC 84 ms 7828 KiB
02_handmade_28.txt AC 85 ms 7776 KiB
02_handmade_29.txt AC 143 ms 8340 KiB
02_handmade_30.txt AC 96 ms 7152 KiB
02_handmade_31.txt AC 95 ms 7232 KiB
02_handmade_32.txt AC 87 ms 7044 KiB
02_handmade_33.txt AC 101 ms 7336 KiB
02_handmade_34.txt AC 139 ms 11468 KiB