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>
#define int long long
using namespace std;
const int M = 1 << 20, N = 2 * M, INF = 1e18 + 42;
int n, m, maxi[N], tag[N], sum[N];
vector<int> a[2], s[2], p[2], pos[2];
void add(int i, int pt) {
i = max(i, 0ll);
i += M;
while(i) {
sum[i] += pt;
i >>= 1;
}
}
int getSum(int i) {
if(i == -1) return 0;
i += M;
int ans = sum[i];
while(i) {
if(i & 1) ans += sum[i-1];
i >>= 1;
}
return ans;
}
void applyOp(int i, int add) {
tag[i] += add;
maxi[i] += add;
}
void propage(int i) {
applyOp(i*2, tag[i]);
applyOp(i*2+1, tag[i]);
tag[i] = 0;
}
int update(int i, int deb, int fin, int l, int r, int add) {
if(r <= deb || fin <= l)
return 0;
if(l <= deb && fin <= r) {
applyOp(i, add);
return maxi[i];
}
propage(i);
int mid = ((deb + fin) >> 1);
int ans = max(update(i*2, deb, mid, l, r, add),
update(i*2+1, mid, fin, l, r, add));
maxi[i] = max(maxi[i*2], maxi[i*2+1]);
return ans;
}
int solve() {
for(int i = 0; i < N; i++)
maxi[i] = tag[i] = sum[i] = 0;
vector<int> ver[M];
for(int i = 1; i <= m; i++)
if(pos[1][i] >= 0)
ver[pos[1][i]].push_back(i);
int ans = 0;
for(int i = n; i >= 0; i--) {
for(int j : ver[i]) {
add(j, p[1][j]);
update(1, 0, M, j, M, p[1][j]);
}
int dp = update(1, 0, M, pos[0][i], M, 0);
ans = max(ans, dp - getSum(pos[0][i]));
int val = update(1, 0, M, pos[0][i], pos[0][i]+1, 0);
update(1, 0, M, pos[0][i], pos[0][i]+1, -val);
update(1, 0, M, pos[0][i], pos[0][i]+1, dp);
update(1, 0, M, 0, pos[0][i]+1, p[0][i]);
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int sz[] = {n+1, m+1};
for(int i = 0; i < 2; i++) {
a[i].resize(sz[i]);
s[i].resize(sz[i]);
p[i].resize(sz[i]);
pos[i].resize(sz[i]);
}
for(int i = 1; i <= n; i++)
cin >> a[0][i] >> s[0][i] >> p[0][i];
for(int i = 1; i <= m; i++)
cin >> a[1][i] >> s[1][i] >> p[1][i];
for(int i = 1; i <= n; i++)
a[0][i] += a[0][i-1];
for(int i = 1; i <= m; i++)
a[1][i] += a[1][i-1];
for(int i = 0; i <= n; i++)
pos[0][i] = (int)(upper_bound(a[1].begin(), a[1].end(), s[0][i] - a[0][i]) - a[1].begin())-1;
for(int i = 0; i <= m; i++)
pos[1][i] = (int)(upper_bound(a[0].begin(), a[0].end(), s[1][i] - a[1][i]) - a[0].begin())-1;
int ans = solve();
swap(n, m);
swap(a[0], a[1]);
swap(s[0], s[1]);
swap(p[0], p[1]);
swap(pos[0], pos[1]);
cout << max(ans, solve()) << '\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... |