This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (1000006)
#define ll long long int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(int ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define MSB(bm) ((bm==0)?-1:(63-__builtin_clzll((bm))))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
// #define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
// #define degug(...) 663
#define cerr if(0)cout
#endif
#warning "// FOR has been inclusive now!!!! && arr size"
#define sdb __attribute((optimize("-O3"), target("arch=sandybridge")))
ll n, m;
struct dish {
// ll ct, bf, add, i;
int ct; ll bf; int add;
} A[MAXN], B[MAXN];
ll Bsum[MAXN], Asum[MAXN];
vector<pi> points[MAXN];
ll addback;
struct node {
int s,e,m;
ll v, add, mxv;
node *l,*r;
node(int _S,int _E){
s=_S,e=_E,m=(s+e)>>1;
v=add=0; mxv=-LLINF;
if(s^e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void sdb update(ll x,ll y,ll nval) { value();
if(s==x&&e==y) {
add+=nval;
return;
}
if(x>m)r->update(x,y,nval);
else if(y<=m) l->update(x,y,nval);
else l->update(x,m,nval), r->update(m+1,y,nval);
v=max(l->value(),r->value());
}
ll sdb rmq(ll x,ll y) { value();
if(s==x&&e==y) {
return v;
}
if(x>m) return r->rmq(x,y);
else if(y<=m) return l->rmq(x,y);
else return max(l->rmq(x,m),r->rmq(m+1,y));
}
void sdb mx(ll x,ll y,ll nval) { //value();
if(s==x&&e==y) {
mxv=nval-add;
return;
}
if(x>m) r->mx(x,y,nval);
else if(y<=m) l->mx(x,y,nval);
else l->mx(x,m,nval),r->mx(m+1,y,nval);
v=max(l->value(),r->value());
}
ll sdb value() {
if(mxv!=-LLINF&&add){
v=max(v,mxv);
v+=add;
if(s^e){
// if(l->add) { l->value(); }
// if(r->add) { r->value(); }
l->mxv=max(l->mxv, mxv-l->add);
r->mxv=max(r->mxv, mxv-r->add);
l->add+=add;
r->add+=add;
}
add=0;
mxv=-LLINF;
} else if(mxv==-LLINF) {
v+=add;
if(s^e){
l->add+=add;
r->add+=add;
}
add=0;
} else {
v=max(v,mxv);
if(s^e){
l->mxv=max(l->mxv, mxv-l->add);
r->mxv=max(r->mxv, mxv-r->add);
}
mxv=-LLINF;
}
return v;
}
} *seg;
#define gc getchar_unlocked
void sdb in(ll &x) {
x=0; bool neg=0;
char ch=gc(); if(ch=='-') neg=1;
while(ch<'0'||ch>'9') { ch=gc();if(ch=='-') neg=1; }
while(ch>='0'&&ch<='9'){
x=(x<<3ll)+(x<<1ll)+ch-'0';
ch=gc();
}
if(neg) x=-x;
}
void sdb in(int &x) {
x=0; bool neg=0;
char ch=gc(); if(ch=='-') neg=1;
while(ch<'0'||ch>'9') { ch=gc();if(ch=='-') neg=1; }
while(ch>='0'&&ch<='9'){
x=(x<<3ll)+(x<<1ll)+ch-'0';
ch=gc();
}
if(neg) x=-x;
}
int sdb main()
{
FAST
in(n), in(m);
FOR(i,1,n) in(A[i].ct), in(A[i].bf), in(A[i].add);
FOR(i,1,m) in(B[i].ct), in(B[i].bf), in(B[i].add);
FOR(i,1,n) Asum[i]=Asum[i-1]+A[i].ct;
FOR(i,1,m) Bsum[i]=Bsum[i-1]+B[i].ct;
FOR(i,1,n) {
if(A[i].bf>=Asum[i]) {
points[upper_bound(Bsum+1, Bsum+m+1,A[i].bf-Asum[i])-(Bsum+1)+1-1].eb(i, A[i].add);
// cerr<<i<<": "<<upper_bound(Bsum+1, Bsum+m+1,A[i].bf-Asum[i])-(Bsum+1)+1-1<<' '<<i<<'\n';
}
}
FOR(i,1,m) { if(B[i].bf-Bsum[i]<0) continue; addback+=B[i].add;
ll y=i;
ll x=upper_bound(Asum+1, Asum+n+1, B[i].bf-Bsum[i])-(Asum+1)+1-1;
// assert(x>=0);
if(x+1<=n) {
points[y-1].eb(x+1, -B[i].add);
// cerr<<i<<": "<<y-1<<' '<<x+1<<'\n';
}
}
seg=new node(0,n);
FOR(col, 0, m) {
/* updates */
for(auto i:points[col]) {
seg->update(i.f, n, i.s);
}
/* */
if(col==m) continue;
// sort(all(points[col]));
for(auto i:points[col]) if(i.f) {
seg->mx(i.f, n, seg->rmq(i.f-1,i.f-1));
}
}
cout<<seg->rmq(n, n)+addback<<'\n';
}
Compilation message (stderr)
dishes.cpp:42:2: warning: #warning "// FOR has been inclusive now!!!! && arr size" [-Wcpp]
#warning "// FOR has been inclusive now!!!! && arr size"
^~~~~~~
# | 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... |