Submission #71383757


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int; using ll = long long; using ull = unsigned long long;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using ld = long double; using i128 = __int128; using u128 = unsigned __int128;
template<typename T, size_t N, size_t M> using matrix = array<array<T, M>, N>;
template<typename T1, typename T2> bool chkmin(T1& a, const T2& b) { return b < a ? (a = b, true) : false; }
template<typename T1, typename T2> bool chkmax(T1& a, const T2& b) { return b > a ? (a = b, true) : false; }
template<class... T> void debug(T&&... x) { cout << "(debug) "; ((cout << x << " "), ...); cout << "\n"; }
void fileIO(const string& x) { freopen((x + ".in").c_str(), "r", stdin), freopen((x + ".out").c_str(), "w", stdout); }
mt19937_64 gen(random_device{}());
int test_num = 1, test_case = 1;
const int int_Inf = 1e9; const ll Inf = 1e18;

const int N = 4e4 + 5, Q = 2e5 + 5, LOGN = 16, T = 500;

using DP = array<ll, T + 1>;

// void convolition(DP &c, const DP &a, const DP &b) {
// 	for(int i = 0; i <= T; i++) for(int j = 0; i + j <= T; j++) {
// 		chkmax(c[i + j], a[i] + b[j]);
// 	}
// }
DP add(const DP &a, int wt, ll val) {
	DP c = a;
	for(int i = T; i >= wt; i--) {
		chkmax(c[i], c[i - wt] + val);
	}
	return c;
}

int n, q, h[N], w[N];
ll ans[Q];
DP a[N], f[N];

void solve(int l, int r, vector<array<int, 4> > qry) {
	if(l == r) {
		for(auto [l, r, t, id] : qry) ans[id] = a[l][t];
		return;
	}
	int mid = (l + r) / 2;
	vector<array<int, 4> > Lq, Rq, Mq;

	for(auto [l, r, t, id] : qry) {
		if(l <= mid && mid + 1 <= r) Mq.push_back({ l, r, t, id });
		else if(r <= mid) Lq.push_back({ l, r, t, id });
		else Rq.push_back({ l, r, t, id });
	}
	solve(l, mid, Lq); solve(mid + 1, r, Rq);

	for(int i = l; i <= r; i++) f[i] = {};
	f[mid] = a[mid], f[mid + 1] = a[mid + 1];
	for(int i = mid - 1; i >= l; i--) f[i] = add(f[i + 1], h[i], w[i]);
	for(int i = mid + 2; i <= r; i++) f[i] = add(f[i - 1], h[i], w[i]);
	for(auto [l, r, t, id] : Mq) {
		for(int k = 0; k <= t; k++) {
			chkmax(ans[id], f[l][k] + f[r][t - k]);
		}
	}
}

