# include <bits/stdc++.h>
# define pb push_back
# define fr first
# define sc second
# define mk make_pair
using namespace std;
const int inf = 1e9 + 7;
const int N = 1e5 + 5;
typedef long long ll;
int n, a[N];
ll ans, b[N], c[N], cb[N], cc[N], s1, s2;
vector < pair <int, int> > t[N * 4];
void Merge(vector < pair <int, int> > a, vector < pair <int, int> > b, vector < pair <int, int> > &c)
{
int i = 0, j = 0;
while(i < a.size() && j < b.size())
if(a[i] < b[j])
c.pb(a[i ++]);
else
c.pb(b[j ++]);
while(i < a.size())
c.pb(a[i ++]);
while(j < b.size())
c.pb(b[j ++]);
for(int i = c.size() - 2; i >= 0; i --)
c[i].sc = max(c[i].sc, c[i + 1].sc);
}
void build(int v = 1, int tl = 1, int tr = n)
{
if(tl == tr)
t[v].pb(mk(c[tl], b[tl]));
else
{
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm);
build((v << 1) | 1, tm + 1, tr);
Merge(t[v << 1], t[(v << 1) | 1], t[v]);
}
}
int get(int pos, int v = 1, int tl = 1, int tr = n)
{
if(tr < pos) return 0;
if(pos <= tl)
{
if(t[v].back().fr - s2 < 0) return 0;
int lo = 0, hi = t[v].size() - 1;
while(hi - lo > 1)
{
int md = (lo + hi) >> 1;
if(t[v][md].fr - s2 >= 0)
hi = md;
else
lo = md;
}
if(t[v][lo].fr - s2 >= 0) hi = lo;
return t[v][hi].sc;
}
int tm = (tl + tr) >> 1;
if(pos <= tm)
return get(pos, v << 1, tl, tm);
return get(pos, (v << 1) | 1, tm + 1, tr);
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i] >> b[i] >> c[i];
cb[i] = b[i];
cc[i] = c[i];
b[i] += b[i - 1];
c[i] += c[i - 1];
}
for(int i = 1; i <= n; i ++)
c[i] -= a[i];
build();
for(int i = 1; i <= n; i ++)
{
ans = max(ans, get(i) - s1);
for(int j = i; j <= n; j ++)
{
ll res = b[j] - s1, sum = c[j] - s2;
if(sum + a[i] < 0) continue;
ans = max(ans, res);
}
s1 += cb[i];
s2 += cc[i];
}
cout << ans << endl;
}
Compilation message
divide.cpp: In function 'void Merge(std::vector<std::pair<int, int> >, std::vector<std::pair<int, int> >, std::vector<std::pair<int, int> >&)':
divide.cpp:22:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < a.size() && j < b.size())
^
divide.cpp:22:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < a.size() && j < b.size())
^
divide.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(i < a.size())
^
divide.cpp:30:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(j < b.size())
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14908 KB |
Output is correct |
2 |
Correct |
0 ms |
14908 KB |
Output is correct |
3 |
Correct |
3 ms |
14908 KB |
Output is correct |
4 |
Correct |
3 ms |
14908 KB |
Output is correct |
5 |
Correct |
3 ms |
14908 KB |
Output is correct |
6 |
Correct |
3 ms |
14908 KB |
Output is correct |
7 |
Correct |
3 ms |
14908 KB |
Output is correct |
8 |
Correct |
3 ms |
14908 KB |
Output is correct |
9 |
Correct |
0 ms |
14908 KB |
Output is correct |
10 |
Correct |
3 ms |
14908 KB |
Output is correct |
11 |
Correct |
0 ms |
14908 KB |
Output is correct |
12 |
Correct |
3 ms |
14908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14908 KB |
Output is correct |
2 |
Correct |
3 ms |
14908 KB |
Output is correct |
3 |
Correct |
3 ms |
15040 KB |
Output is correct |
4 |
Correct |
0 ms |
15040 KB |
Output is correct |
5 |
Correct |
6 ms |
15040 KB |
Output is correct |
6 |
Correct |
6 ms |
15172 KB |
Output is correct |
7 |
Correct |
3 ms |
15040 KB |
Output is correct |
8 |
Correct |
3 ms |
15040 KB |
Output is correct |
9 |
Correct |
3 ms |
15172 KB |
Output is correct |
10 |
Correct |
6 ms |
15172 KB |
Output is correct |
11 |
Correct |
19 ms |
16112 KB |
Output is correct |
12 |
Correct |
23 ms |
16112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
16112 KB |
Output is correct |
2 |
Correct |
103 ms |
17212 KB |
Output is correct |
3 |
Correct |
103 ms |
17212 KB |
Output is correct |
4 |
Execution timed out |
1000 ms |
26352 KB |
Execution timed out |
5 |
Halted |
0 ms |
0 KB |
- |