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] = b; auto &[l2, r2] = a;
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) - max(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); }
};
const int N = 1e6 + 10;
line t[4 * N];
inline void upd(int v, int l, int r, line f) {
if (l == r) return void(t[v] = f(l) < t[v](l) ? f : t[v]);
int m = l + r >> 1;
bool le = f(l) < t[v](l);
bool me = f(m) < t[v](m);
if (me) swap(t[v], f);
if (le != me) upd(2 * v + 1, l, m, f);
else upd(2 * v + 2, m + 1, r, f);
}
inline pll get(int v, int l, int r, int x) {
pll cur = t[v](x);
if (l == r) return cur;
int m = l + r >> 1;
if (x <= m) return min(cur, get(2 * v + 1, l, m, x));
else return min(cur, get(2 * v + 2, m + 1, r, x));
}
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.se, a.fi) < mpp(b.se, b.fi); });
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); reverse(all(pts));
n = sz(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) {
for (int i = 0; i < 4 * m; ++i) t[i] = line(0, inf, 0);
ve<pll> dp(n, {inf, inf});
for (int i = 0; i < n; ++i) {
upd(0, 0, m, line(-2 * l[i], (!i ? 0 : dp[i - 1].fi) - isec[i] + l[i] * 1ll * l[i], !i ? 0 : dp[i - 1].se));
auto need = get(0, 0, m, r[i]);
chmin(dp[i], {need.fi + u + r[i] * 1ll * r[i], need.se + 1});
} return dp[n - 1];
};
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 'void upd(int, int, int, line)':
aliens.cpp:42:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int m = l + r >> 1;
| ~~^~~
aliens.cpp: In function 'pll get(int, int, int, int)':
aliens.cpp:52:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int m = l + r >> 1;
| ~~^~~
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:89:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | 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... |