void solution() {
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> h[i] >> w[i];
	for(int i = 1; i <= n; i++) {
		a[i] = {};
		for(int j = h[i]; j <= T; j++) a[i][j] = w[i];
	}
	cin >> q;
	vector<array<int, 4> > qry;
	for(int i = 1; i <= q; i++) {
		int l, r, t; cin >> l >> r >> t;
		qry.push_back({ l, r, t, i });
	}
	solve(1, n, qry);
	for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
void sol_clear() {}
void prework() {}

int main() {
	// fileIO("");
	ios::sync_with_stdio(false), cin.tie(nullptr);
	prework();
	// cin >> test_num;
	for(test_case = 1; test_case <= test_num; test_case++) solution(), sol_clear();
	cerr << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
	return 0;
}

Submission Info

Submission Time
Task G - Range Knapsack Query
User _xm_
Language C++ 20 (gcc 12.2)
Score 575
Code Size 2869 Byte
Status AC
Exec Time 453 ms
Memory 177420 KiB

Compile Error

Main.cpp: In function ‘void fileIO(const std::string&)’:
Main.cpp:10:39: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 | void fileIO(const string& x) { freopen((x + ".in").c_str(), "r", stdin), freopen((x + ".out").c_str(), "w", stdout); }
      |                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:10:81: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 | void fileIO(const string& x) { freopen((x + ".in").c_str(), "r", stdin), freopen((x + ".out").c_str(), "w", stdout); }
      |                                                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 575 / 575
Status
AC × 2
AC × 51
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 01_small_07.txt, 02_large_00.txt, 02_large_01.txt, 02_large_02.txt, 02_large_03.txt, 02_large_04.txt, 02_large_05.txt, 02_large_06.txt, 02_large_07.txt, 02_large_08.txt, 02_large_09.txt, 02_large_10.txt, 02_large_11.txt, 02_large_12.txt, 02_large_13.txt, 02_large_14.txt, 02_large_15.txt, 02_large_16.txt, 02_large_17.txt, 02_large_18.txt, 02_large_19.txt, 02_large_20.txt, 02_large_21.txt, 02_large_22.txt, 02_large_23.txt, 02_large_24.txt, 02_large_25.txt, 02_large_26.txt, 02_large_27.txt, 02_large_28.txt, 02_large_29.txt, 02_large_30.txt, 02_large_31.txt, 02_large_32.txt, 02_large_33.txt, 02_large_34.txt, 02_large_35.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3768 KiB
00_sample_01.txt AC 1 ms 3620 KiB
01_small_00.txt AC 61 ms 14660 KiB
01_small_01.txt AC 112 ms 18036 KiB
01_small_02.txt AC 102 ms 21684 KiB
01_small_03.txt AC 2 ms 3972 KiB
01_small_04.txt AC 92 ms 18272 KiB
01_small_05.txt AC 15 ms 5680 KiB
01_small_06.txt AC 85 ms 18432 KiB
01_small_07.txt AC 126 ms 18132 KiB
02_large_00.txt AC 342 ms 173624 KiB
02_large_01.txt AC 422 ms 173892 KiB
02_large_02.txt AC 334 ms 173940 KiB
02_large_03.txt AC 410 ms 174064 KiB
02_large_04.txt AC 277 ms 170940 KiB
02_large_05.txt AC 308 ms 171000 KiB
02_large_06.txt AC 341 ms 173804 KiB
02_large_07.txt AC 430 ms 173568 KiB
02_large_08.txt AC 359 ms 173876 KiB
02_large_09.txt AC 453 ms 173932 KiB
02_large_10.txt AC 309 ms 170940 KiB
02_large_11.txt AC 334 ms 171012 KiB
02_large_12.txt AC 344 ms 173892 KiB
02_large_13.txt AC 429 ms 173268 KiB
02_large_14.txt AC 352 ms 174000 KiB
02_large_15.txt AC 453 ms 174004 KiB
02_large_16.txt AC 324 ms 170944 KiB
02_large_17.txt AC 344 ms 170648 KiB
02_large_18.txt AC 334 ms 173712 KiB
02_large_19.txt AC 383 ms 173292 KiB
02_large_20.txt AC 335 ms 173564 KiB
02_large_21.txt AC 376 ms 173948 KiB
02_large_22.txt AC 290 ms 171028 KiB
02_large_23.txt AC 324 ms 170988 KiB
02_large_24.txt AC 335 ms 174208 KiB
02_large_25.txt AC 392 ms 173416 KiB
02_large_26.txt AC 335 ms 173696 KiB
02_large_27.txt AC 383 ms 173908 KiB
02_large_28.txt AC 299 ms 170644 KiB
02_large_29.txt AC 322 ms 170956 KiB
02_large_30.txt AC 344 ms 173300 KiB
02_large_31.txt AC 389 ms 173364 KiB
02_large_32.txt AC 335 ms 173764 KiB
02_large_33.txt AC 391 ms 173932 KiB
02_large_34.txt AC 292 ms 170980 KiB
02_large_35.txt AC 332 ms 170884 KiB
03_handmade_00.txt AC 261 ms 177420 KiB
03_handmade_01.txt AC 32 ms 10836 KiB
03_handmade_02.txt AC 338 ms 173328 KiB
03_handmade_03.txt AC 247 ms 177352 KiB
03_handmade_04.txt AC 319 ms 177348 KiB