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>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define fopen freopen("input.txt", "r", stdin)
#define eb emplace_back
#define em emplace
#define prec(a) cout<<fixed;cout.precision(a);
#define all(a) (a).begin(), (a).end()
typedef long long ll;typedef long double ld;typedef unsigned long long ul;typedef unsigned int ui;typedef pair<int,int> pii;typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tiii;
const ll INF = 2e18;
const int inf = 2e9;
template<class T>
void pr(T t) {cerr << t << " ";}
template<class T, class ...Args>
void pr(T a, Args ...args) {cerr << a << " ";pr(args...);}
template<class ...Args>
void prl(Args ...args) {pr(args...);cerr << endl;}
int n, m;
struct In{
ll ti, lim, cost;
}A[1000100], B[1000100];
ll ans;
vector<pll> ad[1000100];
ll asum[1000100],bsum[1000100];
ll seg[4000100];
bool flag[4000100];
void prop(int x, int l, int r){
if(!flag[x]) return ;
if(l!=r) flag[x*2]=flag[x*2+1]=true;
seg[x] = flag[x] = 0;
return ;
}
ll get(int x, int l, int r, int s, int e){
prop(x, l, r);
if(r<s||e<l||s>e) return 0;
if(s<=l&&r<=e) return seg[x];
return get(x*2, l, (l+r)/2, s, e)+get(x*2+1, (l+r)/2+1,r,s,e);
}
void clr(int x, int l, int r, int s, int e){
prop(x, l, r);
if(r<s||e<l||s>e) return ;
if(s<=l&&r<=e){
flag[x]=true;
prop(x,l,r);
return ;
}
clr(x*2, l, (l+r)/2, s, e);clr(x*2+1, (l+r)/2+1,r,s,e);
seg[x]=seg[x*2]+seg[x*2+1];
}
void add(int x, int l, int r, int i, ll v){
prop(x,l,r);
if(r<i||i<l) return ;
if(l==r){
seg[x]+=v;return ;
}
int m = (l+r)/2;
if(i<=m) add(x*2, l, m, i, v);
else add(x*2+1, m+1,r,i,v);
seg[x] = seg[x*2]*(1-flag[x*2])+seg[x*2+1]*(1-flag[x*2+1]);
}
int get_left(int x, int l, int r, ll v){
prop(x,l,r);
if(l==r) return l;
ll t = (flag[x*2]?0:seg[x*2]);
if(t>v) return get_left(x*2, l,(l+r)/2, v);
return get_left(x*2+1, (l+r)/2+1, r, v-t);
}
int main(){
fastio;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>A[i].ti>>A[i].lim>>A[i].cost;
asum[i]=asum[i-1]+A[i].ti;
}
for(int i=1;i<=m;i++){
cin>>B[i].ti>>B[i].lim>>B[i].cost;
bsum[i]=bsum[i-1]+B[i].ti;
}
for(int i=1;i<=n;i++){
if(asum[i]>A[i].lim) continue;
int j = upper_bound(bsum+1,bsum+m+1, A[i].lim-asum[i])-bsum-1;
ans+=A[i].cost;
if(j!=m) ad[i-1].eb(j+1, -A[i].cost);
}
for(int i=1;i<=m;i++){
if(bsum[i]>B[i].lim) continue;
int j = upper_bound(asum+1,asum+n+1, B[i].lim-bsum[i])-asum-1;
if(j!=n) ad[j].eb(i,B[i].cost);
else ans+=B[i].cost;
}
for(int i=0;i<=n;i++){
sort(all(ad[i]), [](pll &a,pll &b){
return a.se>b.se;
});
for(auto j:ad[i]){
if(j.se>=0){
add(1,0,m,j.fi,j.se);
continue;
}
else{
ll t = get(1,0,m,0,j.fi-1)-j.se;
if(seg[1]<=t){
clr(1,0,m,j.fi,m);
continue;
}
int ind=get_left(1, 0, m, t);
ll v=get(1, 0, m, 0, ind)-get(1, 0, m, ind,ind)-t;
clr(1, 0, m, j.fi, ind-1);
add(1, 0, m, ind, v);
}
}
}
cout<<ans+seg[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... |