Submission #859136

# Submission time Handle Problem Language Result Execution time Memory
859136 2023-10-09T19:37:57 Z Hacv16 Building Bridges (CEOI17_building) C++17
100 / 100
49 ms 66652 KB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long int
 
const int MAX = 1e5 + 15;
const int MAXC = 1e6 + 10;
const int INF = 1e18 + 10;

int n, h[MAX], sw[MAX], dp[MAX];
 
struct Line
{
	int a, b;
	Line(int x = 0, int y = INF) : a(x), b(y) { }

	int operator ()(int x){ return a * x + b; }
 
} seg[4 * MAXC];
 
void update(int p, int l, int r, Line f)
{
	int m = (l + r) >> 1, e = 2 * p, d = e + 1;
 
	bool lessMid = f(m) < seg[p](m);
	bool lessLeft = f(l) < seg[p](l);
 
	if(lessMid) swap(seg[p], f);
 
	if(l == r) return;
 
	if(lessMid != lessLeft) update(e, l , m , f);
	else update(d, m + 1 , r , f);
}
 
int query(int x, int p, int l, int r)
{
	if(l == r) return seg[p](x);
	int m = (l + r) >> 1, e = 2 * p, d = e + 1;
 
	if(x < m) return min(seg[p](x), query(x, e, l, m));
	else return min(seg[p](x), query(x, d, m + 1, r));
}
 
int32_t main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
 
	cin >> n;
 
	for(int i = 1; i <= n; i++) cin >> h[i];
	for(int i = 1; i <= n; i++) cin >> sw[i], sw[i] += sw[i - 1];
 
	auto getA = [&](int i){ return - 2 * h[i]; };
	auto getB = [&](int i){ return h[i] * h[i] - sw[i] + dp[i]; };
 
	dp[1] = 0;
	update(1, 1, MAXC, Line(getA(1), getB(1)));
 
	for(int i = 2; i <= n; i++)
	{
    	dp[i] = h[i] * h[i] + sw[i - 1] + query(h[i], 1, 1, MAXC);
    	update(1, 1, MAXC, Line(getA(i), getB(i)));
	}
 
	cout << dp[n] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64088 KB Output is correct
2 Correct 10 ms 64092 KB Output is correct
3 Correct 9 ms 64176 KB Output is correct
4 Correct 10 ms 64092 KB Output is correct
5 Correct 10 ms 64144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 65400 KB Output is correct
2 Correct 43 ms 66384 KB Output is correct
3 Correct 42 ms 66384 KB Output is correct
4 Correct 42 ms 66140 KB Output is correct
5 Correct 37 ms 66128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64088 KB Output is correct
2 Correct 10 ms 64092 KB Output is correct
3 Correct 9 ms 64176 KB Output is correct
4 Correct 10 ms 64092 KB Output is correct
5 Correct 10 ms 64144 KB Output is correct
6 Correct 49 ms 65400 KB Output is correct
7 Correct 43 ms 66384 KB Output is correct
8 Correct 42 ms 66384 KB Output is correct
9 Correct 42 ms 66140 KB Output is correct
10 Correct 37 ms 66128 KB Output is correct
11 Correct 45 ms 66396 KB Output is correct
12 Correct 44 ms 66412 KB Output is correct
13 Correct 40 ms 66652 KB Output is correct
14 Correct 44 ms 66572 KB Output is correct
15 Correct 36 ms 66132 KB Output is correct
16 Correct 38 ms 66336 KB Output is correct
17 Correct 31 ms 66388 KB Output is correct
18 Correct 30 ms 66484 KB Output is correct