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>
#define fi first
#define se second
using namespace std;
typedef long long ll;
struct nod
{
ll x, y, sz;
nod *fii[2];
nod(ll X = 0, ll Y = 0): x(X), y(Y), sz(0)
{
fii[0] = fii[1] = NULL;
}
nod* add(nod *tbi, int kid)
{
++sz;
if(!fii[kid])
{
fii[kid] = tbi;
return tbi;
}
if(tbi -> x < fii[kid] -> x || tbi -> y < fii[kid] -> y)
{
tbi -> fii[kid] = fii[kid];
tbi -> sz += fii[kid] -> sz;
fii[kid] = tbi;
return tbi;
}
if (tbi -> x > fii[kid] -> x || tbi -> y > fii[kid] -> y)
return fii[kid] -> add(tbi, kid);
return fii[kid];
}
};
nod *rad = new nod();
int n;
ll sum, minans = (1LL<<60);
pair<int, int> pct[100002];
bool cmp(pair<int, int> a, pair<int, int> b)
{
return (a.fi + a.se) > (b.fi + b.se);
}
void dfs(nod *cur)
{
minans = min(minans, sum);
for(int i = 0; i <= 1; i++)
if(cur -> fii[i])
{
ll dx = (cur -> x) - (cur -> fii[i] -> x) + (cur -> y) - (cur -> fii[i] -> y);
sum += dx * (2 * cur -> fii[i] -> sz - n);
dfs(cur -> fii[i]);
sum -= dx * (2 * cur -> fii[i] -> sz - n);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> pct[i].fi >> pct[i].se;
sum += pct[i].fi + pct[i].se;
}
sort(pct + 1, pct + n + 1, cmp);
for(int i = 1; i <= n; ++i)
{
nod *cur = rad;
ll cx = 0, cy = 0;
for(int j = 29; j >= 0; --j)
{
if(pct[i].fi & (1<<j))
{
cx |= (1<<j);
cur = cur -> add(new nod(cx, cy), 0);
}
else
if(pct[i].se & (1<<j))
{
cy |= (1<<j);
cur = cur -> add(new nod(cx, cy), 1);
}
}
cur -> sz++;
}
dfs(rad);
cout << minans;
return 0;
}
# | 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... |