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>
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
using namespace std;
const int N = 2e5 + 10;
ll A[N], B[N], SA[N], SB[N], S[N], T[N], Q[N], P[N];
vector <pair <ll, ll>> pos[N];
struct ST{
vector <ll> seg, laz;
ST(int n) {
for (int i = 0; i <= n * 4 + 100; i++) {
seg.pb(0);
laz.pb(0);
}
}
void push(int id) {
ll &t = laz[id];
laz[id * 2] += t;
laz[id * 2 + 1] += t;
seg[id * 2] += t;
seg[id * 2 + 1] += t;
t = 0;
}
void upd(int id, int l, int r, int u, int v, ll t) {
if (l > v || r < u)
return;
if (l >= u && r <= v) {
laz[id] += t;
seg[id] += t;
return;
}
push(id);
int m = (l + r) / 2;
upd(id * 2, l, m, u, v, t);
upd(id * 2 + 1, m + 1, r, u, v, t);
seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
void point(int id, int l, int r, int p, ll v) {
if (l > p || r < p)
return;
if (l == r) {
seg[id] = max(seg[id], v);
return;
}
push(id);
int m = (l + r) / 2;
point(id * 2, l, m, p, v);
point(id * 2 + 1, m + 1, r, p, v);
seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
ll get(int id, int l, int r, int u, int v) {
if (u > v || l > v || r < u)
return -1e12;
if (l >= u && r <= v)
return seg[id];
push(id);
int m = (l + r) / 2;
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
};
vector <pair <ll, ll>> compress(vector < pair <ll, ll>> &v) {
vector <pair <ll, ll>> st;
for (auto x : v) {
if (st.empty() || st.back().f != x.f) {
st.pb(x);
} else {
st[sz(st) - 1].s += x.s;
}
}
return st;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll ans = 0;
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> A[i] >> S[i] >> P[i];
SA[i] = SA[i - 1] + A[i];
}
for (int i = 1; i <= m; i++) {
cin >> B[i] >> T[i] >> Q[i];
SB[i] = SB[i - 1] + B[i];
}
for (int i = 1; i <= n; i++) {
if (SA[i] > S[i])
continue;
int id = upper_bound(SB, SB + 1 + m, S[i] - SA[i]) - SB - 1;
pos[i].pb({id, P[i]});
}
for (int i = 1; i <= m; i++) {
if (SB[i] > T[i])
continue;
ans += Q[i];
int id = upper_bound(SA, SA + 1 + n, T[i] - SB[i]) - SA - 1;
if (i < m && id)
pos[id - 1].pb({i + 1, -Q[i]});
}
for (int i = 1; i <= n; i++) {
sort(all(pos[i]));
pos[i] = compress(pos[i]);
}
ST seg(m);
int lx = 0;
for (int i = 0; i <= n; i++) {
ll cur = 0;
for (auto x : pos[i]) {
cur += x.s;
}
if (sz(pos[i]))
lx = pos[i].back().f;
vector <pair <ll, ll>> save;
for (int j = sz(pos[i]) - 1; j >= 0; j--) {
ll t = seg.get(1, 0, m, 0, pos[i][j].f) + cur;
save.pb({pos[i][j].f, t});
seg.upd(1, 0, m, pos[i][j].f, m, pos[i][j].s);
cur -= pos[i][j].s;
}
for (auto x : save) {
seg.point(1, 0, m, x.f, x.s);
}
}
cout << seg.get(1, 0, m, lx, lx) + ans << '\n';
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... |