This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// trans rights
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll L = 1e16 + 69;
struct Node
{
ll mx;
ll lazy_sum, lazy_mx;
};
vector<Node> nodes(5000000);
void clean(int i, int l, int r)
{
auto &node = nodes[i];
node.mx += node.lazy_sum;
node.mx = max(node.mx, node.lazy_mx);
if (l != r)
{
int lc = 2 * i;
int rc = 2 * i + 1;
nodes[lc].lazy_mx = max(nodes[lc].lazy_mx + node.lazy_sum, node.lazy_mx);
nodes[rc].lazy_mx = max(nodes[rc].lazy_mx + node.lazy_sum, node.lazy_mx);
nodes[lc].lazy_sum += node.lazy_sum;
nodes[rc].lazy_sum += node.lazy_sum;
}
node.lazy_mx = -L, node.lazy_sum = 0;
}
void upd(int i, int l, int r, int ql, int qr, ll mx, ll sum)
{
clean(i, l, r);
auto &node = nodes[i];
if (qr < l or ql > r)
return;
if (ql <= l and qr >= r)
{
node.lazy_mx = max(node.lazy_mx, mx);
node.lazy_mx += sum;
node.lazy_sum += sum;
clean(i, l, r);
return;
}
int lc = 2 * i;
int rc = 2 * i + 1;
int m = l + (r - l) / 2;
upd(lc, l, m, ql, qr, mx, sum);
upd(rc, m + 1, r, ql, qr, mx, sum);
node.mx = max(nodes[lc].mx, nodes[rc].mx);
}
ll query(int i, int l, int r, int ql, int qr)
{
clean(i, l, r);
auto &node = nodes[i];
if (ql <= l and qr >= r)
return node.mx;
int lc = 2 * i;
int rc = 2 * i + 1;
int m = l + (r - l) / 2;
if (ql >= m + 1) return query(rc, m + 1, r, ql, qr);
if (qr <= m) return query(lc, l, m, ql, qr);
return max(query(lc, l, m, ql, qr), query(rc, m + 1, r, ql, qr));
}
int N, M;
ll A[1000069], B[1000069], S[1000069], T[1000069], P[1000069], Q[1000069];
vector<tuple<int, int, int>> lines;
int main(int argc, const char *argv[])
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++)
{
int a, p;
ll s;
cin >> a >> s >> p;
A[i] = a + A[i - 1];
S[i] = s;
P[i] = p;
}
for (int i = 1; i <= M; i++)
{
int b, q;
ll t;
cin >> b >> t >> q;
B[i] = b + B[i - 1];
T[i] = t;
Q[i] = q;
}
ll base = 0;
for (int i = 1; i <= N; i++)
{
int a = 0, b = M + 2;
while (b - a > 1)
{
int m = a + (b - a) / 2;
if (A[i] + B[m - 1] > S[i])
b = m;
else
a = m;
}
if (a == M + 1)
{
base += P[i];
continue;
}
if (a)
lines.push_back({i, -(a - 1), P[i]});
}
for (int i = 1; i <= M; i++)
{
int a = 0, b = N + 2;
while (b - a > 1)
{
int m = a + (b - a) / 2;
if (B[i] + A[m - 1] > T[i])
b = m;
else
a = m;
}
if (a == N + 1)
{
base += Q[i];
continue;
}
if (a)
{
lines.push_back({a, -(i - 1), -Q[i]});
base += Q[i];
}
}
sort(lines.begin(), lines.end());
int root = 1;
for (auto [x, ny, a] : lines)
{
int y = -ny;
upd(root, 0, M, 0, y, -L, a);
upd(root, 0, M, y + 1, M, query(root, 0, M, 0, y), 0);
}
cout << nodes[root].mx + base;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |