Submission #891988

#TimeUsernameProblemLanguageResultExecution timeMemory
891988vjudge1Wiring (IOI17_wiring)C++17
0 / 100
0 ms348 KiB
#include "wiring.h"

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
using ii = pair<int, int>;

template<class T>
struct bivec {
    vector<T> neg, poz;
    bivec(int sz, T v0) : neg(sz, v0), poz(sz, v0) {}
    T &operator[](ll p) {
        if(p >= 0) return poz[p];
        return neg[-p - 1];
    }
};


struct AIB {
    ll n;
    vll V;
    AIB(ll N) : n(N), V(2 * N + 3, 0) {}
    
    void update(ll p, ll v) {
        p += n + 1;
        while(p < V.size()) {
            V[p] += v;
            p += p & -p;
        }
    }

    ll query(ll p) {
        p += n + 1;
        ll re = 0;
        while(p) {
            re += V[p];
            p -= p & -p;
        }
        return re;
    }

    ll query(ll st, ll dr) {
        return query(dr) - query(st - 1);
    }

};

ll min_total_length(vi r, vi b) {
    ll n = r.size(), m = b.size();
    vector<ii> V;
    for(auto it : r)
        V.push_back({it, 1});
    for(auto it : b)
        V.push_back({it, -1});
    sort(V.begin(), V.end());
    const ll INF = 1e18;

    ll len = n + m;
    AIB delta(len);

    ll orig = 0, slope0 = 0, ult = -1;
    ll re = 0;

    for(auto [it, cul] : V) {
        if(ult != -1) {
            delta.update(orig, 2 * (it - ult));
            slope0 -= it - ult;

            if(cul == 1) {
                orig--;
                re -= (delta.query(orig) + slope0);
            } else {
                re += (delta.query(orig) + slope0);
                orig++;
            }
        }

        if(cul == 1) {
            ll sl = slope0;
            for(ll i = -len; i < len; ++i) {
                sl += delta.query(i, i);
                if(sl > 0) {
                    delta.update(i, -sl);
                    sl = 0;
                }
            }
        } else {
            ll sl = slope0 + delta.query(-len, len);
            for(ll i = len; i > -len; --i) {
                sl -= delta.query(i, i);
                if(sl < 0) {
                    delta.update(i, sl);
                    slope0 -= sl;
                    sl = 0;
                }
            }
        }

    //    ll v = re;
    //    for(ll i = orig - 1; i >= -len; --i) {
    //        v -= delta.query(-len, i) + slope0;
    //    }
    //    for(ll i = -len; i <= len; ++i) {
    //        if(i == orig) 
    //            cout << "(" << v << ") ";
    //        else
    //            cout << v << " ";
    //        v += delta.query(-len, i) + slope0;
    //    }
    //    cout << "\n";
        ult = it;
    }
    return re;
//    ll orig = 0;
//    bivec<ll> dp(n + m + 5, INF);
//    dp[0] = 0;
//    int ult = -1, len = n + m + 1;
//    for(auto [it, cul] : V) {
//        if(ult != -1) {
//            for(int i = -len; i <= len; ++i) {
//                dp[i] += abs(i - orig) * (it - ult);
//            }
//            orig -= cul;
//        }
//        ult = it;
//        if(cul == 1) {
//            for(int i = -len; i < len; ++i)
//                dp[i + 1] = min(dp[i + 1], dp[i]);
//        } else {
//            for(int i = len; i > -len; --i)
//                dp[i - 1] = min(dp[i - 1], dp[i]);
//        }
//
//        for(int i = -len; i <= len; ++i) {
//            string s = to_string(dp[i]);
//            if(dp[i] == INF) s = "INF";
//            if(i == orig)
//                cout << "(" << s << ") ";
//            else
//                cout << s << " ";
//        }
//        cout << "\n";
//    }
//    return dp[orig];
}

Compilation message (stderr)

wiring.cpp: In member function 'void AIB::update(ll, ll)':
wiring.cpp:30:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         while(p < V.size()) {
      |               ~~^~~~~~~~~~
wiring.cpp: In function 'll min_total_length(vi, vi)':
wiring.cpp:60:14: warning: unused variable 'INF' [-Wunused-variable]
   60 |     const ll INF = 1e18;
      |              ^~~
#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...