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 "wiring.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cerr << H << ' ';
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 2e5 + 10;
const ll inf = 1e18;
int n, m, val[maxn];
ll dp[maxn], seg[maxn << 2], lazy[maxn << 2];
void shift(int v, int l, int r);
void build(int v, int l, int r){
lazy[v] = seg[v] = 0;
if (l + 1 == r) return;
int mid = (l + r) >> 1;
build(v << 1, l, mid);
build(v << 1 | 1, mid, r);
}
void add(int v, int l,int r, int ql, int qr, ll val){
//if (l == 1 && r == n+m+1) debug(ql, qr, val);
if (qr <= l || r <= ql) return;
if (ql <= l && r <= qr){
seg[v] += val;
lazy[v] += val;
return;
}
shift(v, l, r);
int mid = (l + r) >> 1;
add(v << 1, l, mid, ql, qr, val);
add(v << 1 | 1, mid, r, ql, qr, val);
seg[v] = min(seg[v << 1], seg[v << 1 | 1]);
}
ll get(int v, int l, int r, int ql, int qr){
if (qr <= l || r <= ql) return inf;
if (ql <= l && r <= qr) return seg[v];
shift(v, l, r);
int mid = (l + r) >> 1;
return min(get(v << 1, l, mid, ql, qr)
, get(v << 1 | 1, mid, r, ql, qr));
}
void shift(int v, int l, int r){
if (lazy[v] == 0) return;
int mid = (l + r) >> 1;
add(v << 1, l, mid, l, mid, lazy[v]);
add(v << 1 | 1, mid, r, mid, r, lazy[v]);
lazy[v] = 0;
}
ll min_total_length(std::vector<int> r, std::vector<int> b) {
n = r.size();
m = b.size();
int ptl = 0, ptr = 0;
for (int i = 1; i <= n+m; i++){
if (ptr == b.size() || (ptl < r.size() && r[ptl] < b[ptr])){
r[ptl]++;
val[i] = r[ptl];
ptl++;
}
else{
b[ptr]++;
val[i] = -b[ptr];
ptr++;
}
}
int idx = 1;
dp[1] = inf;
for (int i = 2; i <= n+m; i++){
if ((val[i] < 0) == (val[i-1] < 0)){
dp[i] = inf;
//debug(i, dp[i]);
add(1, 1, n+m+1, i, i+1, inf);
continue;
}
idx = i;
break;
}
int lstptr = 1, lst = ptr-1;
ptr = 1;
for (int i = idx; i <= n+m; i++){
if ((val[i] < 0) != (val[i-1] < 0)){
for (int j = ptr; j < i; j++){
add(1, 1, n+m+1, j, j+1, 1ll * (i-j) * abs(val[i]));
add(1, 1, n+m+1, ptr, j+1, -abs(val[j]));
}
lstptr = ptr;
ptr = i;
lst = i-1;
}
else{
add(1, 1, n+m+1, lst, ptr, -abs(val[ptr-1]));
add(1, 1, n+m+1, lstptr, lst, -abs(val[ptr]));
add(1, 1, n+m+1, lstptr, ptr, abs(val[i]));
lst--;
}
dp[i] = get(1, 1, n+m+1, lstptr, ptr);
//debug(i, dp[i]);
add(1, 1, n+m+1, i, i+1, min(dp[i], dp[i-1]));
}
return dp[n+m];
}
Compilation message (stderr)
wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:79:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | if (ptr == b.size() || (ptl < r.size() && r[ptl] < b[ptr])){
| ~~~~^~~~~~~~~~~
wiring.cpp:79:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | if (ptr == b.size() || (ptl < r.size() && r[ptl] < b[ptr])){
| ~~~~^~~~~~~~~~
# | 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... |