제출 #510889

#제출 시각아이디문제언어결과실행 시간메모리
510889couplefireTwo Dishes (JOI19_dishes)C++17
74 / 100
4233 ms265912 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 1000005;

int n, m; ll upa[N], upb[N], val[N];
ll sa[N], sb[N], bda[N], bdb[N], pa[N], pb[N];
vector<pair<ll, ll>> stuff[N];
ll ans = 0; set<pair<ll, ll>> st;

int main(){
    // freopen("a.in", "r", stdin);
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for(int i = 1; i<=n; ++i)
        cin >> sa[i] >> bda[i] >> pa[i];
    for(int i = 1; i<=m; ++i)
        cin >> sb[i] >> bdb[i] >> pb[i];
    for(int i = 1; i<=n; ++i)
        sa[i] += sa[i-1];
    for(int i = 1; i<=m; ++i)
        sb[i] += sb[i-1];
    for(int i = 1; i<=n; ++i)
        upa[i] = upper_bound(sb, sb+m+1, bda[i]-sa[i])-sb-1;
    for(int i = 1; i<=m; ++i)
        upb[i] = upper_bound(sa, sa+n+1, bdb[i]-sb[i])-sa-1;
    for(int i = 1; i<=n; ++i)
        if(upa[i]+1<=m) stuff[i-1].push_back({upa[i]+1, -pa[i]});
    for(int i = 1; i<=m; ++i)
        if(upb[i]>=0) stuff[upb[i]].push_back({i, pb[i]});
    for(int i = 0; i<=n; ++i)
        sort(stuff[i].begin(), stuff[i].end());
    for(int i = 0; i<=n; ++i){
        vector<int> pos;
        for(auto [y, p] : stuff[i]){
            if(y==0ll) ans += p;
            else{
                auto it = st.find({y, val[y]});
                if(it!=st.end()) st.erase(it);
                if((val[y] += p)<0) pos.push_back(y);
                if(val[y]!=0) st.insert({y, val[y]});
            }
        }
        // for(auto [y, p] : st)
        //     cout << '[' << y << ", " << p << ']' << ' ';
        // cout << '\n';
        for(auto y : pos) while(val[y]<0){
            auto it = st.upper_bound({y, val[y]});
            if(it==st.end()){
                st.erase({y, val[y]});
                val[y] = 0; continue;
            }
            int yy = it->first; st.erase(it);
            st.erase({y, val[y]});
            ll d = min(-val[y], val[yy]);
            val[y] += d; val[yy] -= d;
            if(val[y]!=0) st.insert({y, val[y]});
            if(val[yy]!=0) st.insert({yy, val[yy]});
        }
        // for(auto [y, p] : st)
        //     cout << '[' << y << ", " << p << ']' << ' ';
        // cout << '\n';
        // cout << '\n';
    }
    for(int i = 1; i<=n; ++i)
        ans += pa[i];
    for(auto [y, p] : st)
        ans += p;
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...