제출 #1343143

#제출 시각아이디문제언어결과실행 시간메모리
1343143gkos5678ČVENK (COI15_cvenk)C++20
38 / 100
3094 ms1996 KiB
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second

typedef long long ll;
typedef pair<ll, ll> ii;

const int maxn = 1e5 + 7;

ll n, r[maxn], s[maxn];

ii lift(ll x, ll y, ll k){
    if (k <= 0) return {x, y};
    if (x == 0) return {0, y - k};
    if (y == 0) return {x - k, 0};
    if ((x & -x) < (y & -y))
        return lift(x - min(k, x & -x), y, k - (x & -x));
    else
        return lift(x, y - min(k, y & -y), k - (y & -y));
}

ii lca(ll x1, ll y1, ll x2, ll y2){
    if ((x1 + y1) > (x2 + y2)){
        swap(x1, x2);
        swap(y1, y2);
    }
    ii lf = lift(x2, y2, x2 + y2 - x1 - y1);
    x2 = lf.ff, y2 = lf.ss;
    ll db = x1 + y1;
    ll l = 0, r = db;
    while(l < r){
        ll m = (l + r) / 2;
        ii la = lift(x1, y1, m);
        ii lb = lift(x2, y2, m);
        if (la == lb) r = m;
        else l = m + 1;
    }
    return lift(x1, y1, l);
}

ll subtree(ll x, ll y){
    ll ret = 0;
    for (int i = 1; i <= n; i++){
        ii lf = lift(r[i], s[i], r[i] + s[i] - x - y);
        if (lf == make_pair(x, y)) ret++;
    }
    return ret;
}

bool check(ll x, ll y){
    ll ch1 = subtree(x + 1, y), ch2 = subtree(x, y + 1);
    if (ch1 * 2 > n || ch2 * 2 > n)
        return 0;
    return 1;
}

int main(){
    srand(time(0));
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> r[i] >> s[i];

    ll ansr, anss;
    while(true){
        int i = (rand() * rand()) % n + 1;
        ll le = 0, ri = r[i] + s[i];
        while(le < ri){
            ll m = (le + ri) / 2;
            ii lf = lift(r[i], s[i], m);
            ll st = subtree(lf.ff, lf.ss);
            if (2 * st >= n) ri = m;
            else le = m + 1;
        }
        ii lf = lift(r[i], s[i], le);
        ll x = lf.ff, y = lf.ss;
        if (check(x, y)){
            ansr = x, anss = y;
            break;
        }
    }

    ll ans = 0;
    for (int i = 1; i <= n; i++){
        ii lc = lca(r[i], s[i], ansr, anss);
        ll x = lc.ff, y = lc.ss;
        ans += r[i];
        ans += s[i];
        ans += ansr;
        ans += anss;
        ans -= x;
        ans -= x;
        ans -= y;
        ans -= y;
    }
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...