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;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=1e6+9;
long long a[maxn],b[maxn],s[maxn],t[maxn],p[maxn],q[maxn];
long long f[maxn];
struct IT{
vector<long long>st;
void resz(int n){
st.resize(4*n+9);
}
void update(int id,int l,int r,int u,long long val){
if (l>u||r<u)return;
if (l==r){
st[id]=val;
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,u,val);
update(id*2+1,mid+1,r,u,val);
st[id]=min(st[id*2],st[id*2+1]);
}
void cong(int id,int l,int r,int u,long long val){
if (l>u||r<u)return;
if (l==r){
st[id]+=val;
return;
}
int mid=(l+r)/2;
cong(id*2,l,mid,u,val);
cong(id*2+1,mid+1,r,u,val);
st[id]=min(st[id*2],st[id*2+1]);
}
long long get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return 2e15;
if (u<=l&&r<=v)return st[id];
int mid=(l+r)/2;
return min(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
int findlow(int id,int l,int r){
if (st[id]>=0)return -1;
if (l==r)return l;
int mid=(l+r)/2;
if (st[id*2]<0)return findlow(id*2,l,mid);
else return findlow(id*2+1,mid+1,r);
}
}st;
vector<int>del[maxn];
long long g[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,m;
cin>>n>>m;
for1(i,1,n)cin>>a[i]>>s[i]>>p[i];
for1(i,1,m)cin>>b[i]>>t[i]>>q[i];
for1(i,1,n)a[i]+=a[i-1];
for1(i,1,m)b[i]+=b[i-1];
f[0]=0;
long long prefixsum=0,mx=0;
set<int>opt;
opt.insert(0);
st.resz(m+1);
st.update(1,0,m,0,f[0]);
for1(i,1,m){
f[i]=f[i-1];
if (b[i]<=t[i]){
f[i]+=q[i];
prefixsum+=q[i];
}
if (f[i]-prefixsum>mx){
st.update(1,0,m,i,f[i]-prefixsum-mx);
opt.insert(i);
g[i]=f[i]-prefixsum-mx;
mx=f[i]-prefixsum;
}
}
for1(i,1,m){
if (b[i]>t[i])continue;
int l=1,r=n,ans=-1;
while (l<=r){
int mid=(l+r)/2;
if (a[mid]+b[i]>t[i]){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
if (ans!=-1)del[ans].pb(i);
}
for1(i,1,n){
int l=0,r=m,ans=-1;
while (l<=r){
int mid=(l+r)/2;
if (a[i]+b[mid]<=s[i]){
ans=mid;
l=mid+1;
}
else r=mid-1;
}
if (ans!=-1){
st.cong(1,0,m,0,p[i]);
st.cong(1,0,m,ans+1,-p[i]);
opt.insert(ans+1);
}
for (auto v:del[i]){
if (q[v]>=0){
st.cong(1,0,m,v,q[v]);
opt.insert(v);
}
else {
st.cong(1,0,m,v,q[v]);
opt.insert(v-1);
}
//cout<<i<<" "<<v<<'\n';
}
while (st.st[1]<0){
int pos=st.findlow(1,0,m);
auto it=opt.upper_bound(pos);
if (it==opt.end()){
st.update(1,0,m,pos,0);
}
else {
long long val=st.get(1,0,m,(*it),(*it))+st.get(1,0,m,pos,pos);
st.update(1,0,m,pos,0);
st.update(1,0,m,(*it),val);
}
opt.erase(pos);
}
}
prefixsum=0,mx=0;
for1(i,0,m){
mx+=st.get(1,0,m,i,i);
if (a[n]+b[i]<=t[i])prefixsum+=q[i];
}
cout<<prefixsum+mx;
//for1(i,0,m)cout<<st.get(1,0,m,i,i)<<'\n';
//cout<<st.get(1,0,m,0,0)+st.get(1,0,m,1,1)+st.get(1,0,m,2,2);
}
# | 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... |