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>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) cerr << #A << " = "; for (int64_t _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';
#define PR0(A, n) cerr << #A << " = "; for (int64_t _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';
const int64_t maxn = 200002;
const int64_t inf = 1e18;
struct rmq {
int64_t n;
int64_t container[maxn*4];
void init(int64_t _n) {
n = _n;
for (int64_t i=0; i<=4*n; ++i)
container[i] = inf;
}
void update(int64_t pos, int64_t val, int64_t l, int64_t r, int64_t id) {
if (pos<l || pos>r)
return;
if (l==pos && pos==r) {
container[id] = val;
return;
}
int64_t mid = (l+r)/2;
update(pos, val, l, mid, id*2);
update(pos, val, mid+1, r, id*2+1);
container[id] = min(container[id*2], container[id*2+1]);
}
int64_t get(int64_t u, int64_t v, int64_t l, int64_t r, int64_t id) {
if (v<l || u>r)
return inf;
if (u<=l && r<=v)
return container[id];
int64_t mid = (l+r)/2;
return min(get(u, v, l, mid, id*2),
get(u, v, mid+1, r, id*2+1));
}
void update(int64_t pos, int64_t val) {
update(pos, val, 1, n, 1);
}
int64_t get(int64_t u, int64_t v) {
if (u>v)
return inf;
return get(u, v, 1, n, 1);
}
} T1, T2;
int64_t m, n, L[maxn], R[maxn], pos[maxn];
pair<int64_t, int64_t> a[maxn];
int64_t f[maxn], ps[2][maxn];
void enter(vector<int> r, vector<int> b) {
m = r.size(); n = b.size();
for (int64_t i=1; i<=m; ++i) {
a[i] = make_pair(r[i-1], 0);
}
for (int64_t i=1; i<=n; ++i) {
a[m+i] = make_pair(b[i-1], 1);
}
}
void init() {
n = m+n;
sort(a+1, a+n+1);
int64_t cnt = 0;
for (int64_t i=1; i<=n+1; ++i) {
if (i==1 || i==n+1 || a[i].second!=a[i-1].second) {
++cnt;
L[cnt] = i;
R[cnt-1] = i-1;
}
pos[i] = cnt;
}
for (int64_t i=1; i<=n; ++i) {
for (int64_t j=0; j<2; ++j)
ps[j][i] = ps[j][i-1] + (a[i].second==j ? a[i].first : 0);
}
T1.init(n);
T2.init(n);
}
int64_t min_total_length(vector<int> red, vector<int> blue) {
// freopen("wiring.inp", "r", stdin);
// freopen("wiring.out", "w", stdout);
// ios::sync_with_stdio(0); cin.tie(0);
enter(red, blue);
init();
int64_t extra1 = 1LL*(-1+R[pos[1]])*a[L[pos[1]+1]].first;
int64_t extra2 = -1LL*(1-R[pos[1]])*a[R[pos[1]]].first;
T1.update(1, extra1);
T2.update(1, extra2);
for (int64_t i=1; i<=n; ++i) {
f[i] = inf;
if (pos[i]==1) {
continue;
}
int64_t l = L[pos[i]-1], r = R[pos[i]-1];
int64_t w = a[i].second, b = w^1;
f[i] = T1.get(l, r-i+L[pos[i]]) + (-i + L[pos[i]])*a[L[pos[i]]].first
+ ps[w][i] - ps[b][i];
f[i] = min(f[i], T2.get(max(l, r-i+L[pos[i]]), r) - (i-L[pos[i]])*a[R[pos[i]-1]].first
+ ps[w][i] - ps[b][i]);
int64_t extra1 = 1LL*(-i+R[pos[i]])*a[L[pos[i]+1]].first;
int64_t extra2 = -1LL*(i-R[pos[i]])*a[R[pos[i]]].first;
T1.update(i, min(f[i], f[i-1])+ps[w][i-1]-ps[b][i-1] + extra1);
T2.update(i, min(f[i], f[i-1])+ps[w][i-1]-ps[b][i-1] + extra2);
}
return f[n];
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |