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>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
struct Line{
	LL m, c;
	Line(LL m = 0, LL c = 0): m(m), c(c){}
	LL get(LL x){return m * x + c;}
};
struct ConvexHull{
	int sz, B;
	Line *hull;
	ConvexHull(int n): sz(0), B(0){
		hull = new Line[++n];
	}
	bool is_bad(int curr, int prev, int next){
		Line c = hull[curr], p = hull[prev], n = hull[next];
		return (c.c - n.c) * (c.m - p.m) <= (p.c - c.c) * (n.m - c.m);
	}
	void add_line(LL m, LL c){
		hull[sz++] = Line(m, c);
		while (sz > 2 && is_bad(sz-2, sz-3, sz-1)){
			hull[sz-2] = hull[sz-1]; sz--;
		}
		B = min(B, sz - 1);
	}
	LL query(LL x){
		//	int l = -1, r = sz-1;
		//	while (r - l > 1){
		//		int m = (l + r) >> 1;
		//		if (hull[m].get(x) >= hull[m+1].get(x)) l = m;
		//		else r = m;
		//	}
		//	return hull[r].get(x);
		while (B < sz - 1 && hull[B].get(x) >= hull[B+1].get(x)) B++;
		return hull[B].get(x);
	}
};
const LL LINF = 1e18;
const int MX = 5e4;
int n, m, k;
vector<pii> ori;
LL L[MX + 5], R[MX + 5], nx[MX + 5];
LL dp[105][MX + 5];
ConvexHull ch(MX + 5);
bool cmp(pii a, pii b){
	return a.fi != b.fi ? a.fi < b.fi : a.se > b.se;
}
/*
   dp[k][n] = min(dp[k-1][n], min{j<=n} (dp[k-1][j-1] + (L[j]^2-2L[j]) + max(0,R[j-1]-L[j]+1)^2 - 2L[j]•R[n]) + R[n]^2 + 2R[n] + 1;
										(             c1             )   (      c2            ) (  m  ) ( x )   (   extra        )
*/
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++){
		ori.emplace_back(r[i], c[i]);
		if (ori.back().fi > ori.back().se) swap(ori.back().fi, ori.back().se);
	}
	sort(ori.begin(), ori.end());
	int idx = 0;
	for (int i = 0; i < n; i++){
		if (idx && L[idx] <= ori[i].fi && R[idx] >= ori[i].se) continue;
		while (idx && ori[i].fi <= L[idx] && ori[i].se >= R[idx]) idx--;
		idx++; tie(L[idx], R[idx]) = ori[i];
	}
	n = idx;
	L[0] = R[0] = -1;
	for (int i = 1; i <= n; i++) dp[0][i] = LINF;
	for (int i = 1; i <= n; i++)
		nx[i] = max(0LL, R[i-1] - L[i] + 1);
	for (int i = 1; i <= k; i++){
		for (int j = 1; j <= n; j++){
			dp[i][j] = dp[i-1][j];
			ch.add_line(-2LL * L[j], dp[i-1][j-1] + L[j] * (L[j] - 2) + nx[i] * nx[i]);
			dp[i][j] = min(dp[i][j], ch.query(R[j]) + R[j] * R[j] + 2LL * R[j] + 1);
		}
	}
	return dp[k][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... |