Submission #240451

# Submission time Handle Problem Language Result Execution time Memory
240451 2020-06-19T16:39:19 Z DS007 Divide and conquer (IZhO14_divide) C++14
100 / 100
504 ms 9336 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
const int N = 1e5;
int x[N], g[N], d[N], pg[N], pd[N], t[N * 4], n;
 
int build(int v, int l, int r) {
	if (l == r)
		return t[v] = pd[l] - x[l];
 
	int mid = (l + r) / 2;
	return t[v] = max(build(v * 2, l, mid), build(v * 2 + 1, mid + 1, r));
}
 
int query(int v, int l, int r, int tl, int tr) {
	if (tl > tr)
		return -1e18;
	else if (l == tl && r == tr)
		return t[v];
 
	int mid = (l + r) / 2;
	return max(query(v * 2, l, mid, tl, min(mid, tr)), query(v * 2 + 1, mid + 1, r, max(mid + 1, tl), tr));
}
 
int solve(int l, int r, int val) {
	if (query(1, 0, n - 1, l, r) < val)
		return -1;
	else if (l == r)
		return l;
 
	int mid = (l + r) / 2;
	if (query(1, 0, n - 1, mid + 1, r) >= val)
		return solve(mid + 1, r, val);
	else
		return solve(l, mid, val);
}
 
int solveTestCase() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> x[i] >> g[i] >> d[i];
 
	pg[0] = g[0], pd[0] = d[0];
	for (int i = 1; i < n; i++)
		pg[i]  = pg[i - 1] + g[i], pd[i] = pd[i - 1] + d[i];
 
	build(1, 0, n - 1);
	int ans = *max_element(g, g + n);
	//cout << pd[3] - x[3] << "\n";
	//for (int i = 0; i < n; i++)
	//    cout << query(1, 0, n - 1, i, i) << "\n";
 
	for (int i = n - 2; i >= 0; i--) {
		int j = solve(i + 1, n - 1, pd[i] - d[i] - x[i]);
		//cout << j << "\n";
		if (j != -1)
			ans = max(ans, pg[j] - pg[i] + g[i]);
	}
 
	cout << ans;
}
 
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
 
	int t = 1;
	//cin >> t;
	while (t--)
		solveTestCase();
	return 0;
}

Compilation message

divide.cpp: In function 'long long int solveTestCase()':
divide.cpp:62:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 384 KB Output is correct
16 Correct 6 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 384 KB Output is correct
21 Correct 8 ms 512 KB Output is correct
22 Correct 7 ms 512 KB Output is correct
23 Correct 7 ms 896 KB Output is correct
24 Correct 8 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 384 KB Output is correct
16 Correct 6 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 384 KB Output is correct
21 Correct 8 ms 512 KB Output is correct
22 Correct 7 ms 512 KB Output is correct
23 Correct 7 ms 896 KB Output is correct
24 Correct 8 ms 768 KB Output is correct
25 Correct 16 ms 768 KB Output is correct
26 Correct 32 ms 1152 KB Output is correct
27 Correct 29 ms 1152 KB Output is correct
28 Correct 164 ms 4352 KB Output is correct
29 Correct 167 ms 4600 KB Output is correct
30 Correct 400 ms 9336 KB Output is correct
31 Correct 387 ms 8184 KB Output is correct
32 Correct 376 ms 8312 KB Output is correct
33 Correct 392 ms 7928 KB Output is correct
34 Correct 504 ms 7932 KB Output is correct
35 Correct 53 ms 8444 KB Output is correct
36 Correct 119 ms 8568 KB Output is correct