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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
inline bool in(const pii &a, const pii &b) {
auto &[l1, r1] = a; auto &[l2, r2] = b;
return (l2 >= l1 && r2 <= r1);
}
inline int is(const pii &a, const pii &b) {
auto &[l1, r1] = a; auto &[l2, r2] = b;
return max(0, min(r1, r2) - min(l1, l2) + 1);
}
struct line {
ll k, b, id;
line() : k(0), b(0) {}
line(ll k, ll b, ll id) : k(k), b(b), id(id) {}
inline pll operator()(const ll &x) { return mpp(k * x + b, id); }
};
inline bool bad(const line &a, const line &b, const line &c) {
return (a.b - c.b) * (b.k - a.k) <= (a.b - b.b) * (c.k - a.k);
}
deque<line> s;
inline void add(const line &c) {
if (!sz(s)) return void(s.push_front(c));
while (sz(s) >= 2) {
line &a = s[1], &b = s[0];
if (bad(a, b, c)) s.pop_back();
else break;
} s.push_front(c);
}
inline pll qry(const ll &x) {
int l = 0, r = sz(s) - 2, m, ans = -1;
while (l <= r) {
m = l + r >> 1;
s[m + 1](x) < s[m](x) ? ans = x, l = m + 1 : r = m - 1;
}
pll res = {inf, inf};
for (int d = 0; d <= 2; ++d) {
if (ans + d >= 0 && ans + d < sz(s)) {
chmin(res, s[ans + d](x));
}
} return res;
}
ll take_photos(int n, int m, int k, ve<int> R, ve<int> C) {
ve<pii> pts(n);
for (int i = 0; i < n; ++i) pts[i] = {min(R[i], C[i]), max(R[i], C[i])};
sort(all(pts), [](const pii &a, const pii &b) { return mpp(a.fi, -a.se) < mpp(b.fi, -b.se); });
pts.erase(unique(all(pts)), pts.end());
ve<pii> nw;
for (int i = sz(pts) - 1; ~i; --i) {
if (!sz(nw) || !in(pts[i], nw.back())) nw.pb(pts[i]);
} pts.swap(nw);
n = sz(pts); reverse(all(pts));
ve<ll> isec(n);
for (int i = 1; i < n; ++i) isec[i] = is(pts[i - 1], pts[i]);
for (int i = 0; i < n; ++i) isec[i] *= isec[i];
ve<int> l(n), r(n);
for (int i = 0; i < n; ++i) l[i] = pts[i].fi, r[i] = pts[i].se + 1;
auto ok = [&](ll u) {
s.clear();
ve<pll> dp(n, {inf, inf});
for (int i = 0; i < n; ++i) {
chmin(dp[i], {u + (r[i] - l[0]) * 1ll * (r[i] - l[0]), 1ll});
auto need = qry(r[i]);
chmin(dp[i], {need.fi + u + r[i] * 1ll * r[i], need.se + 1});
add(line(2 * r[i], dp[i].fi - isec[i] + l[i] * 1ll * l[i], dp[i].se));
/**
for (int j = 0; j < i; ++j) {
ll val = (r[i] - l[j]) * 1ll * (r[i] - l[j]);
// r[i]^2 - 2r[i]l[j] + l[j]^2 - isec[j] + dp[j].fi
val += -isec[j] + u + dp[j].fi;
chmin(dp[i], mpp(val, dp[j].se + 1));
}
*/
} return dp[n - 1];
};
/**
ve<ve<ll>> dp(n, ve<ll> (k + 1, inf));
for (int i = 0; i < n; ++i) dp[i][1] = (r[i] - l[0]) * (r[i] - l[0]);
for (int j = 2; j <= k; ++j) {
for (int i = 0; i < n; ++i) chmin(dp[i][j], dp[i][j - 1]);
for (int i = 0; i < n; ++i) {
for (int z = 0; z < i; ++z) {
ll val = dp[z][j - 1];
val += r[i] * r[i] + l[z] * l[z] - 2 * r[i] * l[z];
val -= isec[z];
chmin(dp[i][j], val);
}
}
}
*/
ll le = 0, re = m * 1ll * m + 228, mid;
ll ans = re;
while (le <= re) {
mid = le + re >> 1;
ok(mid).se <= k ? ans = mid, re = mid - 1 : le = mid + 1;
}
return ok(ans).fi - ans * k;
}
#ifdef LOCAL
int main() {
int n, m, k;
assert(3 == scanf("%d %d %d", &n, &m, &k));
std::vector<int> r(n), c(n);
for (int i = 0; i < n; i++) {
assert(2 == scanf("%d %d", &r[i], &c[i]));
}
long long ans = take_photos(n, m, k, r, c);
printf("%lld\n", ans);
return 0;
}
#endif
Compilation message (stderr)
aliens.cpp: In function 'pll qry(const long long int&)':
aliens.cpp:53:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | m = l + r >> 1;
| ~~^~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:123:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
123 | mid = le + re >> 1;
| ~~~^~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |