# include <bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pb push_back
using namespace std;
const int N = 3e5 + 5;
int n,x[N],g[N],d[N],pr[N],pr1[N];
int tree[4*N],val,dist,ans,idx;
void build(int node,int le, int ri) {
if (le == ri) {
tree[node] = pr[le];
return ;
}
int mid = (le + ri) / 2;
build(2*node,le,mid);
build(2 * node + 1, mid + 1, ri);
tree[node] = max(tree[2*node], tree[2*node + 1]);
}
int getans(int node, int le, int ri, int start,int end, int val) {
if (le > end || ri < start) return 0;
int mid = (le + ri) / 2;
if (le >= start && ri <= end) {
if (tree[node] < val) return 0;
if (le == ri) {
return le;
}
if (tree[2*node + 1] >= val) return getans(2*node + 1, mid + 1, ri, start,end,val);
else return getans(2*node, le,mid, start,end,val);
}
return max(getans(2*node,le,mid,start,end,val), getans(2*node + 1,mid + 1, ri, start,end,val));
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>x[i]>>g[i]>>d[i];
if (i > 1) {
dist = (x[i] - x[i - 1]);
pr[i] = pr[i - 1] + d[i] - dist;
// update(1,1,n,i, pr[i]);
}
ans = max(ans,g[i]);
pr1[i] = pr1[i - 1] + g[i];
}
build(1,1,n);
for (int i = 1; i <= n; i++) {
// pr[j] - pr[i] + d[i] >= 0 pr[j] >= pr[i] - d[i]
val = pr[i] - d[i];
// i + 1-----> n >= val
idx = getans(1,1,n,i + 1, n, val);
if (idx == 0) {
continue;
}
ans = max(ans, pr1[idx] - pr1[i - 1]);
}cout<<ans<<endl;
}
Compilation message
divide.cpp:33:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
33 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
448 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
736 KB |
Output is correct |
24 |
Correct |
5 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
448 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
736 KB |
Output is correct |
24 |
Correct |
5 ms |
716 KB |
Output is correct |
25 |
Correct |
3 ms |
716 KB |
Output is correct |
26 |
Correct |
5 ms |
1124 KB |
Output is correct |
27 |
Correct |
9 ms |
1100 KB |
Output is correct |
28 |
Correct |
28 ms |
4300 KB |
Output is correct |
29 |
Correct |
29 ms |
4552 KB |
Output is correct |
30 |
Correct |
71 ms |
9200 KB |
Output is correct |
31 |
Correct |
57 ms |
7996 KB |
Output is correct |
32 |
Correct |
62 ms |
8036 KB |
Output is correct |
33 |
Correct |
78 ms |
7932 KB |
Output is correct |
34 |
Correct |
42 ms |
7936 KB |
Output is correct |
35 |
Correct |
48 ms |
8444 KB |
Output is correct |
36 |
Correct |
47 ms |
8572 KB |
Output is correct |