#include <bits/stdc++.h>
#undef BALBIT
using namespace std;
#define ll long long
#define int ll
#define y1 zck_is_king
#define pii pair<ll, ll>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const ll inf = LLONG_MAX;
const ll mod = 1e9+7 ;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int maxn = 1e6+5;
int t1[maxn],t2[maxn],d1[maxn],d2[maxn],v1[maxn],v2[maxn]; // time, deadlline, value
ll ps1[maxn], ps2[maxn]; // how much time it takes to get here (before me)
vector<pii> event[maxn]; //
ll df[maxn]; // max a[i] - a[i-1]
//int tmp[500][500];
signed main(){
IOS();
int n,m; cin>>n>>m;
REP(i,n) {
cin>>t1[i]>>d1[i]>>v1[i];
ps1[i+1] = ps1[i] + t1[i];
}
REP(i,m) {
cin>>t2[i]>>d2[i]>>v2[i];
ps2[i+1] = ps2[i] + t2[i];
}
ps2[m+1] = ps1[n+1] = inf; // check if inf is big enough later
ll base = 0; // base value to add to return, used when reversing a range
REP(i,n) {
// where can I accept up to???
ll left = d1[i] - ps1[i+1]; // remaining time after completing this dish
int pos = upper_bound(ps2, ps2+m+2, left) - ps2 - 1;
bug(i,pos);
// when I get to i
if (pos >= 0) {
event[i+1].pb({pos, v1[i]});
}
}
bug("moving on");
REP(i,m) {
// where can I accept up to???
ll left = d2[i] - ps2[i+1]; // remaining time after completing this dish
int pos = upper_bound(ps1, ps1+n+2, left) - ps1 - 1;
bug(i,pos);
// when I get to i
if (pos >= 0 && i >= 0){
base += v2[i];
event[pos+1].pb({i, -v2[i]});
bug(pos, -v2[i]);
}
}
bug(base);
map<int, ll> mp;
// mp[m+2] = inf;
REP(i, n+1) {
sort(ALL(event[i]), [&](pii a, pii b){ return a.f > b.f;});
// ll now = 0;
// for (pii &p : event[i]) now += p.s;
// int from = 0;
// bug(now);
for (pii &p : event[i]) {
base += p.s;
mp[p.f+1] -= p.s;
if (mp[p.f+1] <= 0) {
ll poo = mp[p.f+1];
auto it = mp.erase(mp.find(p.f+1));
while (poo && it != mp.end()) {
// bug(poo, it->f, it->s);
it->s += poo;
if (it->s <= 0) {
poo = it->s;
it = mp.erase(it);
}else{
poo = 0;
}
}
}
}
// for (pii p : event[i]) {
// REP(j, p.f+1) tmp[i][j] += p.s;
// }
// REP(j, 100) {
// if (i) tmp[i][j] += tmp[i-1][j];
// if (j) MX(tmp[i][j], tmp[i][j-1]);
// }
// REP(j, m+1) {
// cout<<df[j]<<" \n"[j==m];
// }
}
// cout<<endl<<endl;
//
// REP(i,n+1) {
// REP(j, m+1) {
// cout<<tmp[i][j]<<" \n"[j==m];
// }
// }
for (pii p : mp) {
bug(p.f, p.s);
if (p.f <= m)
base += p.s;
}
cout<<base<<endl;
}
/*
1 1
3 10 -3
7 10 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
57400 KB |
Output is correct |
2 |
Incorrect |
206 ms |
46748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
57400 KB |
Output is correct |
2 |
Incorrect |
206 ms |
46748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
57400 KB |
Output is correct |
2 |
Incorrect |
206 ms |
46748 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |