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 "aliens.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
#define sq(a) ((ll)(a) * (a))
struct cht {
vector<ll> m, b, k; int ptr = 0;
bool bad(int l1, int l2, int l3) {
return 1.0 * (b[l3] - b[l1]) * (m[l1] - m[l2]) <= 1.0 * (b[l2] - b[l1]) * (m[l1] - m[l3]);
}
void add(ll _m, ll _b, int _k) {
m.push_back(_m); b.push_back(_b); k.push_back(_k);
int s = m.size();
while(s >= 3 && bad(s - 3, s - 2, s - 1)) {
m.erase(m.end() - 2);
b.erase(b.end() - 2);
k.erase(k.end() - 2);
s--;
}
}
ll f(int i, ll x) { return m[i] * x + b[i]; }
li query(ll x) {
if(ptr >= m.size()) ptr = m.size() - 1;
while(ptr < m.size() - 1 &&
f(ptr, x) > f(ptr + 1, x)) ptr++;
return { f(ptr, x), k[ptr] };
}
void clear() { m.clear(), b.clear(), k.clear(); ptr = 0; }
} ds;
vector<int> l = {-1}, r = {-1};
li get(ll C) {
int n = l.size() - 1;
li dp[n + 1];
dp[0] = {0, 0};
ds.clear(); ds.add(-2ll * l[1], sq(l[1]), 0);
for(int i = 1; i <= n; i++) {
ll x = r[i] + 1;
li tmp = ds.query(x);
dp[i].first = tmp.first + sq(x) + C;
dp[i].second = tmp.second + 1;
if(i == n) break;
ds.add(-2ll * l[i + 1], dp[i].first + sq(l[i + 1])- sq(max(0, r[i] - l[i + 1] + 1)), dp[i].second);
}
return dp[n];
}
ll take_photos(int n, int m, int K, vector<int> R, vector<int> C) {
vector<ii> tmp;
for(int i = 0; i < n; i++) {
if(R[i] > C[i]) swap(R[i], C[i]);
tmp.emplace_back(C[i], R[i]);
}
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
for(ii seg : tmp) {
if(seg.first == r.back()) continue;
while(l.size() && seg.second <= l.back() && r.back() <= seg.first)
r.pop_back(), l.pop_back();
l.push_back(seg.second);
r.push_back(seg.first);
}
ll lo = 0, hi = sq(m);
ll l = -1, r = n + 1, lval, rval;
while(lo <= hi) {
ll mid = lo + hi >> 1;
li tmp = get(mid);
if(tmp.second == K)
return tmp.first - K * mid;
if(tmp.second < K) {
hi = mid - 1;
if(l <= tmp.second) {
l = tmp.second;
lval = tmp.first - l * mid;
}
} else {
lo = mid + 1;
if(tmp.second <= r) {
r = tmp.second;
rval = tmp.first - r * mid;
}
}
}
return rval + ((lval - rval) / (r - l)) * (r - K);
}
#ifdef LOCAL_TESTING
int main() {
freopen("in", "r", stdin);
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]));
}
ll ans = take_photos(n, m, k, r, c);
printf("%lld\n", ans);
return 0;
}
#endif
Compilation message (stderr)
aliens.cpp: In member function 'li cht::query(ll)':
aliens.cpp:28:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ptr >= m.size()) ptr = m.size() - 1;
~~~~^~~~~~~~~~~
aliens.cpp:29:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < m.size() - 1 &&
~~~~^~~~~~~~~~~~~~
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:72:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll mid = lo + hi >> 1;
~~~^~~~
aliens.cpp:90:50: warning: 'rval' may be used uninitialized in this function [-Wmaybe-uninitialized]
return rval + ((lval - rval) / (r - l)) * (r - K);
^
aliens.cpp:90:23: warning: 'lval' may be used uninitialized in this function [-Wmaybe-uninitialized]
return rval + ((lval - rval) / (r - l)) * (r - K);
~~~~~~^~~~~~~
# | 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... |