Submission #1352462

#TimeUsernameProblemLanguageResultExecution timeMemory
1352462Born_To_LaughPalembang Bridges (APIO15_bridge)C++17
0 / 100
3 ms4932 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const int MOD = 998244353, INF = 1e9 + 7;

const int maxn = 2e5 + 10;
int m, k, n, a[maxn], b[maxn];
ll basecost;
vector<int> temp, ids[maxn]; // pp that have a = x | b = x
inline int get_pos(int x){
    return lower_bound(alle(temp), x) - temp.begin();
}

namespace sub1
{
    bool check(){
        return k == 1;
    }
    void solve(){
        int cntl = 0, cntr = n;
        int lsti = 0;
        ll ans = 3e18 + 7;
        ll cost = 0;
        for(int i=1; i<=n; ++i) cost += a[i] * 2;
        for(int i=1; i<=temp.size() - 1; ++i){
            int nxti = temp[i];
            cost += cntl * (nxti - lsti) * 2;
            cost -= cntr * (nxti - lsti) * 2;
            for(auto &elm: ids[i]){
                if(nxti == a[elm])cntr--;
                if(nxti == b[elm])cntl++;
            }
            ans = min(ans, cost + basecost);
            lsti = nxti;
        }
        cout << ans << '\n';
    }
}
namespace sub2
{
    void solve(){
        ll ans = 3e18 + 7;
        for(int i=1; i < temp.size() - 1; ++i){
            int nxti = temp[i];
            ll costi = 0;
            for(int j=1; j<=n; ++j){
                if(b[j] <= nxti) costi += (nxti - b[j]) * 2;
            }

            //qr j
            ll cost = 0;
            int cntl = 0, cntr = 0;
            int lst = temp[i + 1];
            for(int j=1; j<=n; ++j){
                if(a[j] <= nxti) continue;
                cntr++;
                cost += (a[j] - lst) * 2;
            }

            //
            priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
            for(int j=i + 1; j <= temp.size() - 1; ++j){
                int nxt = temp[j];
                while(!pq.empty()){
                    if(nxt < pq.top().fi) break;
                    int id = pq.top().se;

                    cost -= (lst - b[id]) * 2;
                    cost += (a[id] - nxti) * 2;
                    
                    pq.pop();
                    cntl--;
                }
                cost += cntl * (nxt - lst) * 2;
                cost -= cntr * (nxt - lst) * 2;
                for(auto &elm: ids[j]){
                    if(a[elm] <= nxti) continue;

                    if(nxt == a[elm]) cntr--;
                    if(nxt == b[elm]){
                        cntl++;
                        pq.push({b[elm] + a[elm] - nxti, elm});
                    }
                }
                ans = min(ans, basecost + costi + cost);
                lst = nxt;
            }
        }
        cout << ans << '\n';
    }
}



void solve(){
    n = 0; cin >> k >> m; basecost = 0;
    temp.assign(1, -INF);
    for(int i=1; i<=m; ++i){
        int s, t;
        char p, q;
        cin >> p >> s >> q >> t;
        if(p == q) basecost += abs(s - t);
        else{
            if(s > t) swap(s, t);
            a[++n] = s; b[n] = t;
            basecost += t - s + 1;
            temp.push_back(s);
            temp.push_back(t);
        }
    }
    sort(alle(temp));
    temp.erase(unique(alle(temp)), temp.end());
    for(int i=1; i<=n; ++i){
        ids[get_pos(a[i])].push_back(i);
        ids[get_pos(b[i])].push_back(i);
    }
    if(n <= k){
        cout << basecost << '\n';
        return;
    }
    if(sub1::check()) sub1::solve();
    else sub2::solve();
}
signed main(){
    // freopen("inp.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#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...