#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const long long MAXN = 1e5+10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long n;
long long x[MAXN], gold[MAXN], energy[MAXN];
long long a[MAXN], pref[MAXN];
void read()
{
cin >> n;
for (long long i = 1; i <= n; ++ i)
{
cin >> x[i] >> gold[i] >> energy[i];
a[i] = a[i-1];
pref[i] = pref[i-1] + gold[i];
if(i > 1)a[i] -= x[i] - x[i-1];
a[i] += energy[i];
}
}
long long t[MAXN * 4];
long long lazy[MAXN * 4];
long long inf = -1;
void make_tree(long long i, long long l, long long r)
{
if(l == r)
{
t[i] = a[l];
// cout << l << " " << r << " " << a[l] << endl;
return;
}
long long mid = (l + r)/2;
make_tree(2*i, l, mid);
make_tree(2*i+1, mid+1, r);
t[i] = max(t[2*i], t[2*i+1]);
}
void push_lazy(long long i, long long l, long long r)
{
t[i] += lazy[i];
if(l != r)
{
lazy[2*i] += lazy[i];
lazy[2*i+1] += lazy[i];
}
lazy[i] = 0;
}
long long ql, qr, val;
void update(long long i, long long l, long long r)
{
push_lazy(i, l, r);
if(qr < l || ql > r)return;
if(ql <= l && r <= qr)
{
lazy[i] = val;
push_lazy(i, l, r);
return;
}
long long mid = (l + r)/2;
update(2*i, l, mid);
update(2*i+1, mid+1, r);
t[i] = max(t[2*i], t[2*i+1]);
}
long long xx;
void clear_update(long long i, long long l, long long r)
{
if(l == r)
{
t[i] = -1;
return;
}
long long mid = (l + r)/2;
if(xx <= mid)clear_update(2*i, l, mid);
else clear_update(2*i+1, mid+1, r);
t[i] = max(t[2*i], t[2*i+1]);
}
long long query(long long i, long long l, long long r)
{
push_lazy(i, l, r);
if(l == r)
{
if(t[i] >= 0)return l;
else return -1;
}
long long mid = (l + r)/2;
push_lazy(2*i, l, mid);
push_lazy(2*i+1, mid + 1, r);
long long tleft = t[2*i], tright = t[2*i+1];
if(tright >= 0)return query(2*i+1, mid+1, r);
else return query(2*i, l, mid);
}
int main()
{
speed();
read();
make_tree(1, 1, n);
long long ans = 0;
for (long long starter = 1; starter <= n; ++ starter)
{
long long index = query(1, 1, n);
// cout << index << endl;
if(index == -1)continue;
long long sol = pref[index] - pref[starter-1];
ans = max(ans, sol);
long long change = -energy[starter];
if(starter < n)change += x[starter+1] - x[starter];
ql = starter + 1;
qr = n;
val = change;
if(ql <= qr)
update(1, 1, n);
xx = starter;
clear_update(1, 1, n);
}
cout << ans << endl;
return 0;
}
Compilation message
divide.cpp: In function 'long long int query(long long int, long long int, long long int)':
divide.cpp:93:15: warning: unused variable 'tleft' [-Wunused-variable]
93 | long long tleft = t[2*i], tright = t[2*i+1];
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4596 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4596 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
3 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
3 ms |
4444 KB |
Output is correct |
23 |
Correct |
4 ms |
4700 KB |
Output is correct |
24 |
Correct |
4 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4596 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4444 KB |
Output is correct |
20 |
Correct |
3 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
3 ms |
4444 KB |
Output is correct |
23 |
Correct |
4 ms |
4700 KB |
Output is correct |
24 |
Correct |
4 ms |
4812 KB |
Output is correct |
25 |
Correct |
4 ms |
4952 KB |
Output is correct |
26 |
Correct |
6 ms |
7004 KB |
Output is correct |
27 |
Correct |
8 ms |
7204 KB |
Output is correct |
28 |
Correct |
30 ms |
8620 KB |
Output is correct |
29 |
Correct |
31 ms |
8784 KB |
Output is correct |
30 |
Correct |
64 ms |
12372 KB |
Output is correct |
31 |
Correct |
67 ms |
11236 KB |
Output is correct |
32 |
Correct |
59 ms |
11148 KB |
Output is correct |
33 |
Correct |
57 ms |
11164 KB |
Output is correct |
34 |
Correct |
58 ms |
11092 KB |
Output is correct |
35 |
Correct |
59 ms |
11604 KB |
Output is correct |
36 |
Correct |
60 ms |
11600 KB |
Output is correct |