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 "aliens.h"
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
 
const ll nx=5e4+5, inf=1e17;
 
ll dp[2][nx], area[nx];
stack<ll> s;
 
struct point
{
    ll x, y;
    bool operator< (const point &o) const {
        if (x==o.x) return y>o.y;
        return x<o.x;
    }
} v[nx], p[nx];
 
struct line
{
    ll m, c;
    double p;
    line(ll m, ll c, ll p): m(m), c(c), p(p){}
};
 
struct convexhull
{
    deque<line> dq;
    double div(ll a, ll b)
    {
        return 1.0*a/b;
    }
    void add(ll m, ll c)
    {
        line nw=line(m, c, inf);
        while (dq.size()>=2&&div(nw.c-dq[dq.size()-2].c, dq[dq.size()-2].m-nw.m)<=dq[dq.size()-2].p) dq.pop_back();
        if (!dq.empty()) dq.back().p=div(nw.c-dq.back().c,dq.back().m-nw.m);
        dq.push_back(nw);
    }
    ll query(ll x)
    {
        while (x>=dq.front().p) dq.pop_front();
        return dq.front().m*x+dq.front().c;
    }
} cvh;
 
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
    for (int i=0; i<n; i++) if (r[i]>c[i]) swap(r[i], c[i]);
    for (int i=1; i<=n; i++) v[i].x=c[i-1], v[i].y=r[i-1];
    sort(v+1, v+n+1);
    for (int i=1; i<=n; i++)
    {
        while (!s.empty()&&v[s.top()].y>=v[i].y) s.pop();
        s.push(i);
    }
    n=s.size();
    while (!s.empty()) p[s.size()]=v[s.top()], s.pop();
    for (int i=2; i<=n; i++) if (p[i-1].x>=p[i].y) area[i]=(p[i-1].x-p[i].y+1)*(p[i-1].x-p[i].y+1);
    for (int i=1; i<=n; i++) dp[1][i]=(p[i].x-p[1].y+1)*(p[i].x-p[1].y+1);
    k=min(k, n);
    for (int i=2; i<=k; i++)
    {
        int current=i%2, pre=1-current;
        while (cvh.dq.size()>0) cvh.dq.pop_back();
        for (int j=i; j<=n; j++)
        {
            cvh.add(-2*p[j].y, p[j].y*p[j].y-2*p[j].y+1-area[j]+dp[pre][j-1]);
            dp[current][j]=cvh.query(p[j].x)+p[j].x*p[j].x+2*p[j].x;
        }
    }
    return dp[k%2][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... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |