This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\n")));
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
using namespace std;
const int maxn = 1000025;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
struct Item {
int t;
int64_t ed;
int val;
} v[maxn], u[maxn];
int64_t sumv[maxn], sumu[maxn];
int posv[maxn], posu[maxn];
int64_t dp[maxn], dif[maxn], lim[maxn];
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> v[i].t >> v[i].ed >> v[i].val;
for (int i = 1; i <= m; i++)
cin >> u[i].t >> u[i].ed >> u[i].val;
/*
// int T = time(0); debug(T);
int T = 1624472002;
srand(T);
// srand(2300);
for (int i = 1; i <= n; i++)
v[i].t = rand() % 10000, v[i].ed = rand() % (10000 * i), v[i].val = rand() % 101 - 50;
for (int i = 1; i <= m; i++)
u[i].t = rand() % 10000, u[i].ed = rand() % (10000 * i), u[i].val = rand() % 101 - 50;
*/
for (int i = 1; i <= n; i++)
sumv[i] = sumv[i-1] + v[i].t;
for (int i = 1; i <= m; i++)
sumu[i] = sumu[i-1] + u[i].t;
for (int i = 1; i <= n; i++)
posv[i] = upper_bound(sumu, sumu+m+1, v[i].ed - sumv[i]) - sumu - 1;
for (int i = 1; i <= m; i++)
posu[i] = upper_bound(sumv, sumv+n+1, u[i].ed - sumu[i]) - sumv - 1;
for (int j = 1; j <= m; j++) dp[j] = dp[j-1] + (posu[j] >= 0 ? u[j].val : 0);
for (int j = 1; j <= m; j++) lim[j] = dif[j] = (posu[j] >= 0 ? u[j].val : 0);
/*
cerr << "posu:\t";
cerr << 0 << ' ';
for (int i = 1; i <= m; i++) cerr << posu[i] << ' ';
cerr << '\n';
cerr << "u.val:\t";
cerr << 0 << ' ';
for (int i = 1; i <= m; i++) cerr << u[i].val << ' ';
cerr << '\n';
*/
set<int> st;
min_heap<pair<int,int>> pq;
for (int j = 1; j <= m; j++) if (posu[j] >= 0) pq.emplace(posu[j], -j);
// for (int j = 1; j <= m; j++) debug(j, posu[j], u[j].val);
function<void(int,int,int64_t)> update = [&](int i, int j, int64_t val) {
if (j == m+1) return;
dif[j] -= val;
// debug(i, j, val);
if (dif[j] <= lim[j]) {
int64_t c = lim[j] - dif[j];
dif[j] = lim[j];
st.erase(j);
auto it = st.lower_bound(j);
if (it != st.end() && c)
update(i, *it, c);
} else {
st.insert(j);
}
// int64_t c = max<int64_t>(0, (j != 0 && i <= posu[j] ? u[j].val : 0) - dif[j]);
};
for (int i = 1; i <= n; i++) {
vector<int> tmp;
while (!pq.empty() && i > pq.top().first) {
int j = -pq.top().second;
pq.pop();
lim[j] = 0;
if (dif[j] > lim[j]) {
// debug(j, "ins");
st.insert(j);
} else {
tmp.push_back(j);
}
}
if (posv[i] >= 0)
dif[0] += v[i].val, update(i, posv[i]+1, v[i].val);
for (int j: tmp)
update(i, j, 0);
/*
for (int j = 0; j <= posv[i]; j++) dp[j] += v[i].val;
for (int j = 1; j <= m; j++) {
dp[j] = max(dp[j], dp[j-1] + (i <= posu[j] ? u[j].val : 0));
// for (int j = 0; j <= m; j++) cerr << (dp[j] - (j ? dp[j-1] : 0)) << ' ';
// cerr << '\n';
}
cerr << posv[i] << ';' << v[i].val << ";\n";
for (int j = 0; j <= m; j++) cerr << (dp[j] - (j ? dp[j-1] : 0)) << ' ';
cerr << '\n';
for (int j = 0; j <= m; j++) cerr << dif[j] << ' ';
cerr << '\n';
for (int j = 0; j <= m; j++) cerr << lim[j] << ' ';
cerr << '\n';
for (int j = 0; j <= m; j++)
cerr << (i<=posu[j]?u[j].val:0)<<' ';
cerr<<'\n';
for (int j = 0; j <= m; j++) assert (dif[j] == dp[j] - (j ? dp[j-1] : 0));
*/
}
debug(dp[m]);
int64_t sum = 0;
for (int i = 0; i <= m; i++) sum += dif[i];
cout << sum << '\n';
}
# | 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... |