#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1'000'100;
const ll INF = 1'000'000'000'000'000'000;
int n, m, s[N + 10], t[N + 10];
ll a[N + 10], c[N + 10], x[N + 10];
ll b[N + 10], d[N + 10], y[N + 10];
ll sumA[N + 10], sumB[N + 10];
ll add[4 * N + 10], lazy[4 * N + 10];
vector<pair<int, pair<int, ll>>> vec[N + 10];
void readInput() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> x[i] >> c[i];
sumA[i] = sumA[i - 1] + a[i];
}
for (int i = 1; i <= m; i++) {
cin >> b[i] >> y[i] >> d[i];
sumB[i] = sumB[i - 1] + b[i];
}
}
void init() {
for (int i = 1; i <= n; i++) {
s[i] = upper_bound(sumB, sumB + m + 1, x[i] - sumA[i]) - sumB - 1;
if (s[i] != -1)
vec[i - 1].push_back({0, {s[i], c[i]}});
//cout << "s " << i << " = " << s[i] << endl;
}
for (int i = 1; i <= m; i++) {
t[i] = upper_bound(sumA, sumA + n + 1, y[i] - sumB[i]) - sumA - 1;
if (t[i] != -1)
vec[t[i]].push_back({1, {i, d[i]}});
//cout << "t " << i << " = " << t[i] << endl;
}
}
void shift(int, int, int);
ll get(int idx, int id = 1, int l = 0, int r = m + 1) {
if (l + 1 == r)
return max(add[id], lazy[id]);
shift(id, l, r);
int mid = (l + r) >> 1;
if (idx < mid)
return get(idx, id << 1, l, mid);
return get(idx, id << 1 | 1, mid, r);
}
void updateAdd(int st, int en, ll val, int id = 1, int l = 0, int r = m + 1) {
if (en <= l || r <= st)
return;
if (st <= l && r <= en) {
add[id] += val;
if (lazy[id] != -INF)
lazy[id] += val;
return;
}
shift(id, l, r);
int mid = (l + r) >> 1;
updateAdd(st, en, val, id << 1, l, mid);
updateAdd(st, en, val, id << 1 | 1, mid, r);
}
void updateMax(int st, int en, ll val, int id = 1, int l = 0, int r = m + 1) {
if (en <= l || r <= st)
return;
if (st <= l && r <= en) {
lazy[id] = max(lazy[id], val);
return;
}
shift(id, l, r);
int mid = (l + r) >> 1;
updateMax(st, en, val, id << 1, l, mid);
updateMax(st, en, val, id << 1 | 1, mid, r);
}
void shiftAdd(int id, int l, int r) {
if (!add[id])
return;
int mid = (l + r) >> 1;
updateAdd(l, r, add[id], id << 1, l, mid);
updateAdd(l, r, add[id], id << 1 | 1, mid, r);
add[id] = 0;
}
void shiftMax(int id, int l, int r) {
if (lazy[id] == -INF)
return;
int mid = (l + r) >> 1;
updateMax(l, r, lazy[id], id << 1, l, mid);
updateMax(l, r, lazy[id], id << 1 | 1, mid, r);
lazy[id] = -INF;
}
void shift(int id, int l, int r) {
shiftAdd(id, l, r);
shiftMax(id, l, r);
}
void addVec(int i) {
for (auto [type, p]: vec[i])
if (type == 0)
updateAdd(0, p.first + 1, p.second);
else
updateAdd(p.first, m + 1, p.second);
}
void maxVec(int i) {
for (auto [type, p]: vec[i]) {
int idx = p.first - type;
//cout << idx << ' ' << get(idx) << endl;
updateMax(idx, m + 1, get(idx));
}
}
void calcDP() {
for (int i = 0; i <= n; i++) {
addVec(i);
/*cout << i << " -> ";
for (int j = 0; j <= m; j++)
cout << get(j) << ' ';
cout << endl;*/
if (i != n)
maxVec(i);
/*cout << i << " -> ";
for (int j = 0; j <= m; j++)
cout << get(j) << ' ';
cout << endl;*/
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
readInput();
init();
calcDP();
cout << get(m) << flush;
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... |