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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6;
struct Point
{
int x, y; ll w;
};
vector<Point> V;
int N, M;
ll ans;
ll A[MAXN+10], S[MAXN+10], P[MAXN+10], B[MAXN+10], T[MAXN+10], Q[MAXN+10];
ll dp[MAXN+10];
pll lazy[MAXN*4+10];
void busy(int node, int tl, int tr)
{
if(tl!=tr)
{
lazy[node*2]={lazy[node].first+lazy[node*2].first, max(lazy[node*2].second+lazy[node].first, lazy[node].second)};
lazy[node*2+1]={lazy[node].first+lazy[node*2+1].first, max(lazy[node*2+1].second+lazy[node].first, lazy[node].second)};
}
else
{
dp[tl]=max(dp[tl]+lazy[node].first, lazy[node].second);
}
lazy[node]={0, 0};
}
void update(int node, int tl, int tr, int l, int r, pll k)
{
busy(node, tl, tr);
if(r<tl || tr<l) return;
if(l<=tl && tr<=r)
{
lazy[node]=k;
busy(node, tl, tr);
return;
}
int mid=tl+tr>>1;
update(node*2, tl, mid, l, r, k);
update(node*2+1, mid+1, tr, l, r, k);
}
ll query(int node, int tl, int tr, int p)
{
busy(node, tl, tr);
if(tl==tr) return dp[tl];
int mid=tl+tr>>1;
if(p<=mid) return query(node*2, tl, mid, p);
else return query(node*2+1, mid+1, tr, p);
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) scanf("%lld%lld%lld", &A[i], &S[i], &P[i]), A[i]+=A[i-1];
for(int i=1; i<=M; i++) scanf("%lld%lld%lld", &B[i], &T[i], &Q[i]), B[i]+=B[i-1];
for(int i=1; i<=N; i++)
{
int t=upper_bound(B, B+M+1, S[i]-A[i])-B-1;
if(t<0) continue;
if(t==M) { ans+=P[i]; continue; }
int x=i, y=t;
x--; y++;
V.push_back({x, y, -P[i]}); ans+=P[i];
}
for(int i=1; i<=M; i++)
{
int t=upper_bound(A, A+N+1, T[i]-B[i])-A-1;
if(t<0) continue;
if(t==N) { ans+=Q[i]; continue; }
int y=i, x=t;
V.push_back({x, y, Q[i]});
}
sort(V.begin(), V.end(), [&](const Point &p, const Point &q)
{
if(p.x!=q.x) return p.x<q.x;
if(p.y!=q.y) return p.y>q.y;
return p.w>q.w;
});
for(int i=0; i<V.size(); i++)
{
ll t=0;
if(V[i].y!=0) t=query(1, 0, M, V[i].y-1);
update(1, 0, M, V[i].y, M, pll(V[i].w, t));
}
printf("%lld\n", query(1, 0, M, M)+ans);
}
Compilation message (stderr)
dishes.cpp: In function 'void update(int, int, int, int, int, pll)':
dishes.cpp:49:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int mid=tl+tr>>1;
| ~~^~~
dishes.cpp: In function 'll query(int, int, int, int)':
dishes.cpp:58:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int mid=tl+tr>>1;
| ~~^~~
dishes.cpp: In function 'int main()':
dishes.cpp:95:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i=0; i<V.size(); i++)
| ~^~~~~~~~~
dishes.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
65 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
dishes.cpp:66:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | for(int i=1; i<=N; i++) scanf("%lld%lld%lld", &A[i], &S[i], &P[i]), A[i]+=A[i-1];
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dishes.cpp:67:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | for(int i=1; i<=M; i++) scanf("%lld%lld%lld", &B[i], &T[i], &Q[i]), B[i]+=B[i-1];
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |