#if defined(ONPC) && !defined(_GLIBCXX_ASSERTIONS)
#define _GLIBCXX_ASSERTIONS
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define allit(a) (a).begin(), (a).end()
#define sz(a) ((int) (a).size())
using namespace std;
using ll = long long;
using vi = vector<int>;
namespace pd = __gnu_pbds;
template<typename K>
using ordered_set = pd::tree<K, pd::null_type, less<int>, pd::rb_tree_tag, pd::tree_order_statistics_node_update>;
template<typename... T>
using gp_hash_table = pd::gp_hash_table<T...>;//simple using statement gives warnings
const int INF = 1e9;
const ll INFL = 1e18;
const int N = 1e5;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T>
struct SegTree {
int n;
vector<T> tree;
SegTree(int n, T def) : n(n), tree(n*2, def) {}
void set(int i, T v) { tree[i+n] = v; }
void build() {
for (int i = n-1; i >= 1; i--) {
tree[i] = max(tree[i*2], tree[i*2+1]);
}
}
int query(T x) {
int v = 1;
while (v < n) {
if (tree[v*2+1] >= x) v = v*2 + 1;
else if (tree[v*2] >= x) v = v * 2;
else return -1;
}
return v - n;
}
};
int nextPower2(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return 1 + n;
}
int solve() {
int n; cin >> n;
vi x(n), g(n), d(n);
for (int i = 0; i < n; i++) cin >> x[i] >> g[i] >> d[i];
vector<ll> prefg(n+1), prefd(n+1);
for (int i = 0; i < n; i++) {
prefg[i+1] = prefg[i] + g[i];
prefd[i+1] = prefd[i] + d[i];
}
vector<ll> diff(n);
SegTree<ll> tree(nextPower2(n), -INFL);
for (ll i = 0, energy = 0; i < n; i++) {
energy += d[i];
energy -= x[i] - x[max(0LL, i-1)];
diff[i] = energy;
tree.set(i, diff[i]);
}
tree.build();
ll res = 0;
for (int i = 0; i < n; i++) {
int find = diff[i] - d[i];
int index = tree.query(find);
assert(index >= i);
res = max(res, prefg[index+1] - prefg[i]);
}
debug(diff);
cout << res;
return 0;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
//cin >> t;
while (t-- && cin) {
if (solve()) break;
#ifdef ONPC
cout << "____________________" << endl;
#endif
}
return 0;
}
Compilation message
divide.cpp: In function 'int solve()':
divide.cpp:10:24: warning: statement has no effect [-Wunused-value]
10 | #define debug(...) 42
| ^~
divide.cpp:91:5: note: in expansion of macro 'debug'
91 | debug(diff);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
852 KB |
Output is correct |
28 |
Correct |
16 ms |
3104 KB |
Output is correct |
29 |
Correct |
19 ms |
3420 KB |
Output is correct |
30 |
Correct |
36 ms |
6180 KB |
Output is correct |
31 |
Correct |
30 ms |
6144 KB |
Output is correct |
32 |
Correct |
29 ms |
6180 KB |
Output is correct |
33 |
Correct |
27 ms |
6188 KB |
Output is correct |
34 |
Correct |
31 ms |
6220 KB |
Output is correct |
35 |
Correct |
35 ms |
6188 KB |
Output is correct |
36 |
Correct |
36 ms |
6180 KB |
Output is correct